Hashtable.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. namespace ZeroTier {
  30. /**
  31. * A minimal hash table implementation for the ZeroTier core
  32. *
  33. * This is not a drop-in replacement for STL containers, and has several
  34. * limitations. It's designed to be small and fast for use in the
  35. * ZeroTier core.
  36. */
  37. template<typename K,typename V>
  38. class Hashtable
  39. {
  40. private:
  41. struct _Bucket
  42. {
  43. _Bucket(const K &k,const V &v) : k(k),v(v) {}
  44. _Bucket(const K &k) : k(k),v() {}
  45. K k;
  46. V v;
  47. _Bucket *next;
  48. };
  49. public:
  50. /**
  51. * A simple forward iterator (different from STL)
  52. *
  53. * It's safe to erase the last key, but not others. Don't use set() since that
  54. * may rehash and invalidate the iterator. Note the erasing the key will destroy
  55. * the targets of the pointers returned by next().
  56. */
  57. class Iterator
  58. {
  59. public:
  60. /**
  61. * @param ht Hash table to iterate over
  62. */
  63. Iterator(Hashtable &ht) :
  64. _idx(0),
  65. _ht(&ht),
  66. _b(ht._t[0])
  67. {
  68. }
  69. /**
  70. * @param kptr Pointer to set to point to next key
  71. * @param vptr Pointer to set to point to next value
  72. * @return True if kptr and vptr are set, false if no more entries
  73. */
  74. inline bool next(K *&kptr,V *&vptr)
  75. {
  76. for(;;) {
  77. if (_b) {
  78. kptr = &(_b->k);
  79. vptr = &(_b->v);
  80. _b = _b->next;
  81. return true;
  82. }
  83. ++_idx;
  84. if (_idx >= _ht->_bc)
  85. return false;
  86. _b = _ht->_t[_idx];
  87. }
  88. }
  89. private:
  90. unsigned long _idx;
  91. Hashtable *_ht;
  92. Hashtable::_Bucket *_b;
  93. };
  94. friend class Hashtable::Iterator;
  95. /**
  96. * @param bc Initial capacity in buckets (default: 128, must be nonzero)
  97. */
  98. Hashtable(unsigned long bc = 128) :
  99. _t(reinterpret_cast<_Bucket **>(::malloc(sizeof(_Bucket *) * bc))),
  100. _bc(bc),
  101. _s(0)
  102. {
  103. if (!_t)
  104. throw std::bad_alloc();
  105. for(unsigned long i=0;i<bc;++i)
  106. _t[i] = (_Bucket *)0;
  107. }
  108. ~Hashtable()
  109. {
  110. clear();
  111. ::free(_t);
  112. }
  113. /**
  114. * Erase all entries
  115. */
  116. inline void clear()
  117. {
  118. if (_s) {
  119. for(unsigned long i=0;i<_bc;++i) {
  120. _Bucket *b = _t[i];
  121. while (b) {
  122. _Bucket *const nb = b->next;
  123. delete b;
  124. b = nb;
  125. }
  126. _t[i] = (_Bucket *)0;
  127. }
  128. _s = 0;
  129. }
  130. }
  131. /**
  132. * @param k Key
  133. * @return Pointer to value or NULL if not found
  134. */
  135. inline V *get(const K &k)
  136. {
  137. _Bucket *b = _t[_hc(k) % _bc];
  138. while (b) {
  139. if (b->k == k)
  140. return &(b->v);
  141. b = b->next;
  142. }
  143. return (V *)0;
  144. }
  145. inline const V *get(const K &k) const { return const_cast<Hashtable *>(this)->get(k); }
  146. /**
  147. * @param k Key
  148. * @return True if value was present
  149. */
  150. inline bool erase(const K &k)
  151. {
  152. const unsigned long bidx = _hc(k) % _bc;
  153. _Bucket *lastb = (_Bucket *)0;
  154. _Bucket *b = _t[bidx];
  155. while (b) {
  156. if (b->k == k) {
  157. if (lastb)
  158. lastb->next = b->next;
  159. else _t[bidx] = b->next;
  160. delete b;
  161. --_s;
  162. return true;
  163. }
  164. lastb = b;
  165. b = b->next;
  166. }
  167. return false;
  168. }
  169. /**
  170. * @param k Key
  171. * @param v Value
  172. * @return Reference to value in table
  173. */
  174. inline V &set(const K &k,const V &v)
  175. {
  176. const unsigned long bidx = _hc(k) % _bc;
  177. _Bucket *b = _t[bidx];
  178. while (b) {
  179. if (b->k == k) {
  180. b->v = v;
  181. return b->v;
  182. }
  183. b = b->next;
  184. }
  185. if (_s >= _bc)
  186. _grow();
  187. b = new _Bucket(k,v);
  188. b->next = _t[bidx];
  189. _t[bidx] = b;
  190. ++_s;
  191. return b->v;
  192. }
  193. /**
  194. * @param k Key
  195. * @return Value, possibly newly created
  196. */
  197. inline V &operator[](const K &k)
  198. {
  199. const unsigned long bidx = _hc(k) % _bc;
  200. _Bucket *b = _t[bidx];
  201. while (b) {
  202. if (b->k == k)
  203. return b->v;
  204. b = b->next;
  205. }
  206. if (_s >= _bc)
  207. _grow();
  208. b = new _Bucket(k);
  209. b->next = _t[bidx];
  210. _t[bidx] = b;
  211. ++_s;
  212. return b->v;
  213. }
  214. /**
  215. * @return Number of entries
  216. */
  217. inline unsigned long size() const throw() { return _s; }
  218. /**
  219. * @return True if table is empty
  220. */
  221. inline bool empty() const throw() { return (_s == 0); }
  222. private:
  223. template<typename O>
  224. static inline unsigned long _hc(const O &obj)
  225. {
  226. return obj.hashCode();
  227. }
  228. static inline unsigned long _hc(const uint64_t i)
  229. {
  230. // NOTE: this is fine for network IDs, but might be bad for other kinds
  231. // of IDs if they are not evenly or randomly distributed.
  232. return (unsigned long)((i ^ (i >> 32)) * 2654435761ULL);
  233. }
  234. inline void _grow()
  235. {
  236. const unsigned long nc = _bc * 2;
  237. _Bucket **nt = reinterpret_cast<_Bucket **>(::malloc(sizeof(_Bucket *) * nc));
  238. if (nt) {
  239. for(unsigned long i=0;i<nc;++i)
  240. nt[i] = (_Bucket *)0;
  241. for(unsigned long i=0;i<_bc;++i) {
  242. _Bucket *b = _t[i];
  243. while (b) {
  244. _Bucket *const nb = b->next;
  245. const unsigned long nidx = _hc(b->k) % nc;
  246. b->next = nt[nidx];
  247. nt[nidx] = b;
  248. b = nb;
  249. }
  250. }
  251. ::free(_t);
  252. _t = nt;
  253. _bc = nc;
  254. }
  255. }
  256. _Bucket **_t;
  257. unsigned long _bc;
  258. unsigned long _s;
  259. };
  260. } // namespace ZeroTier
  261. #endif