42 virtual void dump(std::ostream &out)
const = 0;
43 virtual void dump()
const = 0;
67 template<
typename T,
typename U>
68 requires (std::same_as<T, key_type> or std::same_as<T, entry_type>)
and
69 (std::same_as<U, key_type> or std::same_as<U, entry_type>)
70 bool operator()(
const T &_lhs,
const U &_rhs)
const {
73 if constexpr(std::same_as<key_type, const char*>)
74 return std::strcmp(lhs,
rhs) < 0;
130 void dump(std::ostream &out)
const override { out <<
"ArrayIndex<" <<
typeid(
key_type).name() <<
'>' << std::endl; }
135template<arithmetic Key>
157 const std::size_t offset = 0,
const double compression_factor = 1.0)
159 std::size_t n = std::distance(first, last);
160 if (n == 0)
return { 0.0, 0.0 };
161 if (n == 1)
return { 0.0,
static_cast<double>(offset) * compression_factor };
162 double numerator =
static_cast<double>(n);
163 double denominator =
static_cast<double>((*(last - 1)).first - (*first).first);
164 double slope = denominator != 0 ? numerator/denominator * compression_factor : 0.0;
165 double intercept = offset * compression_factor -
slope * (*first).first;
172 const std::size_t offset = 0,
const double compression_factor = 1.0)
174 std::size_t n = std::distance(first, last);
175 if (n == 0)
return { 0.0, 0.0 };
176 if (n == 1)
return { 0.0,
static_cast<double>(offset) * compression_factor };
183 for (std::size_t i = 0; i != n; ++i) {
184 auto x = (*(first + i)).first;
185 std::size_t y = offset + i;
187 double dx = x - mean_x;
188 mean_x += dx / (i + 1);
189 mean_y += (y - mean_y) / (i + 1);
190 c += dx * (y - mean_y);
192 double dx2 = x - mean_x;
196 double cov = c / (n - 1);
197 double var = m2 / (n - 1);
199 if (var == 0.f)
return { 0.0, mean_y };
201 double slope = cov / var * compression_factor;
235 void dump(std::ostream &out)
const override { out <<
"RecursiveModelIndex<" <<
typeid(
key_type).name() <<
'>' << std::endl; }
240 auto segment_id = std::clamp<double>(
models_[0](key), 0,
models_.size() - 2);
242 return static_cast<std::size_t
>(pred);
247 std::size_t bound = 1;
250 auto curr = prev + bound;
259 auto curr = prev - bound;
271 std::size_t bound = 1;
274 auto curr = prev + bound;
283 auto curr = prev - bound;
294#define M_INDEX_LIST_TEMPLATED(X) \
295 X(m::idx::ArrayIndex<bool>) \
296 X(m::idx::ArrayIndex<int8_t>) \
297 X(m::idx::ArrayIndex<int16_t>) \
298 X(m::idx::ArrayIndex<int32_t>) \
299 X(m::idx::ArrayIndex<int64_t>) \
300 X(m::idx::ArrayIndex<float>) \
301 X(m::idx::ArrayIndex<double>) \
302 X(m::idx::ArrayIndex<const char*>) \
303 X(m::idx::RecursiveModelIndex<int8_t>) \
304 X(m::idx::RecursiveModelIndex<int16_t>) \
305 X(m::idx::RecursiveModelIndex<int32_t>) \
306 X(m::idx::RecursiveModelIndex<int64_t>) \
307 X(m::idx::RecursiveModelIndex<float>) \
308 X(m::idx::RecursiveModelIndex<double>)
315#define DECLARE(CLASS) \
316 extern template struct CLASS;
#define M_INDEX_LIST_TEMPLATED(X)
#define M_CONSTEXPR_COND(COND, IF_TRUE, IF_FALSE)
IndexMethod
An enum class that lists all supported index methods.
and arithmetic< U > and same_signedness< T, U > U
A Schema represents a sequence of identifiers, optionally with a prefix, and their associated types.
A table is a sorted set of attributes.
A simple index based on a sorted array that maps keys to their tuple_id.
const_iterator cend() const
bool finalized() const
Returns true iff the index is currently finalized.
IndexMethod method() const override
Returns the IndexMethod of the index.
virtual const_iterator upper_bound(const key_type key) const
Returns an iterator pointing to the first entry of the vector such that entry.key < key is true,...
std::size_t num_entries() const override
Returns the number of entries in the index.
std::vector< entry_type > container_type
void bulkload(const Table &table, const Schema &key_schema) override
Bulkloads the index from table on the key contained in key_schema by executing a query and adding one...
const_iterator end() const
Returns an interator pointing to the first element following the last entry of the index.
const_iterator cbegin() const
std::pair< key_type, value_type > entry_type
void dump(std::ostream &out) const override
void add(const key_type key, const value_type value)
Adds a single pair of key and value to the index.
struct m::idx::ArrayIndex::@3 cmp
Custom comparator class to handle the special case of.
bool finalized_
flag to signalize whether index is finalized, i.e. array is sorted
const_iterator begin() const
Returns an iterator pointing to the first entry of the index.
virtual const_iterator lower_bound(const key_type key) const
Returns an iterator pointing to the first entry of the vector such that entry.key < key is false,...
typename container_type::const_iterator const_iterator
container_type data_
A vector holding the index entries consisting of pairs of key and value.
virtual void finalize()
Sorts the underlying vector and flags the index as finalized.
void dump() const override
The base class for indexes.
IndexBase(IndexBase &&)=default
virtual void dump() const =0
virtual void dump(std::ostream &out) const =0
virtual std::size_t num_entries() const =0
static std::string build_query(const Table &table, const Schema &schema)
Constructs a query string to select all attributes in schema from table.
virtual void bulkload(const Table &table, const Schema &key_schema)=0
Bulkloads the index by executing a query on table using key_schema.
virtual IndexMethod method() const =0
Returns the IndexMethod of the index.
LinearModel(double slope, double intercept)
static LinearModel train_linear_regression(const_iterator first, const_iterator last, const std::size_t offset=0, const double compression_factor=1.0)
Builds a linear regression model from all data points between the first and last .
static LinearModel train_linear_spline(const_iterator first, const_iterator last, const std::size_t offset=0, const double compression_factor=1.0)
Builds a linear spline model between the first and last data point.
double operator()(const key_type x) const
A recursive model index with two layers consiting only of linear monels that maps keys to their tuple...
const_iterator lower_bound(const key_type key) const override
Returns an iterator pointing to the first entry of the vector such that entry.key < key is false,...
void finalize() override
Sorts the underlying vector, builds the linear models, and flags the index as finalized.
base_type::entry_type entry_type
base_type::value_type value_type
base_type::key_type key_type
std::vector< LinearModel > models_
A vector of linear models to index the underlying data.
const_iterator lower_bound_exponential_search(const_iterator pred, const key_type value) const
const_iterator upper_bound(const key_type key) const override
Returns an iterator pointing to the first entry of the vector such that entry.key < key is true,...
void dump(std::ostream &out) const override
const_iterator upper_bound_exponential_search(const_iterator pred, const key_type value) const
base_type::const_iterator const_iterator
std::size_t predict(const key_type key) const
IndexMethod method() const override
Returns the IndexMethod of the index.
void dump() const override
base_type::container_type container_type