FixedList.H 15.3 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
30
31
32
33
34
35
36
37
38
39

Class
    Foam::FixedList

Description
    A 1D vector of objects of type \<T\> with a fixed size \<Size\>.

SourceFiles
    FixedList.C
    FixedListI.H
    FixedListIO.C

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

#ifndef FixedList_H
#define FixedList_H

Mark Olesen's avatar
Mark Olesen committed
40
#include "bool.H"
41
#include "label.H"
Mark Olesen's avatar
Mark Olesen committed
42
#include "uLabel.H"
43
#include "zero.H"
44
#include "contiguous.H"
45
#include "autoPtr.H"
46
#include "Swap.H"
47
#include "HashFwd.H"
48
#include "SLListFwd.H"
49

50
#include <initializer_list>
51
#include <iterator>
52
53
#include <type_traits>
#include <limits>
54
55
56
57
58
59

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

namespace Foam
{

60
// Forward declarations
61

Mark Olesen's avatar
Mark Olesen committed
62
template<class T, unsigned Size> class FixedList;
63

Mark Olesen's avatar
Mark Olesen committed
64
template<class T, unsigned Size>
65
66
Istream& operator>>(Istream&, FixedList<T, Size>&);

Mark Olesen's avatar
Mark Olesen committed
67
template<class T, unsigned Size>
68
69
70
71
72
73
74
75
76
Ostream& operator<<(Ostream&, const FixedList<T, Size>&);

template<class T> class UList;


/*---------------------------------------------------------------------------*\
                           Class FixedList Declaration
\*---------------------------------------------------------------------------*/

Mark Olesen's avatar
Mark Olesen committed
77
template<class T, unsigned Size>
78
79
class FixedList
{
80
81
    static_assert
    (
82
        Size && Size <= std::numeric_limits<int>::max(),
83
84
        "Size must be positive (non-zero) and also fit as a signed value"
    );
Mark Olesen's avatar
Mark Olesen committed
85

86
87
88
89
90
91
    // Private data

        //- Vector of values of type T of size Size.
        T v_[Size];


92
93
94
95
protected:

    // Protected Member Functions

96
97
98
99
        //- True if there are two or more entries and all entries have
        //  identical values.
        inline bool uniform() const;

100
101
102
        //- Write the FixedList with its compound type
        void writeEntry(Ostream& os) const;

103
104
public:

105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
    // STL type definitions

        //- The value type the FixedList contains
        typedef T value_type;

        //- The pointer type for non-const access to value_type items
        typedef T* pointer;

        //- The pointer type for const access to value_type items
        typedef const T* const_pointer;

        //- The type used for storing into value_type objects
        typedef T& reference;

        //- The type used for reading from constant value_type objects.
        typedef const T& const_reference;

        //- Random access iterator for traversing FixedList
        typedef T* iterator;

        //- Random access iterator for traversing FixedList
        typedef const T* const_iterator;

        //- The type to represent the size of a FixedList
        typedef label size_type;

        //- The difference between iterator objects
        typedef label difference_type;

        //- Reverse iterator (non-const access)
        typedef std::reverse_iterator<iterator> reverse_iterator;

        //- Reverse iterator (const access)
        typedef std::reverse_iterator<const_iterator> const_reverse_iterator;


141
142
143
144
145
    // Static Member Functions

        //- Return a null FixedList
        inline static const FixedList<T, Size>& null();

146
147
148

    // Constructors

149
        //- Null constructor
150
        inline FixedList() = default;
151

152
        //- Construct and initialize all entries to given value
153
        inline explicit FixedList(const T& val);
154

155
        //- Construct and initialize all entries to zero
156
        inline explicit FixedList(const zero);
157

158
        //- Copy construct from C-array
159
        inline explicit FixedList(const T list[Size]);
160

161
        //- Copy constructor
162
        inline FixedList(const FixedList<T, Size>& list);
163

164
165
        //- Move construct by using move assignment for the individual
        //- list elements
166
        inline FixedList(FixedList<T, Size>&& list);
167

168
169
        //- Construct given begin/end iterators
        //  Uses std::distance when verifying the size.
170
        template<class InputIterator>
171
        inline FixedList(InputIterator begIter, InputIterator endIter);
172

173
        //- Construct from an initializer list
174
        inline FixedList(std::initializer_list<T> list);
175

176
        //- Construct from UList
177
        inline explicit FixedList(const UList<T>& list);
178

179
        //- Construct from SLList
180
        inline explicit FixedList(const SLList<T>& list);
181

182
        //- Construct from Istream
Mark Olesen's avatar
Mark Olesen committed
183
        FixedList(Istream& is);
184
185

        //- Clone
186
        inline autoPtr<FixedList<T, Size>> clone() const;
187
188


189
    // Member Functions
190
191
192

