#ifndef _REAL_HPP_ #define _REAL_HPP_ #include #include #include void RealDebug(char *fmtstr, ...); class RealException { public: RealException (std::string s) { m_what = s; } ~RealException() { } std::string& what() { return m_what; } private: std::string m_what; }; class RealVector { public: struct OpBinary { enum OpType { ADD = 0, SUB, MUL, DIV }; typedef struct _attributes_t { bool negative; } attributes_t; OpBinary(const RealVector &a, const RealVector &b, OpType op) : a(a) , b(b) , op(op) , m_attributes(defaultAttributes()) { } OpBinary operator-() { attributes_t attributes = this->m_attributes; attributes.negative = !attributes.negative; return OpBinary(*this, attributes); } ~OpBinary() { } bool isNegative() const { return m_attributes.negative; } const RealVector &a; const RealVector &b; const OpType op; attributes_t m_attributes; private: OpBinary& operator=(const OpBinary&); OpBinary(const OpBinary &src, attributes_t attributes) : a(src.a) , b(src.b) , op(src.op) , m_attributes(attributes) { } static const attributes_t defaultAttributes() { attributes_t attributes; attributes.negative = false; return attributes; } }; typedef struct _attributes_t { bool negative; } attributes_t; // Standard constructor RealVector(uint32_t size, radio_float_t *real=0) : m_size(size) , m_real(0) , m_pBufReal(0) , m_pPtrReal(real) , m_attributes(defaultAttributes()) { if (!real) { m_pBufReal = new radio_float_t[m_size]; memset(m_pBufReal, 0, m_size*sizeof(radio_float_t)); m_pPtrReal = m_pBufReal; } } // OpBinary resolver copy constructor RealVector(const OpBinary &src) : m_size(src.a.m_size) , m_real(0) , m_pBufReal(0) , m_pPtrReal(0) , m_attributes(defaultAttributes()) { m_pBufReal = new radio_float_t[m_size]; m_pPtrReal = m_pBufReal; *this = src; } // Scalar constructor RealVector(radio_float_t real) : m_size(1) , m_real(real) , m_pBufReal(0) , m_pPtrReal(&m_real) , m_attributes(defaultAttributes()) { } ~RealVector(void) { if (m_pBufReal) delete(m_pBufReal); m_pBufReal = nullptr; } bool isNegative() const { return m_attributes.negative; } uint32_t getSize() { return m_size; } RealVector at(uint32_t const &i) { return RealVector(*this, i, 1, i); } void print(char *pPrefix) { uint32_t i; RealDebug("%s:\n", pPrefix); for (i=0; i < m_size; i++) { RealDebug("%d: %.6f\n", i, m_pPtrReal[i]); } } RealVector operator-() { attributes_t attributes = this->m_attributes; attributes.negative = !attributes.negative; return RealVector(*this, attributes); } // Assignments RealVector& operator=(const RealVector &src); RealVector& operator+=(const RealVector &src); RealVector& operator-=(const RealVector &src); RealVector& operator*=(const RealVector &src); RealVector& operator/=(const RealVector &src); RealVector& operator=(const OpBinary &src); RealVector& operator+=(const OpBinary &src); RealVector& operator-=(const OpBinary &src); RealVector& operator*=(const OpBinary &src); RealVector& operator/=(const OpBinary &src); private: uint32_t m_size; radio_float_t m_real; radio_float_t *m_pBufReal; radio_float_t *m_pPtrReal; attributes_t m_attributes; // Attribute default const attributes_t defaultAttributes() { attributes_t attributes; attributes.negative = 0; return attributes; } // Copy constructor RealVector(const RealVector &src) : m_size(src.m_size) , m_real(0) , m_pBufReal(0) , m_pPtrReal(0) { m_pBufReal = new radio_float_t[m_size]; m_pPtrReal = m_pBufReal; *this = src; } // Re-attribute constructor // Copies no data, just pointers and attributes RealVector(const RealVector &src, attributes_t attributes) : m_size(src.m_size) , m_real(src.m_real) , m_pBufReal(0) , m_pPtrReal(src.m_pPtrReal) , m_attributes(attributes) { } // Slicing constructor RealVector(RealVector &src, uint32_t from, uint32_t step, uint32_t to) : m_size(0) , m_pBufReal(0) , m_pPtrReal(&src.m_pPtrReal[from]) { uint32_t i; for (i=from; i < to; i+=step) { m_size++; } } _inline void checkSize(uint32_t size1, uint32_t size2) { try { if ((size1 != size2) && (size2 != 1)) { throw(RealException("Size mismatch")); } } catch(RealException &exc) { RealDebug("Problem: %s\n", exc.what().c_str()); } } }; // Non-Member // Binary Operators // Returning RealVector::OpBinary RealVector::OpBinary operator+(const RealVector &a, const RealVector &b); RealVector::OpBinary operator-(const RealVector &a, const RealVector &b); RealVector::OpBinary operator*(const RealVector &a, const RealVector &b); RealVector::OpBinary operator/(const RealVector &a, const RealVector &b); #endif // _REAL_HPP_