LongList.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  LongList
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 2-D graph with a fixed size of the chunks used to store the data.
31  This way the data does not get copied every time array is resized, but
32  only the pointers to the chunks of data.
33 
34 SourceFiles
35  LongListI.H
36  LongList.C
37 
38 \*---------------------------------------------------------------------------*/
39 
40 #ifndef LongList_H
41 #define LongList_H
42 
43 #include "label.H"
44 #include "bool.H"
45 #include "IOstreams.H"
46 #include "error.H"
47 
48 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49 
50 namespace Foam
51 {
52 
53 // * * * * * * Forward declaration of template friend fuctions * * * * * * * //
54 
55 template<class T, label Offset>
56 class LongList;
57 
58 template<class T, label Offset>
59 Ostream& operator<<
60 (
61  Ostream&,
62  const LongList<T, Offset>&
63 );
64 template<class T, label Offset>
65 Istream& operator>>
66 (
67  Istream&,
69 );
70 
71 /*---------------------------------------------------------------------------*\
72  Class LongList Declaration
73 \*---------------------------------------------------------------------------*/
74 
75 template<class T, label Offset = 19>
76 class LongList
77 {
78  // Private data
79  //- number of allocated elements
80  label N_;
81 
82  //- number of elements in the list
84 
85  //- number of used blocks of data
87 
88  //- maximum number of blocks that can be allocated
89  //- without reallocating the list containing pointers
90  //- to the chunks of data
92 
93  //- size of blocks is calculated by powers of 2
94  //- and therefore the access can be done using shift and mask
96  label mask_;
97 
98  //- array of pointers to the blocks of data, each of the size WIDTH
99  T** dataPtr_;
100 
101  // Private member functions
102  //- check index
103  void checkIndex(const label i) const;
104 
105  //- initialize width and mask
106  void initializeParameters();
107 
108  //- Allocate memory for the list
109  void allocateSize(const label);
110 
111  //- delete all elements
112  void clearOut();
113 
114 public:
115 
116  // Constructors
117 
118  //- Construct null
119  inline LongList();
120 
121  //- Construct given size
122  explicit inline LongList(const label size);
123 
124  //- Construct to given size and initialize
125  explicit inline LongList(const label size, const T& t);
126 
127  //- Copy contructor
128  inline LongList(const LongList<T, Offset>&);
129 
130  // Destructor
131 
132  inline ~LongList();
133 
134  // Member Functions
135 
136  // Access
137 
138  //- Size of the active part of the list.
139  inline label size() const;
140 
141  //- Return the binary size in number of characters of the UList
142  // if the element is a primitive type
143  // i.e. contiguous<T>() == true
144  inline label byteSize() const;
145 
146  // Edit
147 
148  //- Reset size of List.
149  void setSize(const label);
150 
151  //- Clear the list, i.e. set next free to zero.
152  // Allocated size does not change
153  void clear();
154 
155  //- Shrink the list to the number of elements used
156  inline LongList<T, Offset>& shrink();
157 
158  //- transfer the list from another one without allocating it
159  inline void transfer(LongList<T, Offset>&);
160 
161 
162  // Member Operators
163 
164  //- Append an element at the end of the list
165  inline void append(const T& e);
166 
167  //- Append an element at the end of the list if it is not yet
168  //- present in the list (takes linear time)
169  inline void appendIfNotIn(const T& e);
170 
171  //- check if the element is in the list (takes linear time)
172  inline bool contains(const T& e) const;
173  inline label containsAtPosition(const T& e) const;
174 
175  //- Return and remove the element
176  inline T remove(const label i);
177  inline T removeLastElement();
178 
179  //- get and set operators
180  inline const T& operator[](const label i) const;
181  inline T& operator[](const label i);
182 
183  //- Return non-const access to an element,
184  // resizing the list if necessary
185  inline T& operator()(const label);
186 
187  //- return a non-const access to an element,
188  // resize the list if necessary
189  inline T& newElmt(const label);
190 
191  //- Assignment of all entries to the given value
192  inline void operator=(const T&);
193 
194  //- Assignment operator
195  inline void operator=(const LongList<T, Offset>&);
196 
197 
198  // IOstream operators
199  //- Read from stream and append to the current content
200  void appendFromStream(Istream&);
201 
202  //- Write as a dictionary entry.
203  void writeEntry(Ostream& os) const;
204 
205  //- Write as a dictionary entry with keyword.
206  void writeEntry(const word& keyword, Ostream& os) const;
207 
208  // Write LongList to Ostream.
209  friend Ostream& operator<< <T, Offset>
210  (
211  Ostream&,
212  const LongList<T, Offset>&
213  );
214 
215  //- Read from Istream, discarding contents of existing LongList.
216  friend Istream& operator>> <T, Offset>
217  (
218  Istream&,
220  );
221 };
222 
223 
224 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
225 
226 } // End namespace Foam
227 
228 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
229 
230 #include "LongListI.H"
231 
232 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
233 
234 #ifdef NoRepository
235 # include "LongList.C"
236 #endif
237 
238 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
239 
240 #endif
241 
242 // ************************************************************************* //
Foam::LongList::appendFromStream
void appendFromStream(Istream &)
Read from stream and append to the current content.
Definition: LongList.C:275
Foam::LongList::newElmt
T & newElmt(const label)
return a non-const access to an element,
Definition: LongListI.H:372
Foam::LongList::initializeParameters
void initializeParameters()
initialize width and mask
Definition: LongListI.H:43
Foam::LongList::append
void append(const T &e)
Append an element at the end of the list.
Definition: LongListI.H:265
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::LongList::operator()
T & operator()(const label)
Return non-const access to an element,.
Definition: LongListI.H:362
Foam::LongList::clear
void clear()
Clear the list, i.e. set next free to zero.
Definition: LongListI.H:230
Foam::LongList::shrink
LongList< T, Offset > & shrink()
Shrink the list to the number of elements used.
Definition: LongListI.H:238
Foam::LongList::clearOut
void clearOut()
delete all elements
Definition: LongListI.H:112
Foam::LongList::containsAtPosition
label containsAtPosition(const T &e) const
Definition: LongListI.H:294
Foam::LongList::shift_
label shift_
Definition: LongList.H:94
Foam::LongList::size
label size() const
Size of the active part of the list.
Definition: LongListI.H:203
Foam::LongList::checkIndex
void checkIndex(const label i) const
check index
Definition: LongListI.H:29
Foam::LongList::nextFree_
label nextFree_
number of elements in the list
Definition: LongList.H:82
Foam::LongList::setSize
void setSize(const label)
Reset size of List.
Definition: LongListI.H:223
Foam::LongList
Definition: LongList.H:55
Foam::LongList::N_
label N_
number of allocated elements
Definition: LongList.H:79
LongListI.H
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
error.H
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:57
Foam::LongList::writeEntry
void writeEntry(Ostream &os) const
Write as a dictionary entry.
Definition: LongList.C:33
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:55
Foam::LongList::transfer
void transfer(LongList< T, Offset > &)
transfer the list from another one without allocating it
Definition: LongListI.H:245
Foam::LongList::~LongList
~LongList()
Definition: LongListI.H:195
Foam::LongList::operator[]
const T & operator[](const label i) const
get and set operators
Definition: LongListI.H:342
bool.H
System bool.
Foam::LongList::LongList
LongList()
Construct null.
Definition: LongListI.H:133
Foam::LongList::operator=
void operator=(const T &)
Assignment of all entries to the given value.
Definition: LongListI.H:378
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::e
const double e
Elementary charge.
Definition: doubleFloat.H:94
Foam::LongList::numBlocks_
label numBlocks_
number of used blocks of data
Definition: LongList.H:85
Foam::LongList::remove
T remove(const label i)
Return and remove the element.
Definition: LongListI.H:306
Foam::LongList::mask_
label mask_
Definition: LongList.H:95
Foam::LongList::appendIfNotIn
void appendIfNotIn(const T &e)
Definition: LongListI.H:276
Foam::LongList::byteSize
label byteSize() const
Return the binary size in number of characters of the UList.
Definition: LongListI.H:209
label.H
Foam::LongList::contains
bool contains(const T &e) const
check if the element is in the list (takes linear time)
Definition: LongListI.H:283
Foam::LongList::allocateSize
void allocateSize(const label)
Allocate memory for the list.
Definition: LongListI.H:60
Foam::LongList::dataPtr_
T ** dataPtr_
array of pointers to the blocks of data, each of the size WIDTH
Definition: LongList.H:98
Foam::LongList::numAllocatedBlocks_
label numAllocatedBlocks_
Definition: LongList.H:90
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::LongList::removeLastElement
T removeLastElement()
Definition: LongListI.H:323
LongList.C