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::HLLV1 Class Reference
Collaboration diagram for duckdb::HLLV1:

Public Member Functions

data_ptr_t GetPtr () const
void ToNew (HyperLogLog &new_hll) const
void FromNew (const HyperLogLog &new_hll)

Static Public Member Functions

static idx_t GetSize ()

Private Member Functions

idx_t Count () const
bool IsWithinAcceptableRange (const idx_t &new_hll_count, const idx_t &old_hll_count) const

Private Attributes

duckdb_hll::robj * hll

Static Private Attributes

static constexpr double ACCEPTABLE_Q_ERROR = 1.2

Constructor & Destructor Documentation

◆ HLLV1()

duckdb::HLLV1::HLLV1 ( )
inline
122950 {
122951 hll = duckdb_hll::hll_create();
122953 }
robj * hll_create(void)
Create a new empty HyperLogLog object.
Definition duckdb.cpp:484330
int hllSparseToDense(robj *o)
Convert hll from sparse to dense.
Definition duckdb.cpp:483782

◆ ~HLLV1()

duckdb::HLLV1::~HLLV1 ( )
inline
122955 {
122957 }
void hll_destroy(robj *obj)
Destroy the specified HyperLogLog object.
Definition duckdb.cpp:484362

Member Function Documentation

◆ GetSize()

idx_t duckdb::HLLV1::GetSize ( )
inlinestatic
122960 {
122961 return duckdb_hll::get_size();
122962 }
uint64_t get_size()
Get size (in bytes) of the HLL.
Definition duckdb.cpp:484432

◆ GetPtr()

data_ptr_t duckdb::HLLV1::GetPtr ( ) const
inline
122964 {
122965 return data_ptr_cast((hll)->ptr);
122966 }

◆ ToNew()

void duckdb::HLLV1::ToNew ( HyperLogLog & new_hll) const
inline
122968 {
122969 const idx_t mult = duckdb_hll::num_registers() / HyperLogLog::M;
122970 // Old implementation used more registers, so we compress the registers, losing some accuracy
122971 for (idx_t i = 0; i < HyperLogLog::M; i++) {
122972 uint8_t max_old = 0;
122973 for (idx_t j = 0; j < mult; j++) {
122974 D_ASSERT(i * mult + j < duckdb_hll::num_registers());
122975 max_old = MaxValue<uint8_t>(max_old, duckdb_hll::get_register(hll, i * mult + j));
122976 }
122977 new_hll.Update(i, max_old);
122978 }
122979 }
uint64_t num_registers()
Get the number of registers.
Definition duckdb.cpp:484436
::uint8_t uint8_t
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ FromNew()

void duckdb::HLLV1::FromNew ( const HyperLogLog & new_hll)
inline
122981 {
122982 const auto new_hll_count = new_hll.Count();
122983 if (new_hll_count == 0) {
122984 return;
122985 }
122986
122987 const idx_t mult = duckdb_hll::num_registers() / HyperLogLog::M;
122988 // When going from less to more registers, we cannot just duplicate the registers,
122989 // as each register in the new HLL is the minimum of 'mult' registers in the old HLL.
122990 // Duplicating will make for VERY large over-estimations. Instead, we do the following:
122991
122992 // Set the first of every 'mult' registers in the old HLL to the value in the new HLL
122993 // This ensures that we can convert NEW to OLD and back to NEW without loss of information
122994 double avg = 0;
122995 for (idx_t i = 0; i < HyperLogLog::M; i++) {
122996 const auto max_new = MinValue(new_hll.GetRegister(i), duckdb_hll::maximum_zeros());
122997 duckdb_hll::set_register(hll, i * mult, max_new);
122998 avg += static_cast<double>(max_new);
122999 }
123000 avg /= static_cast<double>(HyperLogLog::M);
123001
123002 // Using the average will ALWAYS overestimate, so we reduce it a bit here
123003 if (avg > 10) {
123004 avg *= 0.75;
123005 } else if (avg > 2) {
123006 avg -= 2;
123007 }
123008
123009 // Set all other registers to a default value, starting with 0 (the initialization value)
123010 // We optimize the default value in 5 iterations or until OLD count is close to NEW count
123011 double default_val = 0;
123012 for (idx_t opt_idx = 0; opt_idx < 5; opt_idx++) {
123013 if (IsWithinAcceptableRange(new_hll_count, Count())) {
123014 break;
123015 }
123016
123017 // Delta is half the average, then a quarter, etc.
123018 const double delta = avg / static_cast<double>(1 << (opt_idx + 1));
123019 if (Count() > new_hll_count) {
123020 default_val = delta > default_val ? 0 : default_val - delta;
123021 } else {
123022 default_val += delta;
123023 }
123024
123025 // If the default value is, e.g., 3.3, then the first 70% gets value 3, and the rest gets value 4
123026 const double floor_fraction = 1 - (default_val - floor(default_val));
123027 for (idx_t i = 0; i < HyperLogLog::M; i++) {
123028 const auto max_new = MinValue(new_hll.GetRegister(i), duckdb_hll::maximum_zeros());
123029 uint8_t register_value;
123030 if (static_cast<double>(i) / static_cast<double>(HyperLogLog::M) < floor_fraction) {
123031 register_value = ExactNumericCast<uint8_t>(floor(default_val));
123032 } else {
123033 register_value = ExactNumericCast<uint8_t>(ceil(default_val));
123034 }
123035 register_value = MinValue(register_value, max_new);
123036 for (idx_t j = 1; j < mult; j++) {
123037 D_ASSERT(i * mult + j < duckdb_hll::num_registers());
123038 duckdb_hll::set_register(hll, i * mult + j, register_value);
123039 }
123040 }
123041 }
123042 }
void set_register(robj *o, size_t index, uint8_t count)
Set the count of the register.
Definition duckdb.cpp:484451

◆ Count()

idx_t duckdb::HLLV1::Count ( ) const
inlineprivate
123045 {
123046 size_t result;
123047 if (duckdb_hll::hll_count(hll, &result) != HLL_C_OK) {
123048 throw InternalException("Could not count HLL?");
123049 }
123050 return result;
123051 }

◆ IsWithinAcceptableRange()

bool duckdb::HLLV1::IsWithinAcceptableRange ( const idx_t & new_hll_count,
const idx_t & old_hll_count ) const
inlineprivate
123053 {
123054 const auto newd = static_cast<double>(new_hll_count);
123055 const auto oldd = static_cast<double>(old_hll_count);
123056 return MaxValue(newd, oldd) / MinValue(newd, oldd) < ACCEPTABLE_Q_ERROR;
123057 }

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