Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::PrimitiveDictionary< SRC, TGT, OP > Class Template Reference
Collaboration diagram for duckdb::PrimitiveDictionary< SRC, TGT, OP >:

Classes

struct  primitive_dictionary_entry_t

Public Member Functions

 PrimitiveDictionary (Allocator &allocator_p, idx_t maximum_size_p, idx_t maximum_target_capacity_p)
template<bool ADD_TO_TARGET = false>
void Insert (SRC value)
 Insert value into dictionary (if not full).
uint32_t GetIndex (const SRC &value) const
 Get dictionary index of an already inserted value.
template<typename U = SRC, typename std::enable_if<!std::is_same< U, string_t >::value, int >::type = 0>
void IterateValues (const std::function< void(const SRC &, const TGT &)> &fun) const
 Iterates over inserted values.
template<typename U = SRC, typename std::enable_if< std::is_same< U, string_t >::value, int >::type = 0>
void IterateValues (const std::function< void(const SRC &, const TGT &)> &fun) const
 Specialized template to iterate over string_t values.
idx_t GetSize () const
 Get the number of unique values in the dictionary.
bool IsFull () const
 If any of the inserts caused the dictionary to be full, this returns true.
unique_ptr< MemoryStream > GetTargetMemoryStream () const
 Get the target written values as a memory stream (zero-copy).
void Reset ()
void Clear ()
primitive_dictionary_entry_t & Lookup (const SRC &value) const
 Look up a value in the dictionary using linear probing.

Static Public Attributes

static constexpr uint32_t MAXIMUM_POSSIBLE_SIZE = INVALID_INDEX - 1
static constexpr idx_t INITIAL_TARGET_CAPACITY = 1048576

Private Member Functions

template<typename S = SRC, typename std::enable_if<!std::is_same< S, string_t >::value, int >::type = 0>
bool AddToTarget (const SRC &src_value)
 Write a value to the target data (if source is not string).
template<typename S = SRC, typename std::enable_if< std::is_same< S, string_t >::value, int >::type = 0>
bool AddToTarget (SRC &src_value)
 Write a value to the target data (if source is string).

Private Attributes

Allocator & allocator
const idx_t maximum_size
 Maximum size and current size.
uint32_t size
const idx_t capacity
 Dictionary capacity (power of two) and corresponding mask.
const idx_t capacity_mask
const idx_t maximum_target_capacity
 Capacity of target encoded data.
AllocatedData allocated_dictionary
 Allocated regions for dictionary/target.
AllocatedData allocated_target
MemoryStream target_stream
primitive_dictionary_entry_t *const dictionary
 Pointers to allocated regions for convenience.
bool full
 More values inserted than possible.

Static Private Attributes

static constexpr idx_t LOAD_FACTOR = 2
static constexpr uint32_t INVALID_INDEX = static_cast<uint32_t>(-1)
static constexpr idx_t MAXIMUM_TARGET_CAPACITY_INCREMENT = 33554432ULL

Constructor & Destructor Documentation

◆ PrimitiveDictionary()

template<class SRC, class TGT = SRC, class OP = PrimitiveCastOperator>
duckdb::PrimitiveDictionary< SRC, TGT, OP >::PrimitiveDictionary ( Allocator & allocator_p,
idx_t maximum_size_p,
idx_t maximum_target_capacity_p )
inline

PrimitiveDictionary is a fixed-size linear probing hash table for primitive types It is used to dictionary-encode data in, e.g., Parquet files

397966 : allocator(allocator_p), maximum_size(maximum_size_p), size(0),
397967 capacity(NextPowerOfTwo(maximum_size * LOAD_FACTOR)), capacity_mask(capacity - 1),
397969 allocated_dictionary(allocator.Allocate(capacity * sizeof(primitive_dictionary_entry_t))),
397970 allocated_target(allocator.Allocate(std::is_same<TGT, string_t>::value
397971 ? MinValue(INITIAL_TARGET_CAPACITY, maximum_target_capacity)
397972 : capacity * sizeof(TGT))),
397973 target_stream(allocated_target.get(), allocated_target.GetSize()),
397974 dictionary(reinterpret_cast<primitive_dictionary_entry_t *>(allocated_dictionary.get())), full(false) {
397975 Clear();
397976 }
Definition duckdb.cpp:397946
primitive_dictionary_entry_t *const dictionary
Pointers to allocated regions for convenience.
Definition duckdb.cpp:398150
const idx_t maximum_target_capacity
Capacity of target encoded data.
Definition duckdb.cpp:398141
bool full
More values inserted than possible.
Definition duckdb.cpp:398153
const idx_t maximum_size
Maximum size and current size.
Definition duckdb.cpp:398133
AllocatedData allocated_dictionary
Allocated regions for dictionary/target.
Definition duckdb.cpp:398145
const idx_t capacity
Dictionary capacity (power of two) and corresponding mask.
Definition duckdb.cpp:398137
Definition duckdb.cpp:397951
Here is the call graph for this function:

