Hashtable.hpp 8.4 KB

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