Test-hashTable.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 "HashTable.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 HashTable
35 
36 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
37 // Main program:
38 
39 int main()
40 {
41  HASHTABLE_CLASS<double> table1(13);
42  HASHTABLE_CLASS<double>::iterator iter;
43 
44  table1.insert("aaa", 1.0);
45  table1.insert("aba", 2.0);
46  table1.insert("aca", 3.0);
47  table1.insert("ada", 4.0);
48  table1.insert("aeq", 5.0);
49  table1.insert("aaw", 6.0);
50  table1.insert("abs", 7.0);
51  table1.insert("acr", 8.0);
52  table1.insert("adx", 9.0);
53  table1.insert("aec", 10.0);
54 
55  // erase by key
56  table1.erase("aaw");
57 
58  // erase by iterator
59  iter = table1.find("abs");
60  table1.erase(iter);
61 
62  Info<< "\ntable1 toc: " << table1.toc() << endl;
63  Info<< "\ntable1 sortedToc: " << table1.sortedToc() << endl;
64  table1.printInfo(Info)
65  << "table1 [" << table1.size() << "] " << endl;
66  forAllIter(HASHTABLE_CLASS<double>, table1, iter)
67  {
68  Info<< iter.key() << " => " << iter() << nl;
69  }
70 
71  table1.set("acr", 108);
72  table1.set("adx", 109);
73  table1.set("aec", 100);
74  table1("aaw") -= 1000;
75  table1("aeq") += 1000;
76 
77  Info<< "\noverwrote some values table1: " << table1 << endl;
78 
79  Info<< "\ntest find:" << endl;
80  Info<< table1.find("aaa")() << nl
81  << table1.find("aba")() << nl
82  << table1.find("aca")() << nl
83  << table1.find("ada")() << nl
84  << table1.find("aeq")() << nl
85  << table1.find("acr")() << nl
86  << table1.find("adx")() << nl
87  << table1.find("aec")() << nl
88  << table1["aaa"] << nl;
89 
90  {
91  OStringStream os;
92  os << table1;
93  HASHTABLE_CLASS<double> readTable(IStringStream(os.str())(), 100);
94 
95  Info<< "Istream constructor:" << readTable << endl;
96  }
97 
98 
99  HASHTABLE_CLASS<double> table2(table1);
100  HASHTABLE_CLASS<double> table3(table1.xfer());
101 
102  Info<< "\ncopy table1 -> table2" << nl
103  << "transfer table1 -> table3 via the xfer() method" << nl;
104 
105  Info<< "\ntable1" << table1 << nl
106  << "\ntable2" << table2 << nl
107  << "\ntable3" << table3 << nl;
108 
109  Info<< "\nerase table2 by iterator" << nl;
110  forAllIter(HASHTABLE_CLASS<double>, table2, iter)
111  {
112  Info<< "erasing " << iter.key() << " => " << iter() << " ... ";
113  table2.erase(iter);
114  Info<< "erased" << endl;
115  }
116 
117  Info<< "\ntable1" << table1 << nl
118  << "\ntable2" << table2 << nl
119  << "\ntable3" << table3 << nl;
120 
121  table3.resize(1);
122  Info<< "\nresize(1) table3" << nl;
123  table3.printInfo(Info)
124  << table3 << nl;
125 
126  table3.resize(10000);
127  Info<< "\nresize(10000) table3" << nl;
128  table3.printInfo(Info)
129  << table3 << nl;
130 
131  HASHTABLE_CLASS<double> table4;
132 
133  table4 = table3;
134  Info<< "\ncopy table3 -> table4 " << table4 << nl;
135 
136  Info<< "\nclear table4 ... ";
137  table4.clear();
138  Info<< "[" << table4.size() << "] " << table4 << nl;
139 
140  table1 = table3;
141  Info<< "\ncopy table3 -> table1 (previously transferred)" << table1 << nl;
142 
143  Info<< "test table1 == table3 : " << (table1 == table3) << nl;
144  table1.erase(table1.begin());
145  Info<< "removed an element - test table1 != table3 : "
146  << (table1 != table3) << nl;
147 
148  // insert a few things into table2
149  table2.set("ada", 14.0);
150  table2.set("aeq", 15.0);
151  table2.set("aaw", 16.0);
152  table2.set("abs", 17.0);
153  table2.set("adx", 20.0);
154 
155  Info<< "\ntable1" << table1 << nl
156  << "\ntable2" << table2 << nl;
157 
158  label nErased = table1.erase(table2);
159 
160  Info<< "\nerase table2 keys from table1 (removed "
161  << nErased << " elements)" << nl
162  << "\ntable1" << table1 << nl
163  << "\ntable2" << table2 << nl;
164 
165 
166  Info<< "\ntable3" << table3
167  << "\nclearStorage table3 ... ";
168  table3.clearStorage();
169  Info<< table3 << nl;
170 
171  Info<< "\nDone\n";
172 
173  return 0;
174 }
175 
176 
177 // ************************************************************************* //
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
HashTable.H
main
int main()
Definition: Test-hashTable.C:39
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
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