Test-hashTable.C 5.08 KB
Newer Older
Henry's avatar
Henry committed
1
2
3
4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
5
    \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
Henry's avatar
Henry committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
     \\/     M anipulation  |
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

\*---------------------------------------------------------------------------*/

#include "HashTable.H"
#include "IOstreams.H"
#include "IStringStream.H"
#include "OStringStream.H"

using namespace Foam;

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
//  Main program:

int main()
{
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
    HashTable<scalar> table1
    {
        {"aaa", 1.0},
        {"aba", 2.0},
        {"aca", 3.0},
        {"ada", 4.0},
        {"aeq", 5.0},
        {"aaw", 6.0},
        {"abs", 7.0},
        {"acr", 8.0},
        {"adx", 9.0},
        {"aec", 10.0}
    };

    // Erase by key
Henry's avatar
Henry committed
53
54
    table1.erase("aaw");

55
56
    // Erase by iterator
    HashTable<scalar>::iterator iter = table1.find("abs");
Henry's avatar
Henry committed
57
58
59
60
61
62
    table1.erase(iter);

    Info<< "\ntable1 toc: " << table1.toc() << endl;
    Info<< "\ntable1 sortedToc: " << table1.sortedToc() << endl;
    table1.printInfo(Info)
        << "table1 [" << table1.size() << "] " << endl;
63
    forAllIter(HashTable<scalar>, table1, iter)
Henry's avatar
Henry committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
    {
        Info<< iter.key() << " => " << iter() << nl;
    }

    table1.set("acr", 108);
    table1.set("adx", 109);
    table1.set("aec", 100);
    table1("aaw") -= 1000;
    table1("aeq") += 1000;

    Info<< "\noverwrote some values table1: " << table1 << endl;

    Info<< "\ntest find:" << endl;
    Info<< table1.find("aaa")() << nl
        << table1.find("aba")() << nl
        << table1.find("aca")() << nl
        << table1.find("ada")() << nl
        << table1.find("aeq")() << nl
        << table1.find("acr")() << nl
        << table1.find("adx")() << nl
        << table1.find("aec")() << nl
        << table1["aaa"] << nl;

    {
        OStringStream os;
        os  << table1;
90
        HashTable<scalar> readTable(IStringStream(os.str())(), 100);
Henry's avatar
Henry committed
91
92
93
94
95

        Info<< "Istream constructor:" << readTable << endl;
    }


96
97
    HashTable<scalar> table2(table1);
    HashTable<scalar> table3(table1.xfer());
Henry's avatar
Henry committed
98
99
100
101
102
103
104
105
106

    Info<< "\ncopy table1 -> table2" << nl
        << "transfer table1 -> table3 via the xfer() method" << nl;

    Info<< "\ntable1" << table1 << nl
        << "\ntable2" << table2 << nl
        << "\ntable3" << table3 << nl;

    Info<< "\nerase table2 by iterator" << nl;
107
    forAllIter(HashTable<scalar>, table2, iter)
Henry's avatar
Henry committed
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
    {
        Info<< "erasing " << iter.key() << " => " << iter() << " ... ";
        table2.erase(iter);
        Info<< "erased" << endl;
    }

    Info<< "\ntable1" << table1 << nl
        << "\ntable2" << table2 << nl
        << "\ntable3" << table3 << nl;

    table3.resize(1);
    Info<< "\nresize(1) table3" << nl;
    table3.printInfo(Info)
        << table3 << nl;

    table3.resize(10000);
    Info<< "\nresize(10000) table3" << nl;
    table3.printInfo(Info)
        << table3 << nl;

128
    HashTable<scalar> table4;
Henry's avatar
Henry committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144

    table4 = table3;
    Info<< "\ncopy table3 -> table4 " << table4 << nl;

    Info<< "\nclear table4 ... ";
    table4.clear();
    Info<< "[" << table4.size() << "] " << table4 << nl;

    table1 = table3;
    Info<< "\ncopy table3 -> table1 (previously transferred)" << table1 << nl;

    Info<< "test table1 == table3 : " << (table1 == table3) << nl;
    table1.erase(table1.begin());
    Info<< "removed an element - test table1 != table3 : "
        << (table1 != table3) << nl;

145
    // Insert a few things into table2
Henry's avatar
Henry committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
    table2.set("ada", 14.0);
    table2.set("aeq", 15.0);
    table2.set("aaw", 16.0);
    table2.set("abs", 17.0);
    table2.set("adx", 20.0);

    Info<< "\ntable1" << table1 << nl
        << "\ntable2" << table2 << nl;

    label nErased = table1.erase(table2);

    Info<< "\nerase table2 keys from table1 (removed "
        << nErased << " elements)" << nl
        << "\ntable1" << table1 << nl
        << "\ntable2" << table2 << nl;


    Info<< "\ntable3" << table3
        << "\nclearStorage table3 ... ";
    table3.clearStorage();
    Info<< table3 << nl;

168
169
170
171
172
173
174
175
176
177
    table1 =
    {
        {"aca", 3.0},
        {"aaw", 6.0},
        {"acr", 8.0},
        {"aec", 10.0}
    };

    Info<< "\ntable1" << table1 << nl;

Henry's avatar
Henry committed
178
179
180
181
182
183
184
    Info<< "\nDone\n";

    return 0;
}


// ************************************************************************* //