1 | // |
---|
2 | // HashMap.h |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/Foundation/include/Poco/HashMap.h#1 $ |
---|
5 | // |
---|
6 | // Library: Foundation |
---|
7 | // Package: Hashing |
---|
8 | // Module: HashMap |
---|
9 | // |
---|
10 | // Definition of the HashMap 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_HashMap_INCLUDED |
---|
40 | #define Foundation_HashMap_INCLUDED |
---|
41 | |
---|
42 | |
---|
43 | #include "Poco/Foundation.h" |
---|
44 | #include "Poco/LinearHashTable.h" |
---|
45 | #include "Poco/Exception.h" |
---|
46 | #include <utility> |
---|
47 | |
---|
48 | |
---|
49 | namespace Poco { |
---|
50 | |
---|
51 | |
---|
52 | template <class Key, class Value> |
---|
53 | struct HashMapEntry |
---|
54 | /// This class template is used internally by HashMap. |
---|
55 | { |
---|
56 | Key first; |
---|
57 | Value second; |
---|
58 | |
---|
59 | HashMapEntry(): |
---|
60 | first(), |
---|
61 | second() |
---|
62 | { |
---|
63 | } |
---|
64 | |
---|
65 | HashMapEntry(const Key& key): |
---|
66 | first(key), |
---|
67 | second() |
---|
68 | { |
---|
69 | } |
---|
70 | |
---|
71 | HashMapEntry(const Key& key, const Value& value): |
---|
72 | first(key), |
---|
73 | second(value) |
---|
74 | { |
---|
75 | } |
---|
76 | |
---|
77 | bool operator == (const HashMapEntry& entry) const |
---|
78 | { |
---|
79 | return first == entry.first; |
---|
80 | } |
---|
81 | |
---|
82 | bool operator != (const HashMapEntry& entry) const |
---|
83 | { |
---|
84 | return first != entry.first; |
---|
85 | } |
---|
86 | }; |
---|
87 | |
---|
88 | |
---|
89 | template <class HME, class KeyHashFunc> |
---|
90 | struct HashMapEntryHash |
---|
91 | /// This class template is used internally by HashMap. |
---|
92 | { |
---|
93 | std::size_t operator () (const HME& entry) const |
---|
94 | { |
---|
95 | return _func(entry.first); |
---|
96 | } |
---|
97 | |
---|
98 | private: |
---|
99 | KeyHashFunc _func; |
---|
100 | }; |
---|
101 | |
---|
102 | |
---|
103 | template <class Key, class Mapped, class HashFunc = Hash<Key> > |
---|
104 | class HashMap |
---|
105 | /// This class implements a map using a LinearHashTable. |
---|
106 | /// |
---|
107 | /// A HashMap can be used just like a std::map. |
---|
108 | { |
---|
109 | public: |
---|
110 | typedef Key KeyType; |
---|
111 | typedef Mapped MappedType; |
---|
112 | typedef Mapped& Reference; |
---|
113 | typedef const Mapped& ConstReference; |
---|
114 | typedef Mapped* Pointer; |
---|
115 | typedef const Mapped* ConstPointer; |
---|
116 | |
---|
117 | typedef HashMapEntry<Key, Mapped> ValueType; |
---|
118 | typedef std::pair<KeyType, MappedType> PairType; |
---|
119 | |
---|
120 | typedef HashMapEntryHash<ValueType, HashFunc> HashType; |
---|
121 | typedef LinearHashTable<ValueType, HashType> HashTable; |
---|
122 | |
---|
123 | typedef typename HashTable::Iterator Iterator; |
---|
124 | typedef typename HashTable::ConstIterator ConstIterator; |
---|
125 | |
---|
126 | HashMap() |
---|
127 | /// Creates an empty HashMap. |
---|
128 | { |
---|
129 | } |
---|
130 | |
---|
131 | HashMap(std::size_t initialReserve): |
---|
132 | _table(initialReserve) |
---|
133 | /// Creates the HashMap with room for initialReserve entries. |
---|
134 | { |
---|
135 | } |
---|
136 | |
---|
137 | HashMap& operator = (const HashMap& map) |
---|
138 | /// Assigns another HashMap. |
---|
139 | { |
---|
140 | HashMap tmp(map); |
---|
141 | swap(tmp); |
---|
142 | return *this; |
---|
143 | } |
---|
144 | |
---|
145 | void swap(HashMap& map) |
---|
146 | /// Swaps the HashMap with another one. |
---|
147 | { |
---|
148 | _table.swap(map._table); |
---|
149 | } |
---|
150 | |
---|
151 | ConstIterator begin() const |
---|
152 | { |
---|
153 | return _table.begin(); |
---|
154 | } |
---|
155 | |
---|
156 | ConstIterator end() const |
---|
157 | { |
---|
158 | return _table.end(); |
---|
159 | } |
---|
160 | |
---|
161 | Iterator begin() |
---|
162 | { |
---|
163 | return _table.begin(); |
---|
164 | } |
---|
165 | |
---|
166 | Iterator end() |
---|
167 | { |
---|
168 | return _table.end(); |
---|
169 | } |
---|
170 | |
---|
171 | ConstIterator find(const KeyType& key) const |
---|
172 | { |
---|
173 | ValueType value(key); |
---|
174 | return _table.find(value); |
---|
175 | } |
---|
176 | |
---|
177 | Iterator find(const KeyType& key) |
---|
178 | { |
---|
179 | ValueType value(key); |
---|
180 | return _table.find(value); |
---|
181 | } |
---|
182 | |
---|
183 | std::pair<Iterator, bool> insert(const PairType& pair) |
---|
184 | { |
---|
185 | ValueType value(pair.first, pair.second); |
---|
186 | return _table.insert(value); |
---|
187 | } |
---|
188 | |
---|
189 | std::pair<Iterator, bool> insert(const ValueType& value) |
---|
190 | { |
---|
191 | return _table.insert(value); |
---|
192 | } |
---|
193 | |
---|
194 | void erase(Iterator it) |
---|
195 | { |
---|
196 | _table.erase(it); |
---|
197 | } |
---|
198 | |
---|
199 | void erase(const KeyType& key) |
---|
200 | { |
---|
201 | Iterator it = find(key); |
---|
202 | _table.erase(it); |
---|
203 | } |
---|
204 | |
---|
205 | void clear() |
---|
206 | { |
---|
207 | _table.clear(); |
---|
208 | } |
---|
209 | |
---|
210 | std::size_t size() const |
---|
211 | { |
---|
212 | return _table.size(); |
---|
213 | } |
---|
214 | |
---|
215 | bool empty() const |
---|
216 | { |
---|
217 | return _table.empty(); |
---|
218 | } |
---|
219 | |
---|
220 | ConstReference operator [] (const KeyType& key) const |
---|
221 | { |
---|
222 | ConstIterator it = _table.find(key); |
---|
223 | if (it != _table.end()) |
---|
224 | return it->second; |
---|
225 | else |
---|
226 | throw NotFoundException(); |
---|
227 | } |
---|
228 | |
---|
229 | Reference operator [] (const KeyType& key) |
---|
230 | { |
---|
231 | ValueType value(key); |
---|
232 | std::pair<Iterator, bool> res = _table.insert(value); |
---|
233 | return res.first->second; |
---|
234 | } |
---|
235 | |
---|
236 | private: |
---|
237 | HashTable _table; |
---|
238 | }; |
---|
239 | |
---|
240 | |
---|
241 | } // namespace Poco |
---|
242 | |
---|
243 | |
---|
244 | #endif // Foundation_HashMap_INCLUDED |
---|