LinearHashTable.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. //
  2. // LinearHashTable.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/LinearHashTable.h#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Hashing
  8. // Module: LinearHashTable
  9. //
  10. // Definition of the LinearHashTable class.
  11. //
  12. // Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_LinearHashTable_INCLUDED
  18. #define Foundation_LinearHashTable_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #include "Poco/Hash.h"
  21. #include <functional>
  22. #include <algorithm>
  23. #include <vector>
  24. #include <utility>
  25. #include <cstddef>
  26. namespace Poco {
  27. template <class Value, class HashFunc = Hash<Value> >
  28. class LinearHashTable
  29. /// This class implements a linear hash table.
  30. ///
  31. /// In a linear hash table, the available address space
  32. /// grows or shrinks dynamically. A linar hash table thus
  33. /// supports any number of insertions or deletions without
  34. /// lookup or insertion performance deterioration.
  35. ///
  36. /// Linear hashing was discovered by Witold Litwin in 1980
  37. /// and described in the paper LINEAR HASHING: A NEW TOOL FOR FILE AND TABLE ADDRESSING.
  38. ///
  39. /// For more information on linear hashing, see <http://en.wikipedia.org/wiki/Linear_hash>.
  40. ///
  41. /// The LinearHashTable is not thread safe.
  42. ///
  43. /// Value must support comparison for equality.
  44. ///
  45. /// Find, insert and delete operations are basically O(1) with regard
  46. /// to the total number of elements in the table, and O(N) with regard
  47. /// to the number of elements in the bucket where the element is stored.
  48. /// On average, every bucket stores one element; the exact number depends
  49. /// on the quality of the hash function. In most cases, the maximum number of
  50. /// elements in a bucket should not exceed 3.
  51. {
  52. public:
  53. typedef Value ValueType;
  54. typedef Value& Reference;
  55. typedef const Value& ConstReference;
  56. typedef Value* Pointer;
  57. typedef const Value* ConstPointer;
  58. typedef HashFunc Hash;
  59. typedef std::vector<Value> Bucket;
  60. typedef std::vector<Bucket> BucketVec;
  61. typedef typename Bucket::iterator BucketIterator;
  62. typedef typename BucketVec::iterator BucketVecIterator;
  63. class ConstIterator: public std::iterator<std::forward_iterator_tag, Value>
  64. {
  65. public:
  66. ConstIterator(): _initialized(false)
  67. {
  68. }
  69. ConstIterator(const BucketVecIterator& vecIt, const BucketVecIterator& endIt, const BucketIterator& buckIt):
  70. _vecIt(vecIt),
  71. _endIt(endIt),
  72. _buckIt(buckIt),
  73. _initialized(true)
  74. {
  75. }
  76. ConstIterator(const ConstIterator& it):
  77. _vecIt(it._vecIt),
  78. _endIt(it._endIt),
  79. _buckIt(it._buckIt),
  80. _initialized(it._initialized)
  81. {
  82. }
  83. ConstIterator& operator = (const ConstIterator& it)
  84. {
  85. ConstIterator tmp(it);
  86. swap(tmp);
  87. return *this;
  88. }
  89. void swap(ConstIterator& it)
  90. {
  91. using std::swap;
  92. // uninitialized iterators crash when swapped
  93. if (_initialized)
  94. {
  95. swap(_vecIt, it._vecIt);
  96. swap(_endIt, it._endIt);
  97. swap(_buckIt, it._buckIt);
  98. swap(_initialized, it._initialized);
  99. }
  100. else
  101. {
  102. _vecIt = it._vecIt;
  103. _endIt = it._endIt;
  104. _buckIt = it._buckIt;
  105. _initialized = it._initialized;
  106. }
  107. }
  108. bool operator == (const ConstIterator& it) const
  109. {
  110. return _vecIt == it._vecIt && (_vecIt == _endIt || _buckIt == it._buckIt);
  111. }
  112. bool operator != (const ConstIterator& it) const
  113. {
  114. return _vecIt != it._vecIt || (_vecIt != _endIt && _buckIt != it._buckIt);
  115. }
  116. const typename Bucket::value_type& operator * () const
  117. {
  118. return *_buckIt;
  119. }
  120. const typename Bucket::value_type* operator -> () const
  121. {
  122. return &*_buckIt;
  123. }
  124. ConstIterator& operator ++ () // prefix
  125. {
  126. if (_vecIt != _endIt)
  127. {
  128. ++_buckIt;
  129. while (_vecIt != _endIt && _buckIt == _vecIt->end())
  130. {
  131. ++_vecIt;
  132. if (_vecIt != _endIt) _buckIt = _vecIt->begin();
  133. }
  134. }
  135. return *this;
  136. }
  137. ConstIterator operator ++ (int) // postfix
  138. {
  139. ConstIterator tmp(*this);
  140. ++*this;
  141. return tmp;
  142. }
  143. protected:
  144. BucketVecIterator _vecIt;
  145. BucketVecIterator _endIt;
  146. BucketIterator _buckIt;
  147. bool _initialized;
  148. friend class LinearHashTable;
  149. };
  150. class Iterator: public ConstIterator
  151. {
  152. public:
  153. Iterator()
  154. {
  155. }
  156. Iterator(const BucketVecIterator& vecIt, const BucketVecIterator& endIt, const BucketIterator& buckIt):
  157. ConstIterator(vecIt, endIt, buckIt)
  158. {
  159. }
  160. Iterator(const Iterator& it):
  161. ConstIterator(it)
  162. {
  163. }
  164. Iterator& operator = (const Iterator& it)
  165. {
  166. Iterator tmp(it);
  167. ConstIterator::swap(tmp);
  168. return *this;
  169. }
  170. void swap(Iterator& it)
  171. {
  172. ConstIterator::swap(it);
  173. }
  174. typename Bucket::value_type& operator * ()
  175. {
  176. return *this->_buckIt;
  177. }
  178. const typename Bucket::value_type& operator * () const
  179. {
  180. return *this->_buckIt;
  181. }
  182. typename Bucket::value_type* operator -> ()
  183. {
  184. return &*this->_buckIt;
  185. }
  186. const typename Bucket::value_type* operator -> () const
  187. {
  188. return &*this->_buckIt;
  189. }
  190. Iterator& operator ++ () // prefix
  191. {
  192. ConstIterator::operator ++ ();
  193. return *this;
  194. }
  195. Iterator operator ++ (int) // postfix
  196. {
  197. Iterator tmp(*this);
  198. ++*this;
  199. return tmp;
  200. }
  201. friend class LinearHashTable;
  202. };
  203. LinearHashTable(std::size_t initialReserve = 64):
  204. _split(0),
  205. _front(1),
  206. _size(0)
  207. /// Creates the LinearHashTable, using the given initialReserve.
  208. {
  209. _buckets.reserve(calcSize(initialReserve));
  210. _buckets.push_back(Bucket());
  211. }
  212. LinearHashTable(const LinearHashTable& table):
  213. _buckets(table._buckets),
  214. _split(table._split),
  215. _front(table._front),
  216. _size(table._size)
  217. /// Creates the LinearHashTable by copying another one.
  218. {
  219. }
  220. ~LinearHashTable()
  221. /// Destroys the LinearHashTable.
  222. {
  223. }
  224. LinearHashTable& operator = (const LinearHashTable& table)
  225. /// Assigns another LinearHashTable.
  226. {
  227. LinearHashTable tmp(table);
  228. swap(tmp);
  229. return *this;
  230. }
  231. void swap(LinearHashTable& table)
  232. /// Swaps the LinearHashTable with another one.
  233. {
  234. using std::swap;
  235. swap(_buckets, table._buckets);
  236. swap(_split, table._split);
  237. swap(_front, table._front);
  238. swap(_size, table._size);
  239. }
  240. ConstIterator begin() const
  241. /// Returns an iterator pointing to the first entry, if one exists.
  242. {
  243. BucketVecIterator it(_buckets.begin());
  244. BucketVecIterator end(_buckets.end());
  245. while (it != end && it->empty())
  246. {
  247. ++it;
  248. }
  249. if (it == end)
  250. return this->end();
  251. else
  252. return ConstIterator(it, end, it->begin());
  253. }
  254. ConstIterator end() const
  255. /// Returns an iterator pointing to the end of the table.
  256. {
  257. return ConstIterator(_buckets.end(), _buckets.end(), _buckets.front().end());
  258. }
  259. Iterator begin()
  260. /// Returns an iterator pointing to the first entry, if one exists.
  261. {
  262. BucketVecIterator it(_buckets.begin());
  263. BucketVecIterator end(_buckets.end());
  264. while (it != end && it->empty())
  265. {
  266. ++it;
  267. }
  268. if (it == end)
  269. return this->end();
  270. else
  271. return Iterator(it, end, it->begin());
  272. }
  273. Iterator end()
  274. /// Returns an iterator pointing to the end of the table.
  275. {
  276. return Iterator(_buckets.end(), _buckets.end(), _buckets.front().end());
  277. }
  278. ConstIterator find(const Value& value) const
  279. /// Finds an entry in the table.
  280. {
  281. std::size_t addr = bucketAddress(value);
  282. BucketVecIterator it(_buckets.begin() + addr);
  283. BucketIterator buckIt(std::find(it->begin(), it->end(), value));
  284. if (buckIt != it->end())
  285. return ConstIterator(it, _buckets.end(), buckIt);
  286. else
  287. return end();
  288. }
  289. Iterator find(const Value& value)
  290. /// Finds an entry in the table.
  291. {
  292. std::size_t addr = bucketAddress(value);
  293. BucketVecIterator it(_buckets.begin() + addr);
  294. BucketIterator buckIt(std::find(it->begin(), it->end(), value));
  295. if (buckIt != it->end())
  296. return Iterator(it, _buckets.end(), buckIt);
  297. else
  298. return end();
  299. }
  300. std::size_t count(const Value& value) const
  301. /// Returns the number of elements with the given
  302. /// value, with is either 1 or 0.
  303. {
  304. return find(value) != end() ? 1 : 0;
  305. }
  306. std::pair<Iterator, bool> insert(const Value& value)
  307. /// Inserts an element into the table.
  308. ///
  309. /// If the element already exists in the table,
  310. /// a pair(iterator, false) with iterator pointing to the
  311. /// existing element is returned.
  312. /// Otherwise, the element is inserted an a
  313. /// pair(iterator, true) with iterator
  314. /// pointing to the new element is returned.
  315. {
  316. std::size_t hash = _hash(value);
  317. std::size_t addr = bucketAddressForHash(hash);
  318. BucketVecIterator it(_buckets.begin() + addr);
  319. BucketIterator buckIt(std::find(it->begin(), it->end(), value));
  320. if (buckIt == it->end())
  321. {
  322. split();
  323. addr = bucketAddressForHash(hash);
  324. it = _buckets.begin() + addr;
  325. buckIt = it->insert(it->end(), value);
  326. ++_size;
  327. return std::make_pair(Iterator(it, _buckets.end(), buckIt), true);
  328. }
  329. else
  330. {
  331. return std::make_pair(Iterator(it, _buckets.end(), buckIt), false);
  332. }
  333. }
  334. void erase(Iterator it)
  335. /// Erases the element pointed to by it.
  336. {
  337. if (it != end())
  338. {
  339. it._vecIt->erase(it._buckIt);
  340. --_size;
  341. merge();
  342. }
  343. }
  344. void erase(const Value& value)
  345. /// Erases the element with the given value, if it exists.
  346. {
  347. Iterator it = find(value);
  348. erase(it);
  349. }
  350. void clear()
  351. /// Erases all elements.
  352. {
  353. LinearHashTable empty;
  354. swap(empty);
  355. }
  356. std::size_t size() const
  357. /// Returns the number of elements in the table.
  358. {
  359. return _size;
  360. }
  361. bool empty() const
  362. /// Returns true iff the table is empty.
  363. {
  364. return _size == 0;
  365. }
  366. std::size_t buckets() const
  367. /// Returns the number of allocated buckets.
  368. {
  369. return _buckets.size();
  370. }
  371. protected:
  372. std::size_t bucketAddress(const Value& value) const
  373. {
  374. std::size_t n = _hash(value);
  375. if (n % _front >= _split)
  376. return n % _front;
  377. else
  378. return n % (2*_front);
  379. }
  380. std::size_t bucketAddressForHash(std::size_t hash)
  381. {
  382. if (hash % _front >= _split)
  383. return hash % _front;
  384. else
  385. return hash % (2*_front);
  386. }
  387. void split()
  388. {
  389. if (_split == _front)
  390. {
  391. _split = 0;
  392. _front *= 2;
  393. _buckets.reserve(_front*2);
  394. }
  395. Bucket tmp;
  396. _buckets.push_back(tmp);
  397. _buckets[_split].swap(tmp);
  398. ++_split;
  399. for (BucketIterator it = tmp.begin(); it != tmp.end(); ++it)
  400. {
  401. using std::swap;
  402. std::size_t addr = bucketAddress(*it);
  403. _buckets[addr].push_back(Value());
  404. swap(*it, _buckets[addr].back());
  405. }
  406. }
  407. void merge()
  408. {
  409. if (_split == 0)
  410. {
  411. _front /= 2;
  412. _split = _front;
  413. }
  414. --_split;
  415. Bucket tmp;
  416. tmp.swap(_buckets.back());
  417. _buckets.pop_back();
  418. for (BucketIterator it = tmp.begin(); it != tmp.end(); ++it)
  419. {
  420. using std::swap;
  421. std::size_t addr = bucketAddress(*it);
  422. _buckets[addr].push_back(Value());
  423. swap(*it, _buckets[addr].back());
  424. }
  425. }
  426. static std::size_t calcSize(std::size_t initialSize)
  427. {
  428. std::size_t size = 32;
  429. while (size < initialSize) size *= 2;
  430. return size;
  431. }
  432. private:
  433. // Evil hack: _buckets must be mutable because both ConstIterator and Iterator hold
  434. // ordinary iterator's (not const_iterator's).
  435. mutable BucketVec _buckets;
  436. std::size_t _split;
  437. std::size_t _front;
  438. std::size_t _size;
  439. HashFunc _hash;
  440. };
  441. } // namespace Poco
  442. #endif // Foundation_LinearHashTable_INCLUDED