1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
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
13 the Free Software Foundation, either version 3 of the License, or
14 (at your 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
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/>.
24 \*---------------------------------------------------------------------------*/
26 #include "StaticHashTable.H"
27 #include "IOstreams.H"
28 #include "IStringStream.H"
29 #include "OStringStream.H"
33 // use define so we can easily test other implementations
34 #define HASHTABLE_CLASS StaticHashTable
36 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
41 HASHTABLE_CLASS<double> table1(13);
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);
57 Info<< "\ntable1 toc: " << table1.toc() << endl;
58 table1.printInfo(Info)
59 << "table1 [" << table1.size() << "] " << endl;
60 forAllIter(HASHTABLE_CLASS<double>, table1, iter)
62 Info<< iter.key() << " => " << iter() << nl;
65 table1.set("acr", 108);
66 table1.set("adx", 109);
67 table1.set("aec", 100);
68 table1("aaw") -= 1000;
69 table1("aeq") += 1000;
71 Info<< "\noverwrote some values table1: " << table1 << endl;
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;
87 HASHTABLE_CLASS<double> readTable(IStringStream(os.str())(), 100);
89 Info<< "Istream constructor:" << readTable << endl;
93 HASHTABLE_CLASS<double> table2(table1);
94 HASHTABLE_CLASS<double> table3(table1.xfer());
96 Info<< "\ncopy table1 -> table2" << nl
97 << "transfer table1 -> table3 via the xfer() method" << nl;
99 Info<< "\ntable1" << table1 << nl
100 << "\ntable2" << table2 << nl
101 << "\ntable3" << table3 << nl;
103 Info<< "\nerase table2 by iterator" << nl;
104 forAllIter(HASHTABLE_CLASS<double>, table2, iter)
106 Info<< "erasing " << iter.key() << " => " << iter() << " ... ";
108 Info<< "erased" << endl;
111 Info<< "\ntable1" << table1 << nl
112 << "\ntable2" << table2 << nl
113 << "\ntable3" << table3 << nl;
116 Info<< "\nresize(1) table3" << nl;
117 table3.printInfo(Info)
120 table3.resize(10000);
121 Info<< "\nresize(10000) table3" << nl;
122 table3.printInfo(Info)
125 HASHTABLE_CLASS<double> table4;
128 Info<< "\ncopy table3 -> table4 " << table4 << nl;
130 Info<< "\nclear table4 ... ";
132 Info<< "[" << table4.size() << "] " << table4 << nl;
135 Info<< "\ncopy table3 -> table1 (previously transferred)" << table1 << nl;
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;
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);
149 Info<< "\ntable1" << table1 << nl
150 << "\ntable2" << table2 << nl;
152 label nErased = table1.erase(table2);
154 Info<< "\nerase table2 keys from table1 (removed "
155 << nErased << " elements)" << nl
156 << "\ntable1" << table1 << nl
157 << "\ntable2" << table2 << nl;
160 Info<< "\ntable3" << table3
161 << "\nclearStorage table3 ... ";
162 table3.clearStorage();
171 // ************************************************************************* //