GemaCoreLib
The GeMA Core library
gmVector.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_H_
25 #define _GEMA_VECTOR_H_
26 
27 #include "gmCoreConfig.h"
28 
30 #ifndef GM_VECTOR_DEFINED
31 #define GM_VECTOR_DEFINED
32 
33 #include <armadillo>
34 typedef arma::vec GmVector;
35 
36 // Register type for storing them inside a QVariant
37 #include <QMetaType>
39 
40 #endif
41 
42 #include "gmVectorUtils.h"
43 #include <assert.h>
44 
45 #if ARMA_VERSION_MAJOR != 5 || ARMA_VERSION_MINOR != 200
46 #error Unexpected Armadillo version. Please check that our internal fiddling done bellow is still valid
47 #endif
48 
61 #define DECLARE_REF_VECTOR(x, ptr, lin) GmVector x((ptr), (lin), false, true)
62 
63 
70 {
71 public:
73  GmCRVector(const double* data, int nlin)
74  : n_rows(nlin), n_cols(1), n_elem(nlin),
75  _vec(const_cast<double*>(data), nlin, false, true)
76  {
77  }
78 
81  : n_rows(0), n_cols(0), n_elem(0), _vec(NULL, 0, false, true)
82  {
83  }
84 
85  typedef double elem_type;
86 
87  const unsigned int n_rows;
88  const unsigned int n_cols;
89  const unsigned int n_elem;
90 
92  operator const GmVector& () { return _vec; }
93 
95  const GmVector& v() { return _vec; }
96 
100  void setMemory(const double* data, int nlin)
101  {
102  // Ugly hack ahead. Please close your eyes. This is dependent on the Armadillo
103  // implementation and is done only because there is really no other way (short of
104  // changing the library itself). Not very different from this whole class, by the way.
105  arma::access::rw(n_rows) = nlin;
106  arma::access::rw(n_cols) = 1; // It might be zero if constructed with the default constructor
107  arma::access::rw(n_elem) = nlin;
108 
109  GmVectorUtils::setVectorMemory(_vec, const_cast<double*>(data), nlin);
110  }
111 
112  const double& operator[] (unsigned int ii) const { return _vec[ii]; }
113  const double& at(unsigned int ii) const { return _vec.at(ii); }
114  const double& operator() (unsigned int ii) const { return _vec(ii); }
115 
116  bool is_empty() const { return _vec.is_empty(); }
117 
118  bool in_range(unsigned int ii) const { return _vec.in_range(ii); }
119 
120  const double* memptr() const { return _vec.memptr(); }
121 
122  bool empty() const { return _vec.empty(); }
123  unsigned int size() const { return _vec.size(); }
124 
125 private:
126  Q_DISABLE_COPY(GmCRVector)
127 
128  GmVector _vec;
129 };
130 
131 
132 #endif
133 
double elem_type
the type of elements stored in the vector
Definition: gmVector.h:85
const unsigned int n_rows
number of rows in the vector (read-only)
Definition: gmVector.h:87
const unsigned int n_elem
number of elements in the vector (read-only)
Definition: gmVector.h:89
GmCRVector()
Constructs an empty vector that needs to be initialized later by a call to setMemory()
Definition: gmVector.h:80
Declaration of usefull configuration definitions for the Core library.
GmCRVector(const double *data, int nlin)
Constructs the vector pointing to a const memory area.
Definition: gmVector.h:73
void setMemory(const double *data, int nlin)
Exchanges the memory area used by the vector. Same caveats explained in the class documentation apply...
Definition: gmVector.h:100
#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
An auxiliary vector WRAPPER that binds the vector to a CONST memory area with data already initialize...
Definition: gmVector.h:69
const GmVector & v()
Returns the wrapped vector as a CONST reference.
Definition: gmVector.h:95
Utilitary functions for working with vectors.
const unsigned int n_cols
number of columns in the vector (read-only)
Definition: gmVector.h:88
arma::vec GmVector
The basic type for a GeMA vector object. Currently based on an Armadillo vector.
Definition: gmVector.h:34
Q_DECLARE_METATYPE(LuaFunction)
void setVectorMemory(GmVector &v, double *data, int nlin)
Updates the memory area used internally by a vector. DANGEROUS. Should be used only by the bold ones ...
Definition: gmVectorUtils.h:113