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