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>
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 // ************************************************************************* //
Foam::FixedList::clone
autoPtr< FixedList< T, Size > > clone() const
Clone.
Definition: FixedListI.H:99
Foam::FixedList::rend
reverse_iterator rend()
Return reverse_iterator to end reverse traversing the FixedList.
Definition: FixedListI.H:385
Foam::FixedList::v_
T v_[Size]
Vector of values of type T of size Size.
Definition: FixedList.H:78
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::SLList
Non-intrusive singly-linked list.
Definition: SLList.H:47
Foam::FixedList::operator<=
bool operator<=(const FixedList< T, Size > &) const
Return true if !(a > b). Takes linear time.
Definition: FixedList.C:109
Foam::FixedList::checkIndex
void checkIndex(const label i) const
Check index i is within valid range (0 ... size-1).
Definition: FixedListI.H:157
Foam::FixedList::operator[]
T & operator[](const label)
Return element of FixedList.
Foam::FixedList::checkStart
void checkStart(const label start) const
Check start is within valid range (0 ... size-1).
Definition: FixedListI.H:130
Foam::FixedList::cend
const_iterator cend() const
Return const_iterator to end traversing the constant FixedList.
Definition: FixedListI.H:353
Foam::FixedList::Hash
Hashing function class.
Definition: FixedList.H:86
Foam::FixedList::cdata
const T * cdata() const
Return a const pointer to the first data element,.
Definition: FixedListI.H:196
Foam::FixedList::size_type
label size_type
The type that can represent the size of a FixedList.
Definition: FixedList.H:240
Foam::FixedList::operator!=
bool operator!=(const FixedList< T, Size > &) const
The opposite of the equality operation. Takes linear time.
Definition: FixedList.C:64
FixedListI.H
Foam::FixedList::first
T & first()
Return the first element of the list.
Definition: FixedListI.H:211
Foam::FixedList::crbegin
const_reverse_iterator crbegin() const
Return const_reverse_iterator to begin reverse traversing FixedList.
Definition: FixedListI.H:377
Foam::FixedList::difference_type
label difference_type
The type that can represent the difference between any two.
Definition: FixedList.H:237
Foam::FixedList::fcIndex
label fcIndex(const label i) const
Return the forward circular index, i.e. the next index.
Definition: FixedListI.H:115
Foam::FixedList::StaticAssert
StaticAssert(Size &&Size<=INT_MAX)
Size must be positive (non-zero) and also fit as a signed value.
Foam::FixedList::reference
T & reference
Type that can be used for storing into.
Definition: FixedList.H:229
Foam::FixedList::const_reverse_iterator
const typedef T * const_reverse_iterator
Reverse iterator for reverse traversal of constant FixedList.
Definition: FixedList.H:288
Foam::FixedList::operator==
bool operator==(const FixedList< T, Size > &) const
Equality operation on FixedLists of the same type.
Definition: FixedList.C:48
Foam::FixedList::last
T & last()
Return the last element of the list.
Definition: FixedListI.H:225
Foam::FixedList::operator
friend Ostream & operator(Ostream &, const FixedList< T, Size > &)
Foam::label
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
Foam::FixedList::resize
void resize(const label)
Dummy resize function.
Definition: FixedListI.H:169
Foam::FixedList::max_size
label max_size() const
Return size of the largest possible FixedList.
Definition: FixedListI.H:415
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:57
Foam::FixedList::iterator
T * iterator
Random access iterator for traversing FixedList.
Definition: FixedList.H:246
Foam::FixedList::end
iterator end()
Return an iterator to end traversing the FixedList.
Definition: FixedListI.H:337
Foam::FixedList::operator>=
bool operator>=(const FixedList< T, Size > &) const
Return true if !(a < b). Takes linear time.
Definition: FixedList.C:116
Foam::FixedList::operator>
bool operator>(const FixedList< T, Size > &) const
Compare two FixedLists lexicographically. Takes linear time.
Definition: FixedList.C:102
Foam::FixedList::cbegin
const_iterator cbegin() const
Return const_iterator to begin traversing the constant FixedList.
Definition: FixedListI.H:329
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:55
Foam::FixedList::const_reference
const typedef T & const_reference
Type that can be used for storing into.
Definition: FixedList.H:233
Foam::FixedList::data
T * data()
Return a pointer to the first data element,.
Definition: FixedListI.H:204
bool.H
System bool.
Foam::operator<<
Ostream & operator<<(Ostream &, const edgeMesh &)
Definition: edgeMeshIO.C:130
Foam::FixedList::setSize
void setSize(const label)
Dummy setSize function.
Definition: FixedListI.H:177
Foam::FixedList::value_type
T value_type
Type of values the FixedList contains.
Definition: FixedList.H:225
Foam::FixedList::crend
const_reverse_iterator crend() const
Return const_reverse_iterator to end reverse traversing FixedList.
Definition: FixedListI.H:401
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::FixedList::transfer
void transfer(const FixedList< T, Size > &)
Copy (not transfer) the argument contents.
Definition: FixedListI.H:185
Foam::FixedList::operator<
bool operator<(const FixedList< T, Size > &) const
Compare two FixedLists lexicographically. Takes linear time.
Definition: FixedList.C:71
StaticAssert.H
Foam::autoPtr
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:117
Foam::FixedList::Hash::Hash
Hash()
Definition: FixedList.H:89
Foam::FixedList::operator=
void operator=(const T v[Size])
Assignment from array operator. Takes linear time.
Foam::FixedList::reverse_iterator
T * reverse_iterator
Reverse iterator for reverse traversal of FixedList.
Definition: FixedList.H:276
label.H
Foam::FixedList::swap
void swap(FixedList< T, Size > &)
Swap two FixedLists of the same type in constant time.
Definition: FixedList.C:32
Foam::FixedList::size
label size() const
Return the number of elements in the FixedList.
Definition: FixedListI.H:408
Foam::FixedList
A 1D vector of objects of type <T> with a fixed size <Size>.
Definition: FixedList.H:53
Foam::UList
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
Hash.H
Foam::operator>>
Istream & operator>>(Istream &, edgeMesh &)
Definition: edgeMeshIO.C:141
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::FixedList::empty
bool empty() const
Return true if the FixedList is empty (ie, size() is zero).
Definition: FixedListI.H:422
Foam::FixedList::const_iterator
const typedef T * const_iterator
Random access iterator for traversing FixedList.
Definition: FixedList.H:258
Foam::FixedList::begin
iterator begin()
Return an iterator to begin traversing the FixedList.
Definition: FixedListI.H:313
Foam::FixedList::rbegin
reverse_iterator rbegin()
Return reverse_iterator to begin reverse traversing the FixedList.
Definition: FixedListI.H:361
uLabel.H
Foam::FixedList::checkSize
void checkSize(const label size) const
Check size is within valid range (0 ... size).
Definition: FixedListI.H:143
Foam::FixedList::writeEntry
void writeEntry(Ostream &) const
Write the FixedList as a dictionary entry.
Foam::FixedList::rcIndex
label rcIndex(const label i) const
Return the reverse circular index, i.e. the previous index.
Definition: FixedListI.H:122
Foam::FixedList::FixedList
FixedList()
Null constructor.
Definition: FixedListI.H:33
FixedList.C
autoPtr.H