Test-hashSet.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
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.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Description
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "HashSet.H"
29 #include "Map.H"
30 
31 using namespace Foam;
32 
33 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
34 // Main program:
35 
36 int main(int argc, char *argv[])
37 {
38  wordHashSet setA(0);
40 
41  HashTable<nil> tableB;
42  Map<label> mapA;
43 
44  setA.insert("kjhk");
45  setA.insert("kjhk2");
46 
47  tableA.insert("value1", 1);
48  tableA.insert("value2", 2);
49  tableA.insert("value3", 3);
50 
51  tableB.insert("value4", nil());
52  tableB.insert("value5", nil());
53  tableB.insert("value6", nil());
54 
55  mapA.set(1, 1);
56  mapA.set(2, 2);
57  mapA.set(3, 3);
58  mapA.set(4, 4);
59 
60  Info<< setA << endl;
61  Info<< tableA << endl;
62  Info<< mapA << endl;
63 
64  Info<< "create from HashSet: ";
65  Info<< wordHashSet(setA) << endl;
66  Info<< "create from HashTable<T>: ";
67  Info<< wordHashSet(tableA) << endl;
68  Info<< "create from HashTable<nil>: ";
69  Info<< wordHashSet(tableB) << endl;
70 
71  Info<< "create from Map<label>: ";
72  Info<< labelHashSet(mapA) << endl;
73 
74  Info<<"combined toc: "
75  << (wordHashSet(setA) | wordHashSet(tableA) | wordHashSet(tableB))
76  << nl;
77 
78 
79  labelHashSet setB(1);
80  setB.insert(11);
81  setB.insert(42);
82 
83  Info<< "setB : " << setB << endl;
84 
85  labelHashSet setC(1);
86  setC.insert(2008);
87  setC.insert(1984);
88 
89  Info<< "setC : " << setC << endl;
90 
91  labelHashSet setD(1);
92  setD.insert(11);
93  setD.insert(100);
94  setD.insert(49);
95  setD.insert(36);
96  setD.insert(2008);
97 
98  Info<< "setD : " << setD << endl;
99 
100  Info<< "setB == setC: " << (setB == setC) << endl;
101  Info<< "setC != setD: " << (setC != setD) << endl;
102 
103  // test operations
104  setB += setC;
105  Info<< "setB += setC : " << setB << endl;
106 
107  setB &= setD;
108  Info<< "setB &= setD : " << setB << endl;
109 
110  Info<< "setB : " << setB << endl;
111  Info<< "setC : " << setC << endl;
112  Info<< "setD : " << setD << endl;
113  Info<< "setB ^ setC ^ setD : " << (setB ^ setC ^ setD) << endl;
114 
115  // test operator[]
116 
117  Info<< "setD : " << setD << endl;
118  if (setD[0])
119  {
120  Info<< "setD has 0" << endl;
121  }
122  else
123  {
124  Info<< "setD has no 0" << endl;
125  }
126 
127 
128  if (setD[11])
129  {
130  Info<< "setD has 11" << endl;
131  }
132  else
133  {
134  Info<< "setD has no 0" << endl;
135  }
136 
137  Info<< "setD : " << setD << endl;
138 
139  // this doesn't work (yet?)
140  // setD[12] = true;
141 
142  List<label> someLst(10);
143  forAll(someLst, elemI)
144  {
145  someLst[elemI] = elemI*elemI;
146  }
147 
148  label added = setD.set(someLst);
149  Info<< "added " << added << " from " << someLst.size() << endl;
150  Info<< "setD : " << setD << endl;
151 
152 
153  return 0;
154 }
155 
156 
157 // ************************************************************************* //
main
int main(int argc, char *argv[])
Definition: Test-hashSet.C:36
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::HashTable::insert
bool insert(const Key &, const T &newElmt)
Insert a new hashedEntry.
Definition: HashTableI.H:80
Foam::Map< label >
Foam::nil
A zero-sized class without any storage. Used, for example, in HashSet.
Definition: nil.H:58
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::HashSet
A HashTable with keys but without contents.
Definition: HashSet.H:59
Map.H
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::Info
messageStream Info
HashSet.H
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::HashTable
An STL-conforming hash table.
Definition: HashTable.H:61
Foam::List< label >
Foam::HashSet::insert
bool insert(const Key &key)
Insert a new entry.
Definition: HashSet.H:116
Foam::wordHashSet
HashSet wordHashSet
A HashSet with word keys.
Definition: HashSet.H:207
Foam::List::size
void size(const label)
Override size to be inconsistent with allocated storage.
Foam::labelHashSet
HashSet< label, Hash< label > > labelHashSet
A HashSet with label keys.
Definition: HashSet.H:210
Foam::HashSet::set
bool set(const Key &key)
Same as insert (cannot overwrite nil content)
Definition: HashSet.H:126