LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
BinaryOp.h
Go to the documentation of this file.
1 // -*- c++ -*-
2 /*
3  * LSST Data Management System
4  * Copyright 2008, 2009, 2010, 2011 LSST Corporation.
5  *
6  * This product includes software developed by the
7  * LSST Project (http://www.lsst.org/).
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the LSST License Statement and
20  * the GNU General Public License along with this program. If not,
21  * see <http://www.lsstcorp.org/LegalNotices/>.
22  */
23 #ifndef NDARRAY_DETAIL_BinaryOp_h_INCLUDED
24 #define NDARRAY_DETAIL_BinaryOp_h_INCLUDED
25 
32 #include "ndarray/ExpressionBase.h"
33 #include "ndarray/vectorize.h"
34 #include <boost/iterator/iterator_adaptor.hpp>
35 #include <boost/iterator/zip_iterator.hpp>
36 #include <boost/tuple/tuple.hpp>
37 
38 namespace ndarray {
39 namespace detail {
40 
48 template <typename Operand1, typename Operand2, typename BinaryFunction>
49 class BinaryOpIterator : public boost::iterator_adaptor<
50  BinaryOpIterator<Operand1,Operand2,BinaryFunction>,
51  boost::zip_iterator<
52  boost::tuple<
53  typename ExpressionTraits<Operand1>::Iterator,
54  typename ExpressionTraits<Operand2>::Iterator
55  >
56  >,
57  typename ExpressionTraits< BinaryOpExpression<Operand1,Operand2,BinaryFunction> >::Value,
58  boost::use_default,
59  typename ExpressionTraits< BinaryOpExpression<Operand1,Operand2,BinaryFunction> >::Reference
60  > {
62 public:
67 
68  BinaryOpIterator() : BinaryOpIterator::iterator_adaptor_(), _functor() {}
69 
71  BaseIterator1 const & baseIter1,
72  BaseIterator2 const & baseIter2,
73  BinaryFunction const & functor
74  ) :
75  BinaryOpIterator::iterator_adaptor_(boost::make_tuple(baseIter1,baseIter2)),
76  _functor(functor) {}
77 
79  BinaryOpIterator::iterator_adaptor_(other), _functor(other._functor) {}
80 
81 private:
83 
85  return vectorize(
86  _functor,
87  this->base_reference()->template get<0>(),
88  this->base_reference()->template get<1>()
89  );
90  }
91 
92  BinaryFunction _functor;
93 };
94 
102 template <typename Operand1, typename Operand2, typename BinaryFunction, int N>
103 class BinaryOpExpression : public ExpressionBase< BinaryOpExpression<Operand1,Operand2,BinaryFunction,N> > {
105 public:
107  typedef typename ExpressionTraits<Self>::ND ND;
112 
114  Operand1 const & operand1,
115  Operand2 const & operand2,
116  BinaryFunction const & functor
117  ) :
118  _operand1(operand1), _operand2(operand2), _functor(functor) {
119  NDARRAY_ASSERT(_operand1.getShape() == _operand2.getShape());
120  }
121 
122  Reference operator[](int n) const {
123  return Reference(_operand1[n],_operand2[n],_functor);
124  }
125 
126  Iterator begin() const {
127  return Iterator(_operand1.begin(),_operand2.begin(),_functor);
128  }
129 
130  Iterator end() const {
131  return Iterator(_operand1.end(),_operand2.end(),_functor);
132  }
133 
134  template <int P> int getSize() const {
135  return _operand1.template getSize<P>();
136  }
137 
138  Index getShape() const {
139  return _operand1.getShape();
140  }
141 
142  Operand1 _operand1;
143  Operand2 _operand2;
144  BinaryFunction _functor;
145 };
146 
147 } // namespace detail
148 } // namespace ndarray
149 
150 #endif // !NDARRAY_DETAIL_BinaryOp_h_INCLUDED
Reference operator[](int n) const
Definition: BinaryOp.h:122
ExpressionTraits< Self >::Iterator Iterator
Definition: BinaryOp.h:108
ExpressionTraits< Operation >::Reference Reference
Definition: BinaryOp.h:66
BinaryOpExpression< Operand1, Operand2, BinaryFunction > Operation
Definition: BinaryOp.h:61
BinaryOpIterator(BinaryOpIterator const &other)
Definition: BinaryOp.h:78
Code to apply arbitrary scalar functors to arrays.
ExpressionTraits< Self >::ND ND
Definition: BinaryOp.h:107
BinaryOpExpression(Operand1 const &operand1, Operand2 const &operand2, BinaryFunction const &functor)
Definition: BinaryOp.h:113
#define NDARRAY_ASSERT(ARG)
Definition: ndarray_fwd.h:51
ExpressionTraits< Operand2 >::Iterator BaseIterator2
Definition: BinaryOp.h:64
ExpressionTraits< Self >::Element Element
Definition: BinaryOp.h:106
ExpressionTraits< Self >::Value Value
Definition: BinaryOp.h:109
Definitions for ExpressionBase.
ExpressionTraits< Operation >::Value Value
Definition: BinaryOp.h:65
Traits for expressions.
BinaryOpExpression< Operand1, Operand2, BinaryFunction, N > Self
Definition: BinaryOp.h:104
UnaryFunction::result_type vectorize(UnaryFunction const &functor, Scalar const &scalar)
Apply a non-mutating unary function object to a scalar.
Definition: vectorize.h:85
ExpressionTraits< Self >::Reference Reference
Definition: BinaryOp.h:110
friend class boost::iterator_core_access
Definition: BinaryOp.h:82
Reference dereference() const
Definition: BinaryOp.h:84
BinaryOpIterator(BaseIterator1 const &baseIter1, BaseIterator2 const &baseIter2, BinaryFunction const &functor)
Definition: BinaryOp.h:70
CRTP base class for all multidimensional expressions.
ExpressionTraits< Operand1 >::Iterator BaseIterator1
Definition: BinaryOp.h:63