ndarray
NumPy-friendly multidimensional arrays in C++
Loading...
Searching...
No Matches
views.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_views_h_INCLUDED
12#define NDARRAY_views_h_INCLUDED
13
18#include <boost/fusion/include/push_back.hpp>
19#include <boost/fusion/include/vector.hpp>
20#include <boost/fusion/include/make_vector.hpp>
21#include <boost/fusion/include/mpl.hpp>
22
23#include "ndarray_fwd.h"
24
25namespace ndarray {
26namespace index {
27
31struct Slice {
32 Size start;
33 Size stop;
34 Offset step;
35
36 Slice(Size start_, Size stop_, Offset step_) : start(start_), stop(stop_), step(step_) {}
37
38 Size computeSize() const { return (step > 1) ? (stop - start + 1) / step : stop - start; }
39};
40
44struct Range {
45 Size start;
46 Size stop;
47
48 Range(Size start_, Size stop_) : start(start_), stop(stop_) {}
49};
50
54struct Full {};
55
59struct Scalar {
60 Size n;
61
62 explicit Scalar(Size n_) : n(n_) {}
63};
64
65} // namespace index
66
73template <typename Seq_ = boost::fusion::vector<> >
74struct View {
75 typedef Seq_ Sequence;
77
78 explicit View(Sequence seq) : _seq(seq) {}
79
80 template <typename OtherSequence>
81 explicit View(OtherSequence const & other) : _seq(other) {}
82
83 template <typename OtherSequence>
84 View(View<OtherSequence> const & other) : _seq(other._seq) {}
85
88
91
94
97
99 Full operator()() const { return Full(boost::fusion::push_back(_seq, index::Full())); }
100
102 Range operator()(Size start, Size stop) const {
103 return Range(boost::fusion::push_back(_seq, index::Range(start, stop)));
104 }
105
107 Slice operator()(Size start, Size stop, Offset step) const {
108 return Slice(boost::fusion::push_back(_seq, index::Slice(start, stop, step)));
109 }
110
112 Scalar operator()(Size n) const {
113 return Scalar(boost::fusion::push_back(_seq, index::Scalar(n)));
114 }
115};
116
119
123 boost::fusion::make_vector(index::Full())
124 );
125}
126
128inline View< boost::fusion::vector1<index::Range> > view(Size start, Size stop) {
130 boost::fusion::make_vector(index::Range(start, stop))
131 );
132}
133
135inline View< boost::fusion::vector1<index::Slice> > view(Size start, Size stop, Offset step) {
137 boost::fusion::make_vector(index::Slice(start, stop, step))
138 );
139}
140
144 boost::fusion::make_vector(index::Scalar(n))
145 );
146}
147
149
150} // namespace ndarray
151
152#endif // !NDARRAY_views_h_INCLUDED
View< boost::fusion::vector1< index::Full > > view()
Start a view definition that includes the entire first dimension.
Definition views.h:121
Forward declarations and default template parameters for ndarray.
A template meta-sequence that defines an arbitrary view into an unspecified array.
Definition views.h:74
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:90
Scalar operator()(Size n) const
Chain a single element of the next dimension to this.
Definition views.h:112
Full operator()() const
Chain the full next dimension to this.
Definition views.h:99
Sequence _seq
A boost::fusion sequence of index objects.
Definition views.h:76
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:96
Slice operator()(Size start, Size stop, Offset step) const
Chain a noncontiguous slice of the next dimension to this.
Definition views.h:107
Seq_ Sequence
A boost::fusion sequence type.
Definition views.h:75
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:87
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:93
Range operator()(Size start, Size stop) const
Chain a contiguous range of the next dimension to this.
Definition views.h:102
Empty structure marking a view of an entire dimension.
Definition views.h:54
Simple structure defining a contiguous range of indices.
Definition views.h:44
Structure marking a single element of a dimension.
Definition views.h:59
Simple structure defining a noncontiguous range of indices.
Definition views.h:31