DynamicListI.H 6.89 KB
Newer Older
1
2
3
4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
5
    \\  /    A nd           | Copyright (C) 1991-2008 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
     \\/     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

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

// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

//- Construct null
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList()
:
    List<T>(SizeInc),
mattijs's avatar
mattijs committed
34
35
36
37
    allocSize_(SizeInc)
{
    List<T>::size() = 0;
}
38
39
40
41
42
43
44
45
46
47


//- Construct given size
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList
(
    const label s
)
:
    List<T>(s),
mattijs's avatar
mattijs committed
48
49
50
51
    allocSize_(s)
{
    List<T>::size() = 0;
}
52
53
54
55
56
57
58
59
60
61


//- Construct given size
template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::DynamicList
(
    const UList<T>& s
)
:
    List<T>(s),
mattijs's avatar
mattijs committed
62
    allocSize_(s.size())
63
64
65
66
67
68
{}


// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //

template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
mattijs's avatar
mattijs committed
69
inline Foam::label Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::allocSize()
70
71
const
{
mattijs's avatar
mattijs committed
72
    return allocSize_;
73
74
75
76
77
78
79
80
81
}


template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::setSize
(
    const label s
)
{
mattijs's avatar
mattijs committed
82
    if (s < List<T>::size())
83
    {
mattijs's avatar
mattijs committed
84
        List<T>::size() = s;
85
86
87
    }
    else
    {
mattijs's avatar
mattijs committed
88
89
        allocSize_ = s;
        List<T>::setSize(allocSize_);
90
91
92
93
94
95
96
97
98
99
100
    }
}


template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::setSize
(
    const label s,
    const T& t
)
{
mattijs's avatar
mattijs committed
101
    if (s < List<T>::size())
102
    {
mattijs's avatar
mattijs committed
103
        List<T>::size() = s;
104
105
106
    }
    else
    {
mattijs's avatar
mattijs committed
107
108
        allocSize_ = s;
        List<T>::setSize(allocSize_, t);
109
110
111
112
113
114
115
    }
}


template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::clear()
{
mattijs's avatar
mattijs committed
116
117
118
119
120
121
122
123
124
125
    List<T>::size() = 0;
}


template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::clearStorage()
{
    List<T>::size() = allocSize_;    // make List<T> consistent
    List<T>::clear();
    allocSize_ = 0;
126
127
128
129
130
131
132
}


template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>&
Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::shrink()
{
mattijs's avatar
mattijs committed
133
134
    allocSize_ = List<T>::size();
    List<T>::setSize(allocSize_);
135
136
137
138
139
140
141
142
    return *this;
}


template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline void
Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::transfer(List<T>& l)
{
mattijs's avatar
mattijs committed
143
    if (l.size() < List<T>::size())
144
145
146
147
148
149
    {
        FatalErrorIn
        (
            "void DynamicList<T, SizeInc, SizeMult"
            ", SizeDiv>::transfer(List<T>&)"
        )   << "Cannot replace the underlying storage of this DynamicList"
mattijs's avatar
mattijs committed
150
            << " of which " << List<T>::size() << " elements are used" << nl
151
152
153
154
            << "with a List of size " << l.size() << abort(FatalError);
    }
    else
    {
mattijs's avatar
mattijs committed
155
        allocSize_ = l.size();
156
157
158
159
160
161
162
163
164
165
166
167
        List<T>::transfer(l);   // take over storage
    }
}


template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline void
Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::transfer
(
    DynamicList<T, SizeInc, SizeMult, SizeDiv>& l
)
{
mattijs's avatar
mattijs committed
168
    allocSize_ = l.allocSize();
169
    List<T>::transfer(l);       // take over storage
mattijs's avatar
mattijs committed
170
    l.allocSize_ = 0;
171
172
173
174
}


template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
mattijs's avatar
mattijs committed
175
inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::append(const T& e)
176
{
mattijs's avatar
mattijs committed
177
178
    // Work on copy free index since gets overwritten by setSize
    label nextFree = List<T>::size();
179

mattijs's avatar
mattijs committed
180
181
182
    nextFree++;

    if (nextFree > allocSize_)
183
    {
mattijs's avatar
mattijs committed
184
        allocSize_ = max
185
        (
mattijs's avatar
mattijs committed
186
187
            nextFree,
            label(SizeMult*allocSize_/SizeDiv + SizeInc)
188
        );
mattijs's avatar
mattijs committed
189
        List<T>::setSize(allocSize_);
190
191
    }

mattijs's avatar
mattijs committed
192
193
194
    this->operator[](nextFree - 1) = e;

    List<T>::size() = nextFree;
195
196
197
198
199
200
}


template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline T Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::remove()
{
mattijs's avatar
mattijs committed
201
    if (List<T>::size() == 0)
202
203
204
205
206
207
208
    {
        FatalErrorIn
        (
            "void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::remove()"
        )   << "List is empty" << abort(FatalError);
    }

mattijs's avatar
mattijs committed
209
    return List<T>::operator[](--List<T>::size());
210
211
212
213
214
215
216
217
}


// * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //

template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline T& Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::operator()
(
218
    const label i
219
220
)
{
mattijs's avatar
mattijs committed
221
    label nextFree = List<T>::size();
222

mattijs's avatar
mattijs committed
223
224
225
    nextFree = max(nextFree, i + 1);

    if (nextFree > allocSize_)
226
    {
Mark Olesen's avatar
Mark Olesen committed
227
        allocSize_ = max
228
        (
mattijs's avatar
mattijs committed
229
230
            nextFree,
            label(SizeMult*allocSize_/SizeDiv + SizeInc)
231
        );
mattijs's avatar
mattijs committed
232
        List<T>::setSize(allocSize_);
233
234
    }

mattijs's avatar
mattijs committed
235
236
    List<T>::size() = nextFree;

237
238
239
240
241
242
243
244
245
246
247
    return this->operator[](i);
}


template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::operator=
(
    const T& t
)
{
    List<T>::operator=(t);
mattijs's avatar
mattijs committed
248
    allocSize_ = List<T>::size();
249
250
251
252
253
254
255
256
257
258
}


template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::operator=
(
    const List<T>& l
)
{
    List<T>::operator=(l);
mattijs's avatar
mattijs committed
259
260
261
262
263
264
265
266
267
268
269
270
    allocSize_ = List<T>::size();
}


template<class T, unsigned SizeInc, unsigned SizeMult, unsigned SizeDiv>
inline void Foam::DynamicList<T, SizeInc, SizeMult, SizeDiv>::operator=
(
    const DynamicList<T, SizeInc, SizeMult, SizeDiv>& l
)
{
    List<T>::operator=(l);
    allocSize_ = l.allocSize();
271
272
273
274
}


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