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
views.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_views_h_INCLUDED
24 #define NDARRAY_views_h_INCLUDED
25 
30 #include <boost/fusion/include/push_back.hpp>
31 #include <boost/fusion/include/vector.hpp>
32 #include <boost/fusion/include/make_vector.hpp>
33 #include <boost/fusion/include/mpl.hpp>
34 
35 namespace ndarray {
36 namespace index {
37 
41 struct Slice {
42  int start;
43  int stop;
44  int step;
45 
46  Slice(int start_, int stop_, int step_) : start(start_), stop(stop_), step(step_) {}
47 
48  int computeSize() const { return (step > 1) ? (stop - start + 1) / step : stop - start; }
49 };
50 
54 struct Range {
55  int start;
56  int stop;
57 
58  Range(int start_, int stop_) : start(start_), stop(stop_) {}
59 };
60 
64 struct Full {};
65 
69 struct Scalar {
70  int n;
71 
72  explicit Scalar(int n_) : n(n_) {}
73 };
74 
75 } // namespace index
76 
83 template <typename Seq_ = boost::fusion::vector<> >
84 struct View {
85  typedef Seq_ Sequence;
87 
88  explicit View(Sequence seq) : _seq(seq) {}
89 
90  template <typename OtherSequence>
91  explicit View(OtherSequence const & other) : _seq(other) {}
92 
93  template <typename OtherSequence>
94  View(View<OtherSequence> const & other) : _seq(other._seq) {}
95 
98 
101 
104 
107 
109  Full operator()() const { return Full(boost::fusion::push_back(_seq, index::Full())); }
110 
112  Range operator()(int start, int stop) const {
113  return Range(boost::fusion::push_back(_seq, index::Range(start, stop)));
114  }
115 
117  Slice operator()(int start, int stop, int step) const {
118  return Slice(boost::fusion::push_back(_seq, index::Slice(start, stop, step)));
119  }
120 
122  Scalar operator()(int n) const {
123  return Scalar(boost::fusion::push_back(_seq, index::Scalar(n)));
124  }
125 };
126 
129 
133  boost::fusion::make_vector(index::Full())
134  );
135 }
136 
138 inline View< boost::fusion::vector1<index::Range> > view(int start, int stop) {
140  boost::fusion::make_vector(index::Range(start, stop))
141  );
142 }
143 
145 inline View< boost::fusion::vector1<index::Slice> > view(int start, int stop, int step) {
147  boost::fusion::make_vector(index::Slice(start, stop, step))
148  );
149 }
150 
154  boost::fusion::make_vector(index::Scalar(n))
155  );
156 }
157 
159 template <typename Sequence>
160 inline View<Sequence> view(Sequence const & sequence) {
161  return View<Sequence>(sequence);
162 }
163 
165 
166 } // namespace ndarray
167 
168 #endif // !NDARRAY_views_h_INCLUDED
View< typename boost::fusion::result_of::push_back< Sequence const, index::Full >::type > Full
The View that results from chaining an full dimension index () to this.
Definition: views.h:97
View< boost::fusion::vector1< index::Full > > view()
Start a view definition that includes the entire first dimension.
Definition: views.h:131
View(Sequence seq)
Definition: views.h:88
Slice operator()(int start, int stop, int step) const
Chain a noncontiguous slice of the next dimension to this.
Definition: views.h:117
View< typename boost::fusion::result_of::push_back< Sequence const, index::Range >::type > Range
The View that results from chaining a range (start,stop) to this.
Definition: views.h:100
Simple structure defining a contiguous range of indices.
Definition: views.h:54
Scalar operator()(int n) const
Chain a single element of the next dimension to this.
Definition: views.h:122
Range operator()(int start, int stop) const
Chain a contiguous range of the next dimension to this.
Definition: views.h:112
Simple structure defining a noncontiguous range of indices.
Definition: views.h:41
View(View< OtherSequence > const &other)
Definition: views.h:94
Full operator()() const
Chain the full next dimension to this.
Definition: views.h:109
View< typename boost::fusion::result_of::push_back< Sequence const, index::Scalar >::type > Scalar
The View that results from chaining a scalar (n) to this.
Definition: views.h:106
int computeSize() const
Definition: views.h:48
Empty structure marking a view of an entire dimension.
Definition: views.h:64
Seq_ Sequence
A boost::fusion sequence type.
Definition: views.h:85
Range(int start_, int stop_)
Definition: views.h:58
Structure marking a single element of a dimension.
Definition: views.h:69
View(OtherSequence const &other)
Definition: views.h:91
A template meta-sequence that defines an arbitrary view into an unspecified array.
Definition: views.h:84
Sequence _seq
A boost::fusion sequence of index objects.
Definition: views.h:86
View< typename boost::fusion::result_of::push_back< Sequence const, index::Slice >::type > Slice
The View that results from chaining a slice (start,stop,step) to this.
Definition: views.h:103
Slice(int start_, int stop_, int step_)
Definition: views.h:46