Forward compatibility: flex
[foam-extend-3.2.git] / src / foam / containers / HashTables / HashSet / HashSet.C
blobcca030abeee6b9752f696e5e985dc02c49397784
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     | Version:     3.2
5     \\  /    A nd           | Web:         http://www.foam-extend.org
6      \\/     M anipulation  | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend 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 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #ifndef HashSet_C
27 #define HashSet_C
29 #include "HashSet.H"
31 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
33 template<class Key, class Hash>
34 template<class AnyType, class AnyHash>
35 Foam::HashSet<Key, Hash>::HashSet
37     const HashTable<AnyType, Key, AnyHash>& h
40     HashTable<nil, Key, Hash>(h.size())
42     for
43     (
44         typename HashTable<AnyType, Key, AnyHash>::const_iterator
45         cit = h.cbegin();
46         cit != h.cend();
47         ++cit
48     )
49     {
50         this->insert(cit.key());
51     }
55 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
57 template<class Key, class Hash>
58 Foam::label Foam::HashSet<Key, Hash>::insert(const UList<Key>& lst)
60     label count = 0;
61     forAll(lst, elemI)
62     {
63         if (this->insert(lst[elemI]))
64         {
65             ++count;
66         }
67     }
69     return count;
73 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
75 template<class Key, class Hash>
76 inline bool Foam::HashSet<Key, Hash>::operator[](const Key& key) const
78     return this->found(key);
82 template<class Key, class Hash>
83 bool Foam::HashSet<Key, Hash>::operator==(const HashSet<Key, Hash>& rhs) const
85     // Are all lhs elements in rhs?
86     for (const_iterator iter = this->cbegin(); iter != this->cend(); ++iter)
87     {
88         if (!rhs.found(iter.key()))
89         {
90             return false;
91         }
92     }
94     // Are all rhs elements in lhs?
95     for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
96     {
97         if (!this->found(iter.key()))
98         {
99             return false;
100         }
101     }
103     return true;
107 template<class Key, class Hash>
108 bool Foam::HashSet<Key, Hash>::operator!=(const HashSet<Key, Hash>& rhs) const
110     return !(operator==(rhs));
114 template<class Key, class Hash>
115 void Foam::HashSet<Key, Hash>::operator|=(const HashSet<Key, Hash>& rhs)
117     // Add rhs elements into lhs
118     for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
119     {
120         this->insert(iter.key());
121     }
125 template<class Key, class Hash>
126 void Foam::HashSet<Key, Hash>::operator&=(const HashSet<Key, Hash>& rhs)
128     // Remove elements not also found in rhs
129     for (iterator iter = this->begin(); iter != this->end(); ++iter)
130     {
131         if (!rhs.found(iter.key()))
132         {
133             this->erase(iter);
134         }
135     }
139 template<class Key, class Hash>
140 void Foam::HashSet<Key, Hash>::operator^=(const HashSet<Key, Hash>& rhs)
142     // Add missed rhs elements, remove duplicate elements
143     for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
144     {
145         if (this->found(iter.key()))
146         {
147             this->erase(iter.key());
148         }
149         else
150         {
151             this->insert(iter.key());
152         }
153     }
157 // same as HashTable::erase()
158 template<class Key, class Hash>
159 void Foam::HashSet<Key, Hash>::operator-=(const HashSet<Key, Hash>& rhs)
161     // Remove rhs elements from lhs
162     for (const_iterator iter = rhs.cbegin(); iter != rhs.cend(); ++iter)
163     {
164         this->erase(iter.key());
165     }
169 /* * * * * * * * * * * * * * * * Global operators  * * * * * * * * * * * * * */
171 template<class Key, class Hash>
172 Foam::HashSet<Key, Hash>
173 Foam::operator|
175     const HashSet<Key, Hash>& hash1,
176     const HashSet<Key, Hash>& hash2
179     HashSet<Key, Hash> out(hash1);
180     out |= hash2;
181     return out;
185 template<class Key, class Hash>
186 Foam::HashSet<Key, Hash>
187 Foam::operator&
189     const HashSet<Key, Hash>& hash1,
190     const HashSet<Key, Hash>& hash2
193     HashSet<Key, Hash> out(hash1);
194     out &= hash2;
195     return out;
199 template<class Key, class Hash>
200 Foam::HashSet<Key, Hash>
201 Foam::operator^
203     const HashSet<Key, Hash>& hash1,
204     const HashSet<Key, Hash>& hash2
207     HashSet<Key, Hash> out(hash1);
208     out ^= hash2;
209     return out;
212 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
214 #endif
216 // ************************************************************************* //