initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / containers / HashTables / HashSet / HashSet.H
blob6353818d7efea8e8b93e8e499f821feecc9b3c13
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 Class
26     Foam::HashSet
28 Description
29     A HashTable with keys but without contents.
31 Typedef
32     Foam::wordHashSet
34 Description
35     A HashSet with (the default) word keys.
37 Typedef
38     Foam::labelHashSet
40 Description
41     A HashSet with label keys.
43 \*---------------------------------------------------------------------------*/
45 #ifndef HashSet_H
46 #define HashSet_H
48 #include "HashTable.H"
49 #include "nil.H"
51 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
53 namespace Foam
56 /*---------------------------------------------------------------------------*\
57                            Class HashSet Declaration
58 \*---------------------------------------------------------------------------*/
60 template<class Key=word, class Hash=string::hash>
61 class HashSet
63     public HashTable<nil, Key, Hash>
66 public:
68     typedef typename HashTable<nil, Key, Hash>::iterator iterator;
69     typedef typename HashTable<nil, Key, Hash>::const_iterator const_iterator;
72     // Constructors
74         //- Construct given initial size
75         HashSet(const label size = 128)
76         :
77             HashTable<nil, Key, Hash>(size)
78         {}
80         //- Construct from Istream
81         HashSet(Istream& is)
82         :
83             HashTable<nil, Key, Hash>(is)
84         {}
86         //- Construct from UList of Key
87         HashSet(const UList<Key>& lst)
88         :
89             HashTable<nil, Key, Hash>(2*lst.size())
90         {
91             forAll(lst, i)
92             {
93                 insert(lst[i]);
94             }
95         }
97         //- Construct as copy
98         HashSet(const HashSet<Key, Hash>& hs)
99         :
100             HashTable<nil, Key, Hash>(hs)
101         {}
103         //- Construct by transferring the parameter contents
104         HashSet(const Xfer<HashSet<Key, Hash> >& hs)
105         :
106             HashTable<nil, Key, Hash>(hs)
107         {}
109         //- Construct by transferring the parameter contents
110         HashSet(const Xfer<HashTable<nil, Key, Hash> >& hs)
111         :
112             HashTable<nil, Key, Hash>(hs)
113         {}
115         //- Construct from the keys of another HashTable,
116         //  the type of values held is arbitrary.
117         template<class AnyType>
118         HashSet(const HashTable<AnyType, Key, Hash>&);
120     // Member Functions
122         // Edit
124         //- Insert a new entry
125         bool insert(const Key& key)
126         {
127             return HashTable<nil, Key, Hash>::insert(key, nil());
128         }
130         //- Same as insert (cannot overwrite nil content)
131         bool set(const Key& key)
132         {
133             return HashTable<nil, Key, Hash>::insert(key, nil());
134         }
136     // Member Operators
138         //- Return true if the entry exists, same as found()
139         inline bool operator[](const Key&) const;
141         //- Equality. Two hashtables are equal when their contents are equal.
142         //  Independent of table size or order.
143         bool operator==(const HashSet<Key, Hash>&) const;
145         //- The opposite of the equality operation.
146         bool operator!=(const HashSet<Key, Hash>&) const;
149         //- Combine entries from HashSets
150         void operator|=(const HashSet<Key, Hash>&);
152         //- Only retain entries found in both HashSets
153         void operator&=(const HashSet<Key, Hash>&);
155         //- Only retain unique entries (xor)
156         void operator^=(const HashSet<Key, Hash>&);
158         //- Add entries listed in the given HashSet to this HashSet
159         inline void operator+=(const HashSet<Key, Hash>& rhs)
160         {
161             this->operator|=(rhs);
162         }
164         //- Remove entries listed in the given HashSet from this HashSet
165         void operator-=(const HashSet<Key, Hash>&);
170 // Global Operators
172 //- Combine entries from HashSets
173 template<class Key, class Hash>
174 HashSet<Key,Hash> operator|
176     const HashSet<Key,Hash>& hash1,
177     const HashSet<Key,Hash>& hash2
181 //- Create a HashSet that only contains entries found in both HashSets
182 template<class Key, class Hash>
183 HashSet<Key,Hash> operator&
185     const HashSet<Key,Hash>& hash1,
186     const HashSet<Key,Hash>& hash2
190 //- Create a HashSet that only contains unique entries (xor)
191 template<class Key, class Hash>
192 HashSet<Key,Hash> operator^
194     const HashSet<Key,Hash>& hash1,
195     const HashSet<Key,Hash>& hash2
199 //- A HashSet with word keys.
200 typedef HashSet<> wordHashSet;
202 //- A HashSet with label keys.
203 typedef HashSet<label, Hash<label> > labelHashSet;
206 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
208 } // End namespace Foam
210 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
212 #ifdef NoRepository
213 #   include "HashSet.C"
214 #endif
216 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
218 #endif
220 // ************************************************************************* //