1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
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 "HashTable.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 HashTable
36 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
41 HASHTABLE_CLASS<double> table1(13);
42 HASHTABLE_CLASS<double>::iterator iter;
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);
59 iter = table1.find("abs");
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)
68 Info<< iter.key() << " => " << iter() << nl;
71 table1.set("acr", 108);
72 table1.set("adx", 109);
73 table1.set("aec", 100);
74 table1("aaw") -= 1000;
75 table1("aeq") += 1000;
77 Info<< "\noverwrote some values table1: " << table1 << endl;
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;
93 HASHTABLE_CLASS<double> readTable(IStringStream(os.str())(), 100);
95 Info<< "Istream constructor:" << readTable << endl;
99 HASHTABLE_CLASS<double> table2(table1);
100 HASHTABLE_CLASS<double> table3(table1.xfer());
102 Info<< "\ncopy table1 -> table2" << nl
103 << "transfer table1 -> table3 via the xfer() method" << nl;
105 Info<< "\ntable1" << table1 << nl
106 << "\ntable2" << table2 << nl
107 << "\ntable3" << table3 << nl;
109 Info<< "\nerase table2 by iterator" << nl;
110 forAllIter(HASHTABLE_CLASS<double>, table2, iter)
112 Info<< "erasing " << iter.key() << " => " << iter() << " ... ";
114 Info<< "erased" << endl;
117 Info<< "\ntable1" << table1 << nl
118 << "\ntable2" << table2 << nl
119 << "\ntable3" << table3 << nl;
122 Info<< "\nresize(1) table3" << nl;
123 table3.printInfo(Info)
126 table3.resize(10000);
127 Info<< "\nresize(10000) table3" << nl;
128 table3.printInfo(Info)
131 HASHTABLE_CLASS<double> table4;
134 Info<< "\ncopy table3 -> table4 " << table4 << nl;
136 Info<< "\nclear table4 ... ";
138 Info<< "[" << table4.size() << "] " << table4 << nl;
141 Info<< "\ncopy table3 -> table1 (previously transferred)" << table1 << nl;
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;
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);
155 Info<< "\ntable1" << table1 << nl
156 << "\ntable2" << table2 << nl;
158 label nErased = table1.erase(table2);
160 Info<< "\nerase table2 keys from table1 (removed "
161 << nErased << " elements)" << nl
162 << "\ntable1" << table1 << nl
163 << "\ntable2" << table2 << nl;
166 Info<< "\ntable3" << table3
167 << "\nclearStorage table3 ... ";
168 table3.clearStorage();
177 // ************************************************************************* //