GemaCoreLib
The GeMA Core library
Loading...
Searching...
No Matches
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>
34typedef arma::vec GmVector;
35typedef arma::vec3 GmVector3;
36typedef arma::vec2 GmVector2;
37
38// Register type for storing them inside a QVariant
39#include <QMetaType>
41
42#endif
43
44#include "gmVectorUtils.h"
45#include <assert.h>
46
47#if ARMA_VERSION_MAJOR != 14 || ARMA_VERSION_MINOR != 0
48#error Unexpected Armadillo version. Please check that our internal fiddling done below is still valid
49#endif
50
63#define DECLARE_REF_VECTOR(x, ptr, lin) GmVector x((ptr), (lin), false, true)
64
65
72{
73public:
75 GmCRVector(const double* data, int nlin)
76 : n_rows(nlin), n_cols(1), n_elem(nlin),
77 _vec(const_cast<double*>(data), nlin, false, true)
78 {
79 }
80
83 : n_rows(0), n_cols(0), n_elem(0), _vec(NULL, 0, false, true)
84 {
85 }
86
87 typedef double elem_type;
88
89 const unsigned int n_rows;
90 const unsigned int n_cols;
91 const unsigned int n_elem;
92
94 operator const GmVector& () { return _vec; }
95
97 const GmVector& v() { return _vec; }
98
102 void setMemory(const double* data, int nlin)
103 {
104 // Ugly hack ahead. Please close your eyes. This is dependent on the Armadillo
105 // implementation and is done only because there is really no other way (short of
106 // changing the library itself). Not very different from this whole class, by the way.
107 arma::access::rw(n_rows) = nlin;
108 arma::access::rw(n_cols) = 1; // It might be zero if constructed with the default constructor
109 arma::access::rw(n_elem) = nlin;
110
111 GmVectorUtils::setVectorMemory(_vec, const_cast<double*>(data), nlin);
112 }
113
114 const double& operator[] (unsigned int ii) const { return _vec[ii]; }
115 const double& at(unsigned int ii) const { return _vec.at(ii); }
116 const double& operator() (unsigned int ii) const { return _vec(ii); }
117
118 bool is_empty() const { return _vec.is_empty(); }
119
120 bool in_range(unsigned int ii) const { return _vec.in_range(ii); }
121
122 const double* memptr() const { return _vec.memptr(); }
123
124 bool empty() const { return _vec.empty(); }
125 unsigned int size() const { return _vec.size(); }
126
127private:
128 Q_DISABLE_COPY(GmCRVector)
129
130 GmVector _vec;
131};
132
133
134#endif
135
An auxiliary vector WRAPPER that binds the vector to a CONST memory area with data already initialize...
Definition gmVector.h:72
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:102
GmCRVector()
Constructs an empty vector that needs to be initialized later by a call to setMemory()
Definition gmVector.h:82
const unsigned int n_elem
number of elements in the vector (read-only)
Definition gmVector.h:91
double elem_type
the type of elements stored in the vector
Definition gmVector.h:87
const unsigned int n_cols
number of columns in the vector (read-only)
Definition gmVector.h:90
GmCRVector(const double *data, int nlin)
Constructs the vector pointing to a const memory area.
Definition gmVector.h:75
const unsigned int n_rows
number of rows in the vector (read-only)
Definition gmVector.h:89
const GmVector & v()
Returns the wrapped vector as a CONST reference.
Definition gmVector.h:97
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
arma::vec2 GmVector2
A 2D vector with fixed size of 2 elements.
Definition gmVector.h:36
arma::vec GmVector
The basic type for a GeMA vector object. Currently based on an Armadillo vector.
Definition gmVector.h:34
arma::vec3 GmVector3
A 3D vector with fixed size of 3 elements.
Definition gmVector.h:35
Utilitary functions for working with vectors.
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:124