Test-staticHashTable.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 \*---------------------------------------------------------------------------*/
25 
26 #include "StaticHashTable.H"
27 #include "IOstreams.H"
28 #include "IStringStream.H"
29 #include "OStringStream.H"
30 
31 using namespace Foam;
32 
33 // use define so we can easily test other implementations
34 #define HASHTABLE_CLASS StaticHashTable
35 
36 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
37 // Main program:
38 
39 int main()
40 {
41  HASHTABLE_CLASS<double> table1(13);
42 
43  table1.insert("aaa", 1.0);
44  table1.insert("aba", 2.0);
45  table1.insert("aca", 3.0);
46  table1.insert("ada", 4.0);
47  table1.insert("aeq", 5.0);
48  table1.insert("aaw", 6.0);
49  table1.insert("abs", 7.0);
50  table1.insert("acr", 8.0);
51  table1.insert("adx", 9.0);
52  table1.insert("aec", 10.0);
53 
54  table1.erase("aaw");
55  table1.erase("abs");
56 
57  Info<< "\ntable1 toc: " << table1.toc() << endl;
58  table1.printInfo(Info)
59  << "table1 [" << table1.size() << "] " << endl;
60  forAllIter(HASHTABLE_CLASS<double>, table1, iter)
61  {
62  Info<< iter.key() << " => " << iter() << nl;
63  }
64 
65  table1.set("acr", 108);
66  table1.set("adx", 109);
67  table1.set("aec", 100);
68  table1("aaw") -= 1000;
69  table1("aeq") += 1000;
70 
71  Info<< "\noverwrote some values table1: " << table1 << endl;
72 
73  Info<< "\ntest find:" << endl;
74  Info<< table1.find("aaa")() << nl
75  << table1.find("aba")() << nl
76  << table1.find("aca")() << nl
77  << table1.find("ada")() << nl
78  << table1.find("aeq")() << nl
79  << table1.find("acr")() << nl
80  << table1.find("adx")() << nl
81  << table1.find("aec")() << nl
82  << table1["aaa"] << nl;
83 
84  {
85  OStringStream os;
86  os << table1;
87  HASHTABLE_CLASS<double> readTable(IStringStream(os.str())(), 100);
88 
89  Info<< "Istream constructor:" << readTable << endl;
90  }
91 
92 
93  HASHTABLE_CLASS<double> table2(table1);
94  HASHTABLE_CLASS<double> table3(table1.xfer());
95 
96  Info<< "\ncopy table1 -> table2" << nl
97  << "transfer table1 -> table3 via the xfer() method" << nl;
98 
99  Info<< "\ntable1" << table1 << nl
100  << "\ntable2" << table2 << nl
101  << "\ntable3" << table3 << nl;
102 
103  Info<< "\nerase table2 by iterator" << nl;
104  forAllIter(HASHTABLE_CLASS<double>, table2, iter)
105  {
106  Info<< "erasing " << iter.key() << " => " << iter() << " ... ";
107  table2.erase(iter);
108  Info<< "erased" << endl;
109  }
110 
111  Info<< "\ntable1" << table1 << nl
112  << "\ntable2" << table2 << nl
113  << "\ntable3" << table3 << nl;
114 
115  table3.resize(1);
116  Info<< "\nresize(1) table3" << nl;
117  table3.printInfo(Info)
118  << table3 << nl;
119 
120  table3.resize(10000);
121  Info<< "\nresize(10000) table3" << nl;
122  table3.printInfo(Info)
123  << table3 << nl;
124 
125  HASHTABLE_CLASS<double> table4;
126 
127  table4 = table3;
128  Info<< "\ncopy table3 -> table4 " << table4 << nl;
129 
130  Info<< "\nclear table4 ... ";
131  table4.clear();
132  Info<< "[" << table4.size() << "] " << table4 << nl;
133 
134  table1 = table3;
135  Info<< "\ncopy table3 -> table1 (previously transferred)" << table1 << nl;
136 
137  Info<< "test table1 == table3 : " << (table1 == table3) << nl;
138  table1.erase(table1.begin());
139  Info<< "removed an element - test table1 != table3 : "
140  << (table1 != table3) << nl;
141 
142  // insert a few things into table2
143  table2.set("ada", 14.0);
144  table2.set("aeq", 15.0);
145  table2.set("aaw", 16.0);
146  table2.set("abs", 17.0);
147  table2.set("adx", 20.0);
148 
149  Info<< "\ntable1" << table1 << nl
150  << "\ntable2" << table2 << nl;
151 
152  label nErased = table1.erase(table2);
153 
154  Info<< "\nerase table2 keys from table1 (removed "
155  << nErased << " elements)" << nl
156  << "\ntable1" << table1 << nl
157  << "\ntable2" << table2 << nl;
158 
159 
160  Info<< "\ntable3" << table3
161  << "\nclearStorage table3 ... ";
162  table3.clearStorage();
163  Info<< table3 << nl;
164 
165  Info<< "\nDone\n";
166 
167  return 0;
168 }
169 
170 
171 // ************************************************************************* //
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
forAllIter
#define forAllIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:431
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
IStringStream.H
Foam::OStringStream::str
string str() const
Return the string.
Definition: OStringStream.H:107
StaticHashTable.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
OStringStream.H
Foam::IStringStream
Input from memory buffer stream.
Definition: IStringStream.H:49
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::OStringStream
Output to memory buffer stream.
Definition: OStringStream.H:49
main
int main()
Definition: Test-staticHashTable.C:39