GemaCoreLib
The GeMA Core library
gmMatrixSet.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_MATRIX_SET_H_
25 #define _GEMA_MATRIX_SET_H_
26 
27 #include "gmMatrix.h"
28 #include "gmThreadLocalBuffer.h"
29 
30 #include <assert.h>
31 
32 #include <QStringList>
33 
34 /* \brief This class holds a fixed size set of square matrices, whose sizes
35  can vary inside a maximum limit.
36 */
38 {
39 public:
40  GmMatrixSet(int maxMatrixTypes, const QStringList& typeNames);
41  ~GmMatrixSet();
42 
43  bool init(int maxNodes, int numMatrices, ...);
44  bool adjustSizes(int maxNodes);
45  void clear();
46 
47  void prepareSet(int nnodes);
48 
50  bool containsType(int type) const { return (index(type) >= 0 && _typeEnabled[type]); }
51 
55  void setTypeEnabled(int type, bool enabled)
56  {
57  assert(type >= 0 && type < _nTypes);
58  if(enabled == _typeEnabled[type])
59  return;
60  _typeEnabled[type] = enabled;
61  _nEnabled += (enabled ? 1 : -1);
62  }
63 
65  int numEnabledTypes() const { return _nEnabled; }
66 
67  GmMatrix& matrix (int type);
68  GmMatrix& useMatrix(int type);
69  GmMatrix& useMatrix(int type, bool fill, bool sym);
70 
75  void setSymmetric(int type, bool symmetric)
76  {
77  assert(index(type) >= 0);
78  _symmetric.localBuffer()[index(type)] = symmetric;
79  }
80 
85  void setFilled(int type, bool filled) { assert(index(type) >= 0); _filled.localBuffer()[index(type)] = filled; }
86 
91  bool symmetric(int type) const { assert(index(type) >= 0); return _symmetric.localBuffer()[index(type)]; }
92 
93  bool filledSymmetric() const;
94 
99  bool filled(int type) const { assert(index(type) >= 0); return _filled.localBuffer()[index(type)]; }
100 
102  int index(int type) const { assert(type >= 0 && type < _nTypes); return _typeIndex[type]; }
103 
105  int typeFromIndex(int index) const { assert(index >= 0 && index < _nMatrices); return _indexType[index]; }
106 
108  QString typeName(int type) const
109  {
110  assert(type >= 0 && type < _nTypes);
111  return type < _typeNames.size() ? _typeNames.at(type) : "";
112  }
113 
115  int numMatrices() const { return _nMatrices; }
116 
118  const GmMatrix& matrixFromIndex(int index) const { assert(index >= 0 && index < _nMatrices); return _matrices.localBuffer()[index]; }
119 
123  bool symmetricFromIndex(int index) const { assert(index >= 0 && index < _nMatrices); return _symmetric.localBuffer()[index]; }
124 
128  bool filledFromIndex(int index) const { assert(index >= 0 && index < _nMatrices); return _filled.localBuffer()[index]; }
129 
130 protected:
131  bool initTypes(int numMatrices, va_list typeList);
132 
133  int _nTypes;
134  int* _typeIndex;
135  int* _indexType;
136  bool* _typeEnabled;
137  int _nEnabled;
139 
141  int _maxNodes;
146 };
147 
148 
149 #endif
150 
GmTLBuffer< bool, true > _filled
Vector stating, for each matrix, if it was filled by the user or not.
Definition: gmMatrixSet.h:145
int numMatrices() const
Return the number of matrices stored in this set.
Definition: gmMatrixSet.h:115
int * _indexType
Reverse index for finding the type from the matrix index.
Definition: gmMatrixSet.h:135
GmTLBuffer< double, true > _matrixMemory
Memory used to hold data for the set matrices.
Definition: gmMatrixSet.h:142
Definition: gmMatrixSet.h:37
GmTLBuffer< GmMatrix, true > _matrices
Vector with matrices prepared by prepareSet()
Definition: gmMatrixSet.h:143
QString typeName(int type) const
Returns a name associated to the given type from the list supplied in the constructor.
Definition: gmMatrixSet.h:108
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
bool symmetric(int type) const
Returns true if the matrix from the current thread, identified by its type, was marked symmetric (def...
Definition: gmMatrixSet.h:91
Declaration of the GmTLBuffer class.
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: gmMatrixSet.h:102
Declaration of the GmMatrix class.
int numEnabledTypes() const
Returns the number of enabled types in the matrix set.
Definition: gmMatrixSet.h:65
void setTypeEnabled(int type, bool enabled)
Enables or disables a type in the set. Disabled types do NOT return matrices in calls to matrix() or ...
Definition: gmMatrixSet.h:55
bool filled(int type) const
Returns true if the matrix from the current thread, identified by its type, was marked as filled (def...
Definition: gmMatrixSet.h:99
int _nEnabled
The number of enabled matrices in _typeEnabled.
Definition: gmMatrixSet.h:137
void setFilled(int type, bool filled)
Marks a matrix, identified by its type in the set, as filled or unfilled. IMPORTANT: The supplied typ...
Definition: gmMatrixSet.h:85
#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
int * _typeIndex
An index mapping matrix types to its position within the set.
Definition: gmMatrixSet.h:134
const GmMatrix & matrixFromIndex(int index) const
Returns a matrix, tied to the current thread, identified by its index in the set.
Definition: gmMatrixSet.h:118
int _nTypes
The number of existing matrix types.
Definition: gmMatrixSet.h:133
bool * _typeEnabled
A vector stating if the type is enabled or not.
Definition: gmMatrixSet.h:136
bool filledFromIndex(int index) const
Returns true if the matrix tied to the current thread, indentified by its index, was marked as filled...
Definition: gmMatrixSet.h:128
int _nMatrices
The number of matrices stored in this set.
Definition: gmMatrixSet.h:140
int _maxNodes
The maximum dimension of a matrix (maximum number of element nodes)
Definition: gmMatrixSet.h:141
GmTLBuffer< bool, true > _symmetric
Vector stating, for each matrix, if it is symmetric or not.
Definition: gmMatrixSet.h:144
void setSymmetric(int type, bool symmetric)
Marks a matrix, identified by its type in the set, as symmetric or unsymmetric. IMPORTANT: The suppli...
Definition: gmMatrixSet.h:75
arma::mat GmMatrix
The basic type for a GeMA matrix object. Currently based on an Armadillo matrix.
Definition: gmMatrix.h:38
bool symmetricFromIndex(int index) const
Returns true if the matrix tied to the current thread, and indentified by its index,...
Definition: gmMatrixSet.h:123
bool containsType(int type) const
Returns true if the matrix set contains a matrix of the requested type AND the type is enabled.
Definition: gmMatrixSet.h:50
int typeFromIndex(int index) const
Given a matrix index, returns its type.
Definition: gmMatrixSet.h:105
QStringList _typeNames
Optional names for matrix types.
Definition: gmMatrixSet.h:138