initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / containers / HashTables / HashSet / HashSet.C
blobd63921f95bac5638f029190f1a5a7077bdbdf2f6
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     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
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #ifndef HashSet_C
28 #define HashSet_C
30 #include "HashSet.H"
32 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
34 template<class Key, class Hash>
35 template<class AnyType>
36 Foam::HashSet<Key, Hash>::HashSet(const HashTable<AnyType, Key, Hash>& h)
38     HashTable<nil, Key, Hash>(h.size())
40     for
41     (
42         typename HashTable<AnyType, Key, Hash>::const_iterator cit = h.cbegin();
43         cit != h.cend();
44         ++cit
45     )
46     {
47         insert(cit.key());
48     }
52 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
54 template<class Key, class Hash>
55 inline bool Foam::HashSet<Key, Hash>::operator[](const Key& key) const
57     return found(key);
61 template<class Key, class Hash>
62 bool Foam::HashSet<Key, Hash>::operator==(const HashSet<Key, Hash>& rhs) const
64     // Are all lhs elements in rhs?
65     for (const_iterator iter = this->cbegin(); iter != this->cend(); ++iter)
66     {
67         if (!rhs.found(iter.key()))
68         {
69             return false;
70         }
71     }
73     // Are all rhs elements in lhs?
74     for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
75     {
76         if (!found(iter.key()))
77         {
78             return false;
79         }
80     }
82     return true;
86 template<class Key, class Hash>
87 bool Foam::HashSet<Key, Hash>::operator!=(const HashSet<Key, Hash>& rhs) const
89     return !(operator==(rhs));
93 template<class Key, class Hash>
94 void Foam::HashSet<Key, Hash>::operator|=(const HashSet<Key, Hash>& rhs)
96     // Add rhs elements into lhs
97     for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
98     {
99         insert(iter.key());
100     }
104 template<class Key, class Hash>
105 void Foam::HashSet<Key, Hash>::operator&=(const HashSet<Key, Hash>& rhs)
107     // Remove elements not also found in rhs
108     for (iterator iter = this->cbegin(); iter != this->cend(); ++iter)
109     {
110         if (!rhs.found(iter.key()))
111         {
112             erase(iter);
113         }
114     }
118 template<class Key, class Hash>
119 void Foam::HashSet<Key, Hash>::operator^=(const HashSet<Key, Hash>& rhs)
121     // Add missed rhs elements, remove duplicate elements
122     for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
123     {
124         if (found(iter.key()))
125         {
126             erase(iter.key());
127         }
128         else
129         {
130             insert(iter.key());
131         }
132     }
136 // same as HashTable::erase()
137 template<class Key, class Hash>
138 void Foam::HashSet<Key, Hash>::operator-=(const HashSet<Key, Hash>& rhs)
140     // Remove rhs elements from lhs
141     for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
142     {
143         erase(iter.key());
144     }
148 // * * * * * * * * * * * * * * * Friend Operators  * * * * * * * * * * * * * //
150 /* * * * * * * * * * * * * * * * Global operators  * * * * * * * * * * * * * */
152 template<class Key, class Hash>
153 Foam::HashSet<Key, Hash>
154 Foam::operator|
156     const HashSet<Key, Hash>& hash1,
157     const HashSet<Key, Hash>& hash2
160     HashSet<Key, Hash> out(hash1);
161     out |= hash2;
162     return out;
166 template<class Key, class Hash>
167 Foam::HashSet<Key, Hash>
168 Foam::operator&
170     const HashSet<Key, Hash>& hash1,
171     const HashSet<Key, Hash>& hash2
174     HashSet<Key, Hash> out(hash1);
175     out &= hash2;
176     return out;
180 template<class Key, class Hash>
181 Foam::HashSet<Key, Hash>
182 Foam::operator^
184     const HashSet<Key, Hash>& hash1,
185     const HashSet<Key, Hash>& hash2
188     HashSet<Key, Hash> out(hash1);
189     out ^= hash2;
190     return out;
193 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
195 #endif
197 // ************************************************************************* //