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

HashSet.C

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 \*---------------------------------------------------------------------------*/
00026 
00027 #ifndef HashSet_C
00028 #define HashSet_C
00029 
00030 #include "HashSet.H"
00031 
00032 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00033 
00034 template<class Key, class Hash>
00035 template<class AnyType>
00036 Foam::HashSet<Key, Hash>::HashSet(const HashTable<AnyType, Key, Hash>& h)
00037 :
00038     HashTable<nil, Key, Hash>(h.size())
00039 {
00040     for
00041     (
00042         typename HashTable<AnyType, Key, Hash>::const_iterator cit = h.cbegin();
00043         cit != h.cend();
00044         ++cit
00045     )
00046     {
00047         insert(cit.key());
00048     }
00049 }
00050 
00051 
00052 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00053 
00054 template<class Key, class Hash>
00055 inline bool Foam::HashSet<Key, Hash>::operator[](const Key& key) const
00056 {
00057     return found(key);
00058 }
00059 
00060 
00061 template<class Key, class Hash>
00062 bool Foam::HashSet<Key, Hash>::operator==(const HashSet<Key, Hash>& rhs) const
00063 {
00064     // Are all lhs elements in rhs?
00065     for (const_iterator iter = this->cbegin(); iter != this->cend(); ++iter)
00066     {
00067         if (!rhs.found(iter.key()))
00068         {
00069             return false;
00070         }
00071     }
00072 
00073     // Are all rhs elements in lhs?
00074     for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
00075     {
00076         if (!found(iter.key()))
00077         {
00078             return false;
00079         }
00080     }
00081 
00082     return true;
00083 }
00084 
00085 
00086 template<class Key, class Hash>
00087 bool Foam::HashSet<Key, Hash>::operator!=(const HashSet<Key, Hash>& rhs) const
00088 {
00089     return !(operator==(rhs));
00090 }
00091 
00092 
00093 template<class Key, class Hash>
00094 void Foam::HashSet<Key, Hash>::operator|=(const HashSet<Key, Hash>& rhs)
00095 {
00096     // Add rhs elements into lhs
00097     for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
00098     {
00099         insert(iter.key());
00100     }
00101 }
00102 
00103 
00104 template<class Key, class Hash>
00105 void Foam::HashSet<Key, Hash>::operator&=(const HashSet<Key, Hash>& rhs)
00106 {
00107     // Remove elements not also found in rhs
00108     for (iterator iter = this->cbegin(); iter != this->cend(); ++iter)
00109     {
00110         if (!rhs.found(iter.key()))
00111         {
00112             erase(iter);
00113         }
00114     }
00115 }
00116 
00117 
00118 template<class Key, class Hash>
00119 void Foam::HashSet<Key, Hash>::operator^=(const HashSet<Key, Hash>& rhs)
00120 {
00121     // Add missed rhs elements, remove duplicate elements
00122     for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
00123     {
00124         if (found(iter.key()))
00125         {
00126             erase(iter.key());
00127         }
00128         else
00129         {
00130             insert(iter.key());
00131         }
00132     }
00133 }
00134 
00135 
00136 // same as HashTable::erase()
00137 template<class Key, class Hash>
00138 void Foam::HashSet<Key, Hash>::operator-=(const HashSet<Key, Hash>& rhs)
00139 {
00140     // Remove rhs elements from lhs
00141     for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
00142     {
00143         erase(iter.key());
00144     }
00145 }
00146 
00147 
00148 // * * * * * * * * * * * * * * * Friend Operators  * * * * * * * * * * * * * //
00149 
00150 /* * * * * * * * * * * * * * * * Global operators  * * * * * * * * * * * * * */
00151 
00152 template<class Key, class Hash>
00153 Foam::HashSet<Key, Hash>
00154 Foam::operator|
00155 (
00156     const HashSet<Key, Hash>& hash1,
00157     const HashSet<Key, Hash>& hash2
00158 )
00159 {
00160     HashSet<Key, Hash> out(hash1);
00161     out |= hash2;
00162     return out;
00163 }
00164 
00165 
00166 template<class Key, class Hash>
00167 Foam::HashSet<Key, Hash>
00168 Foam::operator&
00169 (
00170     const HashSet<Key, Hash>& hash1,
00171     const HashSet<Key, Hash>& hash2
00172 )
00173 {
00174     HashSet<Key, Hash> out(hash1);
00175     out &= hash2;
00176     return out;
00177 }
00178 
00179 
00180 template<class Key, class Hash>
00181 Foam::HashSet<Key, Hash>
00182 Foam::operator^
00183 (
00184     const HashSet<Key, Hash>& hash1,
00185     const HashSet<Key, Hash>& hash2
00186 )
00187 {
00188     HashSet<Key, Hash> out(hash1);
00189     out ^= hash2;
00190     return out;
00191 }
00192 
00193 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00194 
00195 #endif
00196 
00197 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd