DynList.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | cfMesh: A library for mesh generation
4  \\ / O peration |
5  \\ / A nd | Author: Franjo Juretic (franjo.juretic@c-fields.com)
6  \\/ M anipulation | Copyright (C) Creative Fields, Ltd.
7 -------------------------------------------------------------------------------
8 License
9  This file is part of cfMesh.
10 
11  cfMesh 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 3 of the License, or (at your
14  option) any later version.
15 
16  cfMesh 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 cfMesh. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  DynList
26 
27 Description
28  A dynamic list is a 1-D vector of objects of type T which resizes
29  itself as necessary to accept the new objects. Internal storage
30  is a compact array and the list can be shrunk to compact storage.
31  The increase of list size is controlled by three template parameters,
32  which allows the list storage to either increase by the given increment
33  or the given multiplier and divider (allowing non-integer multiples).
34 
35 SourceFiles
36  DynListI.H
37  DynList.C
38 
39 \*---------------------------------------------------------------------------*/
40 
41 #ifndef DynList_H
42 #define DynList_H
43 
44 #include "UList.H"
45 
46 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
47 
48 namespace Foam
49 {
50 
51 // * * * * * * Forward declaration of template friend fuctions * * * * * * * //
52 
53 template<class T, label staticSize>
54 class DynList;
55 
56 template<class T, label staticSize>
57 Ostream& operator<<
58 (
59  Ostream&,
61 );
62 template<class T, label staticSize>
63 Istream& operator>>
64 (
65  Istream&,
67 );
68 
69 
70 /*---------------------------------------------------------------------------*\
71  Class DynList Declaration
72 \*---------------------------------------------------------------------------*/
73 
74 template<class T, label staticSize = 16>
75 class DynList
76 {
77  // Private data
78  //- pointer to the data
79  T* dataPtr_;
80 
81  //- size of the allocated data
83 
84  //- statically allocated data (used for short lists)
85  T staticData_[staticSize];
86 
87  //- Number of next free element
89 
90  // Private member functions
91  //- access to the data pointer
92  inline T* data();
93 
94  //- const access to the data pointer
95  inline const T* data() const;
96 
97  //- allocate list size
98  inline void allocateSize(const label);
99 
100  //- check if index is inside the scope (used for debugging only)
101  inline void checkIndex(const label) const;
102 
103  //- check if nAllocated_ is greater or equal to nextFree_
104  inline void checkAllocation() const;
105 
106 public:
107 
108  // Constructors
109 
110  //- Construct null
111  inline DynList();
112 
113  //- Construct given size
114  explicit inline DynList(const label);
115 
116  //- Construct from given size and defualt value
117  explicit inline DynList(const label, const T&);
118 
119  //- Construct from UList. nextFree_ set to size().
120  explicit inline DynList(const UList<T>&);
121 
122  //- Construct from other ListType
123  template<class ListType>
124  inline DynList(const ListType&);
125 
126  //- Copy constructor
127  inline DynList(const DynList<T, staticSize>&);
128 
129  //- Construct from Istream. nextFree_ set to size().
130  explicit DynList(Istream&);
131 
132  // Destructor
133 
134  inline ~DynList();
135 
136 
137  // Member Functions
138 
139  // Access
140 
141  //- Size of the active part of the list.
142  //- Direct over-ride of list size member function
143  inline label size() const;
144 
145  //- Number of bytes used by the active part of the list
146  //- Direct over-ride of list byteSize member function
147  inline label byteSize() const;
148 
149 
150  // Edit
151 
152  //- Reset size of List.
153  void setSize(const label);
154 
155  //- Clear the list, i.e. set next free to zero.
156  // Allocated size does not change
157  void clear();
158 
159  //- Shrink the List<T> to the number of elements used
160  void shrink();
161 
162 
163  // Member Operators
164 
165  //- Append an element at the end of the list
166  inline void append(const T& e);
167 
168  //- Append an element at the end of the list if it is not yet
169  //- present in the list (takes linear time)
170  inline void appendIfNotIn(const T& e);
171 
172  //- check if the element is in the list (takes linear time)
173  inline bool contains(const T& e) const;
174  inline label containsAtPosition(const T& e) const;
175 
176  //- return a const reference to the last element
177  inline const T& lastElement() const;
178 
179  //- Return and remove the last element
180  inline T removeLastElement();
181  inline T removeElement(const label i);
182 
183  //- return a refence to the element. Resize the list if necessary
184  inline T& newElmt(const label);
185 
186  //- Return non-const access to an element,
187  //- resizing the list if necessary
188  inline T& operator()(const label);
189 
190  //- return access to an element
191  inline const T& operator[](const label) const;
192  inline T& operator[](const label);
193 
194  //- return forward and reverse circular indices
195  inline label fcIndex(const label index, const label offset = 1) const;
196  inline label rcIndex(const label index, const label offset = 1) const;
197 
198  //- return forward and reverse circular elements
199  inline const T& fcElement
200  (
201  const label index,
202  const label offset = 1
203  ) const;
204 
205  inline const T& rcElement
206  (
207  const label index,
208  const label offset = 1
209  ) const;
210 
211  //- Assignment of all entries to the given value
212  inline void operator=(const T&);
213 
214  //- Copy of another list
215  inline void operator=(const DynList<T, staticSize>&);
216 
217  //- Copy of another list type
218  template<class ListType>
219  inline void operator=(const ListType&);
220 
221  //- Compare the list with the another one
222  inline bool operator==(const DynList<T, staticSize>&) const;
223  inline bool operator!=(const DynList<T, staticSize>&) const;
224 
225 
226  // IOstream operators
227 
228  // Write DynList to Ostream.
229  friend Ostream& operator<< <T, staticSize>
230  (
231  Ostream&,
233  );
234 
235  //- Read from Istream, discarding contents of existing DynList.
236  friend Istream& operator>> <T, staticSize>
237  (
238  Istream&,
240  );
241 };
242 
243 
244 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
245 
246 } // End namespace Foam
247 
248 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
249 
250 #include "DynListI.H"
251 
252 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
253 
254 #ifdef NoRepository
255 # include "DynList.C"
256 #endif
257 
258 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
259 
260 #endif
261 
262 // ************************************************************************* //
Foam::DynList::newElmt
T & newElmt(const label)
return a refence to the element. Resize the list if necessary
Definition: DynListI.H:430
Foam::DynList::~DynList
~DynList()
Definition: DynListI.H:226
Foam::DynList::data
T * data()
access to the data pointer
Definition: DynListI.H:27
Foam::DynList::operator()
T & operator()(const label)
Definition: DynListI.H:442
Foam::DynList::removeElement
T removeElement(const label i)
Definition: DynListI.H:404
Foam::DynList::fcIndex
label fcIndex(const label index, const label offset=1) const
return forward and reverse circular indices
Definition: DynListI.H:486
Foam::DynList::operator!=
bool operator!=(const DynList< T, staticSize > &) const
Definition: DynListI.H:593
Foam::DynList::byteSize
label byteSize() const
Definition: DynListI.H:245
Foam::DynList::removeLastElement
T removeLastElement()
Return and remove the last element.
Definition: DynListI.H:384
Foam::DynList::shrink
void shrink()
Shrink the List<T> to the number of elements used.
Definition: DynListI.H:290
Foam::DynList::contains
bool contains(const T &e) const
check if the element is in the list (takes linear time)
Definition: DynListI.H:339
Foam::DynList::DynList
DynList()
Construct null.
Definition: DynListI.H:120
Foam::DynList::rcElement
const T & rcElement(const label index, const label offset=1) const
Definition: DynListI.H:516
Foam::DynList::allocateSize
void allocateSize(const label)
allocate list size
Definition: DynListI.H:39
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::DynList::operator==
bool operator==(const DynList< T, staticSize > &) const
Compare the list with the another one.
Definition: DynListI.H:577
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:57
Foam::DynList::operator=
void operator=(const T &)
Assignment of all entries to the given value.
Definition: DynListI.H:525
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:55
Foam::DynList::appendIfNotIn
void appendIfNotIn(const T &e)
Definition: DynListI.H:324
Foam::DynList::nextFree_
label nextFree_
Number of next free element.
Definition: DynList.H:87
Foam::DynList::staticData_
T staticData_[staticSize]
statically allocated data (used for short lists)
Definition: DynList.H:84
DynList.C
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::e
const double e
Elementary charge.
Definition: doubleFloat.H:94
Foam::DynList
Definition: DynList.H:53
Foam::DynList::checkIndex
void checkIndex(const label) const
check if index is inside the scope (used for debugging only)
Definition: DynListI.H:90
Foam::DynList::nAllocated_
label nAllocated_
size of the allocated data
Definition: DynList.H:81
Foam::DynList::rcIndex
label rcIndex(const label index, const label offset=1) const
Definition: DynListI.H:496
Foam::DynList::containsAtPosition
label containsAtPosition(const T &e) const
Definition: DynListI.H:356
UList.H
Foam::DynList::lastElement
const T & lastElement() const
return a const reference to the last element
Definition: DynListI.H:374
Foam::DynList::setSize
void setSize(const label)
Reset size of List.
Definition: DynListI.H:263
DynListI.H
Foam::UList< T >
Foam::DynList::checkAllocation
void checkAllocation() const
check if nAllocated_ is greater or equal to nextFree_
Definition: DynListI.H:104
Foam::DynList::fcElement
const T & fcElement(const label index, const label offset=1) const
return forward and reverse circular elements
Definition: DynListI.H:506
Foam::DynList::dataPtr_
T * dataPtr_
pointer to the data
Definition: DynList.H:78
Foam::DynList::size
label size() const
Definition: DynListI.H:235
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::DynList::operator[]
const T & operator[](const label) const
return access to an element
Definition: DynListI.H:463
Foam::DynList::clear
void clear()
Clear the list, i.e. set next free to zero.
Definition: DynListI.H:279
Foam::DynList::append
void append(const T &e)
Append an element at the end of the list.
Definition: DynListI.H:304