LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
arange.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_arange_h_INCLUDED
24 #define NDARRAY_arange_h_INCLUDED
25 
32 #include "ndarray/vectorize.h"
33 
34 #include <boost/iterator/counting_iterator.hpp>
35 
36 namespace ndarray {
37 
43 template <>
44 struct ExpressionTraits<detail::CountingExpression> {
45  typedef int Element;
46  typedef boost::mpl::int_<1> ND;
47  typedef boost::counting_iterator<int> Iterator;
48  typedef int Value;
49  typedef int Reference;
50 };
51 
52 namespace detail {
53 
60 class CountingExpression : public ExpressionBase<CountingExpression> {
61 public:
68 
69  CountingExpression(int stop=0) : _stop(stop) { NDARRAY_ASSERT(stop >= 0); }
70 
71  Reference operator[](int n) const {
72  return n;
73  }
74 
75  Iterator begin() const {
76  return Iterator(0);
77  }
78 
79  Iterator end() const {
80  return Iterator(_stop);
81  }
82 
83  template <int P> int getSize() const {
84  BOOST_STATIC_ASSERT(P==0);
85  return _stop;
86  }
87 
88  Index getShape() const {
89  return makeVector(_stop);
90  }
91 
92 private:
93  int _stop;
94 };
95 
96 template <typename T>
99  T _scale;
100 public:
101  typedef int argument_type;
102  typedef T result_type;
103 
104  explicit RangeTransformer(T const & offset, T const & scale) : _offset(offset), _scale(scale) {}
105 
106  T operator()(int n) const { return static_cast<T>(n) * _scale + _offset; }
107 };
108 
109 } // namespace detail
110 
113  return detail::CountingExpression(stop);
114 }
115 
117 inline detail::UnaryOpExpression< detail::CountingExpression, detail::RangeTransformer<int> >
118 arange(int start, int stop, int step = 1) {
119  NDARRAY_ASSERT(step != 0);
120  int size = stop - start;
121  if (step < -1) ++size;
122  if (step > 1) --size;
123  size /= step;
124  return vectorize(
125  detail::RangeTransformer<int>(start,step),
127  );
128 }
129 
130 } // namespace ndarray
131 
132 #endif // !NDARRAY_arange_h_INCLUDED
ExpressionTraits< CountingExpression >::Reference Reference
Definition: arange.h:66
Code to apply arbitrary scalar functors to arrays.
detail::CountingExpression arange(int stop)
Create 1D Expression that contains integer values in the range [0,stop).
Definition: arange.h:112
#define NDARRAY_ASSERT(ARG)
Definition: ndarray_fwd.h:51
boost::counting_iterator< int > Iterator
Definition: arange.h:47
A fixed-size 1D array class.
Definition: Vector.h:93
ExpressionTraits< CountingExpression >::Value Value
Definition: arange.h:65
ExpressionTraits< CountingExpression >::Iterator Iterator
Definition: arange.h:64
Vector< T, N > makeVector(T v1, T v2,..., T vN)
Variadic constructor for Vector.
Traits for expressions.
ExpressionTraits< CountingExpression >::ND ND
Definition: arange.h:63
T operator()(int n) const
Definition: arange.h:106
Reference operator[](int n) const
Definition: arange.h:71
ExpressionTraits< CountingExpression >::Element Element
Definition: arange.h:62
UnaryFunction::result_type vectorize(UnaryFunction const &functor, Scalar const &scalar)
Apply a non-mutating unary function object to a scalar.
Definition: vectorize.h:85
RangeTransformer(T const &offset, T const &scale)
Definition: arange.h:104
CRTP base class for all multidimensional expressions.