HashSet.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // HashSet.h
  3. //
  4. // $Id: //poco/1.3/Foundation/include/Poco/HashSet.h#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Hashing
  8. // Module: HashSet
  9. //
  10. // Definition of the HashSet class.
  11. //
  12. // Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // Permission is hereby granted, free of charge, to any person or organization
  16. // obtaining a copy of the software and accompanying documentation covered by
  17. // this license (the "Software") to use, reproduce, display, distribute,
  18. // execute, and transmit the Software, and to prepare derivative works of the
  19. // Software, and to permit third-parties to whom the Software is furnished to
  20. // do so, all subject to the following:
  21. //
  22. // The copyright notices in the Software and this entire statement, including
  23. // the above license grant, this restriction and the following disclaimer,
  24. // must be included in all copies of the Software, in whole or in part, and
  25. // all derivative works of the Software, unless such copies or derivative
  26. // works are solely in the form of machine-executable object code generated by
  27. // a source language processor.
  28. //
  29. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  30. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  31. // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  32. // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  33. // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  34. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  35. // DEALINGS IN THE SOFTWARE.
  36. //
  37. #ifndef Foundation_HashSet_INCLUDED
  38. #define Foundation_HashSet_INCLUDED
  39. #include "Poco/Foundation.h"
  40. #include "Poco/LinearHashTable.h"
  41. namespace Poco {
  42. template <class Value, class HashFunc = Hash<Value> >
  43. class HashSet
  44. /// This class implements a set using a LinearHashTable.
  45. ///
  46. /// A HashSet can be used just like a std::set.
  47. {
  48. public:
  49. typedef Value ValueType;
  50. typedef Value& Reference;
  51. typedef const Value& ConstReference;
  52. typedef Value* Pointer;
  53. typedef const Value* ConstPointer;
  54. typedef HashFunc Hash;
  55. typedef LinearHashTable<ValueType, Hash> HashTable;
  56. typedef typename HashTable::Iterator Iterator;
  57. typedef typename HashTable::ConstIterator ConstIterator;
  58. HashSet()
  59. /// Creates an empty HashSet.
  60. {
  61. }
  62. HashSet(std::size_t initialReserve):
  63. _table(initialReserve)
  64. /// Creates the HashSet, using the given initialReserve.
  65. {
  66. }
  67. HashSet(const HashSet& set):
  68. _table(set._table)
  69. /// Creates the HashSet by copying another one.
  70. {
  71. }
  72. ~HashSet()
  73. /// Destroys the HashSet.
  74. {
  75. }
  76. HashSet& operator = (const HashSet& table)
  77. /// Assigns another HashSet.
  78. {
  79. HashSet tmp(table);
  80. swap(tmp);
  81. return *this;
  82. }
  83. void swap(HashSet& set)
  84. /// Swaps the HashSet with another one.
  85. {
  86. _table.swap(set._table);
  87. }
  88. ConstIterator begin() const
  89. /// Returns an iterator pointing to the first entry, if one exists.
  90. {
  91. return _table.begin();
  92. }
  93. ConstIterator end() const
  94. /// Returns an iterator pointing to the end of the table.
  95. {
  96. return _table.end();
  97. }
  98. Iterator begin()
  99. /// Returns an iterator pointing to the first entry, if one exists.
  100. {
  101. return _table.begin();
  102. }
  103. Iterator end()
  104. /// Returns an iterator pointing to the end of the table.
  105. {
  106. return _table.end();
  107. }
  108. ConstIterator find(const ValueType& value) const
  109. /// Finds an entry in the table.
  110. {
  111. return _table.find(value);
  112. }
  113. Iterator find(const ValueType& value)
  114. /// Finds an entry in the table.
  115. {
  116. return _table.find(value);
  117. }
  118. std::size_t count(const ValueType& value) const
  119. /// Returns the number of elements with the given
  120. /// value, with is either 1 or 0.
  121. {
  122. return _table.count(value);
  123. }
  124. std::pair<Iterator, bool> insert(const ValueType& value)
  125. /// Inserts an element into the set.
  126. ///
  127. /// If the element already exists in the set,
  128. /// a pair(iterator, false) with iterator pointing to the
  129. /// existing element is returned.
  130. /// Otherwise, the element is inserted an a
  131. /// pair(iterator, true) with iterator
  132. /// pointing to the new element is returned.
  133. {
  134. return _table.insert(value);
  135. }
  136. void erase(Iterator it)
  137. /// Erases the element pointed to by it.
  138. {
  139. _table.erase(it);
  140. }
  141. void erase(const ValueType& value)
  142. /// Erases the element with the given value, if it exists.
  143. {
  144. _table.erase(value);
  145. }
  146. void clear()
  147. /// Erases all elements.
  148. {
  149. _table.clear();
  150. }
  151. std::size_t size() const
  152. /// Returns the number of elements in the table.
  153. {
  154. return _table.size();
  155. }
  156. bool empty() const
  157. /// Returns true iff the table is empty.
  158. {
  159. return _table.empty();
  160. }
  161. private:
  162. HashTable _table;
  163. };
  164. } // namespace Poco
  165. #endif // Foundation_HashSet_INCLUDED