List.H 11 KB
Newer Older
1
2
3
4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
5
    \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
6
     \\/     M anipulation  | Copyright (C) 2017-2018 OpenCFD Ltd.
7
8
9
10
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

11
12
13
14
    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.
15
16
17
18
19
20
21

    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
22
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
23
24
25
26
27
28
29

Class
    Foam::List

Description
    A 1D array of objects of type \<T\>, where the size of the vector
    is known and used for subscript bounds checking, etc.
30

31
32
33
34
35
36
37
38
39
40
41
42
43
44
    Storage is allocated on free-store during construction.

SourceFiles
    List.C
    ListI.H
    ListIO.C

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

#ifndef List_H
#define List_H

#include "UList.H"
#include "autoPtr.H"
45
#include "one.H"
46
47
#include "SLListFwd.H"

48
#include <initializer_list>
49
50
51
52
53
54

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

namespace Foam
{

55
// Forward declarations
56

57
58
59
60
class Istream;
class Ostream;

template<class T> class List;
Mark Olesen's avatar
Mark Olesen committed
61
template<class T, unsigned Size> class FixedList;
62
template<class T, int SizeMin> class DynamicList;
63

64
template<class T> class PtrList;
65
template<class T> class SortableList;
66
template<class T> class IndirectList;
mattijs's avatar
mattijs committed
67
template<class T> class UIndirectList;
68
69
template<class T> class BiIndirectList;

70
template<class T> Istream& operator>>(Istream& is, List<T>& list);
71

72
// Common list types
73
typedef List<char> charList;
74
75
76
77
typedef List<label> labelList;

//- A zero-sized list of labels
extern const labelList emptyLabelList;
78

79

80
81
82
83
84
85
86
87
88
/*---------------------------------------------------------------------------*\
                           Class List Declaration
\*---------------------------------------------------------------------------*/

template<class T>
class List
:
    public UList<T>
{
89
    // Private Member Functions
90

91
        //- Allocate list storage
92
        inline void doAlloc();
93
94

        //- Reallocate list storage to the given size
95
        inline void reAlloc(const label len);
96

97
        //- Copy list of given type.
98
        template<class List2>
99
100
101
102
        inline void copyList(const List2& list);

        //- Change allocation size of List. Backend for resize/setSize.
        void doResize(const label newSize);
103

104
105
        //- Construct given begin/end iterators and number of elements
        //  Since the size is provided, the end iterator is actually ignored.
106
        template<class InputIterator>
107
108
109
110
        inline List
        (
            InputIterator begIter,
            InputIterator endIter,
111
            const label len
112
        );
113

114
115
116

public:

117
118
119
120
121
    // Static Member Functions

        //- Return a null List
        inline static const List<T>& null();

122

123
124
    // Constructors

125
        //- Null constructor
126
        inline constexpr List() noexcept;
127

128
        //- Construct with given size
129
        explicit List(const label len);
130

131
        //- Construct with given size and value for all elements
132
        List(const label len, const T& val);
133

134
        //- Construct with given size initializing all elements to zero
135
        List(const label len, const zero);
136

137
138
139
140
141
142
143
144
145
        //- Construct with length=1, copying the value as the only content
        List(const one, const T& val);

        //- Construct with length=1, moving the value as the only content
        List(const one, T&& val);

        //- Construct with length=1, initializing content to zero
        List(const one, const zero);

146
        //- Copy construct from list
147
        List(const List<T>& a);
148

149
150
        //- Copy construct contents from list
        explicit List(const UList<T>& a);
151

152
        //- Construct as copy or re-use as specified
153
        List(List<T>& a, bool reuse);
154

155
        //- Construct as subset
156
        List(const UList<T>& list, const labelUList& mapAddressing);
mattijs's avatar
mattijs committed
157

158
        //- Construct given begin/end iterators.
159
        //  Uses std::distance for the size.
160
        template<class InputIterator>
161
        List(InputIterator begIter, InputIterator endIter);
162

163
        //- Construct as copy of FixedList<T, Size>
Mark Olesen's avatar
Mark Olesen committed
164
        template<unsigned Size>
165
        explicit List(const FixedList<T, Size>& list);
166
167

        //- Construct as copy of PtrList<T>
168
        explicit List(const PtrList<T>& list);
169
170

        //- Construct as copy of SLList<T>
171
        explicit List(const SLList<T>& list);
172

mattijs's avatar
mattijs committed
173
        //- Construct as copy of UIndirectList<T>
174
        explicit List(const UIndirectList<T>& list);
mattijs's avatar
mattijs committed
175

176
        //- Construct as copy of BiIndirectList<T>
177
        explicit List(const BiIndirectList<T>& list);
178

179
        //- Construct from an initializer list
180
        List(std::initializer_list<T> list);
181

182
        //- Move construct from List
183
        List(List<T>&& list);
184
185
186

        //- Move construct from DynamicList
        template<int SizeMin>
187
        List(DynamicList<T, SizeMin>&& list);
188
189

        //- Move construct from SortableList
190
        List(SortableList<T>&& list);
191

192
        //- Move construct from SLList
193
        List(SLList<T>&& list);
194

195
        //- Construct from Istream
196
        List(Istream& is);
197
198

        //- Clone
199
        inline autoPtr<List<T>> clone() const;
200
201


Andrew Heather's avatar
Andrew Heather committed
202
203
    //- Destructor
    ~List();
204
205
206
207
208
209
210
211


    // Related types

        //- Declare type of subList
        typedef SubList<T> subList;


212
    // Member Functions
213
214
215

        // Edit

216
217
            //- Adjust allocated size of list.
            //  The boolList version fills new memory with false.
218
            inline void resize(const label newSize);
219

220
221
            //- Adjust allocated size of list and set val for new elements
            void resize(const label newSize, const T& val);
222

223
224
            //- Alias for resize(const label)
            inline void setSize(const label newSize);
225

226
227
            //- Alias for resize(const label, const T&)
            inline void setSize(const label newSize, const T& val);
228

229
            //- Clear the list, i.e. set size to zero
230
            inline void clear();
231

232
            //- Append an element at the end of the list
233
            inline void append(const T& val);
234

235
236
237
238
            //- Move append an element at the end of the list
            inline void append(T&& val);

            //- Append a List to the end of this list
239
            inline void append(const UList<T>& list);
Mark Olesen's avatar
Mark Olesen committed
240
241

            //- Append a UIndirectList at the end of this list
242
            inline void append(const UIndirectList<T>& list);
Mark Olesen's avatar
Mark Olesen committed
243

Mark Olesen's avatar
Mark Olesen committed
244
            //- Transfer the contents of the argument List into this list
245
            //- and annul the argument list
246
            void transfer(List<T>& list);
247

Mark Olesen's avatar
Mark Olesen committed
248
            //- Transfer the contents of the argument List into this list
249
            //- and annul the argument list
250
            template<int SizeMin>
251
            void transfer(DynamicList<T, SizeMin>& list);
mattijs's avatar
mattijs committed
252

Mark Olesen's avatar
Mark Olesen committed
253
            //- Transfer the contents of the argument List into this list
254
            //- and annul the argument list
255
            void transfer(SortableList<T>& list);
256

257
258
            //- Return subscript-checked element of UList and resizing the list
            //- if required.
259
            inline T& newElmt(const label i);
260

261

262
    // Member Operators
263

264
        //- Assignment to UList operator. Takes linear time
265
        void operator=(const UList<T>& a);
266

267
        //- Assignment operator. Takes linear time
268
        void operator=(const List<T>& list);
269

270
        //- Assignment to SLList operator. Takes linear time
271
        void operator=(const SLList<T>& list);
272

273
        //- Assignment to UIndirectList operator. Takes linear time
274
        void operator=(const UIndirectList<T>& list);
mattijs's avatar
mattijs committed
275

276
        //- Assignment to BiIndirectList operator. Takes linear time
277
        void operator=(const BiIndirectList<T>& list);
278

279
        //- Assignment to an initializer list
280
        void operator=(std::initializer_list<T> list);
281

282
        //- Assignment of all entries to the given value
283
        inline void operator=(const T& val);
284

285
286
287
        //- Assignment of all entries to zero
        inline void operator=(const zero);

288
        //- Move assignment. Takes constant time
289
        void operator=(List<T>&& list);
290
291
292

        //- Move assignment. Takes constant time.
        template<int SizeMin>
293
        void operator=(DynamicList<T, SizeMin>&& list);
294
295

        //- Move assignment. Takes constant time.
296
        void operator=(SortableList<T>&& list);
297

298
        //- Move assignment. Takes constant time
299
        void operator=(SLList<T>&& list);
300

301

302
    // Istream Operator
303

304
        //- Read List from Istream, discarding contents of existing List
Henry Weller's avatar
Henry Weller committed
305
        friend Istream& operator>> <T>
306
307
        (
            Istream& is,
308
            List<T>& list
309
        );
310

311
312
313
314
315

    // Housekeeping

        //- No shallowCopy permitted
        void shallowCopy(const UList<T>&) = delete;
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343


    // Special Methods

        //- A bitSet::set() method for a list of bool
        //  Increases size when setting an out-of-bounds value.
        //
        //  \return True if value changed.
        template<class TypeT = T>
        typename std::enable_if<std::is_same<TypeT, bool>::value, bool>::type
        inline set(const label i, bool val = true)
        {
            if (i < 0)
            {
                return false;               // Out-of-bounds: ignore
            }
            else if (i >= this->size())
            {
                if (!val)                   // Unset out-of-bounds: ignore
                {
                    return false;
                }
                this->resize(i+1, false);   // Adjust size for assign, fill 0
            }

            (*this)[i] = val;
            return true;
        }
344
345
346
};


347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// * * * * * * * * * * * * * * Global Functions  * * * * * * * * * * * * * * //

//- Hashing for List data, which uses Hasher for contiguous data and
//- element-wise incrementally hashing otherwise.
template<class T>
struct Hash<List<T>>
{
    inline unsigned operator()(const UList<T>& obj, unsigned seed=0) const
    {
        if (contiguous<T>())
        {
            return Hasher(obj.cdata(), obj.size()*sizeof(T), seed);
        }

        for (const T& val : obj)
        {
            seed = Hash<T>()(val, seed);
        }
        return seed;
    }
};


370
371
372
373
374
375
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

} // End namespace Foam

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

376
#include "ListI.H"
377
378
379
380

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

#ifdef NoRepository
381
    #include "List.C"
382
383
384
385
386
387
388
#endif

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

#endif

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