-
Notifications
You must be signed in to change notification settings - Fork 3
/
yatemath.h
1705 lines (1544 loc) · 49.4 KB
/
yatemath.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* yatemath.h
* This file is part of the YATE Project http://YATE.null.ro
*
* Math data types
*
* Yet Another Telephony Engine - a fully featured software PBX and IVR
* Copyright (C) 2015 Null Team
*
* This software is distributed under multiple licenses;
* see the COPYING file in the main directory for licensing
* information for this specific distribution.
*
* This use of this software may be subject to additional restrictions.
* See the LEGAL file in the main directory for details.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#ifndef __YATEMATH_H
#define __YATEMATH_H
#include <yateclass.h>
#include <math.h>
#include <string.h>
namespace TelEngine {
#ifdef DEBUG
#ifdef _WINDOWS
#define YMATH_FAIL(cond,...) { \
if (!(cond)) \
Debug(DebugFail,__VA_ARGS__); \
}
#else
#define YMATH_FAIL(cond,args...) { \
if (!(cond)) \
Debug(DebugFail,args); \
}
#endif
#else
#ifdef _WINDOWS
#define YMATH_FAIL do { break; } while
#else
#define YMATH_FAIL(arg...)
#endif
#endif
/**
* This class implements a complex number
* @short A Complex (float) number
*/
class YATE_API Complex
{
public:
/**
* Constructor
*/
inline Complex()
: m_real(0), m_imag(0)
{}
/**
* Constructor
* @param real The real part of the complex number
* @param imag The imaginary part of a complex number
*/
inline Complex(float real, float imag = 0)
: m_real(real), m_imag(imag)
{}
/**
* Copy constructor
* @param c The source complex number
*/
inline Complex(const Complex& c)
: m_real(c.m_real), m_imag(c.m_imag)
{}
/**
* Obtain the real part of the complex number
* @return The real part
*/
inline float re() const
{ return m_real; }
/**
* Set the real part of the complex number
* @param r The new real part value
*/
inline void re(float r)
{ m_real = r; }
/**
* Obtain the imaginary part of a complex number
* @return The imaginary part
*/
inline float im() const
{ return m_imag; }
/**
* Set the imaginary part of the complex number
* @param i The new imaginary part value
*/
inline void im(float i)
{ m_imag = i; }
/**
* Set data
* @param r The real part of the complex number
* @param i The imaginary part of a complex number
* @return A reference to this object
*/
inline Complex& set(float r = 0, float i = 0) {
m_real = r;
m_imag = i;
return *this;
}
/**
* Equality operator
* @param c Complex number to compare with
* @return True if equal, false otherwise
*/
inline bool operator==(const Complex& c) const
{ return m_real == c.m_real && m_imag == c.m_imag; }
/**
* Inequality operator
* @param c Complex number to compare with
* @return True if not equal, false otherwise
*/
inline bool operator!=(const Complex& c) const
{ return m_real != c.m_real || m_imag != c.m_imag; }
/**
* Assignment operator
* @param c Complex number to assign
* @return A reference to this object
*/
inline Complex& operator=(const Complex& c)
{ return set(c.m_real,c.m_imag); }
/**
* Assignment operator. Set the real part, reset the imaginary one
* @param real New real part value
* @return A reference to this object
*/
inline Complex& operator=(float real)
{ return set(real); }
/**
* Addition operator
* @param c Complex number to add
* @return A reference to this object
*/
inline Complex& operator+=(const Complex& c)
{ return set(m_real + c.m_real,m_imag + c.m_imag); }
/**
* Addition operator. Add a value to the real part
* @param real Value to add to real part
* @return A reference to this object
*/
inline Complex& operator+=(float real) {
m_real += real;
return *this;
}
/**
* Substraction operator
* @param c Complex number to substract from this one
* @return A reference to this object
*/
inline Complex& operator-=(const Complex& c)
{ return set(m_real - c.m_real,m_imag - c.m_imag); }
/**
* Substraction operator. Substract a value a value from the real part
* @param real Value to substract from real part
* @return A reference to this object
*/
inline Complex& operator-=(float real) {
m_real -= real;
return *this;
}
/**
* Multiplication operator
* @param c Complex number to multiply with
* @return A reference to this object
*/
inline Complex& operator*=(const Complex& c) {
return set(m_real * c.m_real - m_imag * c.m_imag,
m_real * c.m_imag + m_imag * c.m_real);
}
/**
* Multiplication operator. Multiply this number with a float number
* @param f Value to multiply with
* @return A reference to this object
*/
inline Complex& operator*=(float f)
{ return set(m_real * f,m_imag * f); }
/**
* Division operator
* @param c Complex number to devide with
* @return A reference to this object
*/
inline Complex& operator/=(const Complex& c) {
float tmp = c.norm2();
return set((m_real * c.m_real + m_imag * c.m_imag) / tmp,
(-m_real * c.m_imag + m_imag * c.m_real) / tmp);
}
/**
* Division operator
* @param f Float number to devide with
* @return A reference to this object
*/
inline Complex& operator/=(float f)
{ return set(m_real / f,m_imag / f); }
/**
* Compute the absolute value of this complex number
* @return The result
*/
inline float abs() const
{ return ::sqrtf(norm2()); }
/**
* Compute the modulus value of this complex number
* @return The result
*/
inline float mod() const
{ return abs(); }
/**
* Compute the the argument of this complex number
* @return Ther result
*/
inline float arg() const
{ return ::atan(m_imag / m_real); }
/**
* Computes the exponential of this complex number
* @return The result
*/
inline Complex exp() const {
float r = ::expf(m_real);
return Complex(r * ::cosf(m_imag),r * ::sinf(m_imag));
}
/**
* Compute the norm of this complex number
* @return The result
*/
inline float norm() const
{ return abs(); }
/**
* Compute the norm2 value of this complex number
* @return The result
*/
inline float norm2() const
{ return m_real * m_real + m_imag * m_imag; }
private:
float m_real; // The real part
float m_imag; // The imaginary part
};
/**
* This class holds a ref counted storage
* @short A fixed ref counted storage
*/
class YATE_API RefStorage : public RefObject
{
YCLASS(RefStorage,RefObject)
YNOCOPY(RefStorage); // No automatic copies please
public:
/**
* Constructor
* @param value Data to assign, may be NULL to fill with zeros
* @param len Length of data, may be zero (then value is ignored)
*/
inline RefStorage(const void* value, unsigned int len)
: m_data((void*)value,len)
{}
/**
* Get the length of the stored data
* @return The length of the stored data, zero for NULL
*/
inline unsigned int length() const
{ return m_data.length(); }
/**
* Get a pointer to a byte range inside the stored data
* @param offs Byte offset inside the stored data
* @param len Number of bytes that must be valid starting at offset (must not be 0)
* @return A pointer to the data or NULL if the range is not available
*/
inline void* data(unsigned int offs, unsigned int len) const
{ return len ? m_data.data(offs,len) : 0; }
/**
* Copy data to this storage
* @param buf Buffer to copy
* @param len The number of bytes to copy
* @param offs The start index in this storage
* @return True on success, false if there is not enough space in our storage or
* the buffer pointer is NULL
*/
inline bool set(const void* buf, unsigned int len, unsigned int offs = 0)
{ return copy(data(offs,len),buf,len); }
/**
* Fill a buffer
* @param dest Destination buffer
* @param len The number of bytes to fill
* @param val Value to fill with
*/
static inline void fill(void* dest, unsigned int len, int val = 0) {
if (dest && len)
::memset(dest,val,len);
}
/**
* Copy data
* @param dest Destination buffer
* @param src Source buffer
* @param len The number of bytes to copy
* @return True on success, false if parameters are invalid
*/
static inline bool copy(void* dest, const void* src, unsigned int len) {
if (!(len && dest && src))
return len == 0;
if (dest != src)
::memcpy(dest,src,len);
return true;
}
/**
* Compare data
* @param buf1 First buffer
* @param buf2 Second buffer
* @param len The number of bytes to compare
* @return True if equal
*/
static inline bool equals(const void* buf1, const void* buf2, unsigned int len) {
if (len && buf1 && buf2)
return (buf1 == buf2) || (::memcmp(buf1,buf2,len) == 0);
return true;
}
/**
* Split a string and append lines to another one
* @param buf Destination string
* @param str Input string
* @param lineLen Line length, characters to copy
* @param offset Offset in first line (if incomplete). No data will be
* added on first line if offset is greater then line length
* @param linePrefix Prefix for new lines.
* Set it to empty string or 0 to use the suffix
* @param suffix End of line for the last line
* @return Destination string address
*/
static String& dumpSplit(String& buf, const String& str, unsigned int lineLen,
unsigned int offset = 0, const char* linePrefix = 0,
const char* suffix = "\r\n");
private:
RefStorage() {}; // No default constructor please
DataBlock m_data;
};
/**
* Base class for vector class(es).
* Its purpose is to offer a common interface when processing lists
* @short Base class for vector class(es)
*/
class YATE_API MathVectorBase : public GenObject
{
YCLASS(MathVectorBase,GenObject)
public:
/**
* Destructor. Does nothing, keeps the compiler satisfied
*/
virtual ~MathVectorBase()
{}
/**
* Retrieve vector size in bytes
* @return Vector size in bytes
*/
virtual unsigned int vectorSize() const = 0;
};
/**
* Template for vectors holding a fixed storage and a slice in it.
* This class works with objects not holding pointers: it uses memcpy to copy data
* @short A slice vector
*/
template <class Obj> class SliceVector : public MathVectorBase
{
public:
/**
* Constructor
*/
inline SliceVector()
: m_storage(0), m_data(0), m_length(0), m_maxLen(0)
{}
/**
* Copy constructor.
* Builds a slice of another vector
* @param other Original vector
*/
inline SliceVector(const SliceVector& other)
: m_storage(0), m_data(0), m_length(0), m_maxLen(0)
{ initSlice(false,other); }
/**
* Constructor.
* Build the vector storage
* @param len Length of data
* @param buf Optional init buffer ('len' elements will be copied from it to storage)
* @param maxLen Optional vector maximum length
* (it will be adjusted to be at least len)
*/
explicit inline SliceVector(unsigned int len, const Obj* buf = 0,
unsigned int maxLen = 0)
: m_storage(0), m_data(0), m_length(0), m_maxLen(0)
{ initStorage(len,buf,maxLen); }
/**
* Constructor.
* Build a vector by concatenating two existing ones
* @param v1 The first vector
* @param v2 The second vector
*/
explicit inline SliceVector(const SliceVector& v1, const SliceVector& v2)
: m_storage(0), m_data(0), m_length(0), m_maxLen(0) {
if (!initStorage(v1.length(),v1.data(),v1.length() + v2.length()))
return;
resizeMax();
m_storage->set(v2.data(),v2.size(),v1.size());
}
/**
* Constructor.
* Build a vector by concatenating three existing ones
* @param v1 The first vector
* @param v2 The second vector
* @param v3 The third vector
*/
explicit inline SliceVector(const SliceVector& v1, const SliceVector& v2,
const SliceVector& v3)
: m_storage(0), m_data(0), m_length(0), m_maxLen(0) {
unsigned int n = v1.length() + v2.length() + v3.length();
if (!initStorage(v1.length(),v1.data(),n))
return;
resizeMax();
m_storage->set(v2.data(),v2.size(),v1.size());
m_storage->set(v3.data(),v3.size(),v1.size() + v2.size());
}
/**
* Constructor.
* Builds a slice of another vector
* @param other Original vector
* @param offs Offset in the original vector
* @param len The number of elements (0 to use all available from offset)
*/
explicit inline SliceVector(const SliceVector& other, unsigned int offs,
unsigned int len = 0)
: m_storage(0), m_data(0), m_length(0), m_maxLen(0)
{ initSlice(false,other,offs,len); }
/**
* Destructor
*/
virtual ~SliceVector()
{ setData(); }
/**
* Get a pointer to data if 'len' elements are available from offset
* @param offs The offset
* @param len The number of elements to retrieve (must not be 0)
* @return A pointer to data at requested offset,
* NULL if there is not enough data available
*/
inline Obj* data(unsigned int offs, unsigned int len) {
if (len && length() && offs + len <= length())
return m_data + offs;
return 0;
}
/**
* Get a pointer to data if 'len' elements are available from offset
* @param offs The offset
* @param len The number of elements to retrieve (must not be 0)
* @return A pointer to data at requested offset,
* NULL if there is not enough data available
*/
inline const Obj* data(unsigned int offs, unsigned int len) const {
if (len && length() && offs + len <= length())
return m_data + offs;
return 0;
}
/**
* Get a pointer to data from offset to vector end
* @param offs The offset
* @return A pointer to data at requested offset, NULL if there is no data available
*/
inline Obj* data(unsigned int offs = 0)
{ return data(offs,available(offs)); }
/**
* Get a pointer to data from offset to vector end
* @param offs The offset
* @return A pointer to data at requested offset, NULL if there is no data available
*/
inline const Obj* data(unsigned int offs = 0) const
{ return data(offs,available(offs)); }
/**
* Get a pointer to data from offset to vector end
* @param offs The offset
* @param len The number of elements to retrieve (must not be 0)
* @param eod Pointer to be filled with end of data element
* (pointer to first element after requested number of elements)
* @return A pointer to data data from requested offset,
* NULL if there is not enough data available
*/
inline Obj* data(unsigned int offs, unsigned int len, Obj*& eod) {
Obj* d = data(offs,len);
eod = end(d,len);
return d;
}
/**
* Get a pointer to data from offset to vector end
* @param offs The offset
* @param len The number of elements to retrieve (must not be 0)
* @param eod Pointer to be filled with end of data element
* (pointer to first element after requested number of elements)
* @return A pointer to data data from requested offset,
* NULL if there is not enough data available
*/
inline const Obj* data(unsigned int offs, unsigned int len, const Obj*& eod) const {
const Obj* d = data(offs,len);
eod = end(d,len);
return d;
}
/**
* Get the length of the vector
* @return The length of the vector
*/
inline unsigned int length() const
{ return m_length; }
/**
* Get the maximum length of the vector
* @return The maximum length of the vector
* (0 if the vector don't have a storage buffer)
*/
inline unsigned int maxLen() const
{ return m_maxLen; }
/**
* Get the vector size in bytes
* @return Vector size in bytes
*/
inline unsigned int size() const
{ return size(length()); }
/**
* Retrieve the available number of elements from given offset
* (not more than required number)
* @param offs The offset
* @param len Required number of elements (-1: all available from offset)
* @return The available number of elements from given offset
* (may be less than required)
*/
inline unsigned int available(unsigned int offs, int len = -1) const {
if (len && offs < length()) {
unsigned int rest = length() - offs;
return (len < 0 || rest <= (unsigned int)len) ? rest : (unsigned int)len;
}
return 0;
}
/**
* Retrieve the available number of elements from given offset.
* Clamp the available number of elements to requested value
* @param clamp Maximum number of elements to check
* @param offs The offset
* @param len Required number of elements (-1: all available from offset)
* @return The available number of elements from given offset
*/
inline unsigned int availableClamp(unsigned int clamp, unsigned int offs = 0,
int len = -1) const {
offs = available(offs,len);
return clamp <= offs ? clamp : offs;
}
/**
* Retrieve vector size in bytes
* @return Vector size in bytes
*/
virtual unsigned int vectorSize() const
{ return size(); }
/**
* Change the vector length without changing the contents.
* If the vector length is increased the new elements' value are not reset
* @param len New vector length
* @return True on success, false if requested length is greater than max length
*/
inline bool resize(unsigned int len) {
if (len <= maxLen()) {
m_length = len;
return true;
}
return false;
}
/**
* Change the vector length to maximum allowed without changing the contents
*/
inline void resizeMax()
{ resize(maxLen()); }
/**
* Steal other vector's data
* @param other Original vector
*/
inline void steal(SliceVector& other) {
m_storage = other.m_storage;
m_data = other.m_data;
m_length = other.m_length;
m_maxLen = other.m_maxLen;
other.m_storage = 0;
other.m_data = 0;
other.m_length = other.m_maxLen = 0;
}
/**
* Change the vector storage (re-allocate)
* @param len New vector length (0 to clear vector data)
* @param maxLen Optional vector maximum length
* (it will be adjusted to be at least len)
*/
inline void resetStorage(unsigned int len, unsigned int maxLen = 0) {
setData();
initStorage(len,0,maxLen);
}
/**
* Set a slice containing another vector
* @param other Original vector
* @param offs Offset in the original vector
* @param len The number of elements (0 to use all available from offset)
* @return True on success, false on failure
*/
inline bool setSlice(const SliceVector& other, unsigned int offs = 0,
unsigned int len = 0)
{ return initSlice(true,other,offs,len); }
/**
* Retrieve vector head
* @param len The number of elements to retrieve
* @return A vector containing the first elements of this vector
*/
inline SliceVector head(unsigned int len) const
{ return slice(0,len); }
/**
* Retrieve vector head
* @param dest Destination vector
* @param len The number of elements to retrieve
* @return True on success, false on failure (not enough data in our vector)
*/
inline bool head(SliceVector& dest, unsigned int len) const
{ return slice(dest,0,len); }
/**
* Retrieve vector tail (last elements)
* @param len The number of elements to retrieve
* @return A vector containing the last elements of this vector
*/
inline SliceVector tail(unsigned int len) const {
if (len < length())
return SliceVector(*this,length() - len,len);
return SliceVector();
}
/**
* Retrieve vector tail (last elements)
* @param len The number of elements to retrieve
* @param dest Destination vector
* @return True on success, false on failure (not enough data in our vector)
*/
inline bool tail(SliceVector& dest, unsigned int len) const {
if (len <= length())
return dest.initSlice(true,*this,length() - len,len);
dest.setData();
return false;
}
/**
* Retrieve a vector slice
* @param offs Offset in our vector
* @param len The number of elements to retrieve
* @return A vector containing the requested slice
* (empty if offset/length are invalid)
*/
inline SliceVector slice(unsigned int offs, unsigned int len) const
{ return SliceVector(*this,offs,len); }
/**
* Set a slice of this vector to another one.
* The destination vector will be changed
* @param dest Destination vector
* @param offs Offset in our vector
* @param len The number of elements (0 to use all available from offset)
* @return True on success, false on failure (not enough data in our vector)
*/
inline bool slice(SliceVector& dest, unsigned int offs,
unsigned int len = 0) const
{ return dest.initSlice(true,*this,offs,len); }
/**
* Copies elements from another vector to this one.
* NOTE: This method don't check for overlapping data
* @param src The source vector
* @param len The number of elements to copy
* @param offs The start index in our vector
* @param srcOffs The start index in the source vector
* @return True on success, false on failure (not enough data in source vector or
* not enough space in this vector)
*/
inline bool copy(const SliceVector& src, unsigned int len,
unsigned int offs = 0, unsigned int srcOffs = 0)
{ return RefStorage::copy(data(offs,len),src.data(srcOffs,len),size(len)); }
/**
* Fill the buffer with 0
* @param offs The offset
* @param len The number of elements to retrieve (must not be 0)
*/
inline void bzero(unsigned int offs, unsigned int len)
{ RefStorage::fill(data(offs,len),size(len)); }
/**
* Fill the buffer with 0
*/
inline void bzero()
{ RefStorage::fill(data(),size()); }
/**
* Fill the vector with a given value
* @param value The value to be set in this vector
*/
inline void fill(const Obj& value) {
Obj* d = data();
for (Obj* last = end(d,length()); d != last; ++d)
*d = value;
}
/**
* Apply an unary function to all elements in this vector
* @param func Function to apply
*/
inline void apply(void (*func)(Obj&)) {
Obj* d = data();
for (Obj* last = end(d,length()); d != last; ++d)
(*func)(*d);
}
/**
* Sum vector values
* @return The sum of the vector elements
*/
inline Obj sum() const {
Obj result(0);
const Obj* d = data();
for (const Obj* last = end(d,length()); d != last; ++d)
result += *d;
return result;
}
/**
* Apply a function to all vector elements and take the sum of the results
* @param func Function to apply
* @return The result
*/
inline Obj sumApply(Obj (*func)(const Obj&)) const {
Obj result(0);
const Obj* d = data();
for (const Obj* last = end(d,length()); d != last; ++d)
result += (*func)(*d);
return result;
}
/**
* Apply a function to all vector elements and take the sum of the results
* @param func Function to apply
* @return The result
*/
inline float sumApplyF(float (*func)(const Obj&)) const {
float result = 0;
const Obj* d = data();
for (const Obj* last = end(d,length()); d != last; ++d)
result += (*func)(*d);
return result;
}
/**
* Sum this vector with another one
* @param other Vector to sum with this one
* @return True on sucess, false on failure (vectors don't have the same length)
*/
inline bool sum(const SliceVector& other) {
if (length() != other.length())
return false;
const Obj* od = other.m_data;
Obj* d = data();
for (Obj* last = end(d,length()); d != last; ++d, ++od)
*d += *od;
return true;
}
/**
* Add a value to each element of the vector
* @param value Value to add
*/
inline void sum(const Obj& value) {
Obj* d = data();
for (Obj* last = end(d,length()); d != last; ++d)
*d += value;
}
/**
* Substract another vector from this one
* @param other Vector to substract
* @return True on sucess, false on failure (vectors don't have the same length)
*/
inline bool sub(const SliceVector& other) {
if (length() != other.length())
return false;
const Obj* od = other.m_data;
Obj* d = data();
for (Obj* last = end(d,length()); d != last; ++d, ++od)
*d -= *od;
return true;
}
/**
* Substract a value from each element of the vector
* @param value Value to substract
*/
inline void sub(const Obj& value) {
Obj* d = data();
for (Obj* last = end(d,length()); d != last; ++d)
*d -= value;
}
/**
* Multiply this vector with another one
* @param other Vector to multiply with
* @return True on sucess, false on failure (vectors don't have the same length)
*/
inline bool mul(const SliceVector& other) {
if (length() != other.length())
return false;
const Obj* od = other.m_data;
Obj* d = data();
for (Obj* last = end(d,length()); d != last; ++d, ++od)
*d *= *od;
return true;
}
/**
* Multiply this vector with a value
* @param value Value to multiply with
*/
inline void mul(const Obj& value) {
Obj* d = data();
for (Obj* last = end(d,length()); d != last; ++d)
*d *= value;
}
/**
* Multiply this vector with a value
* @param value Value to multiply with
*/
inline void mul(float value) {
Obj* d = data();
for (Obj* last = end(d,length()); d != last; ++d)
*d *= value;
}
/**
* Indexing operator with unsigned int
* @param index Index of element to retrieve
* @return The element at requested index
*/
inline Obj& operator[](unsigned int index) {
YMATH_FAIL(index < m_length,
"SliceVector::operator[] index out of bounds [%p]",this);
return m_data[index];
}
/**
* Indexing operator with unsigned int
* @param index Index of element to retrieve
* @return The element at requested index
*/
inline const Obj& operator[](unsigned int index) const {
YMATH_FAIL(index < m_length,
"SliceVector::operator[] index out of bounds [%p]",this);
return m_data[index];
}
/**
* Indexing operator with signed int
* @param index Index of element to retrieve
* @return The element at requested index
*/
inline Obj& operator[](signed int index) {
YMATH_FAIL((unsigned int)index < m_length,
"SliceVector::operator[] index out of bounds [%p]",this);
return m_data[index];
}
/**
* Indexing operator with signed int
* @param index Index of element to retrieve
* @return The element at requested index
*/
inline const Obj& operator[](signed int index) const {
YMATH_FAIL((unsigned int)index < m_length,
"SliceVector::operator[] index out of bounds [%p]",this);
return m_data[index];
}
/**
* Equality operator
* @param other Original vector
* @return True if the vectors are equal, false otherwise
*/
inline bool operator==(const SliceVector& other) const
{ return equals(other); }
/**
* Inequality operator
* @param other Original vector
* @return True if the vectors are not equal, false otherwise
*/
inline bool operator!=(const SliceVector& other) const
{ return !equals(other); }
/**
* Asignment operator. Builds a slice of another vector
* @param other Original vector
* @return A reference to this vector
*/
inline SliceVector& operator=(const SliceVector& other) {
setSlice(other);
return *this;
}
/**
* Sum this vector with another one
* @param other Vector to sum with this one
* @return A reference to this vector
*/
inline SliceVector& operator+=(const SliceVector& other) {
YMATH_FAIL(length() == other.length(),
"SliceVector(+=): invalid lengths [%p]",this);
sum(other);
return *this;
}
/**
* Add a value to each element of the vector
* @param value Value to add
* @return A reference to this vector