UList.H 13.6 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
7
8
9
10
     \\/     M anipulation  |
-------------------------------------------------------------------------------
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

Class
    Foam::UList

Description
    A 1D vector of objects of type \<T\>, where the size of the vector is
Mark Olesen's avatar
Mark Olesen committed
29
    known and can be used for subscript bounds checking, etc.
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

    Storage is not allocated during construction or use but is supplied to
    the constructor as an argument.  This type of list is particularly useful
    for lists that refer to parts of existing lists such as SubList.

SourceFiles
    UList.C
    UListI.H
    UListIO.C

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

#ifndef UList_H
#define UList_H

#include "bool.H"
Mark Olesen's avatar
Mark Olesen committed
46
47
#include "label.H"
#include "uLabel.H"
48
#include "nullObject.H"
49
50
51
52
53
54

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

namespace Foam
{

Mark Olesen's avatar
Mark Olesen committed
55
56
57
// Forward declaration of friend classes
template<class T> class List;
template<class T> class SubList;
58
59
60
61

// Forward declaration of friend functions and operators
template<class T> class UList;
template<class T> Ostream& operator<<(Ostream&, const UList<T>&);
mattijs's avatar
mattijs committed
62
template<class T> Istream& operator>>(Istream&, UList<T>&);
63

64
typedef UList<label> labelUList;
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91

/*---------------------------------------------------------------------------*\
                           Class UList Declaration
\*---------------------------------------------------------------------------*/

template<class T>
class UList
{
    // Private data

        //- Number of elements in UList.
        label size_;

        //- Vector of values of type T.
        T* __restrict__ v_;


public:

    // Related types

        //- Declare friendship with the List class
        friend class List<T>;

        //- Declare friendship with the SubList class
        friend class SubList<T>;

92
93
94
95
96
    // Static Member Functions

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

Mark Olesen's avatar
Mark Olesen committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
    // Public classes

        //- Less function class that can be used for sorting
        class less
        {
            const UList<T>& values_;

        public:

            less(const UList<T>& values)
            :
                values_(values)
            {}

            bool operator()(const label a, const label b)
            {
                return values_[a] < values_[b];
            }
        };

117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
        //- Greater function class that can be used for sorting
        class greater
        {
            const UList<T>& values_;

        public:

            greater(const UList<T>& values)
            :
                values_(values)
            {}

            bool operator()(const label a, const label b)
            {
                return values_[a] > values_[b];
            }
        };

135
136
137
138
139
140
141
142
143
144

    // Constructors

        //- Null constructor.
        inline UList();

        //- Construct from components
        inline UList(T* __restrict__ v, label size);


145
    // Member Functions
146
147
148
149
150
151
152
153
154


        // 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
155
            //  which returns to the last at the beginning of the list
156
157
158
159
            inline label rcIndex(const label i) const;

            //- Return the binary size in number of characters of the UList
            //  if the element is a primitive type
160
161
162
            //  i.e. contiguous<T>() == true.
            //  Note that is of type streamsize since used in stream ops
            std::streamsize byteSize() const;
163
164


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

            //- Return a pointer to the first data element,
            //  similar to the STL front() method and the string::data() method
            //  This can be used (with caution) when interfacing with C code.
            inline T* data();

175
176
177
178
179
180
181
182
183
184
185
186
            //- Return the first element of the list.
            inline T& first();

            //- Return first element of the list.
            inline const T& first() const;

            //- Return the last element of the list.
            inline T& last();

            //- Return the last element of the list.
            inline const T& last() const;

Mark Olesen's avatar
Mark Olesen committed
187

188
189
190
191
192
193
194
195
196
197
198
199
200
        // Check

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

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

            //- Check index i is within valid range (0 ... size-1).
            inline void checkIndex(const label i) const;


        //- Write the UList as a dictionary entry.
Mark Olesen's avatar
Mark Olesen committed
201
        void writeEntry(Ostream&) const;
202
203

        //- Write the UList as a dictionary entry with keyword.
Mark Olesen's avatar
Mark Olesen committed
204
        void writeEntry(const word& keyword, Ostream&) const;
205
206
207
208
209
210
211

        //- Assign elements to those from UList.
        void assign(const UList<T>&);


    // Member operators

Mark Olesen's avatar
Mark Olesen committed
212
        //- Return element of UList.
213
214
        inline T& operator[](const label);

Mark Olesen's avatar
Mark Olesen committed
215
216
217
        //- Return element of constant UList.
        //  Note that the bool specialization adds lazy evaluation so reading
        //  an out-of-range element returns false without any ill-effects
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
        inline const T& operator[](const label) const;

        //- Allow cast to a const List<T>&
        inline operator const Foam::List<T>&() const;

        //- Assignment of all entries to the given value
        void operator=(const T&);


    // STL type definitions

        //- Type of values the UList contains.
        typedef T value_type;

        //- Type that can be used for storing into
        //  UList::value_type objects.
        typedef T& reference;

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

        //- The type that can represent the difference between any two
        //  UList iterator objects.
        typedef label difference_type;

        //- The type that can represent the size of a UList.
        typedef label size_type;


    // STL iterator

        //- Random access iterator for traversing UList.
        typedef T* iterator;

        //- Return an iterator to begin traversing the UList.
        inline iterator begin();

        //- Return an iterator to end traversing the UList.
        inline iterator end();


    // STL const_iterator

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

265
266
267
268
269
270
271
        //- Return const_iterator to begin traversing the constant UList.
        inline const_iterator cbegin() const;

        //- Return const_iterator to end traversing the constant UList.
        inline const_iterator cend() const;

