-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpr_vector.h
More file actions
898 lines (758 loc) · 40.9 KB
/
expr_vector.h
File metadata and controls
898 lines (758 loc) · 40.9 KB
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
// Provided using BSD license
// Author: Patricio Loncomilla, year 2023
// NOTE: In g++, compile with -O3
// NOTE: In msvc, compile with /std:c++14 /O2 /EHsc. Not using /EHsc will cause the code to crash
#ifndef EXPR_VECTOR_H_PL_
#define EXPR_VECTOR_H_PL_
#include <vector>
#include <cstddef>
#include <stdexcept>
#include <type_traits>
#include <cmath>
#include <functional>
#include <iostream>
#include <limits>
#include <sstream>
#include <memory>
// Start of main classes for ExprVector
/** ExprVectorException represents an exception related to incorrect ExprVector resizing **/
class ExprVectorException : public std::exception
{
public:
ExprVectorException(const char* what_arg) { std::cerr << what_arg << std::endl; what_ptr_ = std::make_shared<std::string>(what_arg);}
const char *what() const noexcept override {return what_ptr_->c_str();}
std::shared_ptr<std::string> what_ptr_;
};
/** BuffData represents a buffer, which can be a std::vector<T> or a buffer pointer T* */
template<typename T>
class BuffDataExt
{
T* buffer_;
size_t n_;
public:
BuffDataExt() : buffer_(nullptr), n_(0) {}
BuffDataExt(const BuffDataExt& other) = delete;
void setBuffer(T* buffer, size_t n) {buffer_=buffer; n_=n;}
void setBuffer(const T* buffer, size_t n) {buffer_=const_cast<T*>(buffer); n_=n;}
inline T operator[](const std::size_t i) const
{
return buffer_[i];
}
inline T& operator[](const std::size_t i)
{
return buffer_[i];
}
inline T* data() {return buffer_;}
inline std::size_t size() const
{
return n_;
}
};
template<typename T, typename Op1>
class BuffDataStrided
{
public:
Op1& op1;
long start;
long end;
long step;
size_t n;
BuffDataStrided(Op1& a, long start, long end, long step) : op1(a), start(start), end(end), step(step)
{
n = (std::abs(end-start) + abs(step)-1) / abs(step);
}
inline T operator[](const std::size_t i) const
{
return op1[start + i*step];
}
inline T& operator[](const std::size_t i)
{
return op1[start + i*step];
}
inline std::size_t size() const
{
return n;
}
};
class ExprVectorDefaultIndex
{
public:
constexpr ExprVectorDefaultIndex() {};
};
/** ExprVectorNeg represents the negation of a ExprVector */
template<typename T, typename Op1>
class ExprVectorNeg {
const Op1& op1;
public:
ExprVectorNeg(const Op1& a) : op1(a) {}
inline T operator[](const std::size_t i) const
{
return -op1[i];
}
inline std::size_t size() const
{
return op1.size();
}
};
namespace expr_vector_default_index
{
#if __GNUC__ < 6 || (__GNUC__ == 6 && __GNUC_MINOR__ <= 1) // Old compiler
static constexpr long _ = std::numeric_limits<long>::max();
#else
static constexpr ExprVectorDefaultIndex _ = ExprVectorDefaultIndex();
#endif
};
namespace ev
{
template< class... >
using void_t = void;
struct nonesuch {
nonesuch() = delete;
~nonesuch() = delete;
nonesuch(nonesuch const&) = delete;
void operator=(nonesuch const&) = delete;
};
namespace detail {
template <class Default, class AlwaysVoid,
template<class...> class Op, class... Args>
struct detector {
using value_t = std::false_type;
using type = Default;
};
template <class Default, template<class...> class Op, class... Args>
struct detector<Default, void_t<Op<Args...>>, Op, Args...> {
using value_t = std::true_type;
using type = Op<Args...>;
};
} // namespace detail
template <template<class...> class Op, class... Args>
using is_detected = typename detail::detector<nonesuch, void, Op, Args...>::value_t;
template <template<class...> class Op, class... Args>
using detected_t = typename detail::detector<nonesuch, void, Op, Args...>::type;
template <class Default, template<class...> class Op, class... Args>
using detected_or = detail::detector<Default, void, Op, Args...>;
template <class Expected, template<class...> class Op, class... Args>
using is_detected_exact = std::is_same<Expected, detected_t<Op, Args...>>;
template<class C>
using has_resize =
decltype(std::declval<C&>().resize(std::declval<size_t>()));
}
/** ExprVector is the main class which represents a vector/buffer using expression templates */
template<typename T, typename Cont = std::vector<T>> //BuffDataExt<T> >
class ExprVector
{
public:
Cont cont;
using DI = ExprVectorDefaultIndex;
// Empty constructed ExprVector must be given a buffer before being used
ExprVector() {}
// ExprVector with initial size
ExprVector(const std::size_t n) : cont(n) {}
// ExprVector with initial size and value
ExprVector(const std::size_t n, const T initialValue) : cont(n, initialValue) {}
// Constructor for underlying container
ExprVector(const Cont& other) : cont(other) {}
template <typename T2> //template <typename T2, typename std::enable_if<std::is_same<Cont, std::vector<T2>>::value, nullptr_t>::type = nullptr>
ExprVector(std::initializer_list<T2> other)
{
try_resize_if_needed(other.size());
for (std::size_t i = 0; i < cont.size(); ++i)
cont[i] = (other.begin())[i];
}
operator ExprVector<T, std::vector<T>>() const {ExprVector<T, std::vector<T>> x; x = *this; return x;}
template <typename Cont2=Cont, typename std::enable_if<ev::is_detected_exact<void, ev::has_resize, Cont2>::value && std::is_same<Cont2,Cont>::value, nullptr_t>::type = nullptr> // template <typename T2=T, typename std::enable_if<!std::is_same<Cont, BuffDataExt<T2>>::value, nullptr_t>::type = nullptr>
void resize(size_t n) {cont.resize(n);}
template <typename T2=T, typename std::enable_if<std::is_same<Cont, BuffDataExt<T2>>::value && std::is_same<T,T2>::value, nullptr_t>::type = nullptr>
void setBuffer(T* buffer, size_t n) {cont.setBuffer(buffer,n);}
template <typename T2=T, typename std::enable_if<std::is_same<Cont, BuffDataExt<T2>>::value && std::is_same<T,T2>::value, nullptr_t>::type = nullptr>
void setBuffer(const T* buffer, size_t n) {cont.setBuffer(buffer,n);} //!< Please don't modify an ExprVector after using this function
template <typename Cont2=Cont, typename std::enable_if<ev::is_detected_exact<void, ev::has_resize, Cont2>::value && std::is_same<Cont2,Cont>::value, nullptr_t>::type = nullptr>
inline void try_resize_if_needed(size_t n)
{
if (cont.size() == 0 || cont.size() != n)
cont.resize(n);
}
template <typename Cont2=Cont, typename std::enable_if<!ev::is_detected_exact<void, ev::has_resize, Cont2>::value && std::is_same<Cont2,Cont>::value, nullptr_t>::type = nullptr> // template<typename T2=T, typename R2=Cont, typename std::enable_if<!std::is_same<Cont, std::vector<T2>>::value, nullptr_t>::type = nullptr>
inline void try_resize_if_needed(size_t n) {}
// assignment operator for ExprVector of different type
template<typename T2=T, typename R2=Cont>
ExprVector& operator=(const ExprVector<T2, R2>& other)
{
try_resize_if_needed(other.size());
for (std::size_t i = 0; i < cont.size(); ++i)
cont[i] = other[i];
return *this;
}
// assignment operator for ExprVector of same type
ExprVector& operator=(const ExprVector& other)
{
try_resize_if_needed(other.size());
for (std::size_t i = 0; i < cont.size(); ++i)
cont[i] = other[i];
return *this;
}
template<typename T2=T, typename R2=Cont, typename std::enable_if<std::is_move_assignable<R2>::value && std::is_same<T2,T>::value && std::is_same<R2,Cont>::value, nullptr_t>::type = nullptr>
ExprVector& operator=(ExprVector&& other)
{
cont = std::move(other.cont);
return *this;
}
void operator=(const T& val)
{
for (std::size_t i = 0; i < cont.size(); ++i)
cont[i] = val;
}
ExprVector& operator=(std::initializer_list<T> other)
{
try_resize_if_needed(other.size());
for (std::size_t i = 0; i < cont.size(); ++i)
cont[i] = (other.begin())[i];
return *this;
}
// size of underlying container
inline std::size_t size() const
{
return cont.size();
}
// index operators
inline T operator[](const std::size_t i) const
{
return cont[i];
}
inline T& operator[](const std::size_t i)
{
return cont[i];
}
// Negative of ExprVector
inline ExprVector<T, ExprVectorNeg<T, Cont> > operator-() {return ExprVector<T, ExprVectorNeg<T, Cont>>(ExprVectorNeg<T, Cont>(contents()));}
#if __GNUC__ < 6 || (__GNUC__ == 6 && __GNUC_MINOR__ <= 1) // Old compiler
inline ExprVector<T, BuffDataStrided<T, Cont>> operator[](std::initializer_list<long> start_end_step)
{
using namespace expr_vector_default_index;
long start = start_end_step.begin()[0];
long end = start_end_step.begin()[1];
long step = start_end_step.begin()[2];
DI x;
if (start_end_step.size() == 3)
{
if (start == _ && end == _ && step == _)
return operator[](std::make_tuple(x,x,x)); //return [](std::make_tuple<DI,DI,DI>(x,x,x));
if (start == _ && end == _ && step != _)
return operator[](std::make_tuple(x,x,step)); //return [](std::make_tuple<DI,DI,long>(x,x,step));
if (start == _ && end != _ && step == _)
return operator[](std::make_tuple(x,end,x)); //return [](std::make_tuple<DI,long,DI>(x,end,x));
if (start == _ && end != _ && step != _)
return operator[](std::make_tuple(x,end,step)); //return [](std::make_tuple<DI,long,long>(x,end,step));
if (start != _ && end == _ && step == _)
return operator[](std::make_tuple(start,x,x)); //return [](std::make_tuple<long,DI,DI>(start,x,x));
if (start != _ && end == _ && step != _)
return operator[](std::make_tuple(start,x,step)); //return [](std::make_tuple<long,DI,long>(start,x,step));
if (start != _ && end != _ && step == _)
return operator[](std::make_tuple(start,end,x)); //return [](std::make_tuple<long,long,DI>(start,end,x));
if (start != _ && end != _ && step != _)
return operator[](std::make_tuple(start,end,step)); //return [](std::make_tuple<long,long,long>(start,end,step));
}
else
{
if (start == _ && end == _)
return operator[](std::make_tuple(x,x)); //return [](std::make_tuple<DI,DI>(x,x));
if (start == _ && end != _)
return operator[](std::make_tuple(x,end)); //return [](std::make_tuple<DI,long>(x,end));
if (start != _ && end == _)
return operator[](std::make_tuple(start,x)); //return [](std::make_tuple<long,DI>(start,x));
if (start != _ && end != _)
return operator[](std::make_tuple(start,end)); //return [](std::make_tuple<long,long>(start,end));
}
return ExprVector<T, BuffDataStrided<T, Cont>>( BuffDataStrided<T, Cont>(contents(), start, end, step) ); // This could not be executed
}
#endif
// Slice of ExprVector
inline ExprVector<T, BuffDataStrided<T, Cont>> operator[](std::tuple<DI,DI,DI> start_end_step)
{
long start = 0;
long end = size();
long step = 1;
return ExprVector<T, BuffDataStrided<T, Cont>>( BuffDataStrided<T, Cont>(contents(), start, end, step) );
}
// Slice of ExprVector
inline ExprVector<T, BuffDataStrided<T, Cont>> operator[](std::tuple<DI,DI,long> start_end_step)
{
long start;
long end;
long step = std::get<2>(start_end_step);
if (step > 0)
{
start = 0;
end = size();
}
else
{
start = size()-1;
end = -1;
}
return ExprVector<T, BuffDataStrided<T, Cont>>( BuffDataStrided<T, Cont>(contents(), start, end, step) );
}
// Slice of ExprVector
inline ExprVector<T, BuffDataStrided<T, Cont>> operator[](std::tuple<DI,long,DI> start_end_step)
{
long start = 0;
long end = std::get<1>(start_end_step);
long step = 1;
return ExprVector<T, BuffDataStrided<T, Cont>>( BuffDataStrided<T, Cont>(contents(), start, end, step) );
}
// Slice of ExprVector
inline ExprVector<T, BuffDataStrided<T, Cont>> operator[](std::tuple<DI,long,long> start_end_step)
{
long start;
long end = std::get<1>(start_end_step);
long step = std::get<2>(start_end_step);
if (step > 0)
start = 0;
else
start = size()-1;
while (end < 0)
end += size();
return ExprVector<T, BuffDataStrided<T, Cont>>( BuffDataStrided<T, Cont>(contents(), start, end, step) );
}
// Slice of ExprVector
inline ExprVector<T, BuffDataStrided<T, Cont>> operator[](std::tuple<long,DI,DI> start_end_step)
{
long start = std::get<0>(start_end_step);
long end = size();
long step = 1;
return ExprVector<T, BuffDataStrided<T, Cont>>( BuffDataStrided<T, Cont>(contents(), start, end, step) );
}
// Slice of ExprVector
inline ExprVector<T, BuffDataStrided<T, Cont>> operator[](std::tuple<long,DI,long> start_end_step)
{
long start = std::get<0>(start_end_step);
long end;
long step = std::get<2>(start_end_step);
if (step > 0)
end = size();
else
end = -1;
while (start < 0)
start += size();
return ExprVector<T, BuffDataStrided<T, Cont>>( BuffDataStrided<T, Cont>(contents(), start, end, step) );
}
// Slice of ExprVector
inline ExprVector<T, BuffDataStrided<T, Cont>> operator[](std::tuple<long,long,DI> start_end_step)
{
long start = std::get<0>(start_end_step);
long end = std::get<1>(start_end_step);
long step = 1;
while (start < 0)
start += size();
while (end < 0)
end += size();
return ExprVector<T, BuffDataStrided<T, Cont>>( BuffDataStrided<T, Cont>(contents(), start, end, step) );
}
// Slice of ExprVector
inline ExprVector<T, BuffDataStrided<T, Cont>> operator[](std::tuple<long,long,long> start_end_step)
{
long start = std::get<0>(start_end_step);
long end = std::get<1>(start_end_step);
long step = std::get<2>(start_end_step);
while (start < 0)
start += size();
while (end < 0)
end += size();
return ExprVector<T, BuffDataStrided<T, Cont>>( BuffDataStrided<T, Cont>(contents(), start, end, step) );
}
// Slice of ExprVector
inline ExprVector<T, BuffDataStrided<T, Cont>> operator[](std::tuple<DI,DI> start_end)
{
long start = 0;
long end = size();
long step = 1;
return ExprVector<T, BuffDataStrided<T, Cont>>( BuffDataStrided<T, Cont>(contents(), start, end, step) );
}
// Slice of ExprVector
inline ExprVector<T, BuffDataStrided<T, Cont>> operator[](std::tuple<DI,long> start_end)
{
long start = 0;
long end = std::get<1>(start_end);
long step = 1;
return ExprVector<T, BuffDataStrided<T, Cont>>( BuffDataStrided<T, Cont>(contents(), start, end, step) );
}
// Slice of ExprVector
inline ExprVector<T, BuffDataStrided<T, Cont>> operator[](std::tuple<long,DI> start_end)
{
long start = std::get<0>(start_end);
long end = size();
long step = 1;
return ExprVector<T, BuffDataStrided<T, Cont>>( BuffDataStrided<T, Cont>(contents(), start, end, step) );
}
// Slice of ExprVector
inline ExprVector<T, BuffDataStrided<T, Cont>> operator[](std::tuple<long,long> start_end)
{
long start = std::get<0>(start_end);
long end = std::get<1>(start_end);
long step = 1;
while (start < 0)
start += size();
while (end < 0)
end += size();
return ExprVector<T, BuffDataStrided<T, Cont>>( BuffDataStrided<T, Cont>(contents(), start, end, step) );
}
inline T sum()
{
if (size() == 0)
{
throw std::logic_error("ExprVector::sum() called with zero length buffer");
}
T val = cont[0];
for (size_t i=1; i<size(); i++)
val = val + cont[i]; // += not used because the base class could not have defined it
return val;
}
inline size_t count(const T& val)
{
size_t amount = 0;
for (size_t i=0; i<size(); i++)
amount += (cont[i] == val);
return amount;
}
// returns the underlying data
inline const Cont& contents() const
{
return cont;
}
inline Cont& contents()
{
return cont;
}
inline T* data()
{
return cont.data();
}
inline T* begin()
{
return cont.data();
}
inline T* end()
{
return cont.data() + cont.size();
}
std::vector<T> vect() {size_t n = size(); std::vector<T> v(n); for (size_t i=0; i<n; i++) v[i] = (*this)[i]; return v;}
ExprVector<T,BuffDataExt<T>> toExt() {ExprVector<T,BuffDataExt<T>> ret; ret.setBuffer(data(), size()); return ret;}
static ExprVector zeros(size_t n) {ExprVector v(n,0); return v;}
static ExprVector linspace(T start, T stop, long n) {ExprVector v(n); for (size_t i=0; i<n; i++) v[i] = start + i * (stop-start)/(n-1); return v;}
//static ExprVector arange(T start, T stop, T step=1) {long n = (stop - start + step - 1) / step; if (n<=0) return ExprVector(0); ExprVector v(n); for (size_t i=0; i<n; i++) v[i] = start + step * i; return v;}
static ExprVector arange(T start, T stop, T step=1) {long n = int(ceil((stop - start) / step)); if (n<=0) return ExprVector(0); ExprVector v(n); for (size_t i=0; i<n; i++) v[i] = start + step * i; return v;}
static ExprVector arange(T stop) {return arange(0, stop, 1);}
static ExprVector iota(T start, T stop) {return arange(start, stop);}
static bool plot_py (const ExprVector& x, const ExprVector& y) {std::stringstream ss; ss << "python -c \"" << "import matplotlib.pyplot as plt; plt.plot(" << x << ", " << y <<"); plt.show()\""; int ret = system(ss.str().c_str()); if (ret != 0) return false; return true;}
static bool plot_py2(const ExprVector& x, const ExprVector& y) {std::stringstream ss; ss << "python2 -c \"" << "import matplotlib.pyplot as plt; plt.plot(" << x << ", " << y <<"); plt.show()\""; int ret = system(ss.str().c_str()); if (ret != 0) return false; return true;}
static bool plot_py3(const ExprVector& x, const ExprVector& y) {std::stringstream ss; ss << "python3 -c \"" << "import matplotlib.pyplot as plt; plt.plot(" << x << ", " << y <<"); plt.show()\""; int ret = system(ss.str().c_str()); if (ret != 0) return false; return true;}
static void plot(const ExprVector& x, const ExprVector& y) { if (!plot_py(x,y) && !plot_py3(x,y) && !plot_py2(x,y)) std::cout << "python+matplotlib was not found for plotting, or too much points to plot" << std::endl;}
static void plot(const std::vector<T>& x, const std::vector<T>& y) {ExprVector<T, BuffDataExt<T>> xx; ExprVector<T, BuffDataExt<T>> yy; xx.setBuffer(x.data(),x.size()); yy.setBuffer(y.data(),y.size()); plot(xx,yy);}
};
template <typename T, typename Cont>
std::ostream& operator<<(std::ostream& os, const ExprVector<T,Cont> & ev)
{
os << "[";
if (ev.size() > 0)
{
os << ev[0];
for (size_t i = 1; i < ev.size(); i++)
os << ", " << ev[i];
}
os << "]";
return os;
}
template <typename Cont>
std::ostream& operator<<(std::ostream& os, const ExprVector<std::string,Cont> & ev)
{
os << "[\"";
if (ev.size() > 0)
{
os << ev[0];
for (size_t i = 1; i < ev.size(); i++)
os << "\", \"" << ev[i];
}
os << "\"]";
return os;
}
#define ADD_EXPR_VECT_OPERATOR_2_ARGS(NAME, OP) \
\
template<typename T, typename Op1, typename Op2> \
class NAME \
{ \
const Op1& op1; \
const Op2& op2; \
\
public: \
using type = decltype(op1[0] OP op2[0]); \
NAME(const Op1& a, const Op2& b) : op1(a), op2(b) {} \
\
inline type operator[](const std::size_t i) const \
{ \
return op1[i] OP op2[i]; \
} \
\
inline std::size_t size() const \
{ \
return op1.size(); \
} \
}; \
\
\
template<typename T, typename R1, typename R2> \
inline ExprVector<typename NAME<T, R1, R2>::type, NAME<T, R1, R2> > \
operator OP (const ExprVector<T, R1>& a, const ExprVector<T, R2>& b) \
{ \
return ExprVector<typename NAME<T, R1, R2>::type, NAME<T, R1, R2> >(NAME<T, R1, R2 >(a.contents(), b.contents())); \
} \
#define ADD_EXPR_VECT_PRE_OP(NAME, OP) \
template<typename T, typename Op2> \
class NAME \
{ \
const T val1; \
const Op2& op2; \
\
public: \
NAME(T a, const Op2& b) : val1(a), op2(b) {} \
\
inline T operator[](const std::size_t i) const \
{ \
return val1 OP op2[i]; \
} \
\
inline std::size_t size() const \
{ \
return op2.size(); \
} \
}; \
\
template<typename T, typename R2> \
inline ExprVector<T, NAME<T, R2> > \
operator OP(T a, const ExprVector<T, R2>& b) \
{ \
return ExprVector<T, NAME<T, R2> >(NAME<T, R2 >(a, b.contents())); \
} \
#define ADD_EXPR_VECT_POST_OP(NAME, OP) \
template<typename T, typename Op1> \
class NAME \
{ \
const Op1& op1; \
const T val2; \
\
public: \
NAME(const Op1& a, T b) : op1(a), val2(b) {} \
\
inline T operator[](const std::size_t i) const \
{ \
return op1[i] OP val2; \
} \
\
inline std::size_t size() const \
{ \
return op1.size(); \
} \
}; \
\
template<typename T, typename R1> \
inline ExprVector<T, NAME<T, R1> > \
operator OP(const ExprVector<T, R1>& a, T b) \
{ \
return ExprVector<T, NAME<T, R1> >(NAME<T, R1 >(a.contents(), b)); \
}
#define ADD_EXPR_VECT_PRE_SCALAR(NAME, OP, TYPE) \
template<typename T, typename Op2, typename std::enable_if<!std::is_same<T,TYPE>::value, nullptr_t>::type = nullptr> \
class NAME \
{ \
const TYPE val1; \
const Op2& op2; \
\
public: \
NAME(TYPE a, const Op2& b) : val1(a), op2(b) {} \
\
inline T operator[](const std::size_t i) const \
{ \
return val1 OP op2[i]; \
} \
\
inline std::size_t size() const \
{ \
return op2.size(); \
} \
}; \
\
template<typename T, typename R2, typename std::enable_if<!std::is_same<T,TYPE>::value, nullptr_t>::type = nullptr> \
inline ExprVector<T, NAME<T, R2> > \
operator OP(TYPE a, const ExprVector<T, R2>& b) \
{ \
return ExprVector<T, NAME<T, R2> >(NAME<T, R2 >(a, b.contents())); \
} \
#define ADD_EXPR_VECT_POST_SCALAR(NAME, OP, TYPE) \
template<typename T, typename Op2, typename std::enable_if<!std::is_same<T,TYPE>::value, nullptr_t>::type = nullptr> \
class NAME \
{ \
const Op2& op2; \
const TYPE val1; \
\
public: \
NAME(TYPE a, const Op2& b) : val1(a), op2(b) {} \
\
inline T operator[](const std::size_t i) const \
{ \
return val1 OP op2[i]; \
} \
\
inline std::size_t size() const \
{ \
return op2.size(); \
} \
}; \
\
template<typename T, typename R2> \
inline ExprVector<T, NAME<T, R2> > \
operator OP(const ExprVector<T, R2>& b, TYPE a) \
{ \
return ExprVector<T, NAME<T, R2> >(NAME<T, R2 >(a, b.contents())); \
} \
\
// This is not too much faster than vectors because of function calls..
#define ADD_EXPR_VECT_FN_1_ARG(NAME, fn) \
\
template<typename T, typename Op1> \
class NAME \
{ \
const Op1& op1; \
\
public: \
using type = decltype(fn(op1[0])); \
NAME(const Op1& a) : op1(a) {} \
\
inline type operator[](const std::size_t i) const \
{ \
return fn(op1[i]); \
} \
\
inline std::size_t size() const \
{ \
return op1.size(); \
} \
}; \
\
\
template<typename T, typename R1> \
ExprVector<typename NAME<T, R1>::type, NAME<T, R1> > \
inline fn (const ExprVector<T, R1>& a) \
{ \
return ExprVector<typename NAME<T, R1>::type, NAME<T, R1> >(NAME<T, R1>(a.contents())); \
} \
// This is not too much faster than vectors because of function calls..
#define ADD_EXPR_VECT_FN_2_ARG(NAME, fn) \
\
template<typename T, typename Op1, typename Op2> \
class NAME \
{ \
const Op1& op1; \
const Op2& op2; \
\
public: \
NAME(const Op1& a, const Op1& b) : op1(a), op2(b) {} \
\
inline T operator[](const std::size_t i) const \
{ \
return fn(op1[i], op2[i]); \
} \
\
inline std::size_t size() const \
{ \
return op1.size(); \
} \
}; \
\
\
template<typename T, typename R1, typename R2> \
ExprVector<T, NAME<T, R1, R2> > \
inline fn (const ExprVector<T, R1>& a, const ExprVector<T, R2>& b) \
{ \
return ExprVector<T, NAME<T, R1, R2> >(NAME<T, R1, R2>(a.contents(), b.contents())); \
} \
ADD_EXPR_VECT_OPERATOR_2_ARGS(ExprVectorAdd, +)
ADD_EXPR_VECT_OPERATOR_2_ARGS(ExprVectorSubtr, -)
ADD_EXPR_VECT_OPERATOR_2_ARGS(ExprVectorMult, *)
ADD_EXPR_VECT_OPERATOR_2_ARGS(ExprVectorDiv, /)
ADD_EXPR_VECT_PRE_OP(ExprVectPreSum, +)
ADD_EXPR_VECT_PRE_OP(ExprVectPreSubtr, -)
ADD_EXPR_VECT_PRE_OP(ExprVectPreMult, *)
ADD_EXPR_VECT_PRE_OP(ExprVectPreDiv, /)
ADD_EXPR_VECT_POST_OP(ExprVectPostSum, +)
ADD_EXPR_VECT_POST_OP(ExprVectPostSubtr, -)
ADD_EXPR_VECT_POST_OP(ExprVectPostMult, *)
ADD_EXPR_VECT_POST_OP(ExprVectPostDiv, /)
ADD_EXPR_VECT_PRE_SCALAR(ExprVectPreMultDouble, *, double)
ADD_EXPR_VECT_POST_SCALAR(ExprVectPostMultDouble, *, double)
ADD_EXPR_VECT_PRE_SCALAR(ExprVectPreMultInt, *, int)
ADD_EXPR_VECT_POST_SCALAR(ExprVectPostMultInt, *, int)
ADD_EXPR_VECT_POST_SCALAR(ExprVectPostDivDouble, /, double)
ADD_EXPR_VECT_FN_1_ARG(ExprVectorSin, sin)
ADD_EXPR_VECT_FN_1_ARG(ExprVectorCos, cos)
ADD_EXPR_VECT_FN_1_ARG(ExprVectorSqrt, sqrt)
ADD_EXPR_VECT_FN_1_ARG(ExprVectorAbs, abs)
ADD_EXPR_VECT_FN_2_ARG(ExprVectorAtan2, atan2)
#define ADD_EXPR_VECT_PRE_OP_VECT(NAME, OP, TYPE) \
template<typename T, typename Op1, typename Op2, typename std::enable_if<!std::is_same<T,TYPE>::value, nullptr_t>::type = nullptr> \
class NAME \
{ \
const Op1& op1; \
const Op2& op2; \
\
public: \
NAME(const Op1& a, const Op2& b) : op1(a), op2(b) {} \
\
inline T operator[](const std::size_t i) const \
{ \
return op1[i] OP op2[i]; \
} \
\
inline std::size_t size() const \
{ \
return op1.size(); \
} \
}; \
\
\
template<typename T, typename R1, typename R2> \
inline ExprVector<T, NAME<T, R1, R2> > \
operator OP (const ExprVector<TYPE, R1>& a, const ExprVector<T, R2>& b) \
{ \
return ExprVector<T, NAME<T, R1, R2> >(NAME<T, R1, R2 >(a.contents(), b.contents())); \
} \
#define ADD_EXPR_VECT_POST_OP_VECT(NAME, OP, TYPE) \
template<typename T, typename Op1, typename Op2, typename std::enable_if<!std::is_same<T,TYPE>::value, nullptr_t>::type = nullptr> \
class NAME \
{ \
const Op1& op1; \
const Op2& op2; \
\
public: \
NAME(const Op1& a, const Op2& b) : op1(a), op2(b) {} \
\
inline T operator[](const std::size_t i) const \
{ \
return op1[i] OP op2[i]; \
} \
\
inline std::size_t size() const \
{ \
return op1.size(); \
} \
}; \
\
\
template<typename T, typename R1, typename R2> \
inline ExprVector<T, NAME<T, R1, R2> > \
operator OP (const ExprVector<T, R1>& a, const ExprVector<TYPE, R2>& b) \
{ \
return ExprVector<T, NAME<T, R1, R2> >(NAME<T, R1, R2 >(a.contents(), b.contents())); \
} \
ADD_EXPR_VECT_PRE_OP_VECT(ExprVectPreMultVectDouble, *, double)
ADD_EXPR_VECT_POST_OP_VECT(ExprVectPostMultVectDouble, *, double)
ADD_EXPR_VECT_POST_OP_VECT(ExprVectPostMultDivDouble, /, double)
#endif // EXPR_VECTOR_H_PL_