initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / containers / Lists / FixedList / FixedList.H
blob76f9527701d4eec378553f1f4767572be282055a
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
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.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Class
26     Foam::FixedList
28 Description
29     A 1D vector of objects of type \<T\> with a fixed size \<Size\>.
31 SourceFiles
32     FixedList.C
33     FixedListI.H
34     FixedListIO.C
36 \*---------------------------------------------------------------------------*/
38 #ifndef FixedList_H
39 #define FixedList_H
41 #include "bool.H"
42 #include "label.H"
43 #include "uLabel.H"
44 #include "Hash.H"
45 #include "autoPtr.H"
46 #include "StaticAssert.H"
48 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
50 namespace Foam
53 // Forward declaration of friend functions and operators
55 template<class T, unsigned Size> class FixedList;
57 template<class T, unsigned Size>
58 Istream& operator>>(Istream&, FixedList<T, Size>&);
60 template<class T, unsigned Size>
61 Ostream& operator<<(Ostream&, const FixedList<T, Size>&);
63 template<class T> class UList;
64 template<class T> class SLList;
67 /*---------------------------------------------------------------------------*\
68                            Class FixedList Declaration
69 \*---------------------------------------------------------------------------*/
71 template<class T, unsigned Size>
72 class FixedList
74     //- Size must be positive (non-zero) and also fit as a signed value
75     StaticAssert(Size && Size <= INT_MAX);
77     // Private data
79         //- Vector of values of type T of size Size.
80         T v_[Size];
83 public:
85     //- Hashing function class.
86     //  Use Hasher directly for contiguous data. Otherwise hash incrementally.
87     template< class HashT=Hash<T> >
88     class Hash
89     {
90     public:
91         Hash()
92         {}
94         inline unsigned operator()
95         (
96             const FixedList<T, Size>&,
97             unsigned seed = 0
98         ) const;
99     };
101     // Static Member Functions
103         //- Return a null FixedList
104         inline static const FixedList<T, Size>& null();
107     // Constructors
109         //- Null constructor.
110         inline FixedList();
112         //- Construct from components
113         inline FixedList(const T v[Size]);
115         //- Construct from value
116         inline FixedList(const T&);
118         //- Construct from UList.
119         inline FixedList(const UList<T>&);
121         //- Construct from SLList.
122         inline FixedList(const SLList<T>&);
124         //- Copy constructor.
125         inline FixedList(const FixedList<T, Size>&);
127         //- Construct from Istream.
128         FixedList(Istream&);
130         //- Clone
131         inline autoPtr< FixedList<T, Size> > clone() const;
134     // Member Functions
136         // Access
138             //- Return the forward circular index, i.e. the next index
139             //  which returns to the first at the end of the list
140             inline label fcIndex(const label i) const;
142             //- Return the reverse circular index, i.e. the previous index
143             //  which returns to the last at the beginning of the list
144             inline label rcIndex(const label i) const;
147             //- Return a const pointer to the first data element,
148             //  similar to the STL front() method and the string::data() method
149             //  This can be used (with caution) when interfacing with C code.
150             inline const T* cdata() const;
152             //- Return a pointer to the first data element,
153             //  similar to the STL front() method and the string::data() method
154             //  This can be used (with caution) when interfacing with C code.
155             inline T* data();
158         // Check
160             //- Check start is within valid range (0 ... size-1).
161             inline void checkStart(const label start) const;
163             //- Check size is within valid range (0 ... size).
164             inline void checkSize(const label size) const;
166             //- Check index i is within valid range (0 ... size-1).
167             inline void checkIndex(const label i) const;
170         // Edit
172             //- Dummy resize function
173             //  needed to make FixedList consistent with List
174             inline void resize(const label);
176             //- Dummy setSize function
177             //  needed to make FixedList consistent with List
178             inline void setSize(const label);
180             //- Copy (not transfer) the argument contents
181             //  needed to make FixedList consistent with List
182             void transfer(const FixedList<T, Size>&);
184         //- Write the FixedList as a dictionary entry
185         void writeEntry(Ostream&) const;
187         //- Write the FixedList as a dictionary entry with keyword
188         void writeEntry(const word& keyword, Ostream&) const;
191     // Member operators
193         //- Return element of FixedList.
194         inline T& operator[](const label);
196         //- Return element of constant FixedList.
197         inline const T& operator[](const label) const;
199         //- Assignment from array operator. Takes linear time.
200         inline void operator=(const T v[Size]);
202         //- Assignment from UList operator. Takes linear time.
203         inline void operator=(const UList<T>&);
205         //- Assignment from SLList operator. Takes linear time.
206         inline void operator=(const SLList<T>&);
208         //- Assignment of all entries to the given value
209         inline void operator=(const T&);
212     // STL type definitions
214         //- Type of values the FixedList contains.
215         typedef T value_type;
217         //- Type that can be used for storing into
218         //  FixedList::value_type objects.
219         typedef T& reference;
221         //- Type that can be used for storing into
222         //  constant FixedList::value_type objects
223         typedef const T& const_reference;
225         //- The type that can represent the difference between any two
226         //  FixedList iterator objects.
227         typedef label difference_type;
229         //- The type that can represent the size of a FixedList.
230         typedef label size_type;
233     // STL iterator
235         //- Random access iterator for traversing FixedList.
236         typedef T* iterator;
238         //- Return an iterator to begin traversing the FixedList.
239         inline iterator begin();
241         //- Return an iterator to end traversing the FixedList.
242         inline iterator end();
245     // STL const_iterator
247         //- Random access iterator for traversing FixedList.
248         typedef const T* const_iterator;
250         //- Return const_iterator to begin traversing the constant FixedList.
251         inline const_iterator cbegin() const;
253         //- Return const_iterator to end traversing the constant FixedList.
254         inline const_iterator cend() const;
256         //- Return const_iterator to begin traversing the constant FixedList.
257         inline const_iterator begin() const;
259         //- Return const_iterator to end traversing the constant FixedList.
260         inline const_iterator end() const;
263     // STL reverse_iterator
265         //- Reverse iterator for reverse traversal of FixedList.
266         typedef T* reverse_iterator;
268         //- Return reverse_iterator to begin reverse traversing the FixedList.
269         inline reverse_iterator rbegin();
271         //- Return reverse_iterator to end reverse traversing the FixedList.
272         inline reverse_iterator rend();
275     // STL const_reverse_iterator
277         //- Reverse iterator for reverse traversal of constant FixedList.
278         typedef const T* const_reverse_iterator;
280         //- Return const_reverse_iterator to begin reverse traversing FixedList.
281         inline const_reverse_iterator crbegin() const;
283         //- Return const_reverse_iterator to end reverse traversing FixedList.
284         inline const_reverse_iterator crend() const;
286         //- Return const_reverse_iterator to begin reverse traversing FixedList.
287         inline const_reverse_iterator rbegin() const;
289         //- Return const_reverse_iterator to end reverse traversing FixedList.
290         inline const_reverse_iterator rend() const;
293     // STL member functions
295         //- Return the number of elements in the FixedList.
296         inline label size() const;
298         //- Return size of the largest possible FixedList.
299         inline label max_size() const;
301         //- Return true if the FixedList is empty (ie, size() is zero).
302         inline bool empty() const;
304         //- Swap two FixedLists of the same type in constant time.
305         void swap(FixedList<T, Size>&);
308     // STL member operators
310         //- Equality operation on FixedLists of the same type.
311         //  Returns true when the FixedLists are elementwise equal
312         //  (using FixedList::value_type::operator==).  Takes linear time.
313         bool operator==(const FixedList<T, Size>&) const;
315         //- The opposite of the equality operation. Takes linear time.
316         bool operator!=(const FixedList<T, Size>&) const;
318         //- Compare two FixedLists lexicographically. Takes linear time.
319         bool operator<(const FixedList<T, Size>&) const;
321         //- Compare two FixedLists lexicographically. Takes linear time.
322         bool operator>(const FixedList<T, Size>&) const;
324         //- Return true if !(a > b). Takes linear time.
325         bool operator<=(const FixedList<T, Size>&) const;
327         //- Return true if !(a < b). Takes linear time.
328         bool operator>=(const FixedList<T, Size>&) const;
331     // IOstream operators
333         //- Read List from Istream, discarding contents of existing List.
334         friend Istream& operator>> <T, Size>
335         (Istream&, FixedList<T, Size>&);
337         // Write FixedList to Ostream.
338         friend Ostream& operator<< <T, Size>
339         (
340             Ostream&,
341             const FixedList<T, Size>&
342         );
346 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
348 } // End namespace Foam
350 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
352 #include "FixedListI.H"
354 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
356 #ifdef NoRepository
357 #   include "FixedList.C"
358 #endif
360 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
362 #endif
364 // ************************************************************************* //