25 #include <yateclass.h>
33 #define YMATH_FAIL(cond,...) { \
35 Debug(DebugFail,__VA_ARGS__); \
38 #define YMATH_FAIL(cond,args...) { \
40 Debug(DebugFail,args); \
45 #define YMATH_FAIL do { break; } while
47 #define YMATH_FAIL(arg...)
62 : m_real(0), m_imag(0)
70 inline Complex(
float real,
float imag = 0)
71 : m_real(real), m_imag(imag)
79 : m_real(c.m_real), m_imag(c.m_imag)
86 inline float re()
const
93 inline void re(
float r)
100 inline float im()
const
107 inline void im(
float i)
128 {
return m_real == c.m_real && m_imag == c.m_imag; }
136 {
return m_real != c.m_real || m_imag != c.m_imag; }
144 {
return set(c.m_real,c.m_imag); }
152 {
return set(real); }
160 {
return set(m_real + c.m_real,m_imag + c.m_imag); }
178 {
return set(m_real - c.m_real,m_imag - c.m_imag); }
196 return set(m_real * c.m_real - m_imag * c.m_imag,
197 m_real * c.m_imag + m_imag * c.m_real);
206 {
return set(m_real * f,m_imag * f); }
214 float tmp = c.
norm2();
215 return set((m_real * c.m_real + m_imag * c.m_imag) / tmp,
216 (-m_real * c.m_imag + m_imag * c.m_real) / tmp);
225 {
return set(m_real / f,m_imag / f); }
232 { return ::sqrtf(norm2()); }
246 { return ::atan(m_imag / m_real); }
253 float r = ::expf(m_real);
254 return Complex(r * ::cosf(m_imag),r * ::sinf(m_imag));
269 {
return m_real * m_real + m_imag * m_imag; }
292 : m_data((void*)value,len)
300 {
return m_data.length(); }
308 inline void*
data(
unsigned int offs,
unsigned int len)
const
309 {
return len ? m_data.data(offs,len) : 0; }
319 inline bool set(
const void* buf,
unsigned int len,
unsigned int offs = 0)
320 {
return copy(data(offs,len),buf,len); }
328 static inline void fill(
void* dest,
unsigned int len,
int val = 0) {
330 ::memset(dest,val,len);
340 static inline bool copy(
void* dest,
const void* src,
unsigned int len) {
341 if (!(len && dest && src))
344 ::memcpy(dest,src,len);
355 static inline bool equals(
const void* buf1,
const void* buf2,
unsigned int len) {
356 if (len && buf1 && buf2)
357 return (buf1 == buf2) || (::memcmp(buf1,buf2,len) == 0);
374 unsigned int offset = 0,
const char* linePrefix = 0,
375 const char* suffix =
"\r\n");
403 virtual unsigned int vectorSize()
const = 0;
419 : m_storage(0), m_data(0), m_length(0), m_maxLen(0)
428 : m_storage(0), m_data(0), m_length(0), m_maxLen(0)
429 { initSlice(
false,other); }
441 : m_storage(0), m_data(0), m_length(0), m_maxLen(0)
442 { initStorage(len,buf,
maxLen); }
451 : m_storage(0), m_data(0), m_length(0), m_maxLen(0) {
467 : m_storage(0), m_data(0), m_length(0), m_maxLen(0) {
484 unsigned int len = 0)
485 : m_storage(0), m_data(0), m_length(0), m_maxLen(0)
486 { initSlice(
false,other,offs,len); }
501 inline Obj*
data(
unsigned int offs,
unsigned int len) {
503 return m_data + offs;
514 inline const Obj*
data(
unsigned int offs,
unsigned int len)
const {
516 return m_data + offs;
525 inline Obj*
data(
unsigned int offs = 0)
533 inline const Obj*
data(
unsigned int offs = 0)
const
545 inline Obj*
data(
unsigned int offs,
unsigned int len, Obj*& eod) {
546 Obj* d =
data(offs,len);
560 inline const Obj*
data(
unsigned int offs,
unsigned int len,
const Obj*& eod)
const {
561 const Obj* d =
data(offs,len);
585 inline unsigned int size()
const
596 inline unsigned int available(
unsigned int offs,
int len = -1)
const {
597 if (len && offs <
length()) {
598 unsigned int rest =
length() - offs;
599 return (len < 0 || rest <= (
unsigned int)len) ? rest : (
unsigned int)len;
613 int len = -1)
const {
615 return clamp <= offs ? clamp : offs;
650 m_storage = other.m_storage;
651 m_data = other.m_data;
652 m_length = other.m_length;
653 m_maxLen = other.m_maxLen;
656 other.m_length = other.m_maxLen = 0;
667 initStorage(len,0,
maxLen);
678 unsigned int len = 0)
679 {
return initSlice(
true,other,offs,len); }
687 {
return slice(0,len); }
696 {
return slice(dest,0,len); }
717 return dest.initSlice(
true,*
this,
length() - len,len);
741 unsigned int len = 0)
const
742 {
return dest.initSlice(
true,*
this,offs,len); }
755 unsigned int offs = 0,
unsigned int srcOffs = 0)
763 inline void bzero(
unsigned int offs,
unsigned int len)
776 inline void fill(
const Obj& value) {
778 for (Obj* last = end(d,
length()); d != last; ++d)
786 inline void apply(
void (*func)(Obj&)) {
788 for (Obj* last = end(d,
length()); d != last; ++d)
798 const Obj* d =
data();
799 for (
const Obj* last = end(d,
length()); d != last; ++d)
809 inline Obj
sumApply(Obj (*func)(
const Obj&))
const {
811 const Obj* d =
data();
812 for (
const Obj* last = end(d,
length()); d != last; ++d)
813 result += (*func)(*d);
822 inline float sumApplyF(
float (*func)(
const Obj&))
const {
824 const Obj* d =
data();
825 for (
const Obj* last = end(d,
length()); d != last; ++d)
826 result += (*func)(*d);
838 const Obj* od = other.m_data;
840 for (Obj* last = end(d,
length()); d != last; ++d, ++od)
849 inline void sum(
const Obj& value) {
851 for (Obj* last = end(d,
length()); d != last; ++d)
863 const Obj* od = other.m_data;
865 for (Obj* last = end(d,
length()); d != last; ++d, ++od)
874 inline void sub(
const Obj& value) {
876 for (Obj* last = end(d,
length()); d != last; ++d)
888 const Obj* od = other.m_data;
890 for (Obj* last = end(d,
length()); d != last; ++d, ++od)
899 inline void mul(
const Obj& value) {
901 for (Obj* last = end(d,
length()); d != last; ++d)
909 inline void mul(
float value) {
911 for (Obj* last = end(d,
length()); d != last; ++d)
921 YMATH_FAIL(index < m_length,
922 "SliceVector::operator[] index out of bounds [%p]",
this);
923 return m_data[index];
932 YMATH_FAIL(index < m_length,
933 "SliceVector::operator[] index out of bounds [%p]",
this);
934 return m_data[index];
943 YMATH_FAIL((
unsigned int)index < m_length,
944 "SliceVector::operator[] index out of bounds [%p]",
this);
945 return m_data[index];
954 YMATH_FAIL((
unsigned int)index < m_length,
955 "SliceVector::operator[] index out of bounds [%p]",
this);
956 return m_data[index];
973 {
return !
equals(other); }
992 "SliceVector(+=): invalid lengths [%p]",
this);
1014 "SliceVector(-=): invalid lengths [%p]",
this);
1036 "SliceVector(*=): invalid lengths [%p]",
this);
1081 String& (*func)(
String& s,
const Obj& o,
const char* sep,
const char* fmt),
1082 const char* sep =
",",
const char* fmt = 0)
const {
1083 const Obj* d =
data();
1087 for (
const Obj* last = end(d,
length()); d != last; ++d)
1088 (*func)(localBuf,*d,sep,fmt);
1089 return buf.
append(localBuf);
1108 String& (*func)(
String& s,
const Obj& o,
const char* sep,
const char* fmt),
1109 unsigned int offset = 0,
const char* linePrefix = 0,
1110 const char* suffix =
"\r\n",
const char* sep =
",",
const char* fmt = 0)
const {
1111 const Obj* d =
data();
1113 return buf.
append(suffix);
1115 linePrefix = suffix;
1117 return dump(buf,func,sep,fmt) << suffix;
1119 for (
const Obj* last = end(d,
length()); d != last;) {
1121 (*func)(tmp,*d,0,fmt);
1125 if (offset > lineLen) {
1126 localBuf << linePrefix;
1131 return buf << localBuf << suffix;
1155 unsigned int offset = 0,
const char* linePrefix = 0,
1156 const char* suffix =
"\r\n")
const {
1176 int unHexify(
const char* str,
unsigned int len,
int sep = 255) {
1179 bool ok = (sep < -128 || sep > 127) ? db.
unHexify(str,len) :
1202 {
return sizeof(Obj); }
1209 static inline unsigned int size(
unsigned int len)
1214 inline Obj* end(Obj* start,
unsigned int len)
1215 {
return start ? (start + len) : 0; }
1216 inline const Obj* end(
const Obj* start,
unsigned int len)
const
1217 {
return start ? (start + len) : 0; }
1220 inline bool setData(Obj*
data = 0,
unsigned int len = 0,
unsigned int maxLen = 0) {
1227 m_length = m_maxLen = 0;
1234 inline bool initStorage(
unsigned int len,
const Obj*
data = 0,
1235 unsigned int maxLen = 0) {
1246 return setData((Obj*)m_storage->
data(0,1),len,
maxLen);
1250 inline bool initSlice(
bool del,
const SliceVector& other,
unsigned int offs = 0,
1251 unsigned int len = 0) {
1253 len = other.length();
1254 Obj* d = (Obj*)other.data(offs,len);
1260 if (m_storage == other.m_storage)
1261 return setData(d,len,len);
1262 RefStorage* tmp = other.m_storage;
1266 return setData(d,len,len);
1268 Debug(DebugFail,
"SliceVector storage ref() failed");
1269 return del ? setData() : false;
1272 RefStorage* m_storage;
1274 unsigned int m_length;
1275 unsigned int m_maxLen;
1278 typedef SliceVector<Complex> ComplexVector;
1279 typedef SliceVector<float> FloatVector;
1280 typedef SliceVector<uint8_t> ByteVector;
1314 explicit inline BitVector(
unsigned int len,
unsigned int maxLen = 0)
1315 : ByteVector(len,0,maxLen)
1326 unsigned int len = 0)
1327 : ByteVector(other,offs,len)
1335 explicit BitVector(
const char* str,
unsigned int maxLen = 0);
1349 bool get(FloatVector& dest)
const;
1357 bool set(
const FloatVector& input);
1367 void xorMsb(uint32_t value,
unsigned int offs = 0, uint8_t len = 32);
1377 inline void xorMsb16(uint16_t value,
unsigned int offs = 0, uint8_t len = 16)
1378 {
return xorMsb((uint32_t)value << 16,offs,len <= 16 ? len : 16); }
1387 uint64_t pack(
unsigned int offs = 0,
int len = -1)
const;
1395 void unpack(uint64_t value,
unsigned int offs = 0, uint8_t len = 64);
1404 void unpackMsb(uint32_t value,
unsigned int offs = 0, uint8_t len = 32);
1413 inline void unpackMsb16(uint16_t value,
unsigned int offs = 0, uint8_t len = 16)
1414 { unpackMsb((uint32_t)value << 16,offs,len <= 16 ? len : 16); }
1424 bool pack(ByteVector& dest)
const;
1433 bool unpack(
const ByteVector& src);
1442 String& appendTo(
String& buf,
unsigned int offs = 0,
int len = -1)
const;
1452 return appendTo(tmp,offs,len);
1463 unsigned int len = 0)
1464 {
return initSlice(
true,other,offs,len); }
1472 {
return slice(0,len); }
1481 {
return slice(dest,0,len); }
1490 return BitVector(*
this,length() - len,len);
1501 if (len <= length())
1502 return dest.initSlice(
true,*
this,length() - len,len);
1526 {
return dest.initSlice(
true,*
this,offs,len); }
1546 const char* fmt = 0);
1556 static String& dumpFloat(
String& buf,
const float& val,
const char* sep = 0,
1557 const char* fmt = 0);
bool operator!=(const SliceVector &other) const
Definition: yatemath.h:972
Complex operator-(const Complex &c1, const Complex &c2)
Definition: yatemath.h:1602
RefStorage(const void *value, unsigned int len)
Definition: yatemath.h:291
bool operator==(const Complex &c) const
Definition: yatemath.h:127
String & appendTo(String &buf, unsigned int offs=0, int len=-1) const
float norm() const
Definition: yatemath.h:261
void * data(unsigned int offs, unsigned int len) const
Definition: yatemath.h:308
const char * c_str() const
Definition: yateclass.h:1938
A class that holds just a block of raw data.
Definition: yateclass.h:3774
SliceVector & operator*=(const SliceVector &other)
Definition: yatemath.h:1034
Definition: yateclass.h:949
Complex & operator-=(float real)
Definition: yatemath.h:185
void Debug(int level, const char *format,...)
float arg() const
Definition: yatemath.h:245
Obj & operator[](signed int index)
Definition: yatemath.h:942
void apply(void(*func)(Obj &))
Definition: yatemath.h:786
const Obj * data(unsigned int offs=0) const
Definition: yatemath.h:533
Complex operator/(const Complex &c1, const Complex &c2)
Definition: yatemath.h:1661
Complex(float real, float imag=0)
Definition: yatemath.h:70
void steal(SliceVector &other)
Definition: yatemath.h:649
void mul(float value)
Definition: yatemath.h:909
BitVector slice(unsigned int offs, unsigned int len) const
Definition: yatemath.h:1514
SliceVector(const SliceVector &other)
Definition: yatemath.h:427
float abs() const
Definition: yatemath.h:231
Obj sumApply(Obj(*func)(const Obj &)) const
Definition: yatemath.h:809
SliceVector slice(unsigned int offs, unsigned int len) const
Definition: yatemath.h:729
Complex & operator*=(const Complex &c)
Definition: yatemath.h:195
unsigned int availableClamp(unsigned int clamp, unsigned int offs=0, int len=-1) const
Definition: yatemath.h:612
void resetStorage(unsigned int len, unsigned int maxLen=0)
Definition: yatemath.h:665
bool setSlice(const BitVector &other, unsigned int offs=0, unsigned int len=0)
Definition: yatemath.h:1462
bool sub(const SliceVector &other)
Definition: yatemath.h:860
static String & dumpSplit(String &buf, const String &str, unsigned int lineLen, unsigned int offset=0, const char *linePrefix=0, const char *suffix="\r\n")
Complex & operator+=(const Complex &c)
Definition: yatemath.h:159
A Complex (float) number.
Definition: yatemath.h:55
const Obj * data(unsigned int offs, unsigned int len) const
Definition: yatemath.h:514
BitVector()
Definition: yatemath.h:1295
void mul(const Obj &value)
Definition: yatemath.h:899
static bool equals(const void *buf1, const void *buf2, unsigned int len)
Definition: yatemath.h:355
Math utilities.
Definition: yatemath.h:1534
void sub(const Obj &value)
Definition: yatemath.h:874
SliceVector head(unsigned int len) const
Definition: yatemath.h:686
A slice vector.
Definition: yatemath.h:412
float mod() const
Definition: yatemath.h:238
SliceVector(unsigned int len, const Obj *buf=0, unsigned int maxLen=0)
Definition: yatemath.h:439
virtual ~MathVectorBase()
Definition: yatemath.h:396
Complex & operator/=(float f)
Definition: yatemath.h:224
const Obj & operator[](unsigned int index) const
Definition: yatemath.h:931
unsigned int length() const
Definition: yatemath.h:299
static bool copy(void *dest, const void *src, unsigned int len)
Definition: yatemath.h:340
const Obj & operator[](signed int index) const
Definition: yatemath.h:953
String & hexify(void *data, unsigned int len, char sep=0, bool upCase=false)
Complex & operator+=(float real)
Definition: yatemath.h:167
void destruct(GenObject *obj)
Definition: yateclass.h:933
void unpackMsb16(uint16_t value, unsigned int offs=0, uint8_t len=16)
Definition: yatemath.h:1413
SliceVector & operator=(const SliceVector &other)
Definition: yatemath.h:980
Obj & operator[](unsigned int index)
Definition: yatemath.h:920
const Obj * data(unsigned int offs, unsigned int len, const Obj *&eod) const
Definition: yatemath.h:560
Complex(const Complex &c)
Definition: yatemath.h:78
bool copy(const SliceVector &src, unsigned int len, unsigned int offs=0, unsigned int srcOffs=0)
Definition: yatemath.h:754
Obj * data(unsigned int offs, unsigned int len, Obj *&eod)
Definition: yatemath.h:545
Complex & operator-=(const Complex &c)
Definition: yatemath.h:177
Complex exp() const
Definition: yatemath.h:252
float im() const
Definition: yatemath.h:100
bool operator==(const SliceVector &other) const
Definition: yatemath.h:964
String & dump(String &buf, unsigned int lineLen, String &(*func)(String &s, const Obj &o, const char *sep, const char *fmt), unsigned int offset=0, const char *linePrefix=0, const char *suffix="\r\n", const char *sep=",", const char *fmt=0) const
Definition: yatemath.h:1107
String & dumpHex(String &buf, unsigned int lineLen, unsigned int offset=0, const char *linePrefix=0, const char *suffix="\r\n") const
Definition: yatemath.h:1154
BitVector tail(unsigned int len) const
Definition: yatemath.h:1488
void * data() const
Definition: yateclass.h:3827
unsigned int maxLen() const
Definition: yatemath.h:578
Obj * data(unsigned int offs, unsigned int len)
Definition: yatemath.h:501
SliceVector(const SliceVector &other, unsigned int offs, unsigned int len=0)
Definition: yatemath.h:483
SliceVector & operator+=(const SliceVector &other)
Definition: yatemath.h:990
A slice vector holding bits.
Definition: yatemath.h:1289
unsigned int length() const
Definition: yatemath.h:570
bool set(const void *buf, unsigned int len, unsigned int offs=0)
Definition: yatemath.h:319
Base class for vector class(es)
Definition: yatemath.h:389
String & append(const char *value, int len)
Complex & set(float r=0, float i=0)
Definition: yatemath.h:116
unsigned int available(unsigned int offs, int len=-1) const
Definition: yatemath.h:596
SliceVector & operator*=(const Obj &value)
Definition: yatemath.h:1046
void sum(const Obj &value)
Definition: yatemath.h:849
bool head(SliceVector &dest, unsigned int len) const
Definition: yatemath.h:695
bool null(const char *str)
Definition: yateclass.h:2829
bool tail(BitVector &dest, unsigned int len) const
Definition: yatemath.h:1500
SliceVector & operator-=(const Obj &value)
Definition: yatemath.h:1024
float norm2() const
Definition: yatemath.h:268
void resizeMax()
Definition: yatemath.h:642
String operator+(const String &s1, const String &s2)
SliceVector()
Definition: yatemath.h:418
unsigned int size() const
Definition: yatemath.h:585
SliceVector(const SliceVector &v1, const SliceVector &v2, const SliceVector &v3)
Definition: yatemath.h:465
String & operator<<(String &str, const Complex &c)
Definition: yatemath.h:1685
Complex & operator=(float real)
Definition: yatemath.h:151
A fixed ref counted storage.
Definition: yatemath.h:281
SliceVector & operator+=(const Obj &value)
Definition: yatemath.h:1002
bool slice(BitVector &dest, unsigned int offs, unsigned int len=0) const
Definition: yatemath.h:1525
SliceVector & operator-=(const SliceVector &other)
Definition: yatemath.h:1012
void bzero()
Definition: yatemath.h:769
void im(float i)
Definition: yatemath.h:107
String & dump(String &buf, String &(*func)(String &s, const Obj &o, const char *sep, const char *fmt), const char *sep=",", const char *fmt=0) const
Definition: yatemath.h:1080
Definition: yateclass.h:217
bool resize(unsigned int len)
Definition: yatemath.h:631
BitVector(const BitVector &other)
Definition: yatemath.h:1303
Complex operator*(const Complex &c1, const Complex &c2)
Definition: yatemath.h:1626
bool equals(const SliceVector &other) const
Definition: yatemath.h:1066
SliceVector & operator*=(float value)
Definition: yatemath.h:1056
A C-style string handling class.
Definition: yateclass.h:1832
String & hexify(String &buf, char sep=0) const
Definition: yatemath.h:1140
int unHexify(const String &str, int sep=255)
Definition: yatemath.h:1194
Complex()
Definition: yatemath.h:61
bool setSlice(const SliceVector &other, unsigned int offs=0, unsigned int len=0)
Definition: yatemath.h:677
bool tail(SliceVector &dest, unsigned int len) const
Definition: yatemath.h:715
Obj sum() const
Definition: yatemath.h:796
void re(float r)
Definition: yatemath.h:93
BitVector(const BitVector &other, unsigned int offs, unsigned int len=0)
Definition: yatemath.h:1325
BitVector head(unsigned int len) const
Definition: yatemath.h:1471
static unsigned int objSize()
Definition: yatemath.h:1201
unsigned int length() const
Definition: yateclass.h:3859
unsigned int length() const
Definition: yateclass.h:1960
Obj * data(unsigned int offs=0)
Definition: yatemath.h:525
SliceVector(const SliceVector &v1, const SliceVector &v2)
Definition: yatemath.h:450
virtual unsigned int vectorSize() const
Definition: yatemath.h:622
bool head(BitVector &dest, unsigned int len) const
Definition: yatemath.h:1480
float re() const
Definition: yatemath.h:86
bool unHexify(const char *data, unsigned int len, char sep)
Complex & operator/=(const Complex &c)
Definition: yatemath.h:213
void YCLASS(class type, class base)
bool operator!=(const Complex &c) const
Definition: yatemath.h:135
void bzero(unsigned int offs, unsigned int len)
Definition: yatemath.h:763
Complex & operator=(const Complex &c)
Definition: yatemath.h:143
void xorMsb16(uint16_t value, unsigned int offs=0, uint8_t len=16)
Definition: yatemath.h:1377
bool sum(const SliceVector &other)
Definition: yatemath.h:835
static String & dumpComplex(String &buf, const Complex &val, const char *sep=0, const char *fmt=0)
float sumApplyF(float(*func)(const Obj &)) const
Definition: yatemath.h:822
SliceVector tail(unsigned int len) const
Definition: yatemath.h:703
String toString(unsigned int offs, int len=-1) const
Definition: yatemath.h:1450
static unsigned int size(unsigned int len)
Definition: yatemath.h:1209
virtual ~SliceVector()
Definition: yatemath.h:491
Complex & operator*=(float f)
Definition: yatemath.h:205
Definition: yateclass.h:830
static void fill(void *dest, unsigned int len, int val=0)
Definition: yatemath.h:328
int unHexify(const char *str, unsigned int len, int sep=255)
Definition: yatemath.h:1176
bool mul(const SliceVector &other)
Definition: yatemath.h:885
void fill(const Obj &value)
Definition: yatemath.h:776
BitVector(unsigned int len, unsigned int maxLen=0)
Definition: yatemath.h:1314
bool slice(SliceVector &dest, unsigned int offs, unsigned int len=0) const
Definition: yatemath.h:740