ndarray
NumPy-friendly multidimensional arrays in C++
Loading...
Searching...
No Matches
UnaryOp.h
Go to the documentation of this file.
1// -*- c++ -*-
2/*
3 * Copyright (c) 2010-2012, Jim Bosch
4 * All rights reserved.
5 *
6 * ndarray is distributed under a simple BSD-like license;
7 * see the LICENSE file that should be present in the root
8 * of the source distribution, or alternately available at:
9 * https://github.com/ndarray/ndarray
10 */
11#ifndef NDARRAY_DETAIL_UnaryOp_h_INCLUDED
12#define NDARRAY_DETAIL_UnaryOp_h_INCLUDED
13
21#include "ndarray/vectorize.h"
22#include <boost/iterator/iterator_adaptor.hpp>
23
24namespace ndarray {
25namespace detail {
26
34template <typename Operand, typename UnaryFunction>
35class UnaryOpIterator : public boost::iterator_adaptor<
36 UnaryOpIterator<Operand,UnaryFunction>,
37 typename ExpressionTraits<Operand>::Iterator,
38 typename ExpressionTraits< UnaryOpExpression<Operand,UnaryFunction> >::Value,
39 boost::use_default,
40 typename ExpressionTraits< UnaryOpExpression<Operand,UnaryFunction> >::Reference
41 > {
43public:
44 typedef typename ExpressionTraits<Operand>::Iterator BaseIterator;
45 typedef typename ExpressionTraits<Operation>::Value Value;
46 typedef typename ExpressionTraits<Operation>::Reference Reference;
47
48 UnaryOpIterator() : UnaryOpIterator::iterator_adaptor_(), _functor() {}
49
50 UnaryOpIterator(BaseIterator const & baseIter, UnaryFunction const & functor) :
51 UnaryOpIterator::iterator_adaptor_(baseIter), _functor(functor) {}
52
53 UnaryOpIterator(UnaryOpIterator const & other) :
54 UnaryOpIterator::iterator_adaptor_(other), _functor(other._functor) {}
55
56private:
57 friend class boost::iterator_core_access;
58
59 Reference dereference() const {
60 return vectorize(_functor,*this->base_reference());
61 }
62
63 UnaryFunction _functor;
64};
65
73template <typename Operand, typename UnaryFunction, int N>
74class UnaryOpExpression : public ExpressionBase< UnaryOpExpression<Operand,UnaryFunction,N> > {
76public:
77 typedef typename ExpressionTraits<Self>::Element Element;
78 typedef typename ExpressionTraits<Self>::ND ND;
79 typedef typename ExpressionTraits<Self>::Iterator Iterator;
80 typedef typename ExpressionTraits<Self>::Value Value;
81 typedef typename ExpressionTraits<Self>::Reference Reference;
82 typedef Vector<Size,N> Index;
83
84 UnaryOpExpression(Operand const & operand, UnaryFunction const & functor) :
85 _operand(operand), _functor(functor) {}
86
87 Reference operator[](Size n) const {
88 return Reference(_operand[n],_functor);
89 }
90
91 Iterator begin() const {
92 return Iterator(_operand.begin(),_functor);
93 }
94
95 Iterator end() const {
96 return Iterator(_operand.end(),_functor);
97 }
98
99 template <int P> Size getSize() const {
100 return _operand.template getSize<P>();
101 }
102
103 Index getShape() const {
104 return _operand.getShape();
105 }
106
107 Operand _operand;
108 UnaryFunction _functor;
109};
110
111} // namespace detail
112} // namespace ndarray
113
114#endif // !NDARRAY_DETAIL_UnaryOp_h_INCLUDED
Definitions for ExpressionBase.
CRTP base class for all multidimensional expressions.
Definition ExpressionBase.h:40
Definition UnaryOp.h:74
Definition UnaryOp.h:41
boost::enable_if< typenameExpressionTraits< Scalar >::IsScalar, typenameUnaryFunction::result_type >::type vectorize(UnaryFunction const &functor, Scalar const &scalar)
Apply a non-mutating unary function object to a scalar.
Definition vectorize.h:73
Traits for expressions.
Definition ExpressionTraits.h:30
A fixed-size 1D array class.
Definition Vector.h:82
Code to apply arbitrary scalar functors to arrays.