Member Function Documentation

◆ Insert()

template<class SRC, class TGT = SRC, class OP = PrimitiveCastOperator>
template<bool ADD_TO_TARGET = false>
void duckdb::PrimitiveDictionary< SRC, TGT, OP >::Insert ( SRC value)
inline

Insert value into dictionary (if not full).

397981 {
397982 if (full) {
397983 return;
397984 }
397985 auto &entry = Lookup(value);
397986 if (entry.IsEmpty()) {
397987 if (size + 1 > maximum_size || (ADD_TO_TARGET && !AddToTarget(value))) {
397988 full = true;
397989 return;
397990 }
397991 entry.value = value;
397992 entry.index = size++;
397993 }
397994 }
primitive_dictionary_entry_t & Lookup(const SRC &value) const
Look up a value in the dictionary using linear probing.
Definition duckdb.cpp:398058
bool AddToTarget(const SRC &src_value)
Write a value to the target data (if source is not string).
Definition duckdb.cpp:398069
Here is the call graph for this function:

◆ GetIndex()

template<class SRC, class TGT = SRC, class OP = PrimitiveCastOperator>
uint32_t duckdb::PrimitiveDictionary< SRC, TGT, OP >::GetIndex ( const SRC & value) const
inline

Get dictionary index of an already inserted value.

397997 {
397998 const auto &entry = Lookup(value);
397999 D_ASSERT(!entry.IsEmpty());
398000 return entry.index;
398001 }
Here is the call graph for this function:

◆ IterateValues() [1/2]

template<class SRC, class TGT = SRC, class OP = PrimitiveCastOperator>
template<typename U = SRC, typename std::enable_if<!std::is_same< U, string_t >::value, int >::type = 0>
void duckdb::PrimitiveDictionary< SRC, TGT, OP >::IterateValues ( const std::function< void(const SRC &, const TGT &)> & fun) const
inline

Iterates over inserted values.

398005 {
398006 const auto target_values = reinterpret_cast<const TGT *>(allocated_target.get());
398007 for (idx_t i = 0; i < capacity; i++) {
398008 auto &entry = dictionary[i];
398009 if (entry.IsEmpty()) {
398010 continue;
398011 }
398012 fun(entry.value, target_values[entry.index]);
398013 }
398014 }

◆ IterateValues() [2/2]

template<class SRC, class TGT = SRC, class OP = PrimitiveCastOperator>
template<typename U = SRC, typename std::enable_if< std::is_same< U, string_t >::value, int >::type = 0>
void duckdb::PrimitiveDictionary< SRC, TGT, OP >::IterateValues ( const std::function< void(const SRC &, const TGT &)> & fun) const
inline

Specialized template to iterate over string_t values.

398018 {
398019 for (idx_t i = 0; i < capacity; i++) {
398020 auto &entry = dictionary[i];
398021 if (entry.IsEmpty()) {
398022 continue;
398023 }
398024 fun(entry.value, entry.value);
398025 }
398026 }

◆ GetSize()

template<class SRC, class TGT = SRC, class OP = PrimitiveCastOperator>
idx_t duckdb::PrimitiveDictionary< SRC, TGT, OP >::GetSize ( ) const
inline

Get the number of unique values in the dictionary.

398029 {
398030 return size;
398031 }
Here is the caller graph for this function:

◆ IsFull()

template<class SRC, class TGT = SRC, class OP = PrimitiveCastOperator>
bool duckdb::PrimitiveDictionary< SRC, TGT, OP >::IsFull ( ) const
inline

If any of the inserts caused the dictionary to be full, this returns true.

398034 {
398035 return full;
398036 }

◆ GetTargetMemoryStream()

template<class SRC, class TGT = SRC, class OP = PrimitiveCastOperator>
unique_ptr< MemoryStream > duckdb::PrimitiveDictionary< SRC, TGT, OP >::GetTargetMemoryStream ( ) const
inline

Get the target written values as a memory stream (zero-copy).

398039 {
398040 auto result = make_uniq<MemoryStream>(target_stream.GetData(), target_stream.GetCapacity());
398041 result->SetPosition(target_stream.GetPosition());
398042 return result;
398043 }

