git-svn-id: http://moon:8086/svn/software/trunk/libsrc/radio@10 b431acfa-c32f-4a4a-93f1-934dc6c82436
235 lines
5.1 KiB
C++
Executable File
235 lines
5.1 KiB
C++
Executable File
#ifndef _REAL_HPP_
|
|
#define _REAL_HPP_
|
|
|
|
#include <string>
|
|
#include <memory.h>
|
|
#include <radio/radio_types.h>
|
|
|
|
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;
|
|
|
|
class RealVector
|
|
{
|
|
public:
|
|
friend class _RealVector;
|
|
struct unary_t
|
|
{
|
|
bool negative;
|
|
|
|
unary_t()
|
|
: negative(0)
|
|
{
|
|
}
|
|
|
|
struct unary_t neg(bool doNegative)
|
|
{
|
|
negative = doNegative;
|
|
return *this;
|
|
}
|
|
|
|
};
|
|
|
|
enum OpType
|
|
{
|
|
Add = 0,
|
|
Sub,
|
|
Mul,
|
|
Div
|
|
};
|
|
|
|
template <OpType op>
|
|
class OpBinary
|
|
{
|
|
public:
|
|
const RealVector &a;
|
|
const RealVector &b;
|
|
unary_t m_unary_modifier;
|
|
|
|
OpBinary(const RealVector &a, const RealVector &b)
|
|
: a(a)
|
|
, b(b)
|
|
, m_unary_modifier()
|
|
{
|
|
}
|
|
|
|
OpBinary(const OpBinary &src, unary_t unary_modifier)
|
|
: a(src.a)
|
|
, b(src.b)
|
|
, m_unary_modifier(unary_modifier)
|
|
{
|
|
}
|
|
|
|
bool isNegative() const
|
|
{
|
|
return m_unary_modifier.negative;
|
|
}
|
|
|
|
bool isPositive() const
|
|
{
|
|
return !m_unary_modifier.negative;
|
|
}
|
|
|
|
OpBinary operator-() const
|
|
{
|
|
unary_t unary = m_unary_modifier;
|
|
unary.negative = !unary.negative;
|
|
|
|
return OpBinary(*this, unary);
|
|
}
|
|
|
|
const OpBinary& operator+() const
|
|
{
|
|
return *this;
|
|
}
|
|
|
|
~OpBinary()
|
|
{
|
|
}
|
|
|
|
private:
|
|
OpBinary& operator=(const OpBinary&);
|
|
};
|
|
|
|
|
|
// Construct from float array
|
|
RealVector(uint32_t size, radio_float_t *real=0);
|
|
|
|
// Copy constructor
|
|
RealVector(const RealVector &src);
|
|
|
|
// Slicing constructor
|
|
RealVector(const RealVector &src, uint32_t from, uint32_t step, uint32_t to);
|
|
|
|
// Unary resolver constructor (no data copy)
|
|
RealVector(const RealVector &src, unary_t unary_modifier);
|
|
|
|
// OpBinaryAdd resolver copy constructor
|
|
RealVector(const OpBinary<Add> &src);
|
|
|
|
// OpBinarySub resolver copy constructor
|
|
RealVector(const OpBinary<Sub> &src);
|
|
|
|
// OpBinaryMul resolver copy constructor
|
|
RealVector(const OpBinary<Mul> &src);
|
|
|
|
// OpBinaryDiv resolver copy constructor
|
|
RealVector(const OpBinary<Div> &src);
|
|
|
|
// Scalar constructor
|
|
RealVector(radio_float_t real);
|
|
|
|
RealVector(const _RealVector &src);
|
|
|
|
~RealVector(void);
|
|
|
|
uint32_t getSize() const;
|
|
radio_float_t getValue() const;
|
|
RealVector at(uint32_t const &i);
|
|
RealVector slice(uint32_t from, uint32_t step, uint32_t to);
|
|
void print(char *pPrefix) const;
|
|
bool isNegative() const;
|
|
bool isPositive() const;
|
|
RealVector operator-() const;
|
|
const RealVector& operator+() const;
|
|
|
|
// 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<Add> &src);
|
|
RealVector& operator+=(const OpBinary<Add> &src);
|
|
RealVector& operator-=(const OpBinary<Add> &src);
|
|
RealVector& operator*=(const OpBinary<Add> &src);
|
|
RealVector& operator/=(const OpBinary<Add> &src);
|
|
|
|
RealVector& operator=(const OpBinary<Sub> &src);
|
|
RealVector& operator+=(const OpBinary<Sub> &src);
|
|
RealVector& operator-=(const OpBinary<Sub> &src);
|
|
RealVector& operator*=(const OpBinary<Sub> &src);
|
|
RealVector& operator/=(const OpBinary<Sub> &src);
|
|
|
|
RealVector& operator=(const OpBinary<Mul> &src);
|
|
RealVector& operator+=(const OpBinary<Mul> &src);
|
|
RealVector& operator-=(const OpBinary<Mul> &src);
|
|
RealVector& operator*=(const OpBinary<Mul> &src);
|
|
RealVector& operator/=(const OpBinary<Mul> &src);
|
|
|
|
RealVector& operator=(const OpBinary<Div> &src);
|
|
RealVector& operator+=(const OpBinary<Div> &src);
|
|
RealVector& operator-=(const OpBinary<Div> &src);
|
|
RealVector& operator*=(const OpBinary<Div> &src);
|
|
RealVector& operator/=(const OpBinary<Div> &src);
|
|
|
|
unary_t m_unary_modifier;
|
|
|
|
private:
|
|
uint32_t m_size;
|
|
radio_float_t m_real;
|
|
radio_float_t *m_pBufReal;
|
|
radio_float_t *m_pPtrReal;
|
|
|
|
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());
|
|
}
|
|
}
|
|
};
|
|
|
|
class _RealVector
|
|
{
|
|
friend class RealVector;
|
|
public:
|
|
_RealVector(const RealVector &src);
|
|
_RealVector(const _RealVector &src, RealVector::unary_t unary_modifier);
|
|
_RealVector operator-() const;
|
|
uint32_t m_size;
|
|
|
|
private:
|
|
radio_float_t m_real;
|
|
radio_float_t *m_pBufReal;
|
|
radio_float_t *m_pPtrReal;
|
|
RealVector::unary_t m_unary_modifier;
|
|
|
|
};
|
|
|
|
// Non-Member
|
|
// Binary Operators
|
|
// Returning RealVector::OpBinary
|
|
RealVector::OpBinary<RealVector::Add> operator+(const RealVector &a, const RealVector &b);
|
|
RealVector::OpBinary<RealVector::Sub> operator-(const RealVector &a, const RealVector &b);
|
|
RealVector::OpBinary<RealVector::Mul> operator*(const RealVector &a, const RealVector &b);
|
|
RealVector::OpBinary<RealVector::Div> operator/(const RealVector &a, const RealVector &b);
|
|
|
|
#endif // _REAL_HPP_
|