GemaCoreLib
The GeMA Core library
Loading...
Searching...
No Matches
gmPackedBuffer.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_PACKED_BUFFER_H_
24#define _GEMA_PACKED_BUFFER_H_
25
26#include "gmCoreConfig.h"
27#include "gmValueSet.h"
28#include "gmSingleVector.h"
29#include "gmDualVector.h"
30
31#include <QVector>
32#include <QMap>
33
36{
37public:
39 GmPackedBufferInfo(int id) : _id(id), _padding(0), _stride(0) { _offset.append(0); }
40
42 int id() const { return _id; }
43
44 void addValueSet(GmValueSet* vs);
45 void update(QVector<int>* newSetIndices = NULL);
46
48 bool ready() const { return _waitingSets.isEmpty(); }
49
50 void removeValueSet(int index);
51 bool removePendingValueSet(GmValueSet* vs);
52
54 int numSets() const { return _sets.size(); }
55
57 int vsIndex(QString id) const { return _map.value(id, -1); }
58
60 GmValueSet* vs(int index) const { return _sets[index]; }
61
66 int offset(int index) const { return _offset[index]; }
67
69 int size(int index) const { return _offset[index + 1] - _offset[index]; }
70
72 int stride() const { return _stride; }
73
74 void notifyLayoutUpdates();
75
77 void clearWaitingList() { _waitingSets.clear(); }
78
79#if defined ENABLE_TESTS || defined ENABLE_VALUESET_TESTS
80 void validateInternalStructure();
81#endif
82
83private:
84 void updateControlFromPos(int pos);
85
87 int sizeofVsStorageType(const GmValueSet* vs) const
88 {
89 GmValueInfo* info = vs->info();
90 return info->canStoreFunctions() ? sizeof(double) : info->sizeofStorageType();
91 }
92
93 int _id;
98 int _stride;
99
101};
102
103
119{
120public:
122 GmPackedBuffer(int id) : _layout(id) {}
123
125 virtual ~GmPackedBuffer() {}
126
128 const GmPackedBufferInfo* layout() const { return &_layout; }
129
130 virtual bool isDual() const = 0;
131
132 void addValueSet(GmValueSet* vs);
133 bool update();
134
135 bool removeValueSet(int index);
136 bool removeValueSet(GmValueSet* vs);
137
139 int numSets() const { return _layout.numSets(); }
140
142 int vsIndex(QString id) const { return _layout.vsIndex(id); }
143
145 GmValueSet* vs(int index) const { return _layout.vs(index); }
146
148 int vsDataSize(int index) const { return _layout.size(index); }
149
151 int vsDataOffset(int index) const { assert(_layout.ready()); return _layout.offset(index); }
152
154 int stride() const { assert(_layout.ready()); return _layout.stride(); }
155
156 virtual int numValues() const = 0;
157 virtual bool addValues(int numAddedValues) = 0;
158 virtual void restoreSize(int oldNumValues) = 0;
159 virtual void removeValues(int lineIndex, int numValues) = 0;
160
161 virtual void clear() = 0;
162
163 virtual size_t usedMemory() const = 0;
164 virtual int numDumpBuffers() const = 0;
165 virtual char* dumpBuffer(int i) const = 0;
166 virtual size_t dumpBufferSize(int i) const = 0;
167
168#if defined ENABLE_TESTS || defined ENABLE_VALUESET_TESTS
169 virtual void validateInternalStructure() = 0;
170#endif
171
172private:
173 Q_DISABLE_COPY(GmPackedBuffer)
174
175protected:
176 virtual bool updateData(const GmPackedBufferInfo& oldLayout, const QVector<int>* newSetIndices) = 0;
177
179};
180
181
186template <template <class> class Vector>
188{
189public:
191 GmPackedBufferImpl(int id) : GmPackedBuffer(id), _data(new Vector<char>), _numValues(0) {}
192
193 virtual ~GmPackedBufferImpl();
194
196 virtual bool isDual() const { return _data->isDual(); }
197
199 virtual int numValues() const { return _numValues; }
200
201 virtual bool addValues (int numAddedValues);
202 virtual void restoreSize (int oldNumValues);
203 virtual void removeValues(int lineIndex, int numValues);
204
206 char* iptr(int vsIndex, int lineIndex)
207 {
208 assert(vsIndex >= 0 && vsIndex < numSets());
209 assert(lineIndex >= 0 && lineIndex < _numValues);
210 return _data->iptr((size_t)lineIndex * _layout.stride() + _layout.offset(vsIndex));
211 }
212
214 const char* iptr(int vsIndex, int lineIndex) const
215 {
216 assert(vsIndex >= 0 && vsIndex < numSets());
217 assert(lineIndex >= 0 && lineIndex < _numValues);
218 return _data->iptr((size_t)lineIndex * _layout.stride() + _layout.offset(vsIndex));
219 }
220
222 virtual void clear() { _data->clear(); _numValues = 0; }
223
229 virtual size_t usedMemory() const { return _data->usedMemory(); }
230
232 virtual int numDumpBuffers() const { return _data->numDumpBuffers(); }
233
235 virtual char* dumpBuffer(int i) const { return _data->dumpBuffer(i); }
236
238 virtual size_t dumpBufferSize(int i) const { return _data->dumpBufferSize(i); }
239
240#if defined ENABLE_TESTS || defined ENABLE_VALUESET_TESTS
241 virtual void validateInternalStructure();
242#endif
243
244protected:
245 virtual bool updateData(const GmPackedBufferInfo& oldLayout, const QVector<int>* newSetIndices);
246
247private:
248 Q_DISABLE_COPY(GmPackedBufferImpl)
249
250
251 void copyBuffer(const GmPackedBufferInfo* oldInfo, const char* oldBuffer,
252 const GmPackedBufferInfo* newInfo, char* newBuffer,
253 size_t numValues, const QVector<const char*>* init = NULL) const;
254
255 void fillInitBuffer(QVector<char>& buf, QVector<const char*>& index, const QVector<int>* newSetIndices) const;
256
257 Vector<char>* _data;
259};
260
261
264
267
268#endif
269
270
The basic interface for a shared buffer storing interleaved data for a group of value sets.
Definition gmPackedBuffer.h:119
int stride() const
Returns the pack size (the sum of each value set data size + padding if needed)
Definition gmPackedBuffer.h:154
GmPackedBufferInfo _layout
The buffer layout for the added value sets.
Definition gmPackedBuffer.h:178
int numSets() const
Returns the number of value sets in the pack.
Definition gmPackedBuffer.h:139
int vsDataSize(int index) const
Returns the data size for the given value set object.
Definition gmPackedBuffer.h:148
int vsIndex(QString id) const
Returns the value set index inside the pack or -1 if not found.
Definition gmPackedBuffer.h:142
GmPackedBuffer(int id)
Basic constructor. Gets as parameter the pack id.
Definition gmPackedBuffer.h:122
GmValueSet * vs(int index) const
Returns the value set object at the given index.
Definition gmPackedBuffer.h:145
int vsDataOffset(int index) const
Returns the pack offset for the given value set object.
Definition gmPackedBuffer.h:151
const GmPackedBufferInfo * layout() const
Returns the internal layout object.
Definition gmPackedBuffer.h:128
virtual ~GmPackedBuffer()
Virtual destructor.
Definition gmPackedBuffer.h:125
The concrete implementation for GmPackedBuffer. The template parameter defines the kind of vector use...
Definition gmPackedBuffer.h:188
int _numValues
The number of values stored in _data. Equal to _data.size() / _layout.stride()
Definition gmPackedBuffer.h:258
GmPackedBufferImpl(int id)
Basic constructor. Gets as parameter the pack id.
Definition gmPackedBuffer.h:191
virtual size_t dumpBufferSize(int i) const
Returns the size in bytes of the i'th internal buffer returned by dumpBuffer(i)
Definition gmPackedBuffer.h:238
char * iptr(int vsIndex, int lineIndex)
Returns the data pointer for the index'th value from the given value set. NOT virtual to avoid the ov...
Definition gmPackedBuffer.h:206
virtual int numDumpBuffers() const
Returns the number of internal buffers used by this implementation.
Definition gmPackedBuffer.h:232
virtual void clear()
Clears the value sets, restoring their size to 0.
Definition gmPackedBuffer.h:222
virtual bool isDual() const
Returns true if the underlying data storage is a dual vector, false if not.
Definition gmPackedBuffer.h:196
Vector< char > * _data
The shared buffer for the value sets data. Must be a pointer due to possible layout changes.
Definition gmPackedBuffer.h:257
virtual size_t usedMemory() const
Returns an estimative of the memory used by the data set in bytes.
Definition gmPackedBuffer.h:229
virtual char * dumpBuffer(int i) const
Returns the i'th internal buffer, i from 0 to numDumpBuffers()-1.
Definition gmPackedBuffer.h:235
virtual int numValues() const
Returns the number of values stored in the value sets.
Definition gmPackedBuffer.h:199
const char * iptr(int vsIndex, int lineIndex) const
Const overload for returning the data pointer for the index'th value from the given value set....
Definition gmPackedBuffer.h:214
A class used for storing and managing the order of the value sets inside a pack.
Definition gmPackedBuffer.h:36
int sizeofVsStorageType(const GmValueSet *vs) const
Returns the storage size for a value set taking into account funstion storage possibility.
Definition gmPackedBuffer.h:87
GmValueSet * vs(int index) const
Returns the value set object at the given index.
Definition gmPackedBuffer.h:60
int _id
The pack buffer id. Used for consistency check of the added value sets.
Definition gmPackedBuffer.h:93
QVector< GmValueSet * > _waitingSets
The set of value sets that where not yet added to _map, _sets and _offset.
Definition gmPackedBuffer.h:100
QVector< int > _offset
The offset of each value set in the pack. Its size is equal to _sets.size() + 1. The last entry store...
Definition gmPackedBuffer.h:96
int offset(int index) const
Returns the data offset(in bytes) for the begining of the data from the value set at the given index....
Definition gmPackedBuffer.h:66
int id() const
Returns the pack id.
Definition gmPackedBuffer.h:42
QVector< GmValueSet * > _sets
The ordered list of value sets for this pack.
Definition gmPackedBuffer.h:95
int _stride
The sum of the set sizes + padding.
Definition gmPackedBuffer.h:98
int numSets() const
Returns the number of value sets in the pack.
Definition gmPackedBuffer.h:54
int stride() const
Returns the buffer stride (sum of each value set size + padding)
Definition gmPackedBuffer.h:72
int size(int index) const
The size in bytes used by the value set at the given index (accounts for multiple states and multi-di...
Definition gmPackedBuffer.h:69
int _padding
The padding to ensure proper alignment when working with heterogeneous data types.
Definition gmPackedBuffer.h:97
int vsIndex(QString id) const
Returns the value set index inside the pack or -1 if not found.
Definition gmPackedBuffer.h:57
QMap< QString, int > _map
A map from value set Id to its position inside _sets.
Definition gmPackedBuffer.h:94
GmPackedBufferInfo(int id)
Basic constructor. Gets as parameter the pack id.
Definition gmPackedBuffer.h:39
void clearWaitingList()
Clears the set of added value sets after the object creation / last call to update()
Definition gmPackedBuffer.h:77
bool ready() const
Returns true if there are no pending calls to update()
Definition gmPackedBuffer.h:48
Auxiliar class used to store the definition of a value. It can be used to store informations about st...
Definition gmValueInfo.h:132
int sizeofStorageType() const
Returns the size in bytes of an entry of the current storage type. Not affected by the data dimension...
Definition gmValueInfo.cpp:235
bool canStoreFunctions() const
Returns true if values associated to this definition can be expressed as functions....
Definition gmValueInfo.h:205
Basic class used to store sets of values, bound to a common definition, on behalf of another object (...
Definition gmValueSet.h:54
GmValueInfo * info() const
Returns the metadata associated with this value set.
Definition gmValueSet.h:72
Declaration of useful configuration definitions for the Core library.
#define GMC_API_EXPORT
Macro for controlling if the class is being exported (GEMA_CORE_LIB defined) or imported (GEMA_CORE_L...
Definition gmCoreConfig.h:35
Implementation of the GmDualVector and GmDualVectorR template classes.
GmPackedBufferImpl< GmDualVectorR > GmDualPackedBuffer
The GmPackedBuffer class using a dual vector.
Definition gmPackedBuffer.h:266
GmPackedBufferImpl< GmSingleVector > GmSinglePackedBuffer
The GmPackedBuffer class using a single vector.
Definition gmPackedBuffer.h:263
Implementation of the GmSingleVector template class.
Declaration of the GmValueSet class.