1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
11 OpenFOAM is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
16 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
28 STL conforming hash table.
31 Uses straight lists as underlying type.
32 Is slower to insert than the standard HashTable, but should be more
33 memory efficient and faster to access.
40 \*---------------------------------------------------------------------------*/
42 #ifndef StaticHashTable_H
43 #define StaticHashTable_H
49 #include "className.H"
51 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
56 // Forward declaration of friend functions and operators
58 template<class T> class List;
59 template<class T, class Key, class Hash> class StaticHashTable;
61 template<class T, class Key, class Hash> Istream& operator>>
64 StaticHashTable<T, Key, Hash>&
67 template<class T, class Key, class Hash> Ostream& operator<<
70 const StaticHashTable<T, Key, Hash>&
74 /*---------------------------------------------------------------------------*\
75 Class StaticHashTableCore Declaration
76 \*---------------------------------------------------------------------------*/
78 //- Template-invariant bits for StaticHashTable
79 struct StaticHashTableCore
81 //- Return a canonical (power-of-two) size
82 static label canonicalSize(const label);
88 //- Define template name and debug
89 ClassName("StaticHashTable");
91 //- A zero-sized end iterator
102 /*---------------------------------------------------------------------------*\
103 Class StaticHashTable Declaration
104 \*---------------------------------------------------------------------------*/
106 template<class T, class Key=word, class Hash=string::hash>
107 class StaticHashTable
109 public StaticHashTableCore
111 // Private data type for table entries
113 //- The lookup keys, ordered per hash value
114 List<List<Key> > keys_;
116 //- For each key the corresponding object.
117 List<List<T> > objects_;
119 //- The current number of elements in table
122 //- Return a canonical (power-of-two) size
123 static label canonicalSize(const label);
125 //- Return the hash index of the Key within the current table size.
126 // No checks for zero-sized tables.
127 inline label hashKeyIndex(const Key&) const;
129 //- Assign a new hashed entry to a possibly already existing key
130 bool set(const Key&, const T& newElmt, bool protect);
136 // Forward declaration of STL iterators
138 template<class TRef, class TableRef>
144 StaticHashTable<T, Key, Hash>&
150 const StaticHashTable<T, Key, Hash>&
154 // Declare friendship with the iterators
156 friend class Iterator
159 StaticHashTable<T, Key, Hash>&
162 friend class Iterator
165 const StaticHashTable<T, Key, Hash>&
171 //- Construct given initial table size
172 StaticHashTable(const label size = 128);
174 //- Construct from Istream
175 StaticHashTable(Istream&, const label size = 128);
177 //- Construct as copy
178 StaticHashTable(const StaticHashTable<T, Key, Hash>&);
180 //- Construct by transferring the parameter contents
181 StaticHashTable(const Xfer<StaticHashTable<T, Key, Hash> >&);
192 //- Return number of elements in table.
193 inline label size() const;
195 //- Return true if the hash table is empty
196 inline bool empty() const;
198 //- Return true if hashed entry is found in table
199 bool found(const Key& key) const;
201 //- Find and return an iterator set at the hashed entry
202 // If not found iterator = end()
203 iterator find(const Key& key);
205 //- Find and return an const_iterator set at the hashed entry
206 // If not found iterator = end()
207 const_iterator find(const Key& key) const;
209 //- Return the table of contents
210 List<Key> toc() const;
212 //- Print information
213 Ostream& printInfo(Ostream&) const;
218 //- Insert a new hashed entry
219 bool insert(const Key& key, const T& newElmt);
221 //- Assign a new hashed entry, overwriting existing entries
222 inline bool set(const Key&, const T& newElmt);
224 //- Erase an hashed entry specified by given iterator
225 bool erase(const iterator& it);
227 //- Erase an hashed entry specified by given key if in table
228 bool erase(const Key& key);
230 //- Resize the hash table for efficiency
231 void resize(const label newSize);
233 //- Remove entries in the given hash table from this hash table
234 // Return the number of elements removed
235 label erase(const StaticHashTable<T, Key, Hash>&);
237 //- Clear all entries from table
240 //- Clear the table entries and the table itself.
241 // Equivalent to clear() followed by resize(1)
244 //- Transfer the contents of the argument table into this table
245 // and annul the argument table.
246 void transfer(StaticHashTable<T, Key, Hash>&);
248 //- Transfer contents to the Xfer container
249 inline Xfer<StaticHashTable<T, Key, Hash> > xfer();
254 //- Find and return an hashed entry
255 inline T& operator[](const Key&);
257 //- Find and return an hashed entry
258 inline const T& operator[](const Key&) const;
260 //- Find and return an hashed entry, create it null if not present.
261 inline T& operator()(const Key&);
264 void operator=(const StaticHashTable<T, Key, Hash>&);
266 //- Equality. Two hash tables are equal if all contents of first are
267 // also in second and vice versa.
268 bool operator==(const StaticHashTable<T, Key, Hash>&) const;
270 //- The opposite of the equality operation.
271 bool operator!=(const StaticHashTable<T, Key, Hash>&) const;
274 // STL type definitions
276 //- Type of values the StaticHashTable contains.
277 typedef T value_type;
279 //- Type that can be used for storing into StaticHashTable::value_type
280 // objects. This type is usually List::value_type&.
281 typedef T& reference;
283 //- Type that can be used for storing into constant
284 // StaticHashTable::value_type objects. This type is usually const
285 // StaticHashTable::value_type&.
286 typedef const T& const_reference;
288 //- The type that can represent the size of a StaticHashTable.
289 typedef label size_type;
295 template<class TRef, class TableRef>
298 friend class StaticHashTable;
300 # ifndef __INTEL_COMPILER
301 template<class TRef2, class TableRef2>
302 friend class Iterator;
307 //- Reference to the StaticHashTable this is an iterator for
310 //- Current hash index
313 //- Index of current element at hashIndex
321 //- Construct from hash table, hash index and element index
329 //- Construct from the non-const iterator
330 inline Iterator(const iterator&);
335 inline void operator=(const iterator&);
337 inline bool operator==(const iterator&) const;
338 inline bool operator==(const const_iterator&) const;
340 inline bool operator!=(const iterator&) const;
341 inline bool operator!=(const const_iterator&) const;
343 inline TRef operator*();
344 inline TRef operator()();
346 inline Iterator& operator++();
347 inline Iterator operator++(int);
349 inline const Key& key() const;
353 //- iterator set to the beginning of the StaticHashTable
354 inline iterator begin();
356 //- iterator set to beyond the end of the StaticHashTable
357 inline const iterator& end();
359 //- const_iterator set to the beginning of the StaticHashTable
360 inline const_iterator cbegin() const;
362 //- const_iterator set to beyond the end of the StaticHashTable
363 inline const const_iterator& cend() const;
365 //- const_iterator set to the beginning of the StaticHashTable
366 inline const_iterator begin() const;
368 //- const_iterator set to beyond the end of the StaticHashTable
369 inline const const_iterator& end() const;
373 friend Istream& operator>> <T, Key, Hash>
376 StaticHashTable<T, Key, Hash>&
379 friend Ostream& operator<< <T, Key, Hash>
382 const StaticHashTable<T, Key, Hash>&
388 //- iterator returned by end()
391 //- const_iterator returned by end()
392 const_iterator endConstIter_;
396 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
398 } // End namespace Foam
400 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
402 # include "StaticHashTableI.H"
404 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
406 #ifndef NoStaticHashTableC
408 # include "StaticHashTable.C"
412 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
416 // ************************************************************************* //