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

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