UList.H 13 KB
Newer Older
1
2
3
4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
5
    \\  /    A nd           | Copyright (C) 2011 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
49
50
51
52
53

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

namespace Foam
{

Mark Olesen's avatar
Mark Olesen committed
54
55
56
// Forward declaration of friend classes
template<class T> class List;
template<class T> class SubList;
57
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>&);

62
typedef UList<label> labelUList;
63
64
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

/*---------------------------------------------------------------------------*\
                           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>;

90
91
92
93
94
    // Static Member Functions

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

Mark Olesen's avatar
Mark Olesen committed
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
    // 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];
            }
        };

115
116
117
118
119
120
121
122
123
124

    // Constructors

        //- Null constructor.
        inline UList();

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


125
    // Member Functions
126
127
128
129
130
131
132
133
134


        // 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
135
            //  which returns to the last at the beginning of the list
136
137
138
139
140
141
142
143
            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
            //  i.e. contiguous<T>() == true
            label byteSize() const;


Mark Olesen's avatar
Mark Olesen committed
144
145
146
147
148
149
150
151
152
153
            //- 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();

154
155
156
157
158
159
160
161
162
163
164
165
            //- 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
166

167
168
169
170
171
172
173
174
175
176
177
178
179
        // 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
180
        void writeEntry(Ostream&) const;
181
182

        //- Write the UList as a dictionary entry with keyword.
Mark Olesen's avatar
Mark Olesen committed
183
        void writeEntry(const word& keyword, Ostream&) const;
184
185
186
187
188
189
190

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


    // Member operators

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

Mark Olesen's avatar
Mark Olesen committed
194
195
196
        //- 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
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
        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;

244
245
246
247
248
249
250
        //- 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.
251
252
        inline const_iterator begin() const;

253
        //- Return const_iterator to end traversing the constant UList.
254
255
256
257
258
259
260
261
        inline const_iterator end() const;


    // STL reverse_iterator

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

262
        //- Return reverse_iterator to begin reverse traversing the UList.
263
264
        inline reverse_iterator rbegin();

265
        //- Return reverse_iterator to end reverse traversing the UList.
266
267
268
269
270
271
272
273
        inline reverse_iterator rend();


    // STL const_reverse_iterator

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

274
275
276
277
278
279
280
        //- 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.
281
282
        inline const_reverse_iterator rbegin() const;

283
        //- Return const_reverse_iterator to end reverse traversing the UList.
284
285
286
287
288
289
290
291
292
293
294
        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
295
        //- Return true if the UList is empty (ie, size() is zero).
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
        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
328
329
330
331
332
        friend Ostream& operator<< <T>
        (
            Ostream&,
            const UList<T>&
        );
333
334
};

mattijs's avatar
mattijs committed
335
336
337
338
339
340
341
342
343
344
345
346
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&);

347
348
349
template<class T>
void shuffle(UList<T>&);

350
351
// Reverse the first n elements of the list
template<class T>
Mark Olesen's avatar
Mark Olesen committed
352
inline void reverse(UList<T>&, const label n);
353
354
355

// Reverse all the elements of the list
template<class T>
Mark Olesen's avatar
Mark Olesen committed
356
inline void reverse(UList<T>&);
357
358
359
360
361
362
363
364
365
366
367
368
369


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

} // End namespace Foam

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

#   include "UListI.H"

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

/**
370
371
372
373
 * \def forAll(list, i)
 * Loop across all elements in \a list
 * \par Usage
 * \code
374
375
376
377
 * forAll(anyList, i)
 * {
 *      statements;
 * }
378
379
 * \endcode
 * \sa forAllReverse
380
381
*/
/**
382
383
384
385
 * \def forAllReverse(list, i)
 * Reverse loop across all elements in \a list
 * \par Usage
 * \code
386
387
388
389
 * forAllReverse(anyList, i)
 * {
 *      statements;
 * }
390
391
 * \endcode
 * \sa forAll
392
393
394
395
396
397
398
399
*/
#define forAll(list, i) \
    for (Foam::label i=0; i<(list).size(); i++)

#define forAllReverse(list, i) \
    for (Foam::label i=(list).size()-1; i>=0; i--)

/**
400
401
402
403
404
 * \def forAllIter(Container, container, iter)
 * Iterate across all elements in the \a container object of type
 * \a Container.
 * \par Usage
 * \code
405
406
407
408
 * forAll(ContainerType, container, iter)
 * {
 *     statements;
 * }
409
410
 * \endcode
 * \sa forAllConstIter
411
412
413
414
415
416
417
418
419
420
*/
#define forAllIter(Container,container,iter)                                   \
    for                                                                        \
    (                                                                          \
        Container::iterator iter = (container).begin();                        \
        iter != (container).end();                                             \
        ++iter                                                                 \
    )

/**
421
422
423
424
425
 * \def forAllConstIter(Container, container, iter)
 * Iterate across all elements in the \a container object of type
 * \a Container with const access.
 * \par Usage
 * \code
426
427
428
429
 * forAllConstIter(ContainerType, container, iter)
 * {
 *     statements;
 * }
430
431
 * \endcode
 * \sa forAllIter
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
*/
#define forAllConstIter(Container,container,iter)                              \
    for                                                                        \
    (                                                                          \
        Container::const_iterator iter = (container).begin();                  \
        iter != (container).end();                                             \
        ++iter                                                                 \
    )


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

#ifdef NoRepository
#   include "UList.C"
#endif

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

#endif

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