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-2018 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>
59 
60 template<class T, unsigned Size>
61 Ostream& operator<<(Ostream&, const FixedList<T, Size>&);
62 
63 template<class T> class UList;
64 
65 class SLListBase;
66 template<class LListBase, class T> class LList;
67 template<class T>
69 
70 /*---------------------------------------------------------------------------*\
71  Class FixedList Declaration
72 \*---------------------------------------------------------------------------*/
73 
74 template<class T, unsigned Size>
75 class FixedList
76 {
77  static_assert
78  (
79  Size && Size <= INT_MAX,
80  "Size must be positive (non-zero) and also fit as a signed value"
81  );
82 
83  // Private data
84 
85  //- Vector of values of type T of size Size.
86  T v_[Size];
87 
88 
89 public:
90 
91  //- Hashing function class.
92  // Use Hasher directly for contiguous data. Otherwise hash incrementally.
93  template<class HashT=Hash<T>>
94  class Hash
95  {
96  public:
97  Hash()
98  {}
99 
100  inline unsigned operator()
101  (
102  const FixedList<T, Size>&,
103  unsigned seed = 0
104  ) const;
105  };
106 
107 
108  // Static Member Functions
109 
110  //- Return a null FixedList
111  inline static const FixedList<T, Size>& null();
112 
113 
114  // Constructors
115 
116  //- Null constructor
117  inline FixedList();
118 
119  //- Construct from value
120  explicit inline FixedList(const T&);
121 
122  //- Construct from C-array
123  explicit inline FixedList(const T v[Size]);
124 
125  //- Construct given start and end iterators
126  template<class InputIterator>
127  inline FixedList(InputIterator first, InputIterator last);
128 
129  //- Construct from an initializer list
130  inline FixedList(std::initializer_list<T>);
131 
132  //- Construct from UList
133  explicit inline FixedList(const UList<T>&);
134 
135  //- Construct from SLList
136  explicit inline FixedList(const SLList<T>&);
137 
138  //- Copy constructor
139  inline FixedList(const FixedList<T, Size>&);
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  //- Write the FixedList as a dictionary entry
211  void writeEntry(Ostream&) const;
212 
213  //- Write the FixedList as a dictionary entry with keyword
214  void writeEntry(const word& keyword, Ostream&) const;
215 
216 
217  // Member operators
218 
219  //- Return element of FixedList
220  inline T& operator[](const label);
221 
222  //- Return element of constant FixedList
223  inline const T& operator[](const label) const;
224 
225  //- Assignment to array operator. Takes linear time
226  inline void operator=(const T v[Size]);
227 
228  //- Assignment to UList operator. Takes linear time
229  inline void operator=(const UList<T>&);
230 
231  //- Assignment to SLList operator. Takes linear time
232  inline void operator=(const SLList<T>&);
233 
234  //- Assignment to an initializer list. Takes linear time
235  inline void operator=(std::initializer_list<T>);
236 
237  //- Assignment of all entries to the given value
238  inline void operator=(const T&);
239 
240 
241  // STL type definitions
242 
243  //- Type of values the FixedList contains
244  typedef T value_type;
245 
246  //- Type that can be used for storing into
247  // FixedList::value_type objects
248  typedef T& reference;
249 
250  //- Type that can be used for storing into
251  // constant FixedList::value_type objects
252  typedef const T& const_reference;
253 
254  //- The type that can represent the difference between any two
255  // FixedList iterator objects
256  typedef label difference_type;
257 
258  //- The type that can represent the size of a FixedList
259  typedef label size_type;
260 
261 
262  // STL iterator
263 
264  //- Random access iterator for traversing FixedList
265  typedef T* iterator;
266 
267  //- Return an iterator to begin traversing the FixedList
268  inline iterator begin();
269 
270  //- Return an iterator to end traversing the FixedList
271  inline iterator end();
272 
273 
274  // STL const_iterator
275 
276  //- Random access iterator for traversing FixedList
277  typedef const T* const_iterator;
278 
279  //- Return const_iterator to begin traversing the constant FixedList
280  inline const_iterator cbegin() const;
281 
282  //- Return const_iterator to end traversing the constant FixedList
283  inline const_iterator cend() const;
284 
285  //- Return const_iterator to begin traversing the constant FixedList
286  inline const_iterator begin() const;
287 
288  //- Return const_iterator to end traversing the constant FixedList
289  inline const_iterator end() const;
290 
291 
292  // STL reverse_iterator
293 
294  //- Reverse iterator for reverse traversal of FixedList
295  typedef T* reverse_iterator;
296 
297  //- Return reverse_iterator to begin reverse traversing the FixedList
298  inline reverse_iterator rbegin();
299 
300  //- Return reverse_iterator to end reverse traversing the FixedList
301  inline reverse_iterator rend();
302 
303 
304  // STL const_reverse_iterator
305 
306  //- Reverse iterator for reverse traversal of constant FixedList
307  typedef const T* const_reverse_iterator;
308 
309  //- Return const_reverse_iterator to begin reverse traversing FixedList
310  inline const_reverse_iterator crbegin() const;
311 
312  //- Return const_reverse_iterator to end reverse traversing FixedList
313  inline const_reverse_iterator crend() const;
314 
315  //- Return const_reverse_iterator to begin reverse traversing FixedList
316  inline const_reverse_iterator rbegin() const;
317 
318  //- Return const_reverse_iterator to end reverse traversing FixedList
319  inline const_reverse_iterator rend() const;
320 
321 
322  // STL member functions
323 
324  //- Return the number of elements in the FixedList
325  inline label size() const;
326 
327  //- Return size of the largest possible FixedList
328  inline label max_size() const;
329 
330  //- Return true if the FixedList is empty (ie, size() is zero)
331  inline bool empty() const;
332 
333  //- Swap two FixedLists of the same type in constant time
334  void swap(FixedList<T, Size>&);
335 
336 
337  // STL member operators
338 
339  //- Equality operation on FixedLists of the same type.
340  // Returns true when the FixedLists are elementwise equal
341  // (using FixedList::value_type::operator==). Takes linear time
342  bool operator==(const FixedList<T, Size>&) const;
343 
344  //- The opposite of the equality operation. Takes linear time
345  bool operator!=(const FixedList<T, Size>&) const;
346 
347  //- Compare two FixedLists lexicographically. Takes linear time
348  bool operator<(const FixedList<T, Size>&) const;
349 
350  //- Compare two FixedLists lexicographically. Takes linear time
351  bool operator>(const FixedList<T, Size>&) const;
352 
353  //- Return true if !(a > b). Takes linear time
354  bool operator<=(const FixedList<T, Size>&) const;
355 
356  //- Return true if !(a < b). Takes linear time
357  bool operator>=(const FixedList<T, Size>&) const;
358 
359 
360  // IOstream operators
361 
362  //- Read List from Istream, discarding contents of existing List
363  friend Istream& operator>> <T, Size>
365 
366  //- Write FixedList to Ostream
367  friend Ostream& operator<< <T, Size>
368  (
369  Ostream&,
370  const FixedList<T, Size>&
371  );
372 };
373 
374 
375 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
376 
377 } // End namespace Foam
378 
379 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
380 
381 #include "FixedListI.H"
382 
383 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
384 
385 #ifdef NoRepository
386  #include "FixedList.C"
387 #endif
388 
389 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
390 
391 #endif
392 
393 // ************************************************************************* //
reverse_iterator rbegin()
Return reverse_iterator to begin reverse traversing the FixedList.
Definition: FixedListI.H:382
void writeEntry(Ostream &) const
Write the FixedList as a dictionary entry.
Definition: FixedListIO.C:137
label difference_type
The type that can represent the difference between any two.
Definition: FixedList.H:255
static const FixedList< T, Size > & null()
Return a null FixedList.
Definition: FixedListI.H:128
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:374
const T & const_reference
Type that can be used for storing into.
Definition: FixedList.H:251
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:398
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:247
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:173
reverse_iterator rend()
Return reverse_iterator to end reverse traversing the FixedList.
Definition: FixedListI.H:406
const_reverse_iterator crend() const
Return const_reverse_iterator to end reverse traversing FixedList.
Definition: FixedListI.H:422
label size_type
The type that can represent the size of a FixedList.
Definition: FixedList.H:258
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:93
void operator=(const T v[Size])
Assignment to array operator. Takes linear time.
Definition: FixedListI.H:277
const T * const_iterator
Random access iterator for traversing FixedList.
Definition: FixedList.H:276
A class for handling words, derived from string.
Definition: word.H:59
T & operator[](const label)
Return element of FixedList.
Definition: FixedListI.H:257
void checkStart(const label start) const
Check start is within valid range (0 ... size-1)
Definition: FixedListI.H:149
Istream & operator>>(Istream &, directionInfo &)
void transfer(const FixedList< T, Size > &)
Copy (not transfer) the argument contents.
Definition: FixedListI.H:201
T & first()
Return the first element of the list.
Definition: FixedListI.H:227
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:241
const T * const_reverse_iterator
Reverse iterator for reverse traversal of constant FixedList.
Definition: FixedList.H:306
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:61
label fcIndex(const label i) const
Return the forward circular index, i.e. the next index.
Definition: FixedListI.H:135
void resize(const label)
Dummy resize function.
Definition: FixedListI.H:185
T * iterator
Random access iterator for traversing FixedList.
Definition: FixedList.H:264
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:53
iterator end()
Return an iterator to end traversing the FixedList.
Definition: FixedListI.H:358
void checkSize(const label size) const
Check size is within valid range (0 ... size)
Definition: FixedListI.H:161
label size() const
Return the number of elements in the FixedList.
Definition: FixedListI.H:429
iterator begin()
Return an iterator to begin traversing the FixedList.
Definition: FixedListI.H:334
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:350
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:142
label max_size() const
Return size of the largest possible FixedList.
Definition: FixedListI.H:436
FixedList()
Null constructor.
Definition: FixedListI.H:33
bool empty() const
Return true if the FixedList is empty (ie, size() is zero)
Definition: FixedListI.H:443
T value_type
Type of values the FixedList contains.
Definition: FixedList.H:243
autoPtr< FixedList< T, Size > > clone() const
Clone.
Definition: FixedListI.H:119
T * data()
Return a pointer to the first data element,.
Definition: FixedListI.H:220
void setSize(const label)
Dummy setSize function.
Definition: FixedListI.H:193
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:294
System bool.
const T * cdata() const
Return a const pointer to the first data element,.
Definition: FixedListI.H:212
Namespace for OpenFOAM.