Hashtable.hpp 7.9 KB

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