        // Access

193
            //- Return the forward circular index, i.e. next index
194
            //- which returns to the first at the end of the list
195
196
            inline label fcIndex(const label i) const;

197
198
199
200
201
202
203
            //- Return forward circular value (ie, next value in the list)
            inline const T& fcValue(const label i) const;

            //- Return forward circular value (ie, next value in the list)
            inline T& fcValue(const label i);

            //- Return the reverse circular index, i.e. previous index
204
            //- which returns to the last at the beginning of the list
205
206
            inline label rcIndex(const label i) const;

207
208
209
210
211
212
            //- Return reverse circular value (ie, previous value in the list)
            inline const T& rcValue(const label i) const;

            //- Return reverse circular value (ie, previous value in the list)
            inline T& rcValue(const label i);

213

214
215
            //- Return a const pointer to the first data element.
            //  Similar to the STL front() method and the string::data() method
216
            //  This can be used (with caution) when interfacing with C code
Mark Olesen's avatar
Mark Olesen committed
217
218
            inline const T* cdata() const;

219
220
            //- Return a pointer to the first data element.
            //  Similar to the STL front() method and the string::data() method
221
            //  This can be used (with caution) when interfacing with C code
Mark Olesen's avatar
Mark Olesen committed
222
223
            inline T* data();

224
            //- Return the first element of the list
225
226
            inline T& first();

227
            //- Return first element of the list
228
229
            inline const T& first() const;

230
            //- Return the last element of the list
231
232
            inline T& last();

233
            //- Return the last element of the list
234
235
            inline const T& last() const;

Mark Olesen's avatar
Mark Olesen committed
236

237
238
        // Check

239
            //- Check start is within valid range [0,size)
240
241
            inline void checkStart(const label start) const;

242
            //- Check size is identical to Size template parameter
243
244
            inline void checkSize(const label size) const;

245
            //- Check index is within valid range [0,size)
246
247
248
            inline void checkIndex(const label i) const;


249
    // Search
250
251
252
253
254
255
256
257
258
259

        //- Find index of the first occurence of the value.
        //  Linear search.
        //  \return -1 if not found.
        label find(const T& val, const label start=0) const;

        //- True if the value if found in the list. Linear search.
        inline bool found(const T& val, const label start=0) const;


260
    // Edit
261

262
263
        //- Dummy resize function
        //  needed to make FixedList consistent with List
264
        inline void resize(const label n);
265
266
267

        //- Dummy setSize function
        //  needed to make FixedList consistent with List
268
        inline void setSize(const label n);
269
270
271
272
273
274
275

        //- Move element to the first position.
        void moveFirst(const label i);

        //- Move element to the last position.
        void moveLast(const label i);

276
        //- Swap element with the first element.
277
        void swapFirst(const label i);
278

279
        //- Swap element with the last element.
280
        void swapLast(const label i);
281

282
283
        //- Transfer by swapping using a move assignment for the content
        //- of the individual list elements
284
        inline void transfer(FixedList<T, Size>& list);
285
286
287
288


    // Member operators

289
        //- Return element of FixedList
Mark Olesen's avatar
Mark Olesen committed
290
        inline T& operator[](const label i);
291

292
        //- Return element of constant FixedList
Mark Olesen's avatar
Mark Olesen committed
293
        inline const T& operator[](const label i) const;
294

295
        //- Assignment to array operator. Takes linear time
296
        inline void operator=(const T list[Size]);
297

298
        //- Assignment to UList operator. Takes linear time
299
        inline void operator=(const UList<T>& list);
300

301
        //- Assignment to SLList operator. Takes linear time
302
        inline void operator=(const SLList<T>& list);
303

304
        //- Assignment to an initializer list. Takes linear time
305
        inline void operator=(std::initializer_list<T> list);
306

307
        //- Assignment of all entries to the given value
308
309
310
        inline void operator=(const T& val);

        //- Copy assignment
311
        inline void operator=(const FixedList<T, Size>& list);
312
313

        //- Move assignment
314
        inline void operator=(FixedList<T, Size>&& list);
315
316


317
    // Random access iterator (non-const)
318

319
        //- Return an iterator to begin traversing the FixedList
320
321
        inline iterator begin();

322
        //- Return an iterator to end traversing the FixedList
323
324
325
        inline iterator end();


326
    // Random access iterator (const)
327

328
        //- Return const_iterator to begin traversing the constant FixedList
329
330
        inline const_iterator cbegin() const;

331
        //- Return const_iterator to end traversing the constant FixedList
332
333
        inline const_iterator cend() const;

334
        //- Return const_iterator to begin traversing the constant FixedList
335
336
        inline const_iterator begin() const;

337
        //- Return const_iterator to end traversing the constant FixedList
338
339
340
        inline const_iterator end() const;


341
    // Reverse iterator (non-const)
342

343
        //- Return reverse_iterator to begin reverse traversing the FixedList
344
345
        inline reverse_iterator rbegin();

346
        //- Return reverse_iterator to end reverse traversing the FixedList
347
348
349
        inline reverse_iterator rend();


350
    // Reverse iterator (const)
351

352
        //- Return const_reverse_iterator to begin reverse traversing FixedList
353
354
        inline const_reverse_iterator crbegin() const;

355
        //- Return const_reverse_iterator to end reverse traversing FixedList
356
357
        inline const_reverse_iterator crend() const;

358
        //- Return const_reverse_iterator to begin reverse traversing FixedList
359
360
        inline const_reverse_iterator rbegin() const;

361
        //- Return const_reverse_iterator to end reverse traversing FixedList
362
363
364
365
366
        inline const_reverse_iterator rend() const;


