1313#include " bucket.h"
1414#include " sketch_types.h"
1515
16+ class DenseSketch ;
17+
1618#pragma pack(push,1)
1719struct SparseBucket {
18- uint8_t next; // index of next sparse bucket in this column
19- uint8_t row; // row of sparse bucket
20- Bucket bkt; // actual bucket content
20+ uint16_t next; // index of next sparse bucket in this column
21+ uint8_t row; // row of sparse bucket
22+ Bucket bkt; // actual bucket content
2123};
2224#pragma pack(pop)
2325
@@ -67,9 +69,10 @@ class SparseSketch {
6769 // TODO: evaluate implications of this constant
6870 static constexpr double sparse_bucket_constant = 3 ; // constant factor c (see diagram)
6971 SparseBucket* sparse_buckets; // a pointer into the buckets array
70- uint8_t *ll_metadata; // pointer to heads of column LLs
72+ uint16_t *ll_metadata; // pointer to heads of column LLs
7173 size_t number_of_sparse_buckets = 0 ; // cur number of sparse buckets
7274 size_t sparse_capacity = sparse_bucket_constant * num_columns; // max number of sparse buckets
75+ static constexpr size_t max_columns = uint16_t (-1 ) / sparse_bucket_constant - 1 ;
7376
7477 /* *
7578 * Reallocates the bucket array if necessary to either grow or shrink the dense region
@@ -80,43 +83,43 @@ class SparseSketch {
8083 // These variables let us know how many Buckets to allocate to make space for the SparseBuckets
8184 // and the LL metadata that will use that space
8285 size_t sparse_data_size = ceil(double (sparse_capacity) * sizeof (SparseBucket) / sizeof (Bucket));
83- size_t ll_metadata_size = ceil((double (num_columns) + 1 ) * sizeof (uint8_t ) / sizeof (Bucket));
86+ size_t ll_metadata_size = ceil((double (num_columns) + 1 ) * sizeof (uint16_t ) / sizeof (Bucket));
8487
85- void update_sparse (uint8_t col, const SparseBucket &to_add);
88+ void update_sparse (uint16_t col, const SparseBucket &to_add);
8689 SketchSample sample_sparse (size_t first_col, size_t end_col);
8790
88- inline uint8_t remove_ll_head (size_t col) {
89- uint8_t temp = ll_metadata[col];
91+ inline uint16_t remove_ll_head (size_t col) {
92+ uint16_t temp = ll_metadata[col];
9093 ll_metadata[col] = sparse_buckets[ll_metadata[col]].next ;
9194 return temp;
9295 }
93- inline uint8_t claim_free_bucket () {
94- assert (ll_metadata[num_columns] != uint8_t (-1 ));
96+ inline uint16_t claim_free_bucket () {
97+ assert (ll_metadata[num_columns] != uint16_t (-1 ));
9598 return remove_ll_head (num_columns);
9699 }
97- inline void insert_to_ll_head (size_t col, uint8_t add_idx) {
100+ inline void insert_to_ll_head (size_t col, uint16_t add_idx) {
98101 sparse_buckets[add_idx].next = ll_metadata[col];
99102 ll_metadata[col] = add_idx;
100103 }
101- inline void free_bucket (uint8_t bkt_idx) {
104+ inline void free_bucket (uint16_t bkt_idx) {
102105 sparse_buckets[bkt_idx].row = 0 ;
103106 sparse_buckets[bkt_idx].bkt = {0 , 0 };
104107 insert_to_ll_head (num_columns, bkt_idx);
105108 }
106- inline void insert_to_ll (uint8_t add_idx, SparseBucket &prev) {
109+ inline void insert_to_ll (uint16_t add_idx, SparseBucket &prev) {
107110 sparse_buckets[add_idx].next = prev.next ;
108111 prev.next = add_idx;
109112 }
110113 inline void remove_from_ll (SparseBucket& bkt_to_remove, SparseBucket &prev) {
111114 prev.next = bkt_to_remove.next ;
112115 }
113- inline bool merge_sparse_bkt (uint8_t our_idx, const SparseBucket& oth, uint8_t prev_idx,
116+ inline bool merge_sparse_bkt (uint16_t our_idx, const SparseBucket& oth, uint16_t prev_idx,
114117 size_t col) {
115118 SparseBucket &ours = sparse_buckets[our_idx];
116119 ours.bkt .alpha ^= oth.bkt .alpha ;
117120 ours.bkt .gamma ^= oth.bkt .gamma ;
118121 if (SketchBucket::is_empty (ours.bkt )) {
119- if (prev_idx == uint8_t (-1 ))
122+ if (prev_idx == uint16_t (-1 ))
120123 remove_ll_head (col);
121124 else
122125 remove_from_ll (ours, sparse_buckets[prev_idx]);
@@ -162,11 +165,11 @@ class SparseSketch {
162165
163166 void upd_sparse_ptrs () {
164167 sparse_buckets = (SparseBucket *) &buckets[calc_sparse_index (num_dense_rows)];
165- ll_metadata = (uint8_t *) &buckets[calc_metadata_index (num_dense_rows)];
168+ ll_metadata = (uint16_t *) &buckets[calc_metadata_index (num_dense_rows)];
166169 }
167170
168171 // given another SparseSketch column, merge it into ours
169- void merge_sparse_column (const SparseBucket* oth_sparse_buckets, const uint8_t * oth_ll_metadata,
172+ void merge_sparse_column (const SparseBucket* oth_sparse_buckets, const uint16_t * oth_ll_metadata,
170173 size_t col);
171174 public:
172175 /* *
@@ -274,6 +277,7 @@ class SparseSketch {
274277 void zero_contents ();
275278
276279 friend bool operator ==(const SparseSketch& sketch1, const SparseSketch& sketch2);
280+ friend bool operator ==(const SparseSketch& sparse, const DenseSketch& dense);
277281 friend std::ostream& operator <<(std::ostream& os, const SparseSketch& sketch);
278282
279283 /* *
@@ -294,7 +298,7 @@ class SparseSketch {
294298 // return the size of a sketch given vector size n and number of samples s
295299 static size_t estimate_bytes (size_t /* n*/ , size_t s) {
296300 size_t num_cols = s * default_cols_per_sample;
297- size_t metadata_size = ceil (double (num_cols + 1 ) * sizeof (uint8_t ) / sizeof (Bucket)) * sizeof (Bucket);
301+ size_t metadata_size = ceil (double (num_cols + 1 ) * sizeof (uint16_t ) / sizeof (Bucket)) * sizeof (Bucket);
298302 size_t sparse_size =
299303 ceil (double (num_cols) * sparse_bucket_constant * sizeof (SparseBucket) / sizeof (Bucket)) *
300304 sizeof (Bucket);
0 commit comments