AllianceDB  0.0.1
AllianceDB is an open-source suite, including benchmarks and libs for evaluating and improving stream operation algorithms on modern hardwares.
DupicatedHashTable.hpp
1 //
2 // Created by Xianzhi Zeng 07/02/2022
3 // This is a pure STL header, so the corresponding *.cpp is empty
4 //
5 #pragma once
6 #ifndef _DUPLICATEDHASHTABLE_HPP_
7 #define _DUPLICATEDHASHTABLE_HPP_
8 using namespace std;
9 #include <vector>
10 #include <cstdint>
11 #include <iostream>
12 #include <memory>
13 #include <unordered_map>
14 namespace INTELLI {
15 /*class:DupicatedHashTable
16 description: integrted an unsorted_map, and can deal with duplicated key
17 note: almost all part of this class is similar to the original unsorted_map,
18 However, the find(key) will return the vector of the duplicated keys.
19 You can use the find(key)[index] to get the index-th value in the vector
20 date:20220207
21 */
22 template<typename Key, typename Tp,
23  typename Hash = hash<Key>,
24  typename _SubKey=size_t
25  /*typename _Pred = equal_to<_Key>,
26  typename _Alloc = allocator<std::pair<const _Key, _Tp>>*/>
28  private:
29  class innerStore {
30  public:
31  Tp tps;
32  _SubKey sk;
33  innerStore(Tp tps0, _SubKey sk0) {
34  tps = tps0;
35  sk = sk0;
36  }
37  innerStore(Tp tps0) {
38  tps = tps0;
39  }
40  ~innerStore() {
41 
42  }
43  };
44  /* data */
45  unordered_map<Key, std::vector<innerStore>, Hash/*,_Pred,_Alloc*/ > map;
46  public:
47  DupicatedHashTable(/* args */) {
48  map = unordered_map<Key, std::vector<innerStore> >();
49  }
50  ~DupicatedHashTable() = default;
51  //the empty()
52  bool empty() const noexcept { return map.empty(); }
53  //the emplace, return the key duplication index of emplaced item
54  size_t emplace(Key key, Tp value) {
55  if (map.find(key) == map.end()) {
56  std::vector<innerStore> vecKey = std::vector<innerStore>();
57  vecKey.push_back(innerStore(value));
58  map.emplace(key, vecKey);
59  return 0;
60  } else {
61 
62  map.find(key)->second.push_back(innerStore(value));
63  return map.find(key)->second.size() - 1;
64  }
65 
66  }
67  size_t emplace(Key key, Tp value, _SubKey sk) {
68  auto findru = map.find(key);
69  if (findru == map.end()) {
70  std::vector<innerStore> vecKey = std::vector<innerStore>();
71  vecKey.push_back(innerStore(value, sk));
72  map.emplace(key, vecKey);
73  return 0;
74  } else {
75  //cout<<"key duplication"<<endl;
76  findru->second.push_back(innerStore(value, sk));
77  return findru->second.size() - 1;
78  }
79 
80  }
81  auto find(Key key) {
82  return map.find(key);
83  }
84  auto begin() {
85  return map.begin();
86  }
87  auto end() {
88  return map.end();
89  }
90  auto size() {
91  return map.size();
92  }
93  //everything in this key will be erased
94  auto erase(Key key) {
95  return map.erase(key);
96  }
97  //only try to erase the specific duplicated item (if it exists)
98  bool eraseWithSubKey(Key key, _SubKey sk) { // map.erase(key);
99  auto k = map.find(key);
100  if (k == map.end()) {
101  return false;
102  }
103  if (k->second.size() == 1) {
104  map.erase(key);
105  return true;
106  }
107  auto iter = k->second.begin();
108  while (iter != k->second.end()) {
109  if (iter->sk == sk) {
110  iter = k->second.erase(iter);
111  } else {
112  ++iter;
113  }
114  }
115  if (k->second.size() == 0) {
116  map.erase(key);
117  }
118  return true;
119  }
120  bool erase(Key key, size_t duplication) {
121  auto k = map.find(key);
122  if (k == map.end()) {
123  return false;
124  }
125  if (k->second.size() < duplication + 1) {
126  cout << "error:" << k->second.size() << ":" << (duplication + 1) << endl;
127  return false;
128  }
129  if (k->second.size() == 1) {
130  map.erase(key);
131  return true;
132  }
133  k->second.erase(k->second.begin() + duplication);
134 
135  return true;
136  }
137  //only try to erase the specific duplicated item from start to end (if they exist)
138  bool erase(Key key, size_t duplicationS, size_t duplicationE) {
139  auto k = map.find(key);
140  if (duplicationS > duplicationE) {
141  return false;
142  }
143  if (k == map.end()) {
144  return false;
145  }
146  if (k->second.size() < duplicationE + 1) {
147  return false;
148  }
149  k->second.erase(k->second.begin() + duplicationS, k->second.begin() + duplicationE + 1);
150  // if nothing left, clear this entry
151  if (k->second.size() == 0) {
152  map.erase(key);
153  }
154  return true;
155  }
156 
157 };
158 
159 }
160 
161 #endif
Definition: DupicatedHashTable.hpp:27
Definition: DatasetTool.h:10