1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2009 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 the
13 Free Software Foundation; either version 2 of the License, or (at your
14 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, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "HashTable.H"
28 #include "IOstreams.H"
29 #include "IStringStream.H"
30 #include "OStringStream.H"
34 // use define so we can easily test other implementations
35 #define HASHTABLE_CLASS HashTable
37 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
42 HASHTABLE_CLASS<double> table1(13);
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);
58 Info<< "\ntable1 toc: " << table1.toc() << endl;
59 table1.printInfo(Info)
60 << "table1 [" << table1.size() << "] " << endl;
61 forAllIter(HASHTABLE_CLASS<double>, table1, iter)
63 Info<< iter.key() << " => " << iter() << nl;
66 table1.set("acr", 108);
67 table1.set("adx", 109);
68 table1.set("aec", 100);
69 table1("aaw") -= 1000;
70 table1("aeq") += 1000;
72 Info<< "\noverwrote some values table1: " << table1 << endl;
74 Info<< "\ntest find:" << endl;
75 Info<< table1.find("aaa")() << nl
76 << table1.find("aba")() << nl
77 << table1.find("aca")() << nl
78 << table1.find("ada")() << nl
79 << table1.find("aeq")() << nl
80 << table1.find("acr")() << nl
81 << table1.find("adx")() << nl
82 << table1.find("aec")() << nl
83 << table1["aaa"] << nl;
88 HASHTABLE_CLASS<double> readTable(IStringStream(os.str())(), 100);
90 Info<< "Istream constructor:" << readTable << endl;
94 HASHTABLE_CLASS<double> table2(table1);
95 HASHTABLE_CLASS<double> table3(table1.xfer());
97 Info<< "\ncopy table1 -> table2" << nl
98 << "transfer table1 -> table3 via the xfer() method" << nl;
100 Info<< "\ntable1" << table1 << nl
101 << "\ntable2" << table2 << nl
102 << "\ntable3" << table3 << nl;
104 Info<< "\nerase table2 by iterator" << nl;
105 forAllIter(HASHTABLE_CLASS<double>, table2, iter)
107 Info<< "erasing " << iter.key() << " => " << iter() << " ... ";
109 Info<< "erased" << endl;
112 Info<< "\ntable1" << table1 << nl
113 << "\ntable2" << table2 << nl
114 << "\ntable3" << table3 << nl;
117 Info<< "\nresize(1) table3" << nl;
118 table3.printInfo(Info)
121 table3.resize(10000);
122 Info<< "\nresize(10000) table3" << nl;
123 table3.printInfo(Info)
126 HASHTABLE_CLASS<double> table4;
129 Info<< "\ncopy table3 -> table4 " << table4 << nl;
131 Info<< "\nclear table4 ... ";
133 Info<< "[" << table4.size() << "] " << table4 << nl;
136 Info<< "\ncopy table3 -> table1 (previously transferred)" << table1 << nl;
138 Info<< "test table1 == table3 : " << (table1 == table3) << nl;
139 table1.erase(table1.begin());
140 Info<< "removed an element - test table1 != table3 : "
141 << (table1 != table3) << nl;
143 // insert a few things into table2
144 table2.set("ada", 14.0);
145 table2.set("aeq", 15.0);
146 table2.set("aaw", 16.0);
147 table2.set("abs", 17.0);
148 table2.set("adx", 20.0);
150 Info<< "\ntable1" << table1 << nl
151 << "\ntable2" << table2 << nl;
153 label nErased = table1.erase(table2);
155 Info<< "\nerase table2 keys from table1 (removed "
156 << nErased << " elements)" << nl
157 << "\ntable1" << table1 << nl
158 << "\ntable2" << table2 << nl;
161 Info<< "\ntable3" << table3
162 << "\nclearStorage table3 ... ";
163 table3.clearStorage();
172 // ************************************************************************* //