GemaCoreLib
The GeMA Core library
gmVectorSet.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 
24 #ifndef _GEMA_VECTOR_SET_H_
25 #define _GEMA_VECTOR_SET_H_
26 
27 #include "gmVector.h"
28 #include "gmThreadLocalBuffer.h"
29 
30 #include <assert.h>
31 
32 #include <QStringList>
33 
34 
35 /* \brief This class holds a fixed size set of vectors, whose sizes
36  can vary inside a maximum limit.
37 */
39 {
40 public:
41  GmVectorSet(int maxVectorTypes, const QStringList& typeNames);
42  ~GmVectorSet();
43 
44  bool init(int maxNodes, int numVectors, ...);
45  bool adjustSizes(int maxNodes);
46  void clear();
47 
48  void prepareSet(int nnodes);
49 
51  bool containsType(int type) const { return (index(type) >= 0 && _typeEnabled[type]); }
52 
56  void setTypeEnabled(int type, bool enabled)
57  {
58  assert(type >= 0 && type < _nTypes);
59  if(enabled == _typeEnabled[type])
60  return;
61  _typeEnabled[type] = enabled;
62  _nEnabled += (enabled ? 1 : -1);
63  }
64 
66  int numEnabledTypes() const { return _nEnabled; }
67 
68  GmVector& vector(int type);
69  GmVector& useVector(int type);
70  GmVector& useVector(int type, bool fill);
71 
76  void setFilled(int type, bool filled) { assert(index(type) >= 0); _filled.localBuffer()[index(type)] = filled; }
77 
82  bool filled(int type) const { assert(index(type) >= 0); return _filled.localBuffer()[index(type)]; }
83 
85  int index(int type) const { assert(type >= 0 && type < _nTypes); return _typeIndex[type]; }
86 
88  int typeFromIndex(int index) const { assert(index >= 0 && index < _nVectors); return _indexType[index]; }
89 
91  QString typeName(int type) const
92  {
93  assert(type >= 0 && type < _nTypes);
94  return type < _typeNames.size() ? _typeNames.at(type) : "";
95  }
96 
98  int numVectors() const { return _nVectors; }
99 
101  const GmVector& vectorFromIndex(int index) const { assert(index >= 0 && index < _nVectors); return _vectors.localBuffer()[index]; }
102 
106  bool filledFromIndex(int index) const { assert(index >= 0 && index < _nVectors); return _filled.localBuffer()[index]; }
107 
108 protected:
109  bool initTypes(int numVectors, va_list typeList);
110 
111  int _nTypes;
112  int* _typeIndex;
113  int* _indexType;
114  bool* _typeEnabled;
115  int _nEnabled;
117 
118  int _nVectors;
119  int _maxNodes;
123 };
124 
125 #endif
126 
int typeFromIndex(int index) const
Given a vector index, returns its type.
Definition: gmVectorSet.h:88
int _nTypes
The number of existing vector types.
Definition: gmVectorSet.h:111
GmTLBuffer< double, true > _vectorMemory
Memory used to hold data for the set vectors.
Definition: gmVectorSet.h:120
void fill(GmMatrix &m, const double *data, int nlin, int ncol)
Copy the contents of data to matrix m. If m size is different from nlin, ncol the matrix is resized....
Definition: gmMatrixUtils.h:51
Declaration of the GmTLBuffer class.
bool containsType(int type) const
Returns true if the vector set contains a vector of the requested type AND the type is enabled.
Definition: gmVectorSet.h:51
GmTLBuffer< GmVector, true > _vectors
Vector with vectors prepared by prepareSet()
Definition: gmVectorSet.h:121
int numEnabledTypes() const
Returns the number of enabled types in the vector set.
Definition: gmVectorSet.h:66
int index(int type) const
Returns the index inside the set of the supplied type (-1 if the type doesn't belong to the set).
Definition: gmVectorSet.h:85
int * _typeIndex
An index mapping vector types to its position within the set.
Definition: gmVectorSet.h:112
QStringList _typeNames
Optional names for vector types.
Definition: gmVectorSet.h:116
bool filled(int type) const
Returns true if the vector from the current thread, identified by its type, was marked as filled (def...
Definition: gmVectorSet.h:82
bool * _typeEnabled
A vector stating if the type is enabled or not.
Definition: gmVectorSet.h:114
void setFilled(int type, bool filled)
Marks a vector, identified by its type in the set, as filled or unfilled. IMPORTANT: The supplied typ...
Definition: gmVectorSet.h:76
bool filledFromIndex(int index) const
Returns true if the vector tied to the current thread, indentified by its index, was marked as filled...
Definition: gmVectorSet.h:106
void setTypeEnabled(int type, bool enabled)
Enables or disables a type in the set. Disabled types do NOT return vectors in calls to vector() or u...
Definition: gmVectorSet.h:56
int _nEnabled
The number of enabled vectors in _typeEnabled.
Definition: gmVectorSet.h:115
Declaration of the GmVector class.
#define GMC_API_EXPORT
Macro for controling if the class is being exported (GEMA_CORE_LIB defined) or imported (GEMA_CORE_LI...
Definition: gmCoreConfig.h:35
GmTLBuffer< bool, true > _filled
Vector stating, for each vector, if it was filled by the user or not.
Definition: gmVectorSet.h:122
Definition: gmVectorSet.h:38
int * _indexType
Reverse index for finding the type from the vector index.
Definition: gmVectorSet.h:113
int _nVectors
The number of vectors stored in this set.
Definition: gmVectorSet.h:118
const GmVector & vectorFromIndex(int index) const
Returns a vector, tied to the current thread, identified by its index in the set.
Definition: gmVectorSet.h:101
int numVectors() const
Return the number of vectors stored in this set.
Definition: gmVectorSet.h:98
arma::vec GmVector
The basic type for a GeMA vector object. Currently based on an Armadillo vector.
Definition: gmVector.h:34
QString typeName(int type) const
Returns a name associated to the given type from the list supplied in the constructor.
Definition: gmVectorSet.h:91
int _maxNodes
The maximum dimension of a vector (maximum number of element nodes)
Definition: gmVectorSet.h:119