@@ -91,7 +91,7 @@ namespace pybind11
9191
9292 size_type size () const
9393 {
94- return std::accumulate (shape (), shape () + ndim (), size_type{ 1 } , std::multiplies<size_type>());
94+ return std::accumulate (shape (), shape () + ndim (), size_type ( 1 ) , std::multiplies<size_type>());
9595 }
9696
9797 size_type itemsize () const
@@ -114,61 +114,30 @@ namespace pybind11
114114 return reinterpret_cast <const size_type*>(PyArray_GET_ (m_ptr, strides));
115115 }
116116
117- template <typename ... Ix>
118117 void * data ()
119118 {
120119 return static_cast <void *>(PyArray_GET_ (m_ptr, data));
121120 }
122121
123- template <typename ... Ix>
124122 void * mutable_data ()
125123 {
126- // check_writeable();
127124 return static_cast <void *>(PyArray_GET_ (m_ptr, data));
128125 }
129126
130- template <typename ... Ix>
131- size_type offset_at (Ix... index) const
132- {
133- if (sizeof ...(index) > ndim ())
134- {
135- fail_dim_check (sizeof ...(index), " too many indices for an array" );
136- }
137- return get_byte_offset (index...);
138- }
127+ protected:
139128
140- size_type offset_at () const
129+ template <size_t dim = 0 >
130+ inline size_type byte_offset () const
141131 {
142132 return 0 ;
143133 }
144134
145- protected:
146-
147- void fail_dim_check (size_type dim, const std::string& msg) const
135+ template <size_t dim = 0 , class ... Args>
136+ inline size_type byte_offset (size_type i, Args... args) const
148137 {
149- throw index_error (msg + " : " + std::to_string (dim) +
150- " (ndim = " + std::to_string (ndim ()) + " )" );
138+ return i * strides ()[dim] + byte_offset<dim + 1 >(args...);
151139 }
152140
153- template <typename ... Ix>
154- size_type get_byte_offset (Ix... index) const
155- {
156- const size_type idx[] = { static_cast <size_type>(index)... };
157- if (!std::equal (idx + 0 , idx + sizeof ...(index), shape (), std::less<size_type>{}))
158- {
159- auto mismatch = std::mismatch (idx + 0 , idx + sizeof ...(index), shape (), std::less<size_type>{});
160- throw index_error (std::string (" index " ) + std::to_string (*mismatch.first ) +
161- " is out of bounds for axis " + std::to_string (mismatch.first - idx) +
162- " with size " + std::to_string (*mismatch.second ));
163- }
164- return std::inner_product (idx + 0 , idx + sizeof ...(index), strides (), size_type{0 });
165- }
166-
167- size_type get_byte_offset () const
168- {
169- return 0 ;
170- }
171-
172141 static std::vector<size_type>
173142 default_strides (const std::vector<size_type>& shape, size_type itemsize)
174143 {
0 commit comments