        //- Return const_iterator to begin traversing the constant UList.
272
273
        inline const_iterator begin() const;

274
        //- Return const_iterator to end traversing the constant UList.
275
276
277
278
279
280
281
282
        inline const_iterator end() const;


    // STL reverse_iterator

        //- Reverse iterator for reverse traversal of UList.
        typedef T* reverse_iterator;

283
        //- Return reverse_iterator to begin reverse traversing the UList.
284
285
        inline reverse_iterator rbegin();

286
        //- Return reverse_iterator to end reverse traversing the UList.
287
288
289
290
291
292
293
294
        inline reverse_iterator rend();


    // STL const_reverse_iterator

        //- Reverse iterator for reverse traversal of constant UList.
        typedef const T* const_reverse_iterator;

295
296
297
298
299
300
301
        //- Return const_reverse_iterator to begin reverse traversing the UList.
        inline const_reverse_iterator crbegin() const;

        //- Return const_reverse_iterator to end reverse traversing the UList.
        inline const_reverse_iterator crend() const;

        //- Return const_reverse_iterator to begin reverse traversing the UList.
302
303
        inline const_reverse_iterator rbegin() const;

304
        //- Return const_reverse_iterator to end reverse traversing the UList.
305
306
307
308
309
310
311
312
313
314
315
        inline const_reverse_iterator rend() const;


    // STL member functions

        //- Return the number of elements in the UList.
        inline label size() const;

        //- Return size of the largest possible UList.
        inline label max_size() const;

Mark Olesen's avatar
Mark Olesen committed
316
        //- Return true if the UList is empty (ie, size() is zero).
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
344
345
346
347
348
        inline bool empty() const;

        //- Swap two ULists of the same type in constant time.
        void swap(UList<T>&);


    // STL member operators

        //- Equality operation on ULists of the same type.
        //  Returns true when the ULists are elementwise equal
        //  (using UList::value_type::operator==).  Takes linear time.
        bool operator==(const UList<T>&) const;

        //- The opposite of the equality operation. Takes linear time.
        bool operator!=(const UList<T>&) const;

        //- Compare two ULists lexicographically. Takes linear time.
        bool operator<(const UList<T>&) const;

        //- Compare two ULists lexicographically. Takes linear time.
        bool operator>(const UList<T>&) const;

        //- Return true if !(a > b). Takes linear time.
        bool operator<=(const UList<T>&) const;

        //- Return true if !(a < b). Takes linear time.
        bool operator>=(const UList<T>&) const;


    // Ostream operator

        // Write UList to Ostream.
Henry Weller's avatar
Henry Weller committed
349
350
351
352
353
        friend Ostream& operator<< <T>
        (
            Ostream&,
            const UList<T>&
        );
mattijs's avatar
mattijs committed
354
355
356
357
358
359
360
361

        //- Read UList contents from Istream. Requires size to have been set
        //  before.
        friend Istream& operator>> <T>
        (
            Istream&,
            UList<T>&
        );
362
363
};

mattijs's avatar
mattijs committed
364
365
366
367
368
369
370
371
372
373
374
375
template<class T>
void sort(UList<T>&);

template<class T, class Cmp>
void sort(UList<T>&, const Cmp&);

template<class T>
void stableSort(UList<T>&);

template<class T, class Cmp>
void stableSort(UList<T>&, const Cmp&);

376
377
378
template<class T>
void shuffle(UList<T>&);

379
380
// Reverse the first n elements of the list
template<class T>
Mark Olesen's avatar
Mark Olesen committed
381
inline void reverse(UList<T>&, const label n);
382
383
384

// Reverse all the elements of the list
template<class T>
Mark Olesen's avatar
Mark Olesen committed
385
inline void reverse(UList<T>&);
386
387
388
389
390
391
392
393


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

} // End namespace Foam

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

394
    #include "UListI.H"
395
396
397

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

398
399
400
401
402
403
404
405
406
//- Loop across all elements in \a list
// \par Usage
// \code
// forAll(anyList, i)
// {
//      statements;
// }
// \endcode
// \sa forAllReverse
407
408
409
#define forAll(list, i) \
    for (Foam::label i=0; i<(list).size(); i++)

410
411
412
413
414
415
416
417
418
//- Reverse loop across all elements in \a list
//  \par Usage
//  \code
//  forAllReverse(anyList, i)
//  {
//       statements;
//  }
//  \endcode
//  \sa forAll
419
420
421
#define forAllReverse(list, i) \
    for (Foam::label i=(list).size()-1; i>=0; i--)

422
423
424
425
426
427
428
429
430
431
//- Iterate across all elements in the \a container object of type
//  \a Container.
//  \par Usage
//  \code
//  forAll(ContainerType, container, iter)
//  {
//      statements;
//  }
//  \endcode
//  \sa forAllConstIter
432
433
434
435
436
437
438
439
#define forAllIter(Container,container,iter)                                   \
    for                                                                        \
    (                                                                          \
        Container::iterator iter = (container).begin();                        \
        iter != (container).end();                                             \
        ++iter                                                                 \
    )

440
441
442
443
444
445
446
447
448
449
//- Iterate across all elements in the \a container object of type
//  \a Container with const access.
//  \par Usage
//  \code
//  forAllConstIter(ContainerType, container, iter)
//  {
//      statements;
//  }
//  \endcode
//  \sa forAllIter
450
451
452
453
454
455
456
457
458
459
460
461
#define forAllConstIter(Container,container,iter)                              \
    for                                                                        \
    (                                                                          \
        Container::const_iterator iter = (container).begin();                  \
        iter != (container).end();                                             \
        ++iter                                                                 \
    )


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

#ifdef NoRepository
462
    #include "UList.C"
463
464
465
466
467
468
469
#endif

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

#endif

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