Hashtable.hpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 <stdexcept>
  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. ZT_ALWAYS_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 std::bad_alloc();
  95. memset(_t,0,sizeof(_Bucket *) * bc);
  96. }
  97. ZT_ALWAYS_INLINE Hashtable(const Hashtable<K,V> &ht) :
  98. Hashtable()
  99. {
  100. *this = ht;
  101. }
  102. ZT_ALWAYS_INLINE ~Hashtable()
  103. {
  104. this->clear();
  105. ::free(_t);
  106. }
  107. ZT_ALWAYS_INLINE Hashtable &operator=(const Hashtable<K,V> &ht)
  108. {
  109. this->clear();
  110. if (ht._s) {
  111. for(unsigned long i=0;i<ht._bc;++i) {
  112. const _Bucket *b = ht._t[i];
  113. while (b) {
  114. this->set(b->k,b->v);
  115. b = b->next;
  116. }
  117. }
  118. }
  119. return *this;
  120. }
  121. /**
  122. * Erase all entries
  123. */
  124. ZT_ALWAYS_INLINE void clear()
  125. {
  126. if (_s) {
  127. for(unsigned long i=0;i<_bc;++i) {
  128. _Bucket *b = _t[i];
  129. while (b) {
  130. _Bucket *const nb = b->next;
  131. delete b;
  132. b = nb;
  133. }
  134. _t[i] = (_Bucket *)0;
  135. }
  136. _s = 0;
  137. }
  138. }
  139. /**
  140. * @return Vector of all keys
  141. */
  142. ZT_ALWAYS_INLINE typename std::vector<K> keys() const
  143. {
  144. typename std::vector<K> k;
  145. if (_s) {
  146. k.reserve(_s);
  147. for(unsigned long i=0;i<_bc;++i) {
  148. _Bucket *b = _t[i];
  149. while (b) {
  150. k.push_back(b->k);
  151. b = b->next;
  152. }
  153. }
  154. }
  155. return k;
  156. }
  157. /**
  158. * Append all keys (in unspecified order) to the supplied vector or list
  159. *
  160. * @param v Vector, list, or other compliant container
  161. * @tparam Type of V (generally inferred)
  162. */
  163. template<typename C>
  164. ZT_ALWAYS_INLINE void appendKeys(C &v) const
  165. {
  166. if (_s) {
  167. for(unsigned long i=0;i<_bc;++i) {
  168. _Bucket *b = _t[i];
  169. while (b) {
  170. v.push_back(b->k);
  171. b = b->next;
  172. }
  173. }
  174. }
  175. }
  176. /**
  177. * @return Vector of all entries (pairs of K,V)
  178. */
  179. ZT_ALWAYS_INLINE typename std::vector< std::pair<K,V> > entries() const
  180. {
  181. typename std::vector< std::pair<K,V> > k;
  182. if (_s) {
  183. k.reserve(_s);
  184. for(unsigned long i=0;i<_bc;++i) {
  185. _Bucket *b = _t[i];
  186. while (b) {
  187. k.push_back(std::pair<K,V>(b->k,b->v));
  188. b = b->next;
  189. }
  190. }
  191. }
  192. return k;
  193. }
  194. /**
  195. * @param k Key
  196. * @return Pointer to value or NULL if not found
  197. */
  198. ZT_ALWAYS_INLINE V *get(const K k)
  199. {
  200. _Bucket *b = _t[_hc(k) % _bc];
  201. while (b) {
  202. if (b->k == k)
  203. return &(b->v);
  204. b = b->next;
  205. }
  206. return (V *)0;
  207. }
  208. ZT_ALWAYS_INLINE const V *get(const K k) const { return const_cast<Hashtable *>(this)->get(k); }
  209. /**
  210. * @param k Key
  211. * @param v Value to fill with result
  212. * @return True if value was found and set (if false, v is not modified)
  213. */
  214. ZT_ALWAYS_INLINE bool get(const K &k,V &v) const
  215. {
  216. _Bucket *b = _t[_hc(k) % _bc];
  217. while (b) {
  218. if (b->k == k) {
  219. v = b->v;
  220. return true;
  221. }
  222. b = b->next;
  223. }
  224. return false;
  225. }
  226. /**
  227. * @param k Key to check
  228. * @return True if key is present
  229. */
  230. ZT_ALWAYS_INLINE bool contains(const K &k) const
  231. {
  232. _Bucket *b = _t[_hc(k) % _bc];
  233. while (b) {
  234. if (b->k == k)
  235. return true;
  236. b = b->next;
  237. }
  238. return false;
  239. }
  240. /**
  241. * @param k Key
  242. * @return True if value was present
  243. */
  244. ZT_ALWAYS_INLINE bool erase(const K &k)
  245. {
  246. const unsigned long bidx = _hc(k) % _bc;
  247. _Bucket *lastb = (_Bucket *)0;
  248. _Bucket *b = _t[bidx];
  249. while (b) {
  250. if (b->k == k) {
  251. if (lastb)
  252. lastb->next = b->next;
  253. else _t[bidx] = b->next;
  254. delete b;
  255. --_s;
  256. return true;
  257. }
  258. lastb = b;
  259. b = b->next;
  260. }
  261. return false;
  262. }
  263. /**
  264. * @param k Key
  265. * @param v Value
  266. * @return Reference to value in table
  267. */
  268. ZT_ALWAYS_INLINE V &set(const K &k,const V &v)
  269. {
  270. const unsigned long h = _hc(k);
  271. unsigned long bidx = h % _bc;
  272. _Bucket *b = _t[bidx];
  273. while (b) {
  274. if (b->k == k) {
  275. b->v = v;
  276. return b->v;
  277. }
  278. b = b->next;
  279. }
  280. if (_s >= _bc) {
  281. _grow();
  282. bidx = h % _bc;
  283. }
  284. b = new _Bucket(k,v);
  285. b->next = _t[bidx];
  286. _t[bidx] = b;
  287. ++_s;
  288. return b->v;
  289. }
  290. /**
  291. * @param k Key
  292. * @return Value, possibly newly created
  293. */
  294. ZT_ALWAYS_INLINE V &operator[](const K k)
  295. {
  296. const unsigned long h = _hc(k);
  297. unsigned long bidx = h % _bc;
  298. _Bucket *b = _t[bidx];
  299. while (b) {
  300. if (b->k == k)
  301. return b->v;
  302. b = b->next;
  303. }
  304. if (_s >= _bc) {
  305. _grow();
  306. bidx = h % _bc;
  307. }
  308. b = new _Bucket(k);
  309. b->next = _t[bidx];
  310. _t[bidx] = b;
  311. ++_s;
  312. return b->v;
  313. }
  314. /**
  315. * @return Number of entries
  316. */
  317. ZT_ALWAYS_INLINE unsigned long size() const { return _s; }
  318. /**
  319. * @return True if table is empty
  320. */
  321. ZT_ALWAYS_INLINE bool empty() const { return (_s == 0); }
  322. private:
  323. template<typename O>
  324. static ZT_ALWAYS_INLINE unsigned long _hc(const O &obj) { return (unsigned long)obj.hashCode(); }
  325. static ZT_ALWAYS_INLINE unsigned long _hc(const uint64_t i) { return (unsigned long)(i ^ (i >> 32U)); }
  326. static ZT_ALWAYS_INLINE unsigned long _hc(const uint32_t i) { return ((unsigned long)i * (unsigned long)0x9e3779b1); }
  327. static ZT_ALWAYS_INLINE unsigned long _hc(const uint16_t i) { return ((unsigned long)i * (unsigned long)0x9e3779b1); }
  328. static ZT_ALWAYS_INLINE unsigned long _hc(const uint8_t i) { return ((unsigned long)i * (unsigned long)0x9e3779b1); }
  329. static ZT_ALWAYS_INLINE unsigned long _hc(const int64_t i) { return (unsigned long)(i ^ (i >> 32U)); }
  330. static ZT_ALWAYS_INLINE unsigned long _hc(const int32_t i) { return ((unsigned long)i * (unsigned long)0x9e3779b1); }
  331. static ZT_ALWAYS_INLINE unsigned long _hc(const int16_t i) { return ((unsigned long)i * (unsigned long)0x9e3779b1); }
  332. static ZT_ALWAYS_INLINE unsigned long _hc(const int8_t i) { return ((unsigned long)i * (unsigned long)0x9e3779b1); }
  333. static ZT_ALWAYS_INLINE unsigned long _hc(void *p) { return ((unsigned long)((uintptr_t)p) * (unsigned long)0x9e3779b1); }
  334. static ZT_ALWAYS_INLINE unsigned long _hc(const void *p) { return ((unsigned long)((uintptr_t)p) * (unsigned long)0x9e3779b1); }
  335. ZT_ALWAYS_INLINE void _grow()
  336. {
  337. const unsigned long nc = _bc * 2;
  338. _Bucket **nt = reinterpret_cast<_Bucket **>(::malloc(sizeof(_Bucket *) * nc));
  339. if (nt) {
  340. for(unsigned long i=0;i<nc;++i)
  341. nt[i] = (_Bucket *)0;
  342. for(unsigned long i=0;i<_bc;++i) {
  343. _Bucket *b = _t[i];
  344. while (b) {
  345. _Bucket *const nb = b->next;
  346. const unsigned long nidx = _hc(b->k) % nc;
  347. b->next = nt[nidx];
  348. nt[nidx] = b;
  349. b = nb;
  350. }
  351. }
  352. ::free(_t);
  353. _t = nt;
  354. _bc = nc;
  355. }
  356. }
  357. _Bucket **_t;
  358. unsigned long _bc;
  359. unsigned long _s;
  360. };
  361. } // namespace ZeroTier
  362. #endif