HashSet.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //
  2. // HashSet.h
  3. //
  4. // $Id: //poco/1.4/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. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_HashSet_INCLUDED
  18. #define Foundation_HashSet_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #include "Poco/LinearHashTable.h"
  21. namespace Poco {
  22. template <class Value, class HashFunc = Hash<Value> >
  23. class HashSet
  24. /// This class implements a set using a LinearHashTable.
  25. ///
  26. /// A HashSet can be used just like a std::set.
  27. {
  28. public:
  29. typedef Value ValueType;
  30. typedef Value& Reference;
  31. typedef const Value& ConstReference;
  32. typedef Value* Pointer;
  33. typedef const Value* ConstPointer;
  34. typedef HashFunc Hash;
  35. typedef LinearHashTable<ValueType, Hash> HashTable;
  36. typedef typename HashTable::Iterator Iterator;
  37. typedef typename HashTable::ConstIterator ConstIterator;
  38. HashSet()
  39. /// Creates an empty HashSet.
  40. {
  41. }
  42. HashSet(std::size_t initialReserve):
  43. _table(initialReserve)
  44. /// Creates the HashSet, using the given initialReserve.
  45. {
  46. }
  47. HashSet(const HashSet& set):
  48. _table(set._table)
  49. /// Creates the HashSet by copying another one.
  50. {
  51. }
  52. ~HashSet()
  53. /// Destroys the HashSet.
  54. {
  55. }
  56. HashSet& operator = (const HashSet& table)
  57. /// Assigns another HashSet.
  58. {
  59. HashSet tmp(table);
  60. swap(tmp);
  61. return *this;
  62. }
  63. void swap(HashSet& set)
  64. /// Swaps the HashSet with another one.
  65. {
  66. _table.swap(set._table);
  67. }
  68. ConstIterator begin() const
  69. /// Returns an iterator pointing to the first entry, if one exists.
  70. {
  71. return _table.begin();
  72. }
  73. ConstIterator end() const
  74. /// Returns an iterator pointing to the end of the table.
  75. {
  76. return _table.end();
  77. }
  78. Iterator begin()
  79. /// Returns an iterator pointing to the first entry, if one exists.
  80. {
  81. return _table.begin();
  82. }
  83. Iterator end()
  84. /// Returns an iterator pointing to the end of the table.
  85. {
  86. return _table.end();
  87. }
  88. ConstIterator find(const ValueType& value) const
  89. /// Finds an entry in the table.
  90. {
  91. return _table.find(value);
  92. }
  93. Iterator find(const ValueType& value)
  94. /// Finds an entry in the table.
  95. {
  96. return _table.find(value);
  97. }
  98. std::size_t count(const ValueType& value) const
  99. /// Returns the number of elements with the given
  100. /// value, with is either 1 or 0.
  101. {
  102. return _table.count(value);
  103. }
  104. std::pair<Iterator, bool> insert(const ValueType& value)
  105. /// Inserts an element into the set.
  106. ///
  107. /// If the element already exists in the set,
  108. /// a pair(iterator, false) with iterator pointing to the
  109. /// existing element is returned.
  110. /// Otherwise, the element is inserted an a
  111. /// pair(iterator, true) with iterator
  112. /// pointing to the new element is returned.
  113. {
  114. return _table.insert(value);
  115. }
  116. void erase(Iterator it)
  117. /// Erases the element pointed to by it.
  118. {
  119. _table.erase(it);
  120. }
  121. void erase(const ValueType& value)
  122. /// Erases the element with the given value, if it exists.
  123. {
  124. _table.erase(value);
  125. }
  126. void clear()
  127. /// Erases all elements.
  128. {
  129. _table.clear();
  130. }
  131. std::size_t size() const
  132. /// Returns the number of elements in the table.
  133. {
  134. return _table.size();
  135. }
  136. bool empty() const
  137. /// Returns true iff the table is empty.
  138. {
  139. return _table.empty();
  140. }
  141. private:
  142. HashTable _table;
  143. };
  144. } // namespace Poco
  145. #endif // Foundation_HashSet_INCLUDED