Hashtable.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2023-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_HASHTABLE_HPP
  14. #define ZT_HASHTABLE_HPP
  15. #include "Constants.hpp"
  16. namespace ZeroTier {
  17. /**
  18. * A minimal hash table implementation for the ZeroTier core
  19. *
  20. * This is optimized for smaller data sets.
  21. */
  22. template<typename K,typename V>
  23. class Hashtable
  24. {
  25. private:
  26. struct _Bucket
  27. {
  28. ZT_ALWAYS_INLINE _Bucket(const K &k,const V &v) : k(k),v(v) {}
  29. ZT_ALWAYS_INLINE _Bucket(const K &k) : k(k),v() {}
  30. ZT_ALWAYS_INLINE _Bucket(const _Bucket &b) : k(b.k),v(b.v) {}
  31. ZT_ALWAYS_INLINE _Bucket &operator=(const _Bucket &b) { k = b.k; v = b.v; return *this; }
  32. _Bucket *next; // must be set manually for each _Bucket
  33. const K k;
  34. V v;
  35. };
  36. public:
  37. /**
  38. * A simple forward iterator (different from STL)
  39. *
  40. * It's safe to erase the last key, but not others. Don't use set() since that
  41. * may rehash and invalidate the iterator. Note the erasing the key will destroy
  42. * the targets of the pointers returned by next().
  43. */
  44. class Iterator
  45. {
  46. public:
  47. /**
  48. * @param ht Hash table to iterate over
  49. */
  50. ZT_ALWAYS_INLINE Iterator(Hashtable &ht) :
  51. _idx(0),
  52. _ht(&ht),
  53. _b(ht._t[0])
  54. {
  55. }
  56. /**
  57. * @param kptr Pointer to set to point to next key
  58. * @param vptr Pointer to set to point to next value
  59. * @return True if kptr and vptr are set, false if no more entries
  60. */
  61. ZT_ALWAYS_INLINE bool next(K *&kptr,V *&vptr)
  62. {
  63. for(;;) {
  64. if (_b) {
  65. kptr = &(_b->k);
  66. vptr = &(_b->v);
  67. _b = _b->next;
  68. return true;
  69. }
  70. ++_idx;
  71. if (_idx >= _ht->_bc)
  72. return false;
  73. _b = _ht->_t[_idx];
  74. }
  75. }
  76. private:
  77. unsigned long _idx;
  78. Hashtable *_ht;
  79. _Bucket *_b;
  80. };
  81. //friend class Hashtable<K,V>::Iterator;
  82. /**
  83. * @param bc Initial capacity in buckets (default: 32, must be nonzero)
  84. */
  85. ZT_ALWAYS_INLINE Hashtable(unsigned long bc = 32) :
  86. _t(reinterpret_cast<_Bucket **>(::malloc(sizeof(_Bucket *) * bc))),
  87. _bc(bc),
  88. _s(0)
  89. {
  90. if (!_t)
  91. throw ZT_EXCEPTION_OUT_OF_MEMORY;
  92. for(unsigned long i=0;i<bc;++i)
  93. _t[i] = (_Bucket *)0;
  94. }
  95. ZT_ALWAYS_INLINE Hashtable(const Hashtable<K,V> &ht) :
  96. _t(reinterpret_cast<_Bucket **>(::malloc(sizeof(_Bucket *) * ht._bc))),
  97. _bc(ht._bc),
  98. _s(ht._s)
  99. {
  100. if (!_t)
  101. throw ZT_EXCEPTION_OUT_OF_MEMORY;
  102. for(unsigned long i=0;i<_bc;++i)
  103. _t[i] = (_Bucket *)0;
  104. for(unsigned long i=0;i<_bc;++i) {
  105. const _Bucket *b = ht._t[i];
  106. while (b) {
  107. _Bucket *nb = new _Bucket(*b);
  108. nb->next = _t[i];
  109. _t[i] = nb;
  110. b = b->next;
  111. }
  112. }
  113. }
  114. ZT_ALWAYS_INLINE ~Hashtable()
  115. {
  116. this->clear();
  117. ::free(_t);
  118. }
  119. ZT_ALWAYS_INLINE Hashtable &operator=(const Hashtable<K,V> &ht)
  120. {
  121. this->clear();
  122. if (ht._s) {
  123. for(unsigned long i=0;i<ht._bc;++i) {
  124. const _Bucket *b = ht._t[i];
  125. while (b) {
  126. this->set(b->k,b->v);
  127. b = b->next;
  128. }
  129. }
  130. }
  131. return *this;
  132. }
  133. /**
  134. * Erase all entries
  135. */
  136. ZT_ALWAYS_INLINE void clear()
  137. {
  138. if (_s) {
  139. for(unsigned long i=0;i<_bc;++i) {
  140. _Bucket *b = _t[i];
  141. while (b) {
  142. _Bucket *const nb = b->next;
  143. delete b;
  144. b = nb;
  145. }
  146. _t[i] = (_Bucket *)0;
  147. }
  148. _s = 0;
  149. }
  150. }
  151. /**
  152. * @return Vector of all keys
  153. */
  154. ZT_ALWAYS_INLINE typename std::vector<K> keys() const
  155. {
  156. typename std::vector<K> k;
  157. if (_s) {
  158. k.reserve(_s);
  159. for(unsigned long i=0;i<_bc;++i) {
  160. _Bucket *b = _t[i];
  161. while (b) {
  162. k.push_back(b->k);
  163. b = b->next;
  164. }
  165. }
  166. }
  167. return k;
  168. }
  169. /**
  170. * Append all keys (in unspecified order) to the supplied vector or list
  171. *
  172. * @param v Vector, list, or other compliant container
  173. * @tparam Type of V (generally inferred)
  174. */
  175. template<typename C>
  176. ZT_ALWAYS_INLINE void appendKeys(C &v) const
  177. {
  178. if (_s) {
  179. for(unsigned long i=0;i<_bc;++i) {
  180. _Bucket *b = _t[i];
  181. while (b) {
  182. v.push_back(b->k);
  183. b = b->next;
  184. }
  185. }
  186. }
  187. }
  188. /**
  189. * @return Vector of all entries (pairs of K,V)
  190. */
  191. ZT_ALWAYS_INLINE typename std::vector< std::pair<K,V> > entries() const
  192. {
  193. typename std::vector< std::pair<K,V> > k;
  194. if (_s) {
  195. k.reserve(_s);
  196. for(unsigned long i=0;i<_bc;++i) {
  197. _Bucket *b = _t[i];
  198. while (b) {
  199. k.push_back(std::pair<K,V>(b->k,b->v));
  200. b = b->next;
  201. }
  202. }
  203. }
  204. return k;
  205. }
  206. /**
  207. * @param k Key
  208. * @return Pointer to value or NULL if not found
  209. */
  210. ZT_ALWAYS_INLINE V *get(const K k)
  211. {
  212. _Bucket *b = _t[_hc(k) % _bc];
  213. while (b) {
  214. if (b->k == k)
  215. return &(b->v);
  216. b = b->next;
  217. }
  218. return (V *)0;
  219. }
  220. ZT_ALWAYS_INLINE const V *get(const K k) const { return const_cast<Hashtable *>(this)->get(k); }
  221. /**
  222. * @param k Key
  223. * @param v Value to fill with result
  224. * @return True if value was found and set (if false, v is not modified)
  225. */
  226. ZT_ALWAYS_INLINE bool get(const K &k,V &v) const
  227. {
  228. _Bucket *b = _t[_hc(k) % _bc];
  229. while (b) {
  230. if (b->k == k) {
  231. v = b->v;
  232. return true;
  233. }
  234. b = b->next;
  235. }
  236. return false;
  237. }
  238. /**
  239. * @param k Key to check
  240. * @return True if key is present
  241. */
  242. ZT_ALWAYS_INLINE bool contains(const K &k) const
  243. {
  244. _Bucket *b = _t[_hc(k) % _bc];
  245. while (b) {
  246. if (b->k == k)
  247. return true;
  248. b = b->next;
  249. }
  250. return false;
  251. }
  252. /**
  253. * @param k Key
  254. * @return True if value was present
  255. */
  256. ZT_ALWAYS_INLINE bool erase(const K &k)
  257. {
  258. const unsigned long bidx = _hc(k) % _bc;
  259. _Bucket *lastb = (_Bucket *)0;
  260. _Bucket *b = _t[bidx];
  261. while (b) {
  262. if (b->k == k) {
  263. if (lastb)
  264. lastb->next = b->next;
  265. else _t[bidx] = b->next;
  266. delete b;
  267. --_s;
  268. return true;
  269. }
  270. lastb = b;
  271. b = b->next;
  272. }
  273. return false;
  274. }
  275. /**
  276. * @param k Key
  277. * @param v Value
  278. * @return Reference to value in table
  279. */
  280. ZT_ALWAYS_INLINE V &set(const K &k,const V &v)
  281. {
  282. const unsigned long h = _hc(k);
  283. unsigned long bidx = h % _bc;
  284. _Bucket *b = _t[bidx];
  285. while (b) {
  286. if (b->k == k) {
  287. b->v = v;
  288. return b->v;
  289. }
  290. b = b->next;
  291. }
  292. if (_s >= _bc) {
  293. _grow();
  294. bidx = h % _bc;
  295. }
  296. b = new _Bucket(k,v);
  297. b->next = _t[bidx];
  298. _t[bidx] = b;
  299. ++_s;
  300. return b->v;
  301. }
  302. /**
  303. * @param k Key
  304. * @return Value, possibly newly created
  305. */
  306. ZT_ALWAYS_INLINE V &operator[](const K k)
  307. {
  308. const unsigned long h = _hc(k);
  309. unsigned long bidx = h % _bc;
  310. _Bucket *b = _t[bidx];
  311. while (b) {
  312. if (b->k == k)
  313. return b->v;
  314. b = b->next;
  315. }
  316. if (_s >= _bc) {
  317. _grow();
  318. bidx = h % _bc;
  319. }
  320. b = new _Bucket(k);
  321. b->next = _t[bidx];
  322. _t[bidx] = b;
  323. ++_s;
  324. return b->v;
  325. }
  326. /**
  327. * @return Number of entries
  328. */
  329. ZT_ALWAYS_INLINE unsigned long size() const { return _s; }
  330. /**
  331. * @return True if table is empty
  332. */
  333. ZT_ALWAYS_INLINE bool empty() const { return (_s == 0); }
  334. private:
  335. template<typename O>
  336. static ZT_ALWAYS_INLINE unsigned long _hc(const O &obj) { return (unsigned long)obj.hashCode(); }
  337. static ZT_ALWAYS_INLINE unsigned long _hc(const uint64_t i) { return (unsigned long)(i ^ (i >> 32)); }
  338. static ZT_ALWAYS_INLINE unsigned long _hc(const uint32_t i) { return ((unsigned long)i * (unsigned long)0x9e3779b1); }
  339. static ZT_ALWAYS_INLINE unsigned long _hc(const uint16_t i) { return ((unsigned long)i * (unsigned long)0x9e3779b1); }
  340. static ZT_ALWAYS_INLINE unsigned long _hc(const int i) { return ((unsigned long)i * (unsigned long)0x9e3379b1); }
  341. static ZT_ALWAYS_INLINE unsigned long _hc(void *p) { return ((unsigned long)((uintptr_t)p) * (unsigned long)0x9e3779b1); }
  342. static ZT_ALWAYS_INLINE unsigned long _hc(const void *p) { return ((unsigned long)((uintptr_t)p) * (unsigned long)0x9e3779b1); }
  343. ZT_ALWAYS_INLINE void _grow()
  344. {
  345. const unsigned long nc = _bc * 2;
  346. _Bucket **nt = reinterpret_cast<_Bucket **>(::malloc(sizeof(_Bucket *) * nc));
  347. if (nt) {
  348. for(unsigned long i=0;i<nc;++i)
  349. nt[i] = (_Bucket *)0;
  350. for(unsigned long i=0;i<_bc;++i) {
  351. _Bucket *b = _t[i];
  352. while (b) {
  353. _Bucket *const nb = b->next;
  354. const unsigned long nidx = _hc(b->k) % nc;
  355. b->next = nt[nidx];
  356. nt[nidx] = b;
  357. b = nb;
  358. }
  359. }
  360. ::free(_t);
  361. _t = nt;
  362. _bc = nc;
  363. }
  364. }
  365. _Bucket **_t;
  366. unsigned long _bc;
  367. unsigned long _s;
  368. };
  369. } // namespace ZeroTier
  370. #endif