◆ Reset()

template<class SRC, class TGT = SRC, class OP = PrimitiveCastOperator>
void duckdb::PrimitiveDictionary< SRC, TGT, OP >::Reset ( )
inline
398045 {
398046 allocated_dictionary.Reset();
398047 allocated_target.Reset();
398048 }

◆ Clear()

template<class SRC, class TGT = SRC, class OP = PrimitiveCastOperator>
void duckdb::PrimitiveDictionary< SRC, TGT, OP >::Clear ( )
inline
398050 {
398051 for (idx_t i = 0; i < capacity; i++) {
398052 dictionary[i].index = INVALID_INDEX;
398053 }
398054 size = 0;
398055 full = false;
398056 }

◆ Lookup()

template<class SRC, class TGT = SRC, class OP = PrimitiveCastOperator>
primitive_dictionary_entry_t & duckdb::PrimitiveDictionary< SRC, TGT, OP >::Lookup ( const SRC & value) const
inline

Look up a value in the dictionary using linear probing.

398058 {
398059 auto offset = Hash(value) & capacity_mask;
398060 while (!dictionary[offset].IsEmpty() && dictionary[offset].value != value) {
398061 ++offset &= capacity_mask;
398062 }
398063 return dictionary[offset];
398064 }
Here is the caller graph for this function:

◆ AddToTarget() [1/2]

template<class SRC, class TGT = SRC, class OP = PrimitiveCastOperator>
template<typename S = SRC, typename std::enable_if<!std::is_same< S, string_t >::value, int >::type = 0>
bool duckdb::PrimitiveDictionary< SRC, TGT, OP >::AddToTarget ( const SRC & src_value)
inlineprivate

Write a value to the target data (if source is not string).

398069 {
398071 if (target_stream.GetPosition() + OP::template WriteSize<SRC, TGT>(tgt_value) > target_stream.GetCapacity()) {
398072 return false; // Out of capacity
398073 }
398075 return true;
398076 }
Here is the caller graph for this function:

◆ AddToTarget() [2/2]

template<class SRC, class TGT = SRC, class OP = PrimitiveCastOperator>
template<typename S = SRC, typename std::enable_if< std::is_same< S, string_t >::value, int >::type = 0>
bool duckdb::PrimitiveDictionary< SRC, TGT, OP >::AddToTarget ( SRC & src_value)
inlineprivate

Write a value to the target data (if source is string).

398080 {
398081 // If source is string, target must also be string
398082 const auto required_size = target_stream.GetPosition() + OP::template WriteSize<SRC, TGT>(src_value);
398083 if (required_size > allocated_target.GetSize()) {
398084 // Out of capacity, allocate a new buffer
398085 idx_t new_target_capacity = allocated_target.GetSize();
398088 return false; // Maximum capacity isn't enough
398089 }
398090 // Double the size, or add the maximum increment
398091 new_target_capacity += MinValue(new_target_capacity, MAXIMUM_TARGET_CAPACITY_INCREMENT);
398092 // Bound by maximum capacity
398094 }
398095 auto new_allocated_target = allocator.Allocate(new_target_capacity);
398096
398097 // Copy over data and replace
398098 const auto old_ptr = allocated_target.get();
398099 const auto new_ptr = new_allocated_target.get();
398100 memcpy(new_ptr, old_ptr, allocated_target.GetSize());
398101 allocated_target = std::move(new_allocated_target);
398102
398103 // Also replace the stream
398104 MemoryStream new_target_stream(allocated_target.get(), allocated_target.GetSize());
398105 new_target_stream.SetPosition(target_stream.GetPosition());
398106 target_stream = std::move(new_target_stream);
398107
398108 // Recompute string pointers from old to new buffer
398109 for (idx_t i = 0; i < capacity; i++) {
398110 auto &entry = dictionary[i];
398111 if (entry.IsEmpty() || entry.value.IsInlined()) {
398112 continue;
398113 }
398114 entry.value.SetPointer(
398115 char_ptr_cast(new_ptr + (const_data_ptr_cast(entry.value.GetPointer()) - old_ptr)));
398116 }
398117 }
398118
398119 const auto ptr = target_stream.GetData() + target_stream.GetPosition() + sizeof(uint32_t);
398121
398122 if (!src_value.IsInlined()) {
398123 src_value.SetPointer(char_ptr_cast(ptr));
398124 }
398125
398126 return true;
398127 }
idx_t GetSize() const
Get the number of unique values in the dictionary.
Definition duckdb.cpp:398029
Here is the call graph for this function:

The documentation for this class was generated from the following file: