13 #include <type_traits> 23 #define DEFAULTNUMBEROFCHUNKS 100 29 #define DEFAULTCHUNKEXPONENT 10 31 template<
class T,
int EXP>
34 template<
bool is_const,
class T,
int EXP = DEFAULTCHUNKEXPONENT>
39 typedef typename std::conditional<is_const, const T&, T&>::type
reference;
40 typedef typename std::conditional<is_const, const T*, T*>::type
pointer;
47 inline operator bool()
const {
48 return (
chunkVector && cur_index < chunkVector->NElements());
56 return ! (*
this == other);
108 return std::distance(other.
cur_index, this->cur_index);
126 TPZChunkVectorIterator(vectype *chunkVector,
typename vectype::size_type cur_index) : chunkVector(chunkVector), cur_index(cur_index) {
141 template<typename T, typename std::enable_if<std::is_pointer<T>::value,
int>::type * =
nullptr>
146 template<typename T, typename std::enable_if<!std::is_pointer<T>::value && !
is_arithmetic_pz<T>::value,
int>::type * =
nullptr>
148 output.Read(buf, context);
150 template<typename T, typename std::enable_if<!std::is_pointer<T>::value &&
is_arithmetic_pz<T>::value,
int>::type * =
nullptr>
152 buf.
Read(&output, 1);
155 template<typename T, typename std::enable_if<std::is_pointer<T>::value,
int>::type * =
nullptr>
160 template<typename T, typename std::enable_if<!std::is_pointer<T>::value && !
is_arithmetic_pz<T>::value,
int>::type * =
nullptr>
162 input.Write(buf, withclassid);
165 template<typename T, typename std::enable_if<!std::is_pointer<T>::value &&
is_arithmetic_pz<T>::value,
int>::type * =
nullptr>
175 template<
class T,
int EXP = DEFAULTCHUNKEXPONENT>
209 return iterator(
this, 0);
212 inline const_iterator
begin()
const {
213 return const_iterator(
this, 0);
217 return const_iterator(
this, 0);
221 return iterator(
this, NElements());
224 inline const_iterator
end()
const {
225 return const_iterator(
this, NElements());
228 inline const_iterator
cend()
const {
229 return const_iterator(
this, NElements());
245 void Resize(
const int64_t newsize);
254 T &operator[](
const int64_t nelem)
const;
257 int64_t FindObject(T *
object);
260 return Hash(
"TPZChunkVector") ^ ClassIdOrHash<T>() << 1 ^ (EXP << 2);
266 this->Resize(nObjects);
267 for (uint64_t i = 0; i < nObjects; ++i) {
273 uint64_t nObjects = this->NElements();
274 buf.
Write(&nObjects);
275 for (uint64_t i = 0; i < nObjects; i++) {
291 template<
class T,
int EXP >
293 fVec(numberofchunks) {
296 for (int64_t i = 0; i < numberofchunks; i++) {
303 template<
class T,
int EXP >
307 for (int64_t i = 0; i < nchunks; i++) {
314 template<
class T,
int EXP>
318 PZError <<
"TPZChunkVector::Resize. Bad parameter newsize." << std::endl;
327 int64_t chunksneeded = ((newsize - 1) >> EXP) + 1;
328 if (chunksneeded == nchunks)
return;
332 if (chunksneeded > nchunks)
335 const int64_t sizechunk = 1 << EXP;
337 for (i = 0; i < chunksneeded; ++i) {
339 fVec[i] =
new T[sizechunk];
342 for (; i < nchunks; ++i) {
352 template<
class T,
int EXP>
358 int64_t nelch = 1 << EXP;
359 for (ich = 0; ich < nch; ich++) {
360 if (
fVec[ich] == 0)
continue;
361 if (obj >=
fVec[ich] && obj <
fVec[ich] + nelch) {
362 return index + (obj -
fVec[ich]);
367 if (ich == nch)
return -1;
373 template<
class T,
int EXP >
377 PZError <<
"TPZChunkVector::operator[]. " 378 <<
"Bad parameter nelem." << nelem <<
" NElements " 387 const int64_t mask = (1 << EXP) - 1;
389 return (
fVec[nelem >> EXP])[nelem & mask];
392 template<
class T,
int EXP >
399 for (int64_t i = 0; i < nchunks; i++) {
403 int64_t j, k = 1 << EXP;
404 T* ptr =
new T[1 << EXP];
406 T* ptrcp = TCh.
fVec[i];
407 for (j = 0; j < k; j++) {
414 template <
class T,
int EXP >
416 if (
this == &TCh)
return *
this;
420 for (i = sz; i < prvsz; i++)
428 int64_t nchunks = sz;
430 for (i = 0; i < nchunks; i++) {
434 int64_t j, k = 1 << EXP;
435 fVec[i] =
new T[1 << EXP];
437 for (j = 0; j < k; j++)
pointer operator->() const
#define DEFAULTNUMBEROFCHUNKS
Default number of elements which will be allocated in the chunk vector.
TPZChunkVectorIterator(vectype *chunkVector, typename vectype::size_type cur_index)
TPZChunkVectorIterator< is_const, T, EXP > operator--(int)
void Read(TPZStream &buf, void *context) override
read objects from the stream
Contains declaration of the TPZSavable class which defines the interface to save and restore objects ...
virtual ~TPZChunkVector()
Destructor.
std::conditional< is_const, const TPZChunkVector< T, EXP >, TPZChunkVector< T, EXP > >::type vectype
Implements a vector class which allows to use external storage provided by the user. Utility.
TPZChunkVectorIterator< is_const, T, EXP > & operator++()
T & operator[](const int64_t nelem) const
Returns a reference to the ith element of the vector.
void Write(TPZStream &buf, int withclassid) const override
Writes this object to the TPZStream buffer. Include the classid if withclassid = true.
TPZChunkVectorIterator< is_const, T, EXP > operator+(const ptrdiff_t &movement) const
void WriteInternal(const T &input, TPZStream &buf, int withclassid)
const_iterator cend() const
reference operator*() const
TPZChunkVectorIterator< is_const, T, EXP > operator++(int)
clarg::argString input("-if", "input file", "cube1.txt")
int64_t NElements() const
Access method to query the number of elements of the vector.
const value_type * const_pointer
const_iterator begin() const
static TPZSavable * GetInstance(const int64_t &objId)
void Resize(const int64_t newsize)
Increase the size of the chunk vector.
int64_t FindObject(T *object)
Finds the index of an object by its pointer.
int64_t fNElements
Number of elements of the chunk vector.
virtual void Resize(const int64_t newsize, const T &object)
Resizes the vector object.
TPZManVector< T * > fVec
Vector which points to each chunk of objects.
std::conditional< is_const, const T &, T & >::type reference
~TPZChunkVectorIterator()
TPZChunkVectorIterator(const TPZChunkVectorIterator &other)=default
TPZChunkVector(const TPZChunkVector< T, EXP > &TCh)
Copy constructor.
virtual void Write(const bool val)
vectype::size_type cur_index
#define DebugStop()
Returns a message to user put a breakpoint in.
Free store vector implementation.
TPZChunkVectorIterator< false, value_type, EXP > iterator
void ReadInternal(T &output, TPZStream &buf, void *context)
An object of this class implements a vector which allocates objects by chunks. Utility.
TPZChunkVectorIterator< is_const, T, EXP > & operator-=(const ptrdiff_t &movement)
const_iterator cbegin() const
void Shrink()
It reallocates storage to fit the necessary storage exactly.
TPZChunkVector< T, EXP > & operator=(const TPZChunkVector< T, EXP > &TCh)
Assignment operator, copies all elements from the object TCh.
int32_t Hash(std::string str)
bool operator!=(const TPZChunkVectorIterator< is_const, T, EXP > &other) const
const value_type & const_reference
TPZChunkVectorIterator< true, value_type, EXP > const_iterator
ptrdiff_t operator-(const TPZChunkVectorIterator< is_const, T, EXP > &other) const
TPZChunkVectorIterator< is_const, T, EXP > & operator+=(const ptrdiff_t &movement)
std::conditional< is_const, const T *, T * >::type pointer
Contains declaration of the abstract TPZStream class. TPZStream defines the interface for saving and ...
Defines the interface for saving and reading data. Persistency.
int64_t NElements() const
Returns the number of elements of the vector.
TPZChunkVectorIterator< is_const, T, EXP > operator-(const ptrdiff_t &movement) const
const_iterator end() const
TPZChunkVectorIterator< is_const, T, EXP > & operator=(const TPZChunkVectorIterator< is_const, T, EXP > &other)=default
TPZChunkVectorIterator< is_const, T, EXP > & operator--()
This class defines the interface to save and restore objects from TPZStream objects. Persistency.
static void WritePointer(const TPZSavable *obj, TPZStream *stream)
int ClassId() const override
Define the class id associated with the class.
bool operator==(const TPZChunkVectorIterator< is_const, T, EXP > &other) const
#define PZError
Defines the output device to error messages and the DebugStop() function.
virtual void Read(bool &val)