OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

HashSet.H

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Class
00026     Foam::HashSet
00027 
00028 Description
00029     A HashTable with keys but without contents.
00030 
00031 Typedef
00032     Foam::wordHashSet
00033 
00034 Description
00035     A HashSet with (the default) word keys.
00036 
00037 Typedef
00038     Foam::labelHashSet
00039 
00040 Description
00041     A HashSet with label keys.
00042 
00043 \*---------------------------------------------------------------------------*/
00044 
00045 #ifndef HashSet_H
00046 #define HashSet_H
00047 
00048 #include "HashTable.H"
00049 #include "nil.H"
00050 
00051 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00052 
00053 namespace Foam
00054 {
00055 
00056 /*---------------------------------------------------------------------------*\
00057                            Class HashSet Declaration
00058 \*---------------------------------------------------------------------------*/
00059 
00060 template<class Key=word, class Hash=string::hash>
00061 class HashSet
00062 :
00063     public HashTable<nil, Key, Hash>
00064 {
00065 
00066 public:
00067 
00068     typedef typename HashTable<nil, Key, Hash>::iterator iterator;
00069     typedef typename HashTable<nil, Key, Hash>::const_iterator const_iterator;
00070 
00071 
00072     // Constructors
00073 
00074         //- Construct given initial size
00075         HashSet(const label size = 128)
00076         :
00077             HashTable<nil, Key, Hash>(size)
00078         {}
00079 
00080         //- Construct from Istream
00081         HashSet(Istream& is)
00082         :
00083             HashTable<nil, Key, Hash>(is)
00084         {}
00085 
00086         //- Construct from UList of Key
00087         HashSet(const UList<Key>& lst)
00088         :
00089             HashTable<nil, Key, Hash>(2*lst.size())
00090         {
00091             forAll(lst, i)
00092             {
00093                 insert(lst[i]);
00094             }
00095         }
00096 
00097         //- Construct as copy
00098         HashSet(const HashSet<Key, Hash>& hs)
00099         :
00100             HashTable<nil, Key, Hash>(hs)
00101         {}
00102 
00103         //- Construct by transferring the parameter contents
00104         HashSet(const Xfer<HashSet<Key, Hash> >& hs)
00105         :
00106             HashTable<nil, Key, Hash>(hs)
00107         {}
00108 
00109         //- Construct by transferring the parameter contents
00110         HashSet(const Xfer<HashTable<nil, Key, Hash> >& hs)
00111         :
00112             HashTable<nil, Key, Hash>(hs)
00113         {}
00114 
00115         //- Construct from the keys of another HashTable,
00116         //  the type of values held is arbitrary.
00117         template<class AnyType>
00118         HashSet(const HashTable<AnyType, Key, Hash>&);
00119 
00120     // Member Functions
00121 
00122         // Edit
00123 
00124         //- Insert a new entry
00125         bool insert(const Key& key)
00126         {
00127             return HashTable<nil, Key, Hash>::insert(key, nil());
00128         }
00129 
00130         //- Same as insert (cannot overwrite nil content)
00131         bool set(const Key& key)
00132         {
00133             return HashTable<nil, Key, Hash>::insert(key, nil());
00134         }
00135 
00136     // Member Operators
00137 
00138         //- Return true if the entry exists, same as found()
00139         inline bool operator[](const Key&) const;
00140 
00141         //- Equality. Two hashtables are equal when their contents are equal.
00142         //  Independent of table size or order.
00143         bool operator==(const HashSet<Key, Hash>&) const;
00144 
00145         //- The opposite of the equality operation.
00146         bool operator!=(const HashSet<Key, Hash>&) const;
00147 
00148 
00149         //- Combine entries from HashSets
00150         void operator|=(const HashSet<Key, Hash>&);
00151 
00152         //- Only retain entries found in both HashSets
00153         void operator&=(const HashSet<Key, Hash>&);
00154 
00155         //- Only retain unique entries (xor)
00156         void operator^=(const HashSet<Key, Hash>&);
00157 
00158         //- Add entries listed in the given HashSet to this HashSet
00159         inline void operator+=(const HashSet<Key, Hash>& rhs)
00160         {
00161             this->operator|=(rhs);
00162         }
00163 
00164         //- Remove entries listed in the given HashSet from this HashSet
00165         void operator-=(const HashSet<Key, Hash>&);
00166 
00167 };
00168 
00169 
00170 // Global Operators
00171 
00172 //- Combine entries from HashSets
00173 template<class Key, class Hash>
00174 HashSet<Key,Hash> operator|
00175 (
00176     const HashSet<Key,Hash>& hash1,
00177     const HashSet<Key,Hash>& hash2
00178 );
00179 
00180 
00181 //- Create a HashSet that only contains entries found in both HashSets
00182 template<class Key, class Hash>
00183 HashSet<Key,Hash> operator&
00184 (
00185     const HashSet<Key,Hash>& hash1,
00186     const HashSet<Key,Hash>& hash2
00187 );
00188 
00189 
00190 //- Create a HashSet that only contains unique entries (xor)
00191 template<class Key, class Hash>
00192 HashSet<Key,Hash> operator^
00193 (
00194     const HashSet<Key,Hash>& hash1,
00195     const HashSet<Key,Hash>& hash2
00196 );
00197 
00198 
00199 //- A HashSet with word keys.
00200 typedef HashSet<> wordHashSet;
00201 
00202 //- A HashSet with label keys.
00203 typedef HashSet<label, Hash<label> > labelHashSet;
00204 
00205 
00206 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00207 
00208 } // End namespace Foam
00209 
00210 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00211 
00212 #ifdef NoRepository
00213 #   include "HashSet.C"
00214 #endif
00215 
00216 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00217 
00218 #endif
00219 
00220 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd