Hashtable.hpp 8.2 KB

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