GemaCoreLib
The GeMA Core library
Loading...
Searching...
No Matches
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
36template <class T> class GmSingleVector
37{
38public:
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 bool isDual() const { return false; }
52
54 size_t size() const { return _v.size(); }
55
57 const T* iptr(size_t index) const { assert(index < _v.size()); return _v.data() + index; }
58
60 T* iptr(size_t index) { assert(index < _v.size()); return _v.data() + index; }
61
63 const T& operator[](size_t index) const { return *iptr(index); }
64
66 T& operator[](size_t index) { return *iptr(index); }
67
68
69 // -------------------
70 typedef T* iterator;
71
72 iterator begin() { return _v.data(); }
73 iterator end() { return _v.data() + _v.size(); }
74 // -------------------
75
76
84 bool addValues(size_t numAddedValues, bool tight = false)
85 {
86 if(_v.size() > std::numeric_limits<size_t>::max() - numAddedValues)
87 {
88 gmWarnMsg(GmPanicLogger(), QObject::tr("Single vector: Error adding values. Vector size would surpass the maximum possible capacity."));
89 return false;
90 }
91
92 if(tight || !_v.size())
93 return _v.resize(_v.size() + numAddedValues, GmPODVectorTightGrow);
94 else
95 return _v.resize(_v.size() + numAddedValues);
96 }
97
101 void restoreSize(size_t oldNumValues)
102 {
103 assert(oldNumValues <= _v.size());
104 _v.resize(oldNumValues);
105 }
106
108 void removeValues(size_t index, size_t numValues) { _v.remove(index, numValues); }
109
111 void clear() { _v.clear(); }
112
114 void zero(size_t index = 0) { _v.zero(index); }
115
121 size_t usedMemory() const { return _v.capacity() * sizeof(T); }
122
124 int numDumpBuffers() const { return 1; }
125
127 char* dumpBuffer(int i) const { Q_UNUSED(i); assert(i == 0); return (char*)_v.data(); }
128
130 size_t dumpBufferSize(int i) const { Q_UNUSED(i); assert(i == 0); return _v.size() * sizeof(T); }
131
132private:
133 Q_DISABLE_COPY(GmSingleVector);
134
136};
137
138
139
140
141#endif
A simple vector for Plain Old Data (POD) types with the control size variable type parameterized to a...
Definition gmPODVector.h:88
A class with the same API as GmDualVector storing a single vector. No query "if" overhead....
Definition gmSingleVector.h:37
void removeValues(size_t index, size_t numValues)
Removes numValues from the set, starting from (and including) index.
Definition gmSingleVector.h:108
GmPODVector< T > _v
The real vector.
Definition gmSingleVector.h:135
T DataType
The stored data type.
Definition gmSingleVector.h:42
void clear()
Clears all stored data returning the object to a default constructed state.
Definition gmSingleVector.h:111
int numDumpBuffers() const
Returns the number of internal buffers used by this implementation.
Definition gmSingleVector.h:124
GmSingleVector()
Default constructor.
Definition gmSingleVector.h:45
bool addValues(size_t numAddedValues, bool tight=false)
Adds numAddedValues to the set, without any initialization. Returns true on success,...
Definition gmSingleVector.h:84
void zero(size_t index=0)
Clears the vector area by setting to zero all entries starting at the given index.
Definition gmSingleVector.h:114
T * iptr(size_t index)
Non-const overload for iptr()
Definition gmSingleVector.h:60
size_t usedMemory() const
Returns an estimative of the memory used by the data set in bytes.
Definition gmSingleVector.h:121
const T & operator[](size_t index) const
Standard indexing operator.
Definition gmSingleVector.h:63
size_t size() const
Returns the vector size.
Definition gmSingleVector.h:54
const T * iptr(size_t index) const
Returns a pointer to the index data inside the vector.
Definition gmSingleVector.h:57
T & operator[](size_t index)
Non-const overload for the standard indexing operator.
Definition gmSingleVector.h:66
size_t dumpBufferSize(int i) const
Returns the size in bytes of the i'th internal buffer returned by dumpBuffer(i)
Definition gmSingleVector.h:130
char * dumpBuffer(int i) const
Returns the i'th internal buffer, i from 0 to numDumpBuffers()-1.
Definition gmSingleVector.h:127
GmSingleVector(size_t nvalues)
The constructor initializing the vector with the given size. The vector contents is NOT initialized.
Definition gmSingleVector.h:48
void restoreSize(size_t oldNumValues)
Restores the size of the set to the previous size before the last call to addValues()....
Definition gmSingleVector.h:101
bool isDual() const
Returns false, since this is NOT a Dual vector.
Definition gmSingleVector.h:51
const GmLogCategory & GmPanicLogger()
Returns the global "panic" logger, usually used to report memory allocation fails deep inside class s...
Definition gmLog.cpp:647
Implementation of the GmPODVector template class.
size_t GmPODVectorTightGrow(size_t s, size_t a)
Vector growing to exactly the needed size.
Definition gmPODVector.h:72
QString tr(const char *sourceText, const char *disambiguation, int n)