Hashtable.hpp 8.5 KB

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