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