Hashtable.hpp 8.0 KB

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