DynamicListTest.C 5.59 KB
Newer Older
1
2
3
4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
Mark Olesen's avatar
Mark Olesen committed
5
    \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
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
38
39
40
41
42
43
44
45
     \\/     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 2 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, write to the Free Software Foundation,
    Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

Description

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

#include "DynamicList.H"
#include "IOstreams.H"

using namespace Foam;

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

int main(int argc, char *argv[])
{
    List<DynamicList<label, 1, 0> > ldl(2);

    ldl[0](0) = 0;
    ldl[0](2) = 2;
    ldl[0](3) = 3;
    ldl[0](1) = 1;

Mark Olesen's avatar
Mark Olesen committed
46
47
48
49
50
    ldl[0].setCapacity(5);    // increase allocated size
    ldl[1].setCapacity(10);   // increase allocated size
    ldl[0].reserve(15);       // should increase allocated size
    ldl[1].reserve(5);        // should not decrease allocated size
    ldl[1](3) = 2;            // allocates space and sets value
51
52
53

    // this works without a segfault, but doesn't change the list size
    ldl[0][4] = 4;
54
55
56

    ldl[1] = 3;

57
58
59
    Info<< "<ldl>" << ldl << "</ldl>" << nl << "sizes: ";
    forAll(ldl, i)
    {
Mark Olesen's avatar
Mark Olesen committed
60
        Info<< " " << ldl[i].size() << "/" << ldl[i].capacity();
Mark Olesen's avatar
Mark Olesen committed
61
    }
62
    Info<< endl;
63
64

    List<List<label> > ll(2);
65
    ll[0].transfer(ldl[0]);
66
67
    ll[1].transfer(ldl[1].shrink());

68
69
70
    Info<< "<ldl>" << ldl << "</ldl>" << nl << "sizes: ";
    forAll(ldl, i)
    {
Mark Olesen's avatar
Mark Olesen committed
71
        Info<< " " << ldl[i].size() << "/" << ldl[i].capacity();
Mark Olesen's avatar
Mark Olesen committed
72
    }
73
    Info<< endl;
74

75
    Info<< "<ll>" << ll << "</ll>" << nl << endl;
76

Mark Olesen's avatar
Mark Olesen committed
77
78
79
80
81
82
83
84
85

    // test the transfer between DynamicLists
    DynamicList<label, 1, 0> dlA;
    DynamicList<label, 1, 0> dlB;

    for (label i = 0; i < 5; i++)
    {
        dlA.append(i);
    }
Mark Olesen's avatar
Mark Olesen committed
86
    dlA.setCapacity(10);
Mark Olesen's avatar
Mark Olesen committed
87
88

    Info<< "<dlA>" << dlA << "</dlA>" << nl << "sizes: "
Mark Olesen's avatar
Mark Olesen committed
89
        << " " << dlA.size() << "/" << dlA.capacity() << endl;
Mark Olesen's avatar
Mark Olesen committed
90
91
92
93
94
95
96
97
98

    dlB.transfer(dlA);

    // provokes memory error if previous transfer did not maintain
    // the correct allocated space
    dlB[6] = 6;

    Info<< "Transferred to dlB" << endl;
    Info<< "<dlA>" << dlA << "</dlA>" << nl << "sizes: "
Mark Olesen's avatar
Mark Olesen committed
99
        << " " << dlA.size() << "/" << dlA.capacity() << endl;
Mark Olesen's avatar
Mark Olesen committed
100
    Info<< "<dlB>" << dlB << "</dlB>" << nl << "sizes: "
Mark Olesen's avatar
Mark Olesen committed
101
        << " " << dlB.size() << "/" << dlB.capacity() << endl;
Mark Olesen's avatar
Mark Olesen committed
102

103
104
105
106
107
108
109
    // try with a normal list:
    List<label> lstA;
    lstA.transfer(dlB);
    Info<< "Transferred to normal list" << endl;
    Info<< "<lstA>" << lstA << "</lstA>" << nl << "sizes: "
        << " " << lstA.size() << endl;
    Info<< "<dlB>" << dlB << "</dlB>" << nl << "sizes: "
Mark Olesen's avatar
Mark Olesen committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
        << " " << dlB.size() << "/" << dlB.capacity() << endl;

    // Copy back and append a few time
    for (label i=0; i < 3; i++)
    {
        dlB.append(lstA);
    }

    Info<< "appended list a few times" << endl;
    Info<< "<dlB>" << dlB << "</dlB>" << nl << "sizes: "
        << " " << dlB.size() << "/" << dlB.capacity() << endl;

    // assign the list (should maintain allocated space)
    dlB = lstA;
    Info<< "assigned list" << endl;
    Info<< "<dlB>" << dlB << "</dlB>" << nl << "sizes: "
        << " " << dlB.size() << "/" << dlB.capacity() << endl;


Mark Olesen's avatar
Mark Olesen committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
    // Copy back and append a few time
    for (label i=0; i < 3; i++)
    {
        dlB.append(lstA);
    }


    // check allocation granularity
    DynamicList<label, 6, 0> dlC;

    Info<< "<dlC>" << dlC << "</dlC>" << nl << "sizes: "
        << " " << dlC.size() << "/" << dlC.capacity() << endl;

    dlC.reserve(dlB.size());
    dlC = dlB;

    Info<< "<dlC>" << dlC << "</dlC>" << nl << "sizes: "
        << " " << dlC.size() << "/" << dlC.capacity() << endl;
147

Mark Olesen's avatar
Mark Olesen committed
148
    List<label> lstB(dlC.xfer());
149

Mark Olesen's avatar
Mark Olesen committed
150
    Info<< "Transferred to normal list via the xfer() method" << endl;
151
152
153
154
155
    Info<< "<lstB>" << lstB << "</lstB>" << nl << "sizes: "
        << " " << lstB.size() << endl;
    Info<< "<dlC>" << dlC << "</dlC>" << nl << "sizes: "
        << " " << dlC.size() << "/" << dlC.capacity() << endl;

Mark Olesen's avatar
Mark Olesen committed
156
157
158
159
160
161
162
    DynamicList<label> dlD(lstB.xfer());

    Info<< "Transfer construct from normal list" << endl;
    Info<< "<lstB>" << lstB << "</lstB>" << nl << "sizes: "
        << " " << lstB.size() << endl;
    Info<< "<dlD>" << dlD << "</dlD>" << nl << "sizes: "
        << " " << dlD.size() << "/" << dlD.capacity() << endl;
163
164
165
166
167
168
169
170
171
172
173
174
175
    
    DynamicList<label,10> dlE1(10);
    DynamicList<label> dlE2(dlE1);

    Info<< "<dlE1>" << dlE1 << "</dlE1>" << nl << "sizes: "
        << " " << dlE1.size() << "/" << dlE1.capacity() << endl;
    Info<< "<dlE2>" << dlE2 << "</dlE2>" << nl << "sizes: "
        << " " << dlE2.size() << "/" << dlE2.capacity() << endl;

    dlE2.append(100);
    Info<< "<dlE2>" << dlE2 << "</dlE2>" << endl;
    
    Info<< "\nEnd\n";
Mark Olesen's avatar
Mark Olesen committed
176

177
178
179
180
181
    return 0;
}


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