FreeFOAM The Cross-Platform CFD Toolkit
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) 1991-2010 OpenCFD Ltd.
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 <OpenFOAM/bool.H>
41 #include <OpenFOAM/label.H>
42 #include <OpenFOAM/uLabel.H>
43 #include <OpenFOAM/Hash.H>
44 #include <OpenFOAM/autoPtr.H>
45 #include <OpenFOAM/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>
57 Istream& operator>>(Istream&, FixedList<T, 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 components
112  inline FixedList(const T v[Size]);
113 
114  //- Construct from value
115  inline FixedList(const T&);
116 
117  //- Construct from UList.
118  inline FixedList(const UList<T>&);
119 
120  //- Construct from SLList.
121  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 
157  // Check
158 
159  //- Check start is within valid range (0 ... size-1).
160  inline void checkStart(const label start) const;
161 
162  //- Check size is within valid range (0 ... size).
163  inline void checkSize(const label size) const;
164 
165  //- Check index i is within valid range (0 ... size-1).
166  inline void checkIndex(const label i) const;
167 
168 
169  // Edit
170 
171  //- Dummy resize function
172  // needed to make FixedList consistent with List
173  inline void resize(const label);
174 
175  //- Dummy setSize function
176  // needed to make FixedList consistent with List
177  inline void setSize(const label);
178 
179  //- Copy (not transfer) the argument contents
180  // needed to make FixedList consistent with List
181  void transfer(const FixedList<T, Size>&);
182 
183  //- Write the FixedList as a dictionary entry
184  void writeEntry(Ostream&) const;
185 
186  //- Write the FixedList as a dictionary entry with keyword
187  void writeEntry(const word& keyword, Ostream&) const;
188 
189 
190  // Member operators
191 
192  //- Return element of FixedList.
193  inline T& operator[](const label);
194 
195  //- Return element of constant FixedList.
196  inline const T& operator[](const label) const;
197 
198  //- Assignment from array operator. Takes linear time.
199  inline void operator=(const T v[Size]);
200 
201  //- Assignment from UList operator. Takes linear time.
202  inline void operator=(const UList<T>&);
203 
204  //- Assignment from SLList operator. Takes linear time.
205  inline void operator=(const SLList<T>&);
206 
207  //- Assignment of all entries to the given value
208  inline void operator=(const T&);
209 
210 
211  // STL type definitions
212 
213  //- Type of values the FixedList contains.
214  typedef T value_type;
215 
216  //- Type that can be used for storing into
217  // FixedList::value_type objects.
218  typedef T& reference;
219 
220  //- Type that can be used for storing into
221  // constant FixedList::value_type objects
222  typedef const T& const_reference;
223 
224  //- The type that can represent the difference between any two
225  // FixedList iterator objects.
226  typedef label difference_type;
227 
228  //- The type that can represent the size of a FixedList.
229  typedef label size_type;
230 
231 
232  // STL iterator
233 
234  //- Random access iterator for traversing FixedList.
235  typedef T* iterator;
236 
237  //- Return an iterator to begin traversing the FixedList.
238  inline iterator begin();
239 
240  //- Return an iterator to end traversing the FixedList.
241  inline iterator end();
242 
243 
244  // STL const_iterator
245 
246  //- Random access iterator for traversing FixedList.
247  typedef const T* const_iterator;
248 
249  //- Return const_iterator to begin traversing the constant FixedList.
250  inline const_iterator cbegin() const;
251 
252  //- Return const_iterator to end traversing the constant FixedList.
253  inline const_iterator cend() const;
254 
255  //- Return const_iterator to begin traversing the constant FixedList.
256  inline const_iterator begin() const;
257 
258  //- Return const_iterator to end traversing the constant FixedList.
259  inline const_iterator end() const;
260 
261 
262  // STL reverse_iterator
263 
264  //- Reverse iterator for reverse traversal of FixedList.
265  typedef T* reverse_iterator;
266 
267  //- Return reverse_iterator to begin reverse traversing the FixedList.
268  inline reverse_iterator rbegin();
269 
270  //- Return reverse_iterator to end reverse traversing the FixedList.
271  inline reverse_iterator rend();
272 
273 
274  // STL const_reverse_iterator
275 
276  //- Reverse iterator for reverse traversal of constant FixedList.
277  typedef const T* const_reverse_iterator;
278 
279  //- Return const_reverse_iterator to begin reverse traversing FixedList.
280  inline const_reverse_iterator crbegin() const;
281 
282  //- Return const_reverse_iterator to end reverse traversing FixedList.
283  inline const_reverse_iterator crend() const;
284 
285  //- Return const_reverse_iterator to begin reverse traversing FixedList.
286  inline const_reverse_iterator rbegin() const;
287 
288  //- Return const_reverse_iterator to end reverse traversing FixedList.
289  inline const_reverse_iterator rend() const;
290 
291 
292  // STL member functions
293 
294  //- Return the number of elements in the FixedList.
295  inline label size() const;
296 
297  //- Return size of the largest possible FixedList.
298  inline label max_size() const;
299 
300  //- Return true if the FixedList is empty (ie, size() is zero).
301  inline bool empty() const;
302 
303  //- Swap two FixedLists of the same type in constant time.
304  void swap(FixedList<T, Size>&);
305 
306 
307  // STL member operators
308 
309  //- Equality operation on FixedLists of the same type.
310  // Returns true when the FixedLists are elementwise equal
311  // (using FixedList::value_type::operator==). Takes linear time.
312  bool operator==(const FixedList<T, Size>&) const;
313 
314  //- The opposite of the equality operation. Takes linear time.
315  bool operator!=(const FixedList<T, Size>&) const;
316 
317  //- Compare two FixedLists lexicographically. Takes linear time.
318  bool operator<(const FixedList<T, Size>&) const;
319 
320  //- Compare two FixedLists lexicographically. Takes linear time.
321  bool operator>(const FixedList<T, Size>&) const;
322 
323  //- Return true if !(a > b). Takes linear time.
324  bool operator<=(const FixedList<T, Size>&) const;
325 
326  //- Return true if !(a < b). Takes linear time.
327  bool operator>=(const FixedList<T, Size>&) const;
328 
329 
330  // IOstream operators
331 
332  //- Read List from Istream, discarding contents of existing List.
333  friend Istream& operator>> <T, Size>
335 
336  // Write FixedList to Ostream.
337  friend Ostream& operator<< <T, Size>
338  (
339  Ostream&,
340  const FixedList<T, Size>&
341  );
342 };
343 
344 
345 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
346 
347 } // End namespace Foam
348 
349 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
350 
351 #include "FixedListI.H"
352 
353 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
354 
355 #ifdef NoRepository
356 # include "FixedList.C"
357 #endif
358 
359 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
360 
361 #endif
362 
363 // ************************ vim: set sw=4 sts=4 et: ************************ //