datasketches-cpp
u32_table.hpp
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 // author Kevin Lang, Oath Research
21 
22 #ifndef U32_TABLE_HPP_
23 #define U32_TABLE_HPP_
24 
25 // This is a highly specialized hash table that was designed
26 // to be a part of the library's CPC sketch implementation
27 
28 #include "cpc_common.hpp"
29 
30 namespace datasketches {
31 
32 static const uint32_t U32_TABLE_UPSIZE_NUMER = 3LL;
33 static const uint32_t U32_TABLE_UPSIZE_DENOM = 4LL;
34 
35 static const uint32_t U32_TABLE_DOWNSIZE_NUMER = 1LL;
36 static const uint32_t U32_TABLE_DOWNSIZE_DENOM = 4LL;
37 
38 template<typename A>
39 class u32_table {
40 public:
41  using vector_u32 = std::vector<uint32_t, typename std::allocator_traits<A>::template rebind_alloc<uint32_t>>;
42 
43  u32_table(const A& allocator);
44  u32_table(uint8_t lg_size, uint8_t num_valid_bits, const A& allocator);
45 
46  inline uint32_t get_num_items() const;
47  inline const uint32_t* get_slots() const;
48  inline uint8_t get_lg_size() const;
49  inline void clear();
50 
51  // returns true iff the item was new and was therefore added to the table
52  inline bool maybe_insert(uint32_t item);
53  // returns true iff the item was present and was therefore removed from the table
54  inline bool maybe_delete(uint32_t item);
55 
56  static u32_table make_from_pairs(const uint32_t* pairs, uint32_t num_pairs, uint8_t lg_k, const A& allocator);
57 
58  vector_u32 unwrapping_get_items() const;
59 
60  static void merge(
61  const uint32_t* arr_a, size_t start_a, size_t length_a, // input
62  const uint32_t* arr_b, size_t start_b, size_t length_b, // input
63  uint32_t* arr_c, size_t start_c // output
64  );
65 
66  static void introspective_insertion_sort(uint32_t* a, size_t l, size_t r);
67  static void knuth_shell_sort3(uint32_t* a, size_t l, size_t r);
68 
69 private:
70 
71  uint8_t lg_size; // log2 of number of slots
72  uint8_t num_valid_bits;
73  uint32_t num_items;
74  vector_u32 slots;
75 
76  inline uint32_t lookup(uint32_t item) const;
77  inline void must_insert(uint32_t item);
78  inline void rebuild(uint8_t new_lg_size);
79 };
80 
81 } /* namespace datasketches */
82 
83 #include "u32_table_impl.hpp"
84 
85 #endif
DataSketches namespace.
Definition: binomial_bounds.hpp:38