ndarray
NumPy-friendly multidimensional arrays in C++
Loading...
Searching...
No Matches
ArrayTraits.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_ArrayTraits_h_INCLUDED
12#define NDARRAY_ArrayTraits_h_INCLUDED
13
20#include "ndarray_fwd.h"
22#include "ndarray/detail/Core.h"
23#include <boost/mpl/int.hpp>
24#include <boost/mpl/bool.hpp>
25
26namespace ndarray {
27namespace detail {
28
29template <int N, typename T2, int C2, typename T1, int C1>
30struct Convertible : public boost::mpl::bool_<
31 (((C2>=C1 && C1>=0) || (C2<=C1 && C1<=0) || (N == 1 && C2 == -C1))
32 && boost::is_convertible<T2*,T1*>::value)
33> {};
34
35} // namespace detail
36
37
43template <typename T, int N, int C>
45 typedef T Element;
46 typedef boost::mpl::int_<N> ND;
47 typedef boost::mpl::int_<C> RMC;
49 typedef ArrayRef<T,N-1,(N==C)?(N-1):((C>0)?C:0)> Reference;
50 typedef Array<T,N-1,(N==C)?(N-1):((C>0)?C:0)> Value;
51 typedef detail::Core<N> Core;
52 typedef typename Core::ConstPtr CorePtr;
53
54 static Reference makeReference(Element * data, CorePtr const & core) {
55 return Reference(data, core);
56 }
57 static Iterator makeIterator(Element * data, CorePtr const & core, Offset stride) {
58 return Iterator(Reference(data, core), stride);
59 }
60 static void fill(Iterator iter, Iterator const & end, Element value) {
61 // We can't use std::fill here because NestedIterator is not formally an STL ForwardIterator;
62 // it has random access traversal, but it does not dereference to an addressable type (see
63 // http://www.boost.org/doc/libs/1_55_0/libs/iterator/doc/new-iter-concepts.html#motivation)
64 // Most C++ standard libraries have a fill implementation that will accept NestedIterator
65 // anyway, but Clang's libc++ is more strictly compliant and does not.
66 for (; iter != end; ++iter) {
67 *iter = value;
68 }
69 }
70};
71
72template <typename T>
73struct ArrayTraits<T,1,0> {
74 typedef T Element;
75 typedef boost::mpl::int_<1> ND;
76 typedef boost::mpl::int_<0> RMC;
78 typedef Element & Reference;
79 typedef Element Value;
80 typedef detail::Core<1> Core;
81 typedef typename Core::ConstPtr CorePtr;
82
83 static Reference makeReference(Element * data, CorePtr const & core) {
84 return *data;
85 }
86 static Iterator makeIterator(Element * data, CorePtr const & core, Offset stride) {
87 return Iterator(data, stride);
88 }
89 static void fill(Iterator iter, Iterator const & end, Element value) {
90 std::fill(iter, end, value);
91 }
92};
93
94template <typename T>
95struct ArrayTraits<T,1,1> {
96 typedef T Element;
97 typedef boost::mpl::int_<1> ND;
98 typedef boost::mpl::int_<1> RMC;
99 typedef Element * Iterator;
100 typedef Element & Reference;
101 typedef Element Value;
102 typedef detail::Core<1> Core;
103 typedef typename Core::ConstPtr CorePtr;
104
105 static Reference makeReference(Element * data, CorePtr const & core) {
106 return *data;
107 }
108 static Iterator makeIterator(Element * data, CorePtr const & core, Offset stride) {
109 return data;
110 }
111 static void fill(Iterator iter, Iterator const & end, Element value) {
112 std::fill(iter, end, value);
113 }
114};
115
116template <typename T>
117struct ArrayTraits<T,1,-1> {
118 typedef T Element;
119 typedef boost::mpl::int_<1> ND;
120 typedef boost::mpl::int_<-1> RMC;
121 typedef Element * Iterator;
122 typedef Element & Reference;
123 typedef Element Value;
124 typedef detail::Core<1> Core;
125 typedef typename Core::ConstPtr CorePtr;
126
127 static Reference makeReference(Element * data, CorePtr const & core) {
128 return *data;
129 }
130 static Iterator makeIterator(Element * data, CorePtr const & core, Offset stride) {
131 return data;
132 }
133 static void fill(Iterator iter, Iterator const & end, Element value) {
134 std::fill(iter, end, value);
135 }
136};
137
138template <typename T, int N, int C>
139struct ExpressionTraits< Array<T,N,C> > : public ArrayTraits<T,N,C> {
140 typedef Array<T,N,C> Self;
141 typedef boost::mpl::false_ IsScalar;
142};
143
144template <typename T, int N, int C>
145struct ExpressionTraits< ArrayRef<T,N,C> > : public ArrayTraits<T,N,C> {
146 typedef ArrayRef<T,N,C> Self;
147 typedef boost::mpl::false_ IsScalar;
148};
149
150} // namespace ndarray
151
152#endif // !NDARRAY_ArrayTraits_h_INCLUDED
Definitions for Core.
Traits for Expression.
A proxy class for Array with deep assignment operators.
Definition ArrayRef.h:34
A multidimensional strided array.
Definition Array.h:35
Definition Core.h:47
boost::intrusive_ptr< Core const > ConstPtr
const intrusive_ptr to Core
Definition Core.h:52
Definition NestedIterator.h:46
Definition StridedIterator.h:36
Forward declarations and default template parameters for ndarray.
Dimension-specialized traits shared by Array and ArrayRef.
Definition ArrayTraits.h:44
Traits for expressions.
Definition ExpressionTraits.h:30
Definition ArrayTraits.h:33