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
UnaryOp.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_UnaryOp_h_INCLUDED
24 #define NDARRAY_DETAIL_UnaryOp_h_INCLUDED
25 
32 #include "ndarray/ExpressionBase.h"
33 #include "ndarray/vectorize.h"
34 #include <boost/iterator/iterator_adaptor.hpp>
35 
36 namespace ndarray {
37 namespace detail {
38 
46 template <typename Operand, typename UnaryFunction>
47 class UnaryOpIterator : public boost::iterator_adaptor<
48  UnaryOpIterator<Operand,UnaryFunction>,
49  typename ExpressionTraits<Operand>::Iterator,
50  typename ExpressionTraits< UnaryOpExpression<Operand,UnaryFunction> >::Value,
51  boost::use_default,
52  typename ExpressionTraits< UnaryOpExpression<Operand,UnaryFunction> >::Reference
53  > {
55 public:
59 
60  UnaryOpIterator() : UnaryOpIterator::iterator_adaptor_(), _functor() {}
61 
62  UnaryOpIterator(BaseIterator const & baseIter, UnaryFunction const & functor) :
63  UnaryOpIterator::iterator_adaptor_(baseIter), _functor(functor) {}
64 
66  UnaryOpIterator::iterator_adaptor_(other), _functor(other._functor) {}
67 
68 private:
70 
72  return vectorize(_functor,*this->base_reference());
73  }
74 
75  UnaryFunction _functor;
76 };
77 
85 template <typename Operand, typename UnaryFunction, int N>
86 class UnaryOpExpression : public ExpressionBase< UnaryOpExpression<Operand,UnaryFunction,N> > {
88 public:
90  typedef typename ExpressionTraits<Self>::ND ND;
95 
96  UnaryOpExpression(Operand const & operand, UnaryFunction const & functor) :
97  _operand(operand), _functor(functor) {}
98 
99  Reference operator[](int n) const {
100  return Reference(_operand[n],_functor);
101  }
102 
103  Iterator begin() const {
104  return Iterator(_operand.begin(),_functor);
105  }
106 
107  Iterator end() const {
108  return Iterator(_operand.end(),_functor);
109  }
110 
111  template <int P> int getSize() const {
112  return _operand.template getSize<P>();
113  }
114 
115  Index getShape() const {
116  return _operand.getShape();
117  }
118 
119  Operand _operand;
120  UnaryFunction _functor;
121 };
122 
123 } // namespace detail
124 } // namespace ndarray
125 
126 #endif // !NDARRAY_DETAIL_UnaryOp_h_INCLUDED
ExpressionTraits< Operation >::Reference Reference
Definition: UnaryOp.h:58
UnaryOpIterator(BaseIterator const &baseIter, UnaryFunction const &functor)
Definition: UnaryOp.h:62
ExpressionTraits< Self >::Element Element
Definition: UnaryOp.h:89
ExpressionTraits< Self >::Iterator Iterator
Definition: UnaryOp.h:91
friend class boost::iterator_core_access
Definition: UnaryOp.h:69
Code to apply arbitrary scalar functors to arrays.
Reference dereference() const
Definition: UnaryOp.h:71
UnaryOpIterator(UnaryOpIterator const &other)
Definition: UnaryOp.h:65
Reference operator[](int n) const
Definition: UnaryOp.h:99
ExpressionTraits< Operation >::Value Value
Definition: UnaryOp.h:57
ExpressionTraits< Self >::ND ND
Definition: UnaryOp.h:90
Definitions for ExpressionBase.
ExpressionTraits< Operand >::Iterator BaseIterator
Definition: UnaryOp.h:56
Traits for expressions.
UnaryOpExpression(Operand const &operand, UnaryFunction const &functor)
Definition: UnaryOp.h:96
UnaryFunction::result_type vectorize(UnaryFunction const &functor, Scalar const &scalar)
Apply a non-mutating unary function object to a scalar.
Definition: vectorize.h:85
UnaryOpExpression< Operand, UnaryFunction > Operation
Definition: UnaryOp.h:54
ExpressionTraits< Self >::Reference Reference
Definition: UnaryOp.h:93
UnaryOpExpression< Operand, UnaryFunction, N > Self
Definition: UnaryOp.h:87
ExpressionTraits< Self >::Value Value
Definition: UnaryOp.h:92
CRTP base class for all multidimensional expressions.