Hashtable.hpp 8.1 KB

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