FixedList.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  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 Class
25  Foam::FixedList
26 
27 Description
28  A 1D vector of objects of type <T> with a fixed size <Size>.
29 
30 SourceFiles
31  FixedList.C
32  FixedListI.H
33  FixedListIO.C
34 
35 \*---------------------------------------------------------------------------*/
36 
37 #ifndef FixedList_H
38 #define FixedList_H
39 
40 #include "bool.H"
41 #include "label.H"
42 #include "uLabel.H"
43 #include "Hash.H"
44 #include "autoPtr.H"
45 #include <type_traits>
46 #include <initializer_list>
47 
48 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49 
50 namespace Foam
51 {
52 
53 // Forward declaration of friend functions and operators
54 
55 template<class T, unsigned Size> class FixedList;
56 
57 template<class T, unsigned Size>
58 void writeEntry(Ostream& os, const FixedList<T, Size>&);
59 
60 template<class T, unsigned Size>
62 
63 template<class T, unsigned Size>
64 Ostream& operator<<(Ostream&, const FixedList<T, Size>&);
65 
66 template<class T> class UList;
67 
68 class SLListBase;
69 template<class LListBase, class T> class LList;
70 template<class T>
72 
73 /*---------------------------------------------------------------------------*\
74  Class FixedList Declaration
75 \*---------------------------------------------------------------------------*/
76 
77 template<class T, unsigned Size>
78 class FixedList
79 {
80  static_assert
81  (
82  Size && Size <= INT_MAX,
83  "Size must be positive (non-zero) and also fit as a signed value"
84  );
85 
86  // Private Data
87 
88  //- Vector of values of type T of size Size.
89  T v_[Size];
90 
91 
92 public:
93 
94  //- Hashing function class.
95  // Use Hasher directly for contiguous data. Otherwise hash incrementally.
96  template<class HashT=Hash<T>>
97  class Hash
98  {
99  public:
100  Hash()
101  {}
102 
103  inline unsigned operator()
104  (
105  const FixedList<T, Size>&,
106  unsigned seed = 0
107  ) const;
108  };
109 
110 
111  // Static Member Functions
112 
113  //- Return a null FixedList
114  inline static const FixedList<T, Size>& null();
115 
116 
117  // Constructors
118 
119  //- Null constructor
120  inline FixedList();
121 
122  //- Construct from value
123  explicit inline FixedList(const T&);
124 
125  //- Construct from C-array
126  explicit inline FixedList(const T v[Size]);
127 
128  //- Construct given start and end iterators
129  template<class InputIterator>
130  inline FixedList(InputIterator first, InputIterator last);
131 
132  //- Construct from an initializer list
133  inline FixedList(std::initializer_list<T>);
134 
135  //- Construct from UList
136  explicit inline FixedList(const UList<T>&);
137 
138  //- Construct from SLList
139  explicit inline FixedList(const SLList<T>&);
140 
141  //- Construct from Istream
142  FixedList(Istream&);
143 
144  //- Clone
145  inline autoPtr<FixedList<T, Size>> clone() const;
146 
147 
148  // Member Functions
149 
150  // Access
151 
152  //- Return the forward circular index, i.e. the next index
153  // which returns to the first at the end of the list
154  inline label fcIndex(const label i) const;
155 
156  //- Return the reverse circular index, i.e. the previous index
157  // which returns to the last at the beginning of the list
158  inline label rcIndex(const label i) const;
159 
160 
161  //- Return a const pointer to the first data element,
162  // similar to the STL front() method and the string::data() method
163  // This can be used (with caution) when interfacing with C code
164  inline const T* cdata() const;
165 
166  //- Return a pointer to the first data element,
167  // similar to the STL front() method and the string::data() method
168  // This can be used (with caution) when interfacing with C code
169  inline T* data();
170 
171  //- Return the first element of the list
172  inline T& first();
173 
174  //- Return first element of the list
175  inline const T& first() const;
176 
177  //- Return the last element of the list
178  inline T& last();
179 
180  //- Return the last element of the list
181  inline const T& last() const;
182 
183 
184  // Check
185 
186  //- Check start is within valid range (0 ... size-1)
187  inline void checkStart(const label start) const;
188 
189  //- Check size is within valid range (0 ... size)
190  inline void checkSize(const label size) const;
191 
192  //- Check index i is within valid range (0 ... size-1)
193  inline void checkIndex(const label i) const;
194 
195 
196  // Edit
197 
198  //- Dummy resize function
199  // needed to make FixedList consistent with List
200  inline void resize(const label);
201 
202  //- Dummy setSize function
203  // needed to make FixedList consistent with List
204  inline void setSize(const label);
205 
206  //- Copy (not transfer) the argument contents
207  // needed to make FixedList consistent with List
208  void transfer(const FixedList<T, Size>&);
209 
210 
211  // Member Operators
212 
213  //- Return element of FixedList
214  inline T& operator[](const label);
215 
216  //- Return element of constant FixedList
217  inline const T& operator[](const label) const;
218 
219  //- Assignment to array operator. Takes linear time
220  inline void operator=(const T v[Size]);
221 
222  //- Assignment to UList operator. Takes linear time
223  inline void operator=(const UList<T>&);
224 
225  //- Assignment to SLList operator. Takes linear time
226  inline void operator=(const SLList<T>&);
227 
228  //- Assignment to an initializer list. Takes linear time
229  inline void operator=(std::initializer_list<T>);
230 
231  //- Assignment of all entries to the given value
232  inline void operator=(const T&);
233 
234 
235  // STL type definitions
236 
237  //- Type of values the FixedList contains
238  typedef T value_type;
239 
240  //- Type that can be used for storing into
241  // FixedList::value_type objects
242  typedef T& reference;
243 
244  //- Type that can be used for storing into
245  // constant FixedList::value_type objects
246  typedef const T& const_reference;
247 
248  //- The type that can represent the difference between any two
249  // FixedList iterator objects
250  typedef label difference_type;
251 
252  //- The type that can represent the size of a FixedList
253  typedef label size_type;
254 
255 
256  // STL iterator
257 
258  //- Random access iterator for traversing FixedList
259  typedef T* iterator;
260 
261  //- Return an iterator to begin traversing the FixedList
262  inline iterator begin();
263 
264  //- Return an iterator to end traversing the FixedList
265  inline iterator end();
266 
267 
268  // STL const_iterator
269 
270  //- Random access iterator for traversing FixedList
271  typedef const T* const_iterator;
272 
273  //- Return const_iterator to begin traversing the constant FixedList
274  inline const_iterator cbegin() const;
275 
276  //- Return const_iterator to end traversing the constant FixedList
277  inline const_iterator cend() const;
278 
279  //- Return const_iterator to begin traversing the constant FixedList
280  inline const_iterator begin() const;
281 
282  //- Return const_iterator to end traversing the constant FixedList
283  inline const_iterator end() const;
284 
285 
286  // STL reverse_iterator
287 
288  //- Reverse iterator for reverse traversal of FixedList
289  typedef T* reverse_iterator;
290 
291  //- Return reverse_iterator to begin reverse traversing the FixedList
292  inline reverse_iterator rbegin();
293 
294  //- Return reverse_iterator to end reverse traversing the FixedList
295  inline reverse_iterator rend();
296 
297 
298  // STL const_reverse_iterator
299 
300  //- Reverse iterator for reverse traversal of constant FixedList
301  typedef const T* const_reverse_iterator;
302 
303  //- Return const_reverse_iterator to begin reverse traversing FixedList
304  inline const_reverse_iterator crbegin() const;
305 
306  //- Return const_reverse_iterator to end reverse traversing FixedList
307  inline const_reverse_iterator crend() const;
308 
309  //- Return const_reverse_iterator to begin reverse traversing FixedList
310  inline const_reverse_iterator rbegin() const;
311 
312  //- Return const_reverse_iterator to end reverse traversing FixedList
313  inline const_reverse_iterator rend() const;
314 
315 
316  // STL member functions
317 
318  //- Return the number of elements in the FixedList
319  inline label size() const;
320 
321  //- Return size of the largest possible FixedList
322  inline label max_size() const;
323 
324  //- Return true if the FixedList is empty (ie, size() is zero)
325  inline bool empty() const;
326 
327  //- Swap two FixedLists of the same type in constant time
328  void swap(FixedList<T, Size>&);
329 
330 
331  // STL member operators
332 
333  //- Equality operation on FixedLists of the same type.
334  // Returns true when the FixedLists are elementwise equal
335  // (using FixedList::value_type::operator==). Takes linear time
336  bool operator==(const FixedList<T, Size>&) const;
337 
338  //- The opposite of the equality operation. Takes linear time
339  bool operator!=(const FixedList<T, Size>&) const;
340 
341  //- Compare two FixedLists lexicographically. Takes linear time
342  bool operator<(const FixedList<T, Size>&) const;
343 
344  //- Compare two FixedLists lexicographically. Takes linear time
345  bool operator>(const FixedList<T, Size>&) const;
346 
347  //- Return true if !(a > b). Takes linear time
348  bool operator<=(const FixedList<T, Size>&) const;
349 
350  //- Return true if !(a < b). Takes linear time
351  bool operator>=(const FixedList<T, Size>&) const;
352 
353 
354  // IOstream Operators
355 
356  //- Read List from Istream, discarding contents of existing List
357  friend Istream& operator>> <T, Size>
359 
360  //- Write FixedList to Ostream
361  friend Ostream& operator<< <T, Size>
362  (
363  Ostream&,
364  const FixedList<T, Size>&
365  );
366 };
367 
368 
369 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
370 
371 } // End namespace Foam
372 
373 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
374 
375 #include "FixedListI.H"
376 
377 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
378 
379 #ifdef NoRepository
380  #include "FixedList.C"
381 #endif
382 
383 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
384 
385 #endif
386 
387 // ************************************************************************* //
reverse_iterator rbegin()
Return reverse_iterator to begin reverse traversing the FixedList.
Definition: FixedListI.H:372
label difference_type
The type that can represent the difference between any two.
Definition: FixedList.H:249
static const FixedList< T, Size > & null()
Return a null FixedList.
Definition: FixedListI.H:118
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
const_iterator cend() const
Return const_iterator to end traversing the constant FixedList.
Definition: FixedListI.H:364
const T & const_reference
Type that can be used for storing into.
Definition: FixedList.H:245
A 1D vector of objects of type <T> with a fixed size <Size>.
Definition: FixedList.H:54
const_reverse_iterator crbegin() const
Return const_reverse_iterator to begin reverse traversing FixedList.
Definition: FixedListI.H:388
Template class for non-intrusive linked lists.
Definition: LList.H:51
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
T & reference
Type that can be used for storing into.
Definition: FixedList.H:241
Base singly-linked list.
Definition: SLListBase.H:50
bool operator>=(const FixedList< T, Size > &) const
Return true if !(a < b). Takes linear time.
Definition: FixedList.C:116
friend Ostream & operator(Ostream &, const FixedList< T, Size > &)
Write FixedList to Ostream.
void checkIndex(const label i) const
Check index i is within valid range (0 ... size-1)
Definition: FixedListI.H:163
reverse_iterator rend()
Return reverse_iterator to end reverse traversing the FixedList.
Definition: FixedListI.H:396
const_reverse_iterator crend() const
Return const_reverse_iterator to end reverse traversing FixedList.
Definition: FixedListI.H:412
label size_type
The type that can represent the size of a FixedList.
Definition: FixedList.H:252
bool operator!=(const FixedList< T, Size > &) const
The opposite of the equality operation. Takes linear time.
Definition: FixedList.C:64
Hashing function class.
Definition: FixedList.H:96
void operator=(const T v[Size])
Assignment to array operator. Takes linear time.
Definition: FixedListI.H:267
const T * const_iterator
Random access iterator for traversing FixedList.
Definition: FixedList.H:270
T & operator[](const label)
Return element of FixedList.
Definition: FixedListI.H:247
void checkStart(const label start) const
Check start is within valid range (0 ... size-1)
Definition: FixedListI.H:139
Istream & operator>>(Istream &, directionInfo &)
void transfer(const FixedList< T, Size > &)
Copy (not transfer) the argument contents.
Definition: FixedListI.H:191
T & first()
Return the first element of the list.
Definition: FixedListI.H:217
bool operator==(const FixedList< T, Size > &) const
Equality operation on FixedLists of the same type.
Definition: FixedList.C:48
T & last()
Return the last element of the list.
Definition: FixedListI.H:231
const T * const_reverse_iterator
Reverse iterator for reverse traversal of constant FixedList.
Definition: FixedList.H:300
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:60
label fcIndex(const label i) const
Return the forward circular index, i.e. the next index.
Definition: FixedListI.H:125
void resize(const label)
Dummy resize function.
Definition: FixedListI.H:175
T * iterator
Random access iterator for traversing FixedList.
Definition: FixedList.H:258
void swap(FixedList< T, Size > &)
Swap two FixedLists of the same type in constant time.
Definition: FixedList.C:32
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
iterator end()
Return an iterator to end traversing the FixedList.
Definition: FixedListI.H:348
void checkSize(const label size) const
Check size is within valid range (0 ... size)
Definition: FixedListI.H:151
label size() const
Return the number of elements in the FixedList.
Definition: FixedListI.H:419
iterator begin()
Return an iterator to begin traversing the FixedList.
Definition: FixedListI.H:324
bool operator>(const FixedList< T, Size > &) const
Compare two FixedLists lexicographically. Takes linear time.
Definition: FixedList.C:102
const_iterator cbegin() const
Return const_iterator to begin traversing the constant FixedList.
Definition: FixedListI.H:340
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
label rcIndex(const label i) const
Return the reverse circular index, i.e. the previous index.
Definition: FixedListI.H:132
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
label max_size() const
Return size of the largest possible FixedList.
Definition: FixedListI.H:426
FixedList()
Null constructor.
Definition: FixedListI.H:33
bool empty() const
Return true if the FixedList is empty (ie, size() is zero)
Definition: FixedListI.H:433
T value_type
Type of values the FixedList contains.
Definition: FixedList.H:237
autoPtr< FixedList< T, Size > > clone() const
Clone.
Definition: FixedListI.H:109
T * data()
Return a pointer to the first data element,.
Definition: FixedListI.H:210
void setSize(const label)
Dummy setSize function.
Definition: FixedListI.H:183
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:52
T * reverse_iterator
Reverse iterator for reverse traversal of FixedList.
Definition: FixedList.H:288
System bool.
const T * cdata() const
Return a const pointer to the first data element,.
Definition: FixedListI.H:202
Namespace for OpenFOAM.