    // STL member functions

367
        //- Return the number of elements in the FixedList
368
369
        inline label size() const;

370
        //- Return size of the largest possible FixedList
371
372
        inline label max_size() const;

373
        //- Always false since zero-sized FixedList is compile-time disabled.
374
375
        inline bool empty() const;

376
        //- Swap lists by swapping the content of the individual list elements
377
        inline void swap(FixedList<T, Size>& list);
378
379
380
381
382


    // STL member operators

        //- Equality operation on FixedLists of the same type.
383
        //  Returns true when the FixedLists are element-wise equal
384
385
        //  (using FixedList::value_type::operator==). Takes linear time
        bool operator==(const FixedList<T, Size>& list) const;
386

387
        //- The opposite of the equality operation. Takes linear time
388
        bool operator!=(const FixedList<T, Size>& list) const;
389

390
        //- Compare two FixedLists lexicographically. Takes linear time
391
        bool operator<(const FixedList<T, Size>& list) const;
392

393
        //- Compare two FixedLists lexicographically. Takes linear time
394
        bool operator>(const FixedList<T, Size>& list) const;
395

396
        //- Return true if !(a > b). Takes linear time
397
        bool operator<=(const FixedList<T, Size>& list) const;
398

399
        //- Return true if !(a < b). Takes linear time
400
        bool operator>=(const FixedList<T, Size>& list) const;
401
402


403
404
405
406
407
    // Writing

        //- Write the List as a dictionary entry with keyword
        void writeEntry(const word& keyword, Ostream& os) const;

408
        //- Write the List, with line-breaks in ASCII if the list length
409
410
        //- exceeds shortListLen.
        //  Using '0' suppresses line-breaks entirely.
411
        Ostream& writeList(Ostream& os, const label shortListLen=0) const;
412

Mark Olesen's avatar
Mark Olesen committed
413

414
415
    // IOstream operators

416
        //- Read from Istream, discarding contents of existing List
Henry Weller's avatar
Henry Weller committed
417
        friend Istream& operator>> <T, Size>
Mark Olesen's avatar
Mark Olesen committed
418
419
        (
            Istream& is,
420
            FixedList<T, Size>& list
Mark Olesen's avatar
Mark Olesen committed
421
        );
422

423
        //- Write to Ostream, as per writeList() with shortListLen=10
Henry Weller's avatar
Henry Weller committed
424
        friend Ostream& operator<< <T, Size>
425
        (
Mark Olesen's avatar
Mark Olesen committed
426
            Ostream& os,
427
            const FixedList<T, Size>& list
428
        );
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456


    // Hashing

        //- Hashing function class for FixedList
        //  Normally use the global Hash specialization, but can also use
        //  this one for inheritance in sub-classes
        template<class HashT=Foam::Hash<T>>
        struct Hash
        {
            inline unsigned operator()
            (
                const FixedList<T, Size>& obj,
                unsigned seed=0
            ) const
            {
                if (contiguous<T>())
                {
                    return Hasher(obj.cdata(), Size*sizeof(T), seed);
                }

                for (const T& val : obj)
                {
                    seed = HashT()(val, seed);
                }
                return seed;
            }
        };
457
458
459
};


460
// * * * * * * * * * * * * * * Global Functions  * * * * * * * * * * * * * * //
461

462
463
//- Swap FixedList contents - see FixedList::swap().
//  Internally this actually swaps the individual list elements
464
template<class T, unsigned Size>
465
inline void Swap(FixedList<T,Size>& lhs, FixedList<T,Size>& rhs);
466
467


468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
//- Hashing for FixedList data, which uses Hasher for contiguous data and
//- element-wise incrementally hashing otherwise.
template<class T, unsigned N>
struct Hash<FixedList<T, N>>
{
    inline unsigned operator()
    (
        const FixedList<T, N>& obj,
        unsigned seed=0
    ) const
    {
        if (contiguous<T>())
        {
            return Hasher(obj.cdata(), N*sizeof(T), seed);
        }

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


493
494
495
496
497
498
499
500
501
502
503
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

} // End namespace Foam

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

#include "FixedListI.H"

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

#ifdef NoRepository
504
    #include "FixedList.C"
505
506
507
508
509
510
511
#endif

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

#endif

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