GemaCoreLib
The GeMA Core library
gmSingleVector.h
Go to the documentation of this file.
1 /************************************************************************
2 **
3 ** Copyright (C) 2014 by Carlos Augusto Teixera Mendes
4 ** All rights reserved.
5 **
6 ** This file is part of the "GeMA" software. It's use should respect
7 ** the terms in the license agreement that can be found together
8 ** with this source code.
9 ** It is provided AS IS, with NO WARRANTY OF ANY KIND,
10 ** INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR
11 ** A PARTICULAR PURPOSE.
12 **
13 ************************************************************************/
14 
23 #ifndef _GEMA_SINGLE_VECTOR_H_
24 #define _GEMA_SINGLE_VECTOR_H_
25 
26 #include "gmPODVector.h"
27 
36 template <class T> class GmSingleVector
37 {
38 public:
39  static_assert(!QTypeInfo<T>::isComplex, "Not a POD type");
40 
42  typedef T DataType;
43 
46 
48  GmSingleVector(size_t nvalues) : _v(nvalues) {}
49 
51  size_t size() const { return _v.size(); }
52 
54  const T* iptr(size_t index) const { assert(index < _v.size()); return _v.data() + index; }
55 
57  T* iptr(size_t index) { assert(index < _v.size()); return _v.data() + index; }
58 
60  const T& operator[](size_t index) const { return *iptr(index); }
61 
63  T& operator[](size_t index) { return *iptr(index); }
64 
72  bool addValues(size_t numAddedValues, bool tight = false)
73  {
74  if(_v.size() > std::numeric_limits<size_t>::max() - numAddedValues)
75  {
76  gmWarnMsg(GmPanicLogger(), QObject::tr("Single vector: Error adding values. Vector size would surpass the maximum possible capacity."));
77  return false;
78  }
79 
80  if(tight || !_v.size())
81  return _v.resize(_v.size() + numAddedValues, GmPODVectorTightGrow);
82  else
83  return _v.resize(_v.size() + numAddedValues);
84  }
85 
87  void restoreSize(size_t oldNumValues)
88  {
89  assert(oldNumValues <= _v.size());
90  _v.resize(oldNumValues);
91  }
92 
94  void removeValues(size_t index, size_t numValues) { _v.remove(index, numValues); }
95 
97  void clear() { _v.clear(); }
98 
104  size_t usedMemory() const { return _v.capacity() * sizeof(T); }
105 
107  int numDumpBuffers() const { return 1; }
108 
110  char* dumpBuffer(int i) const { Q_UNUSED(i); assert(i == 0); return (char*)_v.data(); }
111 
113  size_t dumpBufferSize(int i) const { Q_UNUSED(i); assert(i == 0); return _v.size() * sizeof(T); }
114 
115 private:
116  Q_DISABLE_COPY(GmSingleVector);
117 
118  GmPODVector<T> _v;
119 };
120 
121 
122 
123 
124 #endif
const GmLogCategory & GmPanicLogger()
Returns the global "panic" logger, usually used to report memory allocation fails deep inside class s...
Definition: gmLog.cpp:700
const T * iptr(size_t index) const
Returns a pointer to the index data inside the vector.
Definition: gmSingleVector.h:54
A class with the same API as GmDualVector storing a single vector. No query "if" overhead....
Definition: gmSingleVector.h:36
bool addValues(size_t numAddedValues, bool tight=false)
Adds numAddedValues to the set, without any initialization. Returns true on success,...
Definition: gmSingleVector.h:72
size_t dumpBufferSize(int i) const
Returns the size in bytes of the i'th internal buffer returned by dumpBuffer(i)
Definition: gmSingleVector.h:113
T * iptr(size_t index)
Non-const overload for iptr()
Definition: gmSingleVector.h:57
QString tr(const char *sourceText, const char *disambiguation, int n)
const T & operator[](size_t index) const
Standard indexing operator.
Definition: gmSingleVector.h:60
Implementation of the GmPODVector template class.
T & operator[](size_t index)
Non-const overload for the standard indexing operator.
Definition: gmSingleVector.h:63
void restoreSize(size_t oldNumValues)
Restores the size of the set to the previous size before the last call to addValues().
Definition: gmSingleVector.h:87
size_t usedMemory() const
Returns an estimative of the memory used by the data set in bytes.
Definition: gmSingleVector.h:104
void clear()
Clears all stored data returning the object to a default constructed state.
Definition: gmSingleVector.h:97
GmSingleVector()
Default constructor.
Definition: gmSingleVector.h:45
char * dumpBuffer(int i) const
Returns the i'th internal buffer, i from 0 to numDumpBuffers()-1.
Definition: gmSingleVector.h:110
bool resize(ControlSize newSize, GmPODGrowT grow=Grow)
Resizes the vector to the given size using the speciefied grow policy.
Definition: gmPODVector.h:159
void removeValues(size_t index, size_t numValues)
Removes numValues from the set, starting from (and including) index.
Definition: gmSingleVector.h:94
size_t size() const
Returns the vector size.
Definition: gmSingleVector.h:51
GmSingleVector(size_t nvalues)
The constructor initializing the vector with the given size. The vector contents is NOT initialized.
Definition: gmSingleVector.h:48
T DataType
The stored data type.
Definition: gmSingleVector.h:39
void remove(ControlSize index, ControlSize numValues)
Removes numValues entries from the vector starting at index.
Definition: gmPODVector.h:139
ControlSize capacity() const
Returns the vector allocated capacity.
Definition: gmPODVector.h:106
const T * data() const
Returns a pointer to the data inside the vector.
Definition: gmPODVector.h:109
ControlSize size() const
Returns the vector size.
Definition: gmPODVector.h:103
size_t GmPODVectorTightGrow(size_t s, size_t a)
Vector growing to exactly the needed size.
Definition: gmPODVector.h:72
void clear()
Clears the vector deallocating used memory.
Definition: gmPODVector.h:229
int numDumpBuffers() const
Returns the number of internal buffers used by this implementation.
Definition: gmSingleVector.h:107