28 #ifndef CASA_FUNCTORS_H
29 #define CASA_FUNCTORS_H
43 template<
typename InputIterator1,
typename InputIterator2,
typename BinaryOperator>
45 InputIterator2 first2, BinaryOperator op)
47 for (; first1!=last1; ++first1, ++first2) {
48 *first1 = op(*first1, *first2);
55 template<
typename InputIterator1,
typename UnaryOperator>
59 for (; first1!=last1; ++first1) {
60 *first1 = op(*first1);
67 template<
typename InputIterator,
typename MaskIterator,
typename Accum,
typename BinaryOperator>
69 MaskIterator
mask, Accum acc,
70 BinaryOperator op = std::plus<Accum>())
73 if (*mask) acc = op(acc, *first);
81 template<
typename InputIterator,
typename MaskIterator,
typename Accum,
typename BinaryOperator>
83 MaskIterator mask, Accum acc,
84 BinaryOperator op = std::plus<Accum>())
87 if (!*mask) acc = op(acc, *first);
96 template<
typename InputIterator1,
typename InputIterator2,
typename CompareOperator>
97 inline bool compareAll (InputIterator1 first1, InputIterator1 last1,
98 InputIterator2 first2, CompareOperator op)
100 for (; first1!=last1; ++first1, ++first2) {
101 if (!op(*first1, *first2))
return false;
108 template<
typename InputIterator1,
typename T,
typename CompareOperator>
110 T left, CompareOperator op)
112 for (; first1!=last1; ++first1) {
113 if (!op(left, *first1))
return false;
120 template<
typename InputIterator1,
typename T,
typename CompareOperator>
122 T right, CompareOperator op)
124 for (; first1!=last1; ++first1) {
125 if (!op(*first1, right))
return false;
135 template<
typename InputIterator1,
typename InputIterator2,
typename CompareOperator>
136 inline bool compareAny (InputIterator1 first1, InputIterator1 last1,
137 InputIterator2 first2, CompareOperator op)
139 for (; first1!=last1; ++first1, ++first2) {
140 if (op(*first1, *first2))
return true;
147 template<
typename InputIterator1,
typename T,
typename CompareOperator>
149 T left, CompareOperator op)
151 for (; first1!=last1; ++first1) {
152 if (op(left, *first1))
return true;
159 template<
typename InputIterator1,
typename T,
typename CompareOperator>
161 T right, CompareOperator op)
163 for (; first1!=last1; ++first1) {
164 if (op(*first1, right))
return true;
174 template <
typename L,
typename R=L,
typename RES=L>
175 struct Plus :
public std::binary_function<L,R,RES>
183 template <
typename L,
typename R=L,
typename RES=L>
184 struct Minus :
public std::binary_function<L,R,RES>
192 template <
typename L,
typename R=L,
typename RES=L>
201 template <
typename L,
typename R=L,
typename RES=L>
202 struct Divides :
public std::binary_function<L,R,RES>
211 template <
typename L,
typename R=L,
typename RES=L>
212 struct Modulo :
public std::binary_function<L,R,RES>
220 template <
typename L,
typename R=L,
typename RES=L>
221 struct FloorMod :
public std::binary_function<L,R,RES>
224 {
return floormod (RES(x), RES(y)); }
228 template <
typename T>
229 struct BitAnd :
public std::binary_function<T,T,T>
236 template <
typename T>
237 struct BitOr :
public std::binary_function<T,T,T>
244 template <
typename T>
245 struct BitXor :
public std::binary_function<T,T,T>
252 template <
typename T>
266 struct IsNaN :
public std::unary_function<T,bool>
269 {
return isNaN (value); }
274 struct IsInf :
public std::unary_function<T,bool>
277 {
return isInf (value); }
282 struct IsFinite :
public std::unary_function<T,bool>
294 template<
typename L,
typename R=L>
295 struct Near :
public std::binary_function<L,R,bool>
297 explicit Near (
double tolerance=1
e-5)
307 template<
typename L,
typename R=L>
308 struct NearAbs :
public std::binary_function<L,R,bool>
321 template<
typename T,
typename RES=T>
322 struct Sin :
public std::unary_function<T,RES>
325 {
return RES(
sin (value)); }
329 template<
typename T,
typename RES=T>
330 struct Sinh :
public std::unary_function<T,RES>
333 {
return RES(
sinh (value)); }
337 template<
typename T,
typename RES=T>
338 struct Asin :
public std::unary_function<T,RES>
341 {
return RES(
asin (value)); }
345 template<
typename T,
typename RES=T>
346 struct Cos :
public std::unary_function<T,RES>
349 {
return RES(
cos (value)); }
353 template<
typename T,
typename RES=T>
354 struct Cosh :
public std::unary_function<T,RES>
357 {
return RES(
cosh (value)); }
361 template<
typename T,
typename RES=T>
362 struct Acos :
public std::unary_function<T,RES>
365 {
return RES(
acos (value)); }
369 template<
typename T,
typename RES=T>
370 struct Tan :
public std::unary_function<T,RES>
373 {
return RES(
tan (value)); }
377 template<
typename T,
typename RES=T>
378 struct Tanh :
public std::unary_function<T,RES>
381 {
return RES(
tanh (value)); }
385 template<
typename T,
typename RES=T>
386 struct Atan :
public std::unary_function<T,RES>
389 {
return RES(
atan (value)); }
393 template<
typename L,
typename R=L,
typename RES=L>
394 struct Atan2 :
public std::binary_function<L,R,RES>
397 {
return RES(
atan2 (left, L(right))); }
401 template<
typename T,
typename RES=T>
402 struct Sqr :
public std::unary_function<T,RES>
405 {
return RES(value*value); }
409 template<
typename T,
typename RES=T>
410 struct Pow3 :
public std::unary_function<T,RES>
413 {
return RES(value*value*value); }
417 template<
typename T,
typename RES=T>
418 struct Sqrt :
public std::unary_function<T,RES>
421 {
return RES(
sqrt (value)); }
425 template<
typename T,
typename RES=T>
426 struct Exp :
public std::unary_function<T,RES>
429 {
return RES(
exp (value)); }
433 template<
typename T,
typename RES=T>
434 struct Log :
public std::unary_function<T,RES>
437 {
return RES(
log (value)); }
441 template<
typename T,
typename RES=T>
442 struct Log10 :
public std::unary_function<T,RES>
445 {
return RES(
log10 (value)); }
449 template<
typename T,
typename RES=T>
450 struct Abs :
public std::unary_function<T,RES>
453 {
return RES(
abs (value)); }
457 template<
typename T,
typename RES=T>
458 struct Floor :
public std::unary_function<T,RES>
461 {
return RES(
floor (value)); }
465 template<
typename T,
typename RES=T>
466 struct Ceil :
public std::unary_function<T,RES>
469 {
return RES(
ceil (value)); }
473 template<
typename T,
typename RES=T>
474 struct Round :
public std::unary_function<T,RES>
477 {
return RES(value<0 ?
ceil(value-0.5) :
floor(value+0.5)); }
481 template<
typename T,
typename RES=T>
482 struct Sign :
public std::unary_function<T,RES>
485 {
return (value<0 ? -1 : (value>0 ? 1:0)); }
489 template<
typename L,
typename R,
typename RES>
493 {
return RES(l, r); }
498 template<
typename L,
typename R,
typename RES>
502 {
return RES(
real(l), r); }
507 template<
typename L,
typename R,
typename RES>
511 {
return RES(l,
imag(r)); }
516 template<
typename L,
typename R,
typename RES>
524 template<
typename T,
typename RES=T>
525 struct Conj :
public std::unary_function<T,RES>
528 {
return RES(
conj (value)); }
532 template<
typename T,
typename RES>
533 struct Real :
public std::unary_function<T,RES>
536 {
return RES(
real (value)); }
540 template<
typename T,
typename RES>
541 struct Imag :
public std::unary_function<T,RES>
544 {
return RES(
imag (value)); }
548 template<
typename T,
typename RES>
549 struct CArg :
public std::unary_function<T,RES>
552 {
return RES(
arg (value)); }
556 template<
typename T,
typename RES>
557 struct CAbs :
public std::unary_function<T,RES>
560 {
return RES(fabs (value)); }
564 template<
typename T,
typename E=T,
typename RES=T>
565 struct Pow :
public std::binary_function<T,E,RES>
568 {
return RES(
pow (left, exponent)); }
572 template<
typename L,
typename R=L,
typename RES=L>
573 struct Fmod :
public std::binary_function<L,R,RES>
576 {
return RES(
fmod (left, L(right))); }
580 template<
typename L,
typename R=L,
typename RES=L>
581 struct Min :
public std::binary_function<L,R,RES>
584 {
return RES(left<right ? left : right); }
588 template<
typename L,
typename R=L,
typename RES=L>
589 struct Max :
public std::binary_function<L,R,RES>
592 {
return RES(left<right ? right : left); }
596 template<
typename T,
typename Accum=T>
597 struct SumSqr :
public std::binary_function<Accum,T,Accum>
600 {
return left + Accum(right)*Accum(right); }
606 template<
typename T,
typename Accum=T>
607 struct SumSqrDiff :
public std::binary_function<Accum,T,Accum>
622 struct SumSqrDiff<std::complex<T>> :
public std::binary_function<std::complex<T>,std::complex<T>,std::complex<T>>
625 std::complex<T>
operator() (std::complex<T> left, std::complex<T> right)
const
626 {
return left + ((right.real() -
itsBase.real()) * (right.real() -
itsBase.real()) +
627 (right.imag() -
itsBase.imag()) * (right.imag() -
itsBase.imag())); }
634 template<
typename T,
typename Accum=T>
635 struct SumAbsDiff :
public std::binary_function<Accum,T,Accum>
645 struct Downcase :
public std::unary_function<std::string,String>
652 struct Upcase :
public std::unary_function<std::string,String>
659 struct Capitalize :
public std::unary_function<std::string,String>
667 struct Trim :
public std::unary_function<std::string,String>
670 {
return trim(value); }
LatticeExprNode log10(const LatticeExprNode &expr)
Functor to test for if two values are absolutely near each other.
Functor for bitwise and of (integer) values.
RES operator()(L l, R r) const
Accum accumulateTrue(InputIterator first, InputIterator last, MaskIterator mask, Accum acc, BinaryOperator op=std::plus< Accum >())
Define a function (similar to std::accumulate) to do accumulation of elements for which the correspon...
RES operator()(T value) const
String operator()(const std::string &value) const
RES operator()(T value) const
RES operator()(T value) const
LatticeExprNode log(const LatticeExprNode &expr)
RES operator()(T value) const
RES operator()(T value) const
LatticeExprNode arg(const LatticeExprNode &expr)
TableExprNode downcase(const TableExprNode &node)
Functor to apply round (e.g.
Accum operator()(Accum left, T right) const
Functor to add square of right to left.
Functor to add absolute diff of right and base value to left.
RES operator()(T value) const
LatticeExprNode mask(const LatticeExprNode &expr)
This function returns the mask of the given expression.
T operator()(const T &x, const T &y) const
RES operator()(const L &x, const R &y) const
Functor to apply a power of 3.
LatticeExprNode imag(const LatticeExprNode &expr)
String operator()(const std::string &value) const
Functor to form a complex number from the real part of the left value and the imaginary part of the r...
RES operator()(T value) const
Functor for bitwise negate of (integer) values.
Near(double tolerance=1e-5)
RES operator()(T value) const
RES operator()(T value) const
Accum operator()(Accum left, T right) const
bool compareAllLeft(InputIterator1 first1, InputIterator1 last1, T left, CompareOperator op)
For use with a constant left value.
Functor to get maximum of two values.
RES operator()(T value) const
RES operator()(T value) const
Functor to take modulo of variables of possibly different types using the floor modulo (% as used in ...
Functor to downcase a std::string.
Functor to form a complex number from the left and right value.
Bool near(const GaussianBeam &left, const GaussianBeam &other, const Double relWidthTol, const Quantity &absPaTol)
Functor to test for finiteness.
RES operator()(L left, R right) const
T operator()(const T &x) const
Functor to trim a std::string.
std::complex< T > itsBase
Functor for bitwise or of (integer) values.
LatticeExprNode exp(const LatticeExprNode &expr)
Functor to subtract variables of possibly different types.
RES operator()(T value) const
RES operator()(T value) const
RES operator()(T value) const
LatticeExprNode floor(const LatticeExprNode &expr)
RES operator()(T value) const
LatticeExprNode cos(const LatticeExprNode &expr)
RES operator()(L l, R r) const
Functor to apply sqr (power of 2).
bool operator()(T value) const
Functor to get minimum of two values.
RES operator()(L l, R r) const
LatticeExprNode conj(const LatticeExprNode &expr)
bool operator()(T value) const
Functor to add variables of possibly different types.
Functor to apply complex function fabs.
T operator()(const T &x, const T &y) const
RES operator()(T value) const
LatticeExprNode tanh(const LatticeExprNode &expr)
RES operator()(T value) const
void transformInPlace(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryOperator op)
Define a function to do a binary transform in place.
Functor to test if two values are relatively near each other.
TableExprNode isInf(const TableExprNode &node)
RES operator()(T value) const
NearAbs(double tolerance=1e-13)
Functor to apply complex function imag.
Functor for bitwise xor of (integer) values.
bool operator()(L left, R right) const
RES operator()(const L &x, const R &y) const
T operator()(const T &x, const T &y) const
LatticeExprNode abs(const LatticeExprNode &expr)
Numerical 1-argument functions which result in a real number regardless of input expression type...
TableExprNode trim(const TableExprNode &node)
RES operator()(const L &x, const R &y) const
Functor to test for infinity.
Functor to form a complex number from the real part of the left value and the right value...
LatticeExprNode sqrt(const LatticeExprNode &expr)
LatticeExprNode tan(const LatticeExprNode &expr)
TableExprNode isFinite(const TableExprNode &node)
Function to test if a scalar or array is finite.
LatticeExprNode atan(const LatticeExprNode &expr)
Functor to capitalize a std::string.
TableExprNode upcase(const TableExprNode &node)
RES operator()(const L &x, const R &y) const
RES operator()(T value) const
bool compareAnyRight(InputIterator1 first1, InputIterator1 last1, T right, CompareOperator op)
For use with a constant right value.
Functor to apply complex function real.
bool compareAny(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, CompareOperator op)
Define a function to compare all elements of two sequences.
Functor to apply complex function conj.
Functor to multiply variables of possibly different types.
bool compareAllRight(InputIterator1 first1, InputIterator1 last1, T right, CompareOperator op)
For use with a constant right value.
RES operator()(T value) const
LatticeExprNode atan2(const LatticeExprNode &left, const LatticeExprNode &right)
Numerical 2-argument functions.
String operator()(const std::string &value) const
bool compareAnyLeft(InputIterator1 first1, InputIterator1 last1, T left, CompareOperator op)
For use with a constant left value.
Accum accumulateFalse(InputIterator first, InputIterator last, MaskIterator mask, Accum acc, BinaryOperator op=std::plus< Accum >())
Define a function (similar to std::accumulate) to do accumulation of elements for which the correspon...
String operator()(const std::string &value) const
RES operator()(const L &x, const R &y) const
bool operator()(L left, R right) const
RES operator()(T value) const
RES operator()(T value) const
LatticeExprNode fmod(const LatticeExprNode &left, const LatticeExprNode &right)
Functor to apply sign (result is -1, 0, or 1).
SumSqrDiff(std::complex< T > base)
LatticeExprNode asin(const LatticeExprNode &expr)
const Double e
e and functions thereof:
Functor to divide variables of possibly different types.
int floormod(int x, int y)
LatticeExprNode sinh(const LatticeExprNode &expr)
Functor to form a complex number from the left value and the imaginary part of the right value...
RES operator()(L l, R r) const
RES operator()(T left, E exponent) const
LatticeExprNode acos(const LatticeExprNode &expr)
RES operator()(T value) const
TableExprNode capitalize(const TableExprNode &node)
String: the storage and methods of handling collections of characters.
TableExprNode nearAbs(const TableExprNode &left, const TableExprNode &right)
Accum operator()(Accum left, T right) const
Functor to take modulo of (integer) variables of possibly different types in the C way...
LatticeExprNode isNaN(const LatticeExprNode &expr)
Test if a value is a NaN.
bool compareAll(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, CompareOperator op)
Define a function to compare all elements of two sequences.
RES operator()(R left, L right) const
Functor to upcase a std::string.
bool operator()(T value) const
LatticeExprNode ceil(const LatticeExprNode &expr)
LatticeExprNode pow(const LatticeExprNode &left, const LatticeExprNode &right)
RES operator()(T value) const
LatticeExprNode cosh(const LatticeExprNode &expr)
LatticeExprNode real(const LatticeExprNode &expr)
Functor to add squared diff of right and base value to left.
LatticeExprNode value(const LatticeExprNode &expr)
This function returns the value of the expression without a mask.
LatticeExprNode sin(const LatticeExprNode &expr)
Numerical 1-argument functions.
RES operator()(L left, R right) const
RES operator()(T value) const
Functor to apply complex function arg.
RES operator()(L left, R right) const
RES operator()(const L &x, const R &y) const