Hashtable.hpp 8.4 KB

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