FixedList.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2016 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 
47 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48 
49 namespace Foam
50 {
51 
52 // Forward declaration of friend functions and operators
53 
54 template<class T, unsigned Size> class FixedList;
55 
56 template<class T, unsigned Size>
58 
59 template<class T, unsigned Size>
60 Ostream& operator<<(Ostream&, const FixedList<T, Size>&);
61 
62 template<class T> class UList;
63 template<class T> class SLList;
64 
65 
66 /*---------------------------------------------------------------------------*\
67  Class FixedList Declaration
68 \*---------------------------------------------------------------------------*/
69 
70 template<class T, unsigned Size>
71 class FixedList
72 {
73  static_assert
74  (
75  Size && Size <= INT_MAX,
76  "Size must be positive (non-zero) and also fit as a signed value"
77  );
78 
79  // Private data
80 
81  //- Vector of values of type T of size Size.
82  T v_[Size];
83 
84 
85 public:
86 
87  //- Hashing function class.
88  // Use Hasher directly for contiguous data. Otherwise hash incrementally.
89  template<class HashT=Hash<T>>
90  class Hash
91  {
92  public:
93  Hash()
94  {}
95 
96  inline unsigned operator()
97  (
98  const FixedList<T, Size>&,
99  unsigned seed = 0
100  ) const;
101  };
102 
103  // Static Member Functions
104 
105  //- Return a null FixedList
106  inline static const FixedList<T, Size>& null();
107 
108 
109  // Constructors
110 
111  //- Null constructor.
112  inline FixedList();
113 
114  //- Construct from C-array.
115  explicit inline FixedList(const T v[Size]);
116 
117  //- Construct from value
118  explicit inline FixedList(const T&);
119 
120  //- Construct from UList.
121  explicit inline FixedList(const UList<T>&);
122 
123  //- Construct from SLList.
124  explicit inline FixedList(const SLList<T>&);
125 
126  //- Copy constructor.
127  inline FixedList(const FixedList<T, Size>&);
128 
129  //- Construct from Istream.
130  FixedList(Istream&);
131 
132  //- Clone
133  inline autoPtr<FixedList<T, Size>> clone() const;
134 
135 
136  // Member Functions
137 
138  // Access
139 
140  //- Return the forward circular index, i.e. the next index
141  // which returns to the first at the end of the list
142  inline label fcIndex(const label i) const;
143 
144  //- Return the reverse circular index, i.e. the previous index
145  // which returns to the last at the beginning of the list
146  inline label rcIndex(const label i) const;
147 
148 
149  //- Return a const pointer to the first data element,
150  // similar to the STL front() method and the string::data() method
151  // This can be used (with caution) when interfacing with C code.
152  inline const T* cdata() const;
153 
154  //- Return a pointer to the first data element,
155  // similar to the STL front() method and the string::data() method
156  // This can be used (with caution) when interfacing with C code.
157  inline T* data();
158 
159  //- Return the first element of the list.
160  inline T& first();
161 
162  //- Return first element of the list.
163  inline const T& first() const;
164 
165  //- Return the last element of the list.
166  inline T& last();
167 
168  //- Return the last element of the list.
169  inline const T& last() const;
170 
171 
172  // Check
173 
174  //- Check start is within valid range (0 ... size-1).
175  inline void checkStart(const label start) const;
176 
177  //- Check size is within valid range (0 ... size).
178  inline void checkSize(const label size) const;
179 
180  //- Check index i is within valid range (0 ... size-1).
181  inline void checkIndex(const label i) const;
182 
183 
184  // Edit
185 
186  //- Dummy resize function
187  // needed to make FixedList consistent with List
188  inline void resize(const label);
189 
190  //- Dummy setSize function
191  // needed to make FixedList consistent with List
192  inline void setSize(const label);
193 
194  //- Copy (not transfer) the argument contents
195  // needed to make FixedList consistent with List
196  void transfer(const FixedList<T, Size>&);
197 
198  //- Write the FixedList as a dictionary entry
199  void writeEntry(Ostream&) const;
200 
201  //- Write the FixedList as a dictionary entry with keyword
202  void writeEntry(const word& keyword, Ostream&) const;
203 
204 
205  // Member operators
206 
207  //- Return element of FixedList.
208  inline T& operator[](const label);
209 
210  //- Return element of constant FixedList.
211  inline const T& operator[](const label) const;
212 
213  //- Assignment from array operator. Takes linear time.
214  inline void operator=(const T v[Size]);
215 
216  //- Assignment from UList operator. Takes linear time.
217  inline void operator=(const UList<T>&);
218 
219  //- Assignment from SLList operator. Takes linear time.
220  inline void operator=(const SLList<T>&);
221 
222  //- Assignment of all entries to the given value
223  inline void operator=(const T&);
224 
225 
226  // STL type definitions
227 
228  //- Type of values the FixedList contains.
229  typedef T value_type;
230 
231  //- Type that can be used for storing into
232  // FixedList::value_type objects.
233  typedef T& reference;
234 
235  //- Type that can be used for storing into
236  // constant FixedList::value_type objects
237  typedef const T& const_reference;
238 
239  //- The type that can represent the difference between any two
240  // FixedList iterator objects.
241  typedef label difference_type;
242 
243  //- The type that can represent the size of a FixedList.
244  typedef label size_type;
245 
246 
247  // STL iterator
248 
249  //- Random access iterator for traversing FixedList.
250  typedef T* iterator;
251 
252  //- Return an iterator to begin traversing the FixedList.
253  inline iterator begin();
254 
255  //- Return an iterator to end traversing the FixedList.
256  inline iterator end();
257 
258 
259  // STL const_iterator
260 
261  //- Random access iterator for traversing FixedList.
262  typedef const T* const_iterator;
263 
264  //- Return const_iterator to begin traversing the constant FixedList.
265  inline const_iterator cbegin() const;
266 
267  //- Return const_iterator to end traversing the constant FixedList.
268  inline const_iterator cend() const;
269 
270  //- Return const_iterator to begin traversing the constant FixedList.
271  inline const_iterator begin() const;
272 
273  //- Return const_iterator to end traversing the constant FixedList.
274  inline const_iterator end() const;
275 
276 
277  // STL reverse_iterator
278 
279  //- Reverse iterator for reverse traversal of FixedList.
280  typedef T* reverse_iterator;
281 
282  //- Return reverse_iterator to begin reverse traversing the FixedList.
283  inline reverse_iterator rbegin();
284 
285  //- Return reverse_iterator to end reverse traversing the FixedList.
286  inline reverse_iterator rend();
287 
288 
289  // STL const_reverse_iterator
290 
291  //- Reverse iterator for reverse traversal of constant FixedList.
292  typedef const T* const_reverse_iterator;
293 
294  //- Return const_reverse_iterator to begin reverse traversing FixedList.
295  inline const_reverse_iterator crbegin() const;
296 
297  //- Return const_reverse_iterator to end reverse traversing FixedList.
298  inline const_reverse_iterator crend() const;
299 
300  //- Return const_reverse_iterator to begin reverse traversing FixedList.
301  inline const_reverse_iterator rbegin() const;
302 
303  //- Return const_reverse_iterator to end reverse traversing FixedList.
304  inline const_reverse_iterator rend() const;
305 
306 
307  // STL member functions
308 
309  //- Return the number of elements in the FixedList.
310  inline label size() const;
311 
312  //- Return size of the largest possible FixedList.
313  inline label max_size() const;
314 
315  //- Return true if the FixedList is empty (ie, size() is zero).
316  inline bool empty() const;
317 
318  //- Swap two FixedLists of the same type in constant time.
319  void swap(FixedList<T, Size>&);
320 
321 
322  // STL member operators
323 
324  //- Equality operation on FixedLists of the same type.
325  // Returns true when the FixedLists are elementwise equal
326  // (using FixedList::value_type::operator==). Takes linear time.
327  bool operator==(const FixedList<T, Size>&) const;
328 
329  //- The opposite of the equality operation. Takes linear time.
330  bool operator!=(const FixedList<T, Size>&) const;
331 
332  //- Compare two FixedLists lexicographically. Takes linear time.
333  bool operator<(const FixedList<T, Size>&) const;
334 
335  //- Compare two FixedLists lexicographically. Takes linear time.
336  bool operator>(const FixedList<T, Size>&) const;
337 
338  //- Return true if !(a > b). Takes linear time.
339  bool operator<=(const FixedList<T, Size>&) const;
340 
341  //- Return true if !(a < b). Takes linear time.
342  bool operator>=(const FixedList<T, Size>&) const;
343 
344 
345  // IOstream operators
346 
347  //- Read List from Istream, discarding contents of existing List.
348  friend Istream& operator>> <T, Size>
350 
351  // Write FixedList to Ostream.
352  friend Ostream& operator<< <T, Size>
353  (
354  Ostream&,
355  const FixedList<T, Size>&
356  );
357 };
358 
359 
360 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
361 
362 } // End namespace Foam
363 
364 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
365 
366 #include "FixedListI.H"
367 
368 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
369 
370 #ifdef NoRepository
371  #include "FixedList.C"
372 #endif
373 
374 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
375 
376 #endif
377 
378 // ************************************************************************* //
reverse_iterator rbegin()
Return reverse_iterator to begin reverse traversing the FixedList.
Definition: FixedListI.H:355
bool operator!=(const FixedList< T, Size > &) const
The opposite of the equality operation. Takes linear time.
Definition: FixedList.C:64
label difference_type
The type that can represent the difference between any two.
Definition: FixedList.H:240
static const FixedList< T, Size > & null()
Return a null FixedList.
Definition: FixedListI.H:108
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 T & const_reference
Type that can be used for storing into.
Definition: FixedList.H:236
A 1D vector of objects of type <T> with a fixed size <Size>.
Definition: FixedList.H:53
void checkIndex(const label i) const
Check index i is within valid range (0 ... size-1).
Definition: FixedListI.H:153
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:232
const_iterator cbegin() const
Return const_iterator to begin traversing the constant FixedList.
Definition: FixedListI.H:323
bool empty() const
Return true if the FixedList is empty (ie, size() is zero).
Definition: FixedListI.H:416
friend Ostream & operator(Ostream &, const FixedList< T, Size > &)
reverse_iterator rend()
Return reverse_iterator to end reverse traversing the FixedList.
Definition: FixedListI.H:379
label size_type
The type that can represent the size of a FixedList.
Definition: FixedList.H:243
Hashing function class.
Definition: FixedList.H:89
void operator=(const T v[Size])
Assignment from array operator. Takes linear time.
Definition: FixedListI.H:257
const T * const_iterator
Random access iterator for traversing FixedList.
Definition: FixedList.H:261
A class for handling words, derived from string.
Definition: word.H:59
T & operator[](const label)
Return element of FixedList.
Definition: FixedListI.H:237
const_iterator cend() const
Return const_iterator to end traversing the constant FixedList.
Definition: FixedListI.H:347
Istream & operator>>(Istream &, directionInfo &)
void transfer(const FixedList< T, Size > &)
Copy (not transfer) the argument contents.
Definition: FixedListI.H:181
T & first()
Return the first element of the list.
Definition: FixedListI.H:207
label max_size() const
Return size of the largest possible FixedList.
Definition: FixedListI.H:409
T & last()
Return the last element of the list.
Definition: FixedListI.H:221
Non-intrusive singly-linked list.
Definition: SLList.H:47
const T * const_reverse_iterator
Reverse iterator for reverse traversal of constant FixedList.
Definition: FixedList.H:291
label fcIndex(const label i) const
Return the forward circular index, i.e. the next index.
Definition: FixedListI.H:115
label size() const
Return the number of elements in the FixedList.
Definition: FixedListI.H:402
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
void resize(const label)
Dummy resize function.
Definition: FixedListI.H:165
T * iterator
Random access iterator for traversing FixedList.
Definition: FixedList.H:249
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:331
iterator begin()
Return an iterator to begin traversing the FixedList.
Definition: FixedListI.H:307
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
void checkSize(const label size) const
Check size is within valid range (0 ... size).
Definition: FixedListI.H:141
FixedList()
Null constructor.
Definition: FixedListI.H:33
const_reverse_iterator crbegin() const
Return const_reverse_iterator to begin reverse traversing FixedList.
Definition: FixedListI.H:371
T value_type
Type of values the FixedList contains.
Definition: FixedList.H:228
T * data()
Return a pointer to the first data element,.
Definition: FixedListI.H:200
bool operator==(const FixedList< T, Size > &) const
Equality operation on FixedLists of the same type.
Definition: FixedList.C:48
bool operator>(const FixedList< T, Size > &) const
Compare two FixedLists lexicographically. Takes linear time.
Definition: FixedList.C:102
void checkStart(const label start) const
Check start is within valid range (0 ... size-1).
Definition: FixedListI.H:129
void setSize(const label)
Dummy setSize function.
Definition: FixedListI.H:173
void writeEntry(Ostream &) const
Write the FixedList as a dictionary entry.
Definition: FixedListIO.C:137
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:53
const_reverse_iterator crend() const
Return const_reverse_iterator to end reverse traversing FixedList.
Definition: FixedListI.H:395
label rcIndex(const label i) const
Return the reverse circular index, i.e. the previous index.
Definition: FixedListI.H:122
T * reverse_iterator
Reverse iterator for reverse traversal of FixedList.
Definition: FixedList.H:279
const T * cdata() const
Return a const pointer to the first data element,.
Definition: FixedListI.H:192
bool operator>=(const FixedList< T, Size > &) const
Return true if !(a < b). Takes linear time.
Definition: FixedList.C:116
System bool.
Namespace for OpenFOAM.
autoPtr< FixedList< T, Size > > clone() const
Clone.
Definition: FixedListI.H:99