Hashtable.hpp 8.3 KB

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