FixedList.H 12.1 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 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
44
#include "Hash.H"
#include "autoPtr.H"
45
#include <type_traits>
46
#include <initializer_list>
47
48
49
50
51
52
53
54

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

namespace Foam
{

// Forward declaration of friend functions and operators

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

Mark Olesen's avatar
Mark Olesen committed
57
template<class T, unsigned Size>
58
59
Istream& operator>>(Istream&, FixedList<T, Size>&);

Mark Olesen's avatar
Mark Olesen committed
60
template<class T, unsigned Size>
61
62
63
64
Ostream& operator<<(Ostream&, const FixedList<T, Size>&);

template<class T> class UList;

65
66
67
68
class SLListBase;
template<class LListBase, class T> class LList;
template<class T>
using SLList = LList<SLListBase, T>;
69
70
71
72
73

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

Mark Olesen's avatar
Mark Olesen committed
74
template<class T, unsigned Size>
75
76
class FixedList
{
77
78
79
80
81
    static_assert
    (
        Size && Size <= INT_MAX,
        "Size must be positive (non-zero) and also fit as a signed value"
    );
Mark Olesen's avatar
Mark Olesen committed
82

83
84
85
86
87
88
    // Private data

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


89
90
91
92
93
94
95
96
protected:

    // Protected Member Functions

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


97
98
public:

99
100
    //- Hashing function class.
    //  Use Hasher directly for contiguous data. Otherwise hash incrementally.
101
    template<class HashT=Hash<T>>
102
103
104
    class Hash
    {
    public:
Mark Olesen's avatar
Mark Olesen committed
105
106
107
        Hash()
        {}

108
109
110
111
112
        inline unsigned operator()
        (
            const FixedList<T, Size>&,
            unsigned seed = 0
        ) const;
113
114
    };

115

116
117
118
119
120
    // Static Member Functions

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

121
122
123

    // Constructors

124
        //- Null constructor
125
126
        inline FixedList();

127
        //- Construct from value
Mark Olesen's avatar
Mark Olesen committed
128
        explicit inline FixedList(const T& t);
129

130
        //- Construct from C-array
mattijs's avatar
mattijs committed
131
        explicit inline FixedList(const T v[Size]);
132

133
134
135
136
        //- Construct given start and end iterators
        template<class InputIterator>
        inline FixedList(InputIterator first, InputIterator last);

137
        //- Construct from an initializer list
Mark Olesen's avatar
Mark Olesen committed
138
        inline FixedList(std::initializer_list<T> lst);
139

140
        //- Construct from UList
Mark Olesen's avatar
Mark Olesen committed
141
        explicit inline FixedList(const UList<T>& lst);
142

143
        //- Construct from SLList
Mark Olesen's avatar
Mark Olesen committed
144
        explicit inline FixedList(const SLList<T>& lst);
145

146
        //- Copy constructor
Mark Olesen's avatar
Mark Olesen committed
147
        inline FixedList(const FixedList<T, Size>& lst);
148

149
        //- Construct from Istream
Mark Olesen's avatar
Mark Olesen committed
150
        FixedList(Istream& is);
151
152

        //- Clone
153
        inline autoPtr<FixedList<T, Size>> clone() const;
154
155


156
    // Member Functions
157
158
159
160
161
162
163
164

        // Access

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

            //- Return the reverse circular index, i.e. the previous index
Mark Olesen's avatar
Mark Olesen committed
165
            //  which returns to the last at the beginning of the list
166
167
168
            inline label rcIndex(const label i) const;


Mark Olesen's avatar
Mark Olesen committed
169
170
            //- Return a const pointer to the first data element,
            //  similar to the STL front() method and the string::data() method
171
            //  This can be used (with caution) when interfacing with C code
Mark Olesen's avatar
Mark Olesen committed
172
173
174
175
            inline const T* cdata() const;

            //- Return a pointer to the first data element,
            //  similar to the STL front() method and the string::data() method
176
            //  This can be used (with caution) when interfacing with C code
Mark Olesen's avatar
Mark Olesen committed
177
178
            inline T* data();

179
            //- Return the first element of the list
180
181
            inline T& first();

182
            //- Return first element of the list
183
184
            inline const T& first() const;

185
            //- Return the last element of the list
186
187
            inline T& last();

188
            //- Return the last element of the list
189
190
            inline const T& last() const;

Mark Olesen's avatar
Mark Olesen committed
191

192
193
        // Check

194
            //- Check start is within valid range (0 ... size-1)
195
196
            inline void checkStart(const label start) const;

197
            //- Check size is within valid range (0 ... size)
198
199
            inline void checkSize(const label size) const;

200
            //- Check index i is within valid range (0 ... size-1)
201
202
203
204
205
            inline void checkIndex(const label i) const;


        // Edit

206
207
            //- Dummy resize function
            //  needed to make FixedList consistent with List
Mark Olesen's avatar
Mark Olesen committed
208
            inline void resize(const label s);
209

210
211
            //- Dummy setSize function
            //  needed to make FixedList consistent with List
Mark Olesen's avatar
Mark Olesen committed
212
            inline void setSize(const label s);
213

214
215
            //- Copy (not transfer) the argument contents
            //  needed to make FixedList consistent with List
Mark Olesen's avatar
Mark Olesen committed
216
            void transfer(const FixedList<T, Size>& lst);
217
218
219
220


    // Member operators

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

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

227
        //- Assignment to array operator. Takes linear time
Mark Olesen's avatar
Mark Olesen committed
228
        inline void operator=(const T lst[Size]);
229

230
        //- Assignment to UList operator. Takes linear time
Mark Olesen's avatar
Mark Olesen committed
231
        inline void operator=(const UList<T>& lst);
232

233
        //- Assignment to SLList operator. Takes linear time
Mark Olesen's avatar
Mark Olesen committed
234
        inline void operator=(const SLList<T>& lst);
235

236
        //- Assignment to an initializer list. Takes linear time
Mark Olesen's avatar
Mark Olesen committed
237
        inline void operator=(std::initializer_list<T> lst);
238

239
        //- Assignment of all entries to the given value
Mark Olesen's avatar
Mark Olesen committed
240
        inline void operator=(const T& t);
241
242
243
244


    // STL type definitions

245
        //- Type of values the FixedList contains
246
247
248
        typedef T value_type;

        //- Type that can be used for storing into
249
        //  FixedList::value_type objects
250
251
252
253
254
255
256
        typedef T& reference;

        //- Type that can be used for storing into
        //  constant FixedList::value_type objects
        typedef const T& const_reference;

        //- The type that can represent the difference between any two
257
        //  FixedList iterator objects
258
259
        typedef label difference_type;

260
        //- The type that can represent the size of a FixedList
261
262
263
264
265
        typedef label size_type;


    // STL iterator

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

269
        //- Return an iterator to begin traversing the FixedList
270
271
        inline iterator begin();

272
        //- Return an iterator to end traversing the FixedList
273
274
275
276
277
        inline iterator end();


    // STL const_iterator

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

281
        //- Return const_iterator to begin traversing the constant FixedList
282
283
        inline const_iterator cbegin() const;

284
        //- Return const_iterator to end traversing the constant FixedList
285
286
        inline const_iterator cend() const;

287
        //- Return const_iterator to begin traversing the constant FixedList
288
289
        inline const_iterator begin() const;

290
        //- Return const_iterator to end traversing the constant FixedList
291
292
293
294
295
        inline const_iterator end() const;


    // STL reverse_iterator

296
        //- Reverse iterator for reverse traversal of FixedList
297
298
        typedef T* reverse_iterator;

299
        //- Return reverse_iterator to begin reverse traversing the FixedList
300
301
        inline reverse_iterator rbegin();

302
        //- Return reverse_iterator to end reverse traversing the FixedList
303
304
305
306
307
        inline reverse_iterator rend();


    // STL const_reverse_iterator

308
        //- Reverse iterator for reverse traversal of constant FixedList
309
310
        typedef const T* const_reverse_iterator;

311
        //- Return const_reverse_iterator to begin reverse traversing FixedList
312
313
        inline const_reverse_iterator crbegin() const;

314
        //- Return const_reverse_iterator to end reverse traversing FixedList
315
316
        inline const_reverse_iterator crend() const;

317
        //- Return const_reverse_iterator to begin reverse traversing FixedList
318
319
        inline const_reverse_iterator rbegin() const;

320
        //- Return const_reverse_iterator to end reverse traversing FixedList
321
322
323
324
325
        inline const_reverse_iterator rend() const;


    // STL member functions

326
        //- Return the number of elements in the FixedList
327
328
        inline label size() const;

329
        //- Return size of the largest possible FixedList
330
331
        inline label max_size() const;

332
        //- Return true if the FixedList is empty (ie, size() is zero)
333
334
        inline bool empty() const;

335
        //- Swap two FixedLists of the same type in constant time
Mark Olesen's avatar
Mark Olesen committed
336
        void swap(FixedList<T, Size>& a);
337
338
339
340
341
342


    // STL member operators

        //- Equality operation on FixedLists of the same type.
        //  Returns true when the FixedLists are elementwise equal
343
        //  (using FixedList::value_type::operator==).  Takes linear time
Mark Olesen's avatar
Mark Olesen committed
344
        bool operator==(const FixedList<T, Size>& a) const;
345

346
        //- The opposite of the equality operation. Takes linear time
Mark Olesen's avatar
Mark Olesen committed
347
        bool operator!=(const FixedList<T, Size>& a) const;
348

349
        //- Compare two FixedLists lexicographically. Takes linear time
Mark Olesen's avatar
Mark Olesen committed
350
        bool operator<(const FixedList<T, Size>& a) const;
351

352
        //- Compare two FixedLists lexicographically. Takes linear time
Mark Olesen's avatar
Mark Olesen committed
353
        bool operator>(const FixedList<T, Size>& a) const;
354

355
        //- Return true if !(a > b). Takes linear time
Mark Olesen's avatar
Mark Olesen committed
356
        bool operator<=(const FixedList<T, Size>& a) const;
357

358
        //- Return true if !(a < b). Takes linear time
Mark Olesen's avatar
Mark Olesen committed
359
        bool operator>=(const FixedList<T, Size>& a) const;
360
361


362
363
364
365
366
    // Writing

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

367
368
369
        //- Write the List, with line-breaks in ASCII if the list length
        //  exceeds shortListLen. Using '0' suppresses line-breaks entirely.
        Ostream& writeList(Ostream& os, const label shortListLen=0) const;
370

Mark Olesen's avatar
Mark Olesen committed
371

372
373
    // IOstream operators

374
        //- Read List from Istream, discarding contents of existing List
Henry Weller's avatar
Henry Weller committed
375
        friend Istream& operator>> <T, Size>
Mark Olesen's avatar
Mark Olesen committed
376
377
378
379
        (
            Istream& is,
            FixedList<T, Size>& L
        );
380

381
        //- Write List to Ostream, as per writeList() with shortListLen=10
Henry Weller's avatar
Henry Weller committed
382
        friend Ostream& operator<< <T, Size>
383
        (
Mark Olesen's avatar
Mark Olesen committed
384
385
            Ostream& os,
            const FixedList<T, Size>& L
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
        );
};


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

} // End namespace Foam

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

#include "FixedListI.H"

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

#ifdef NoRepository
401
    #include "FixedList.C"
402
403
404
405
406
407
408
#endif

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

#endif

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