NeoPZ
pzadmchunk.h
Go to the documentation of this file.
1 
6 #ifndef PZADMCHUNK_H
7 #define PZADMCHUNK_H
8 
9 #include "TPZChunkVector.h"
10 #include "pzstack.h"
11 #include "pzerror.h"
12 
13 class TPZSavable;
14 
25 template <class T, int EXP = 10 >
26 class TPZAdmChunkVector : public TPZChunkVector<T, EXP> {
27 public:
28 
30  NEVER = 0, NOW = 1, ALWAYS = 2
31  };
32 
42 
48 
59  TPZAdmChunkVector(int numberofchunks = DEFAULTNUMBEROFCHUNKS);
60 
62  virtual ~TPZAdmChunkVector();
63 
74  int AllocateNewElement();
75 
81  void SetFree(int index);
82 
87  inline int64_t NFreeElements() const{
88  return fFree.NElements();
89  }
90 
101  void CompactDataStructure(CompactScheme type = CompactScheme::ALWAYS );
102 
104  inline int PrintFree(int i) {
105  return fFree[i];
106  }
107 
112  void Resize(const int newsize);
113 
114  int ClassId() const override {
115  return Hash("TPZAdmChunkVector") ^ TPZChunkVector<T, EXP>::ClassId() << 1;
116  }
117 
118  void Read(TPZStream& buf, void* context) override{
119  uint64_t c, nc;
120  buf.Read(&nc, 1);
121  this->Resize(nc);
122  for (c = 0; c < nc; c++){
123  ReadInternal(this->operator [](c), buf, context);
124  }
125  int compactScheme;
126  buf.Read(&compactScheme, 1);
128  buf.Read(this->fFree);
129  buf.Read(this->fNFree);
130  }
131 
132  void Write(TPZStream& buf, int withclassid) const override{
133  uint64_t c, nc = this->NElements();
134  buf.Write(&nc);
135  for (c = 0; c < nc; c++){
136  WriteInternal(this->operator [](c), buf, withclassid);
137  }
138  int val = as_integer( this->fCompactScheme );
139  buf.Write(&val);
140  buf.Write(this->fFree);
141  buf.Write(this->fNFree);
142  }
143 
144 private:
145 
146  friend class TPZStream;
147  friend class TPZGeoMesh;
148 
154 
157 
160 };
161 
162 //--| IMPLEMENTATION |----------------------------------------------------------
163 
164 template< class T, int EXP>
166 : TPZChunkVector<T, EXP>(numberofchunks),
167 fCompactScheme(NEVER), // never compact the data structure
168 fNFree(numberofchunks),
169 fFree() {
170  for (int i = 0; i < numberofchunks; i++) {
171  fNFree[i] = 0;
172  }
173 
174  fNFree.Resize(0);
175 }
176 
177 template< class T, int EXP >
179 }
180 
181 // Return the index of a free element
182 
183 template< class T, int EXP >
185  if (fFree.NElements() > 0) {
186  int index = fFree.Pop();
187  int chunk = index >> EXP;
188  fNFree[chunk]--;
189  return index;
190  }
191 
192  Resize(this->NElements() + 1);
193 
194  return this->NElements() - 1;
195 }
196 
197 // Indicate an element as free
198 template< class T, int EXP >
200 #ifndef NODEBUG
201  if (index < 0) {
202  PZError << "TPZAdmChunkVector::SetFree. Bad parameter index." << std::endl;
203  PZError.flush();
204  return;
205  }
206 #endif
207 
208  int chunk = index >> EXP;
209 
210  fNFree[chunk]++;
211  fFree.Push(index);
212 
213  if (fCompactScheme == ALWAYS) {
215 }
216 }
217 
218 // Let to compact the data structure.
219 // If type=0 never compact, (type=1 let to compact now),(type=2 let to compact always)
220 
221 template< class T, int EXP >
223  switch (type) {
224  case NEVER:
226  return;
227  case ALWAYS:
229  case NOW:
230  {
231  int chunksize = 1 << EXP;
232  int nchunksused = 0;
233  if (this->NElements()) nchunksused = ((this->NElements() - 1) >> EXP) + 1;
234  int i = nchunksused - 1;
235  int maxfree = this->NElements()-((nchunksused - 1) << EXP);
236 
237  if (i >= 0 && this->fVec[i] && fNFree[i] == maxfree) {
238  Resize(chunksize * i);
239  --i;
240  while (i >= 0 && this->fVec[i] && fNFree[i] == chunksize) {
241  Resize(chunksize * i);
242  --i;
243  }
244  }
245  this->fVec.Shrink();
246  fNFree.Shrink();
247  fFree.Shrink();
248  break;
249  }
250  default:
251  PZError << "TPZAdmChunkVector::CompactDataStructure. Bad parameter type."
252  << std::endl;
253  PZError.flush();
254  }
255 }
256 
257 template < class T, int EXP >
259 TPZChunkVector<T, EXP>(AdmCh), fCompactScheme(AdmCh.fCompactScheme),
260 fNFree(AdmCh.fNFree), fFree(AdmCh.fFree) {
261  // NOTHING TO DO HERE!
262 }
263 
264 template < class T, int EXP>
266  const TPZAdmChunkVector<T, EXP> &AdmCh) {
267  if (this == &AdmCh)
268  return *this;
269 
271 
272  fFree = AdmCh.fFree;
273  fNFree = AdmCh.fNFree;
275 
276  return *this;
277 }
278 
279 template< class T, int EXP >
280 void TPZAdmChunkVector<T, EXP>::Resize(const int newsize) {
281 #ifndef NODEBUG
282  if (newsize < 0) {
283  PZError << "TPZAdmChunkVector::Resize. Bad parameter newsize." << std::endl;
284  PZError.flush();
285  return;
286  }
287 #endif
288 
290 
291  // int sizechunk = 1 << EXP;
292  int nchunks = fNFree.NElements();
293  int chunksneeded = this->fVec.NElements(); // equivalent to newsize>>fExponent??
294 
295  fNFree.Resize(chunksneeded);
296 
297  for (int i = nchunks; i < chunksneeded; i++) {
298  fNFree[i] = 0;
299  }
300 
301  if (chunksneeded > nchunks) return;
302 
303  // delete all free indexes which are above the new size
304  // update the number of free elements of the last chunk
305  TPZStack<int> temp(fFree);
306  temp.Resize(0);
307 
308  while (fFree.NElements() > 0) {
309  int index = fFree.Pop();
310  if (index < newsize)
311  temp.Push(index);
312  else {
313  int chunk = index >> EXP;
314 
315  if (chunk == chunksneeded - 1)
316  fNFree[chunksneeded - 1]--;
317  }
318  }
319 
320  fFree = temp;
321 }
322 
323 #endif // PZADMCHUNK_H
324 
325 //--| PZ |----------------------------------------------------------------------
#define DEFAULTNUMBEROFCHUNKS
Default number of elements which will be allocated in the chunk vector.
int AllocateNewElement()
Makes more room for new elements.
Definition: pzadmchunk.h:184
void WriteInternal(const T &input, TPZStream &buf, int withclassid)
TPZAdmChunkVector(const TPZAdmChunkVector< T, EXP > &AdmCh)
Copy constructor.
Definition: pzadmchunk.h:258
Defines PZError.
std::underlying_type< Enumeration >::type as_integer(const Enumeration value)
Definition: pzreal.h:37
int PrintFree(int i)
Print index i into the fFree vector.
Definition: pzadmchunk.h:104
int64_t NElements() const
Access method to query the number of elements of the vector.
REAL val(STATE &number)
Returns value of the variable.
Definition: pzartdiff.h:23
Implements a chunk vector with free store administration. Utility.
Definition: TPZStream.h:39
void Resize(const int64_t newsize)
Increase the size of the chunk vector.
virtual void Resize(const int64_t newsize, const T &object)
Resizes the vector object.
Definition: pzmanvector.h:426
TPZManVector< T * > fVec
Vector which points to each chunk of objects.
CompactScheme fCompactScheme
Internal variable indicating the type of compacting scheme.
Definition: pzadmchunk.h:153
void Resize(const int newsize)
Increase the size of the chunk vector.
Definition: pzadmchunk.h:280
void Push(const T object)
Pushes a copy of the object on the stack.
Definition: pzstack.h:80
virtual void Write(const bool val)
Definition: TPZStream.cpp:8
int64_t NFreeElements() const
Access method to return the number of free elements.
Definition: pzadmchunk.h:87
void ReadInternal(T &output, TPZStream &buf, void *context)
An object of this class implements a vector which allocates objects by chunks. Utility.
TPZManVector< int > fNFree
Number of free elements within each chunk.
Definition: pzadmchunk.h:156
void CompactDataStructure(CompactScheme type=CompactScheme::ALWAYS)
Sets the method to compact the data structure based on the.
Definition: pzadmchunk.h:222
int ClassId() const override
Define the class id associated with the class.
Definition: pzadmchunk.h:114
void Shrink()
It reallocates storage to fit the necessary storage exactly.
Definition: pzmanvector.h:388
TPZChunkVector< T, EXP > & operator=(const TPZChunkVector< T, EXP > &TCh)
Assignment operator, copies all elements from the object TCh.
A simple stack.
int32_t Hash(std::string str)
Definition: TPZHash.cpp:10
virtual ~TPZAdmChunkVector()
Destructor.
Definition: pzadmchunk.h:178
T Pop()
Retrieve an object from the stack.
Definition: pzstack.h:87
TPZAdmChunkVector< T, EXP > & operator=(const TPZAdmChunkVector< T, EXP > &TPZAdmCh)
Assignment operator.
Definition: pzadmchunk.h:265
Free store vector implementation in chunks.
This class implements a geometric mesh for the pz environment. Geometry.
Definition: pzgmesh.h:48
TPZStack< int > fFree
List of indexes of freed elements.
Definition: pzadmchunk.h:159
void SetFree(int index)
Indicate an element as free.
Definition: pzadmchunk.h:199
Defines the interface for saving and reading data. Persistency.
Definition: TPZStream.h:50
int64_t NElements() const
Returns the number of elements of the vector.
Definition: pzvec.h:190
void Read(TPZStream &buf, void *context) override
read objects from the stream
Definition: pzadmchunk.h:118
This class defines the interface to save and restore objects from TPZStream objects. Persistency.
Definition: TPZSavable.h:67
int ClassId() const override
Define the class id associated with the class.
#define PZError
Defines the output device to error messages and the DebugStop() function.
Definition: pzerror.h:15
virtual void Read(bool &val)
Definition: TPZStream.cpp:91
void Write(TPZStream &buf, int withclassid) const override
Writes this object to the TPZStream buffer. Include the classid if withclassid = true.
Definition: pzadmchunk.h:132