datasketches-cpp
AuxHashMap.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 #ifndef _AUXHASHMAP_HPP_
21 #define _AUXHASHMAP_HPP_
22 
23 #include <iostream>
24 #include <memory>
25 #include <functional>
26 
27 #include "coupon_iterator.hpp"
28 
29 namespace datasketches {
30 
31 template<typename A>
32 class AuxHashMap final {
33  public:
34  AuxHashMap(uint8_t lgAuxArrInts, uint8_t lgConfigK, const A& allocator);
35  static AuxHashMap* newAuxHashMap(uint8_t lgAuxArrInts, uint8_t lgConfigK, const A& allocator);
36  static AuxHashMap* newAuxHashMap(const AuxHashMap<A>& that);
37 
38  static AuxHashMap* deserialize(const void* bytes, size_t len,
39  uint8_t lgConfigK,
40  uint32_t auxCount, uint8_t lgAuxArrInts,
41  bool srcCompact, const A& allocator);
42  static AuxHashMap* deserialize(std::istream& is, uint8_t lgConfigK,
43  uint32_t auxCount, uint8_t lgAuxArrInts,
44  bool srcCompact, const A& allocator);
45  virtual ~AuxHashMap() = default;
46  static std::function<void(AuxHashMap<A>*)> make_deleter();
47 
48  AuxHashMap* copy() const;
49  uint32_t getUpdatableSizeBytes() const;
50  uint32_t getCompactSizeBytes() const;
51 
52  uint32_t getAuxCount() const;
53  uint32_t* getAuxIntArr();
54  uint8_t getLgAuxArrInts() const;
55 
56  coupon_iterator<A> begin(bool all = false) const;
57  coupon_iterator<A> end() const;
58 
59  void mustAdd(uint32_t slotNo, uint8_t value);
60  uint8_t mustFindValueFor(uint32_t slotNo) const;
61  void mustReplace(uint32_t slotNo, uint8_t value);
62 
63  private:
64  typedef typename std::allocator_traits<A>::template rebind_alloc<AuxHashMap<A>> ahmAlloc;
65 
66  using vector_int = std::vector<uint32_t, typename std::allocator_traits<A>::template rebind_alloc<uint32_t>>;
67 
68  // static so it can be used when resizing
69  static int32_t find(const uint32_t* auxArr, uint8_t lgAuxArrInts, uint8_t lgConfigK, uint32_t slotNo);
70 
71  void checkGrow();
72  void growAuxSpace();
73 
74  const uint8_t lgConfigK;
75  uint8_t lgAuxArrInts;
76  uint32_t auxCount;
77  vector_int entries;
78 };
79 
80 }
81 
82 #include "AuxHashMap-internal.hpp"
83 
84 #endif /* _AUXHASHMAP_HPP_ */
DataSketches namespace.
Definition: binomial_bounds.hpp:38