FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 Class
00025     Foam::HashSet
00026 
00027 Description
00028     A HashTable with keys but without contents.
00029 
00030 Typedef
00031     Foam::wordHashSet
00032 
00033 Description
00034     A HashSet with (the default) word keys.
00035 
00036 Typedef
00037     Foam::labelHashSet
00038 
00039 Description
00040     A HashSet with label keys.
00041 
00042 \*---------------------------------------------------------------------------*/
00043 
00044 #ifndef HashSet_H
00045 #define HashSet_H
00046 
00047 #include <OpenFOAM/HashTable.H>
00048 #include <OpenFOAM/nil.H>
00049 
00050 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00051 
00052 namespace Foam
00053 {
00054 
00055 /*---------------------------------------------------------------------------*\
00056                            Class HashSet Declaration
00057 \*---------------------------------------------------------------------------*/
00058 
00059 template<class Key=word, class Hash=string::hash>
00060 class HashSet
00061 :
00062     public HashTable<nil, Key, Hash>
00063 {
00064 
00065 public:
00066 
00067     typedef typename HashTable<nil, Key, Hash>::iterator iterator;
00068     typedef typename HashTable<nil, Key, Hash>::const_iterator const_iterator;
00069 
00070 
00071     // Constructors
00072 
00073         //- Construct given initial size
00074         HashSet(const label size = 128)
00075         :
00076             HashTable<nil, Key, Hash>(size)
00077         {}
00078 
00079         //- Construct from Istream
00080         HashSet(Istream& is)
00081         :
00082             HashTable<nil, Key, Hash>(is)
00083         {}
00084 
00085         //- Construct from UList of Key
00086         HashSet(const UList<Key>& lst)
00087         :
00088             HashTable<nil, Key, Hash>(2*lst.size())
00089         {
00090             forAll(lst, i)
00091             {
00092                 insert(lst[i]);
00093             }
00094         }
00095 
00096         //- Construct as copy
00097         HashSet(const HashSet<Key, Hash>& hs)
00098         :
00099             HashTable<nil, Key, Hash>(hs)
00100         {}
00101 
00102         //- Construct by transferring the parameter contents
00103         HashSet(const Xfer<HashSet<Key, Hash> >& hs)
00104         :
00105             HashTable<nil, Key, Hash>(hs)
00106         {}
00107 
00108         //- Construct by transferring the parameter contents
00109         HashSet(const Xfer<HashTable<nil, Key, Hash> >& hs)
00110         :
00111             HashTable<nil, Key, Hash>(hs)
00112         {}
00113 
00114         //- Construct from the keys of another HashTable,
00115         //  the type of values held is arbitrary.
00116         template<class AnyType, class AnyHash>
00117         HashSet(const HashTable<AnyType, Key, AnyHash>&);
00118 
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 
00137     // Member Operators
00138 
00139         //- Return true if the entry exists, same as found()
00140         inline bool operator[](const Key&) const;
00141 
00142         //- Equality. Two hashtables are equal when their contents are equal.
00143         //  Independent of table size or order.
00144         bool operator==(const HashSet<Key, Hash>&) const;
00145 
00146         //- The opposite of the equality operation.
00147         bool operator!=(const HashSet<Key, Hash>&) const;
00148 
00149 
00150         //- Combine entries from HashSets
00151         void operator|=(const HashSet<Key, Hash>&);
00152 
00153         //- Only retain entries found in both HashSets
00154         void operator&=(const HashSet<Key, Hash>&);
00155 
00156         //- Only retain unique entries (xor)
00157         void operator^=(const HashSet<Key, Hash>&);
00158 
00159         //- Add entries listed in the given HashSet to this HashSet
00160         inline void operator+=(const HashSet<Key, Hash>& rhs)
00161         {
00162             this->operator|=(rhs);
00163         }
00164 
00165         //- Remove entries listed in the given HashSet from this HashSet
00166         void operator-=(const HashSet<Key, Hash>&);
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 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines