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 | |
---|
38 | |
---|
39 | #ifndef Foundation_HashSet_INCLUDED |
---|
40 | #define Foundation_HashSet_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | #include "Poco/LinearHashTable.h" |
---|
45 | |
---|
46 | |
---|
47 | namespace Poco { |
---|
48 | |
---|
49 | |
---|
50 | template <class Value, class HashFunc = Hash<Value> > |
---|
51 | class HashSet |
---|
52 | /// This class implements a set using a LinearHashTable. |
---|
53 | /// |
---|
54 | /// A HashSet can be used just like a std::set. |
---|
55 | { |
---|
56 | public: |
---|
57 | typedef Value ValueType; |
---|
58 | typedef Value& Reference; |
---|
59 | typedef const Value& ConstReference; |
---|
60 | typedef Value* Pointer; |
---|
61 | typedef const Value* ConstPointer; |
---|
62 | typedef HashFunc Hash; |
---|
63 | |
---|
64 | typedef LinearHashTable<ValueType, Hash> HashTable; |
---|
65 | |
---|
66 | typedef typename HashTable::Iterator Iterator; |
---|
67 | typedef typename HashTable::ConstIterator ConstIterator; |
---|
68 | |
---|
69 | HashSet() |
---|
70 | /// Creates an empty HashSet. |
---|
71 | { |
---|
72 | } |
---|
73 | |
---|
74 | HashSet(std::size_t initialReserve): |
---|
75 | _table(initialReserve) |
---|
76 | /// Creates the HashSet, using the given initialReserve. |
---|
77 | { |
---|
78 | } |
---|
79 | |
---|
80 | HashSet(const HashSet& set): |
---|
81 | _table(set._table) |
---|
82 | /// Creates the HashSet by copying another one. |
---|
83 | { |
---|
84 | } |
---|
85 | |
---|
86 | ~HashSet() |
---|
87 | /// Destroys the HashSet. |
---|
88 | { |
---|
89 | } |
---|
90 | |
---|
91 | HashSet& operator = (const HashSet& table) |
---|
92 | /// Assigns another HashSet. |
---|
93 | { |
---|
94 | HashSet tmp(table); |
---|
95 | swap(tmp); |
---|
96 | return *this; |
---|
97 | } |
---|
98 | |
---|
99 | void swap(HashSet& set) |
---|
100 | /// Swaps the HashSet with another one. |
---|
101 | { |
---|
102 | _table.swap(set._table); |
---|
103 | } |
---|
104 | |
---|
105 | ConstIterator begin() const |
---|
106 | /// Returns an iterator pointing to the first entry, if one exists. |
---|
107 | { |
---|
108 | return _table.begin(); |
---|
109 | } |
---|
110 | |
---|
111 | ConstIterator end() const |
---|
112 | /// Returns an iterator pointing to the end of the table. |
---|
113 | { |
---|
114 | return _table.end(); |
---|
115 | } |
---|
116 | |
---|
117 | Iterator begin() |
---|
118 | /// Returns an iterator pointing to the first entry, if one exists. |
---|
119 | { |
---|
120 | return _table.begin(); |
---|
121 | } |
---|
122 | |
---|
123 | Iterator end() |
---|
124 | /// Returns an iterator pointing to the end of the table. |
---|
125 | { |
---|
126 | return _table.end(); |
---|
127 | } |
---|
128 | |
---|
129 | ConstIterator find(const ValueType& value) const |
---|
130 | /// Finds an entry in the table. |
---|
131 | { |
---|
132 | return _table.find(value); |
---|
133 | } |
---|
134 | |
---|
135 | Iterator find(const ValueType& value) |
---|
136 | /// Finds an entry in the table. |
---|
137 | { |
---|
138 | return _table.find(value); |
---|
139 | } |
---|
140 | |
---|
141 | std::size_t count(const ValueType& value) const |
---|
142 | /// Returns the number of elements with the given |
---|
143 | /// value, with is either 1 or 0. |
---|
144 | { |
---|
145 | return _table.count(value); |
---|
146 | } |
---|
147 | |
---|
148 | std::pair<Iterator, bool> insert(const ValueType& value) |
---|
149 | /// Inserts an element into the set. |
---|
150 | /// |
---|
151 | /// If the element already exists in the set, |
---|
152 | /// a pair(iterator, false) with iterator pointing to the |
---|
153 | /// existing element is returned. |
---|
154 | /// Otherwise, the element is inserted an a |
---|
155 | /// pair(iterator, true) with iterator |
---|
156 | /// pointing to the new element is returned. |
---|
157 | { |
---|
158 | return _table.insert(value); |
---|
159 | } |
---|
160 | |
---|
161 | void erase(Iterator it) |
---|
162 | /// Erases the element pointed to by it. |
---|
163 | { |
---|
164 | _table.erase(it); |
---|
165 | } |
---|
166 | |
---|
167 | void erase(const ValueType& value) |
---|
168 | /// Erases the element with the given value, if it exists. |
---|
169 | { |
---|
170 | _table.erase(value); |
---|
171 | } |
---|
172 | |
---|
173 | void clear() |
---|
174 | /// Erases all elements. |
---|
175 | { |
---|
176 | _table.clear(); |
---|
177 | } |
---|
178 | |
---|
179 | std::size_t size() const |
---|
180 | /// Returns the number of elements in the table. |
---|
181 | { |
---|
182 | return _table.size(); |
---|
183 | } |
---|
184 | |
---|
185 | bool empty() const |
---|
186 | /// Returns true iff the table is empty. |
---|
187 | { |
---|
188 | return _table.empty(); |
---|
189 | } |
---|
190 | |
---|
191 | private: |
---|
192 | HashTable _table; |
---|
193 | }; |
---|
194 | |
---|
195 | |
---|
196 | } // namespace Poco |
---|
197 | |
---|
198 | |
---|
199 | #endif // Foundation_HashSet_INCLUDED |
---|