Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 100542a

Browse files
author
Rue Yokaze
committed
move cpp file
1 parent 77fbcb4 commit 100542a

File tree

3 files changed

+178
-70
lines changed

3 files changed

+178
-70
lines changed

‎.clang-format

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
AccessModifierOffset: -4
2+
AllowShortBlocksOnASingleLine: true
3+
AlwaysBreakTemplateDeclarations: true
4+
BasedOnStyle: LLVM
5+
BreakBeforeBraces: Allman
6+
ColumnLimit: 160
7+
DerivePointerAlignment: false
8+
IndentWidth: 4
9+
NamespaceIndentation: All
10+
PointerAlignment: Left

‎source/python_multi_array.cpp renamed to ‎python_multi_array.cpp

Lines changed: 167 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
// Copyright (C) 2017 Rue Yokaze
66
// Distributed under the MIT License.
77
//
8-
#include <stdint.h>
98
#include <boost/multi_array.hpp>
109
#include <boost/python.hpp>
1110
#include <boost/python/numpy.hpp>
1211
#include <memory>
12+
#include <stdint.h>
1313

1414
// Using from python is avoided because many definitions conflict with names from std.
1515
namespace python = boost::python;
@@ -48,24 +48,33 @@ namespace python_multi_array
4848

4949
namespace impl
5050
{
51-
template<class T>
51+
template<class T>
5252
python::object make_typed_sized(const size_t* s, size_t ndim)
5353
{
5454
switch (ndim)
5555
{
56-
case 1: return python::object(std::make_shared<multi_array<T, 1>>(extents[s[0]]));
57-
case 2: return python::object(std::make_shared<multi_array<T, 2>>(extents[s[0]][s[1]]));
58-
case 3: return python::object(std::make_shared<multi_array<T, 3>>(extents[s[0]][s[1]][s[2]]));
59-
case 4: return python::object(std::make_shared<multi_array<T, 4>>(extents[s[0]][s[1]][s[2]][s[3]]));
60-
case 5: return python::object(std::make_shared<multi_array<T, 5>>(extents[s[0]][s[1]][s[2]][s[3]][s[4]]));
61-
case 6: return python::object(std::make_shared<multi_array<T, 6>>(extents[s[0]][s[1]][s[2]][s[3]][s[4]][s[5]]));
62-
case 7: return python::object(std::make_shared<multi_array<T, 7>>(extents[s[0]][s[1]][s[2]][s[3]][s[4]][s[5]][s[6]]));
63-
case 8: return python::object(std::make_shared<multi_array<T, 8>>(extents[s[0]][s[1]][s[2]][s[3]][s[4]][s[5]][s[6]][s[7]]));
64-
default: throw std::invalid_argument("shape");
56+
case 1:
57+
return python::object(std::make_shared<multi_array<T, 1>>(extents[s[0]]));
58+
case 2:
59+
return python::object(std::make_shared<multi_array<T, 2>>(extents[s[0]][s[1]]));
60+
case 3:
61+
return python::object(std::make_shared<multi_array<T, 3>>(extents[s[0]][s[1]][s[2]]));
62+
case 4:
63+
return python::object(std::make_shared<multi_array<T, 4>>(extents[s[0]][s[1]][s[2]][s[3]]));
64+
case 5:
65+
return python::object(std::make_shared<multi_array<T, 5>>(extents[s[0]][s[1]][s[2]][s[3]][s[4]]));
66+
case 6:
67+
return python::object(std::make_shared<multi_array<T, 6>>(extents[s[0]][s[1]][s[2]][s[3]][s[4]][s[5]]));
68+
case 7:
69+
return python::object(std::make_shared<multi_array<T, 7>>(extents[s[0]][s[1]][s[2]][s[3]][s[4]][s[5]][s[6]]));
70+
case 8:
71+
return python::object(std::make_shared<multi_array<T, 8>>(extents[s[0]][s[1]][s[2]][s[3]][s[4]][s[5]][s[6]][s[7]]));
72+
default:
73+
throw std::invalid_argument("shape");
6574
}
6675
}
6776

68-
template<class T>
77+
template<class T>
6978
python::object make_typed(python::object shape)
7079
{
7180
python::extract<size_t> scalar_shape(shape);
@@ -86,18 +95,54 @@ namespace python_multi_array
8695

8796
python::object make(python::object shape, python::object dtype)
8897
{
89-
if (dtype == bool8) { return impl::make_typed<bool>(shape); }
90-
else if (dtype == int8) { return impl::make_typed<int8_t>(shape); }
91-
else if (dtype == int16) { return impl::make_typed<int16_t>(shape); }
92-
else if (dtype == int32) { return impl::make_typed<int32_t>(shape); }
93-
else if (dtype == int64) { return impl::make_typed<int16_t>(shape); }
94-
else if (dtype == uint8) { return impl::make_typed<uint8_t>(shape); }
95-
else if (dtype == uint16) { return impl::make_typed<uint16_t>(shape); }
96-
else if (dtype == uint32) { return impl::make_typed<uint32_t>(shape); }
97-
else if (dtype == uint64) { return impl::make_typed<uint64_t>(shape); }
98-
else if (dtype == float32) { return impl::make_typed<float>(shape); }
99-
else if (dtype == float64) { return impl::make_typed<double>(shape); }
100-
else { throw std::invalid_argument("dtype"); }
98+
if (dtype == bool8)
99+
{
100+
return impl::make_typed<bool>(shape);
101+
}
102+
else if (dtype == int8)
103+
{
104+
return impl::make_typed<int8_t>(shape);
105+
}
106+
else if (dtype == int16)
107+
{
108+
return impl::make_typed<int16_t>(shape);
109+
}
110+
else if (dtype == int32)
111+
{
112+
return impl::make_typed<int32_t>(shape);
113+
}
114+
else if (dtype == int64)
115+
{
116+
return impl::make_typed<int16_t>(shape);
117+
}
118+
else if (dtype == uint8)
119+
{
120+
return impl::make_typed<uint8_t>(shape);
121+
}
122+
else if (dtype == uint16)
123+
{
124+
return impl::make_typed<uint16_t>(shape);
125+
}
126+
else if (dtype == uint32)
127+
{
128+
return impl::make_typed<uint32_t>(shape);
129+
}
130+
else if (dtype == uint64)
131+
{
132+
return impl::make_typed<uint64_t>(shape);
133+
}
134+
else if (dtype == float32)
135+
{
136+
return impl::make_typed<float>(shape);
137+
}
138+
else if (dtype == float64)
139+
{
140+
return impl::make_typed<double>(shape);
141+
}
142+
else
143+
{
144+
throw std::invalid_argument("dtype");
145+
}
101146
}
102147

103148
//
@@ -109,15 +154,15 @@ namespace python_multi_array
109154
// Example:
110155
// x[2, 4] = 2.0
111156
//
112-
template<class T, size_t N>
157+
template<class T, size_t N>
113158
T getitem(shared_ptr<multi_array<T, N>> t, python::object idx);
114159

115-
template<class T, size_t N>
160+
template<class T, size_t N>
116161
void setitem(shared_ptr<multi_array<T, N>> t, python::object idx, T value);
117162

118163
namespace impl
119164
{
120-
template<class T, size_t N>
165+
template<class T, size_t N>
121166
T getitem_impl(shared_ptr<multi_array<T, N>> t, const size_t* s)
122167
{
123168
T* ptr = t->origin();
@@ -128,7 +173,7 @@ namespace python_multi_array
128173
return *ptr;
129174
}
130175

131-
template<class T, size_t N>
176+
template<class T, size_t N>
132177
void setitem_impl(shared_ptr<multi_array<T, N>> t, const size_t* s, T value)
133178
{
134179
T* ptr = t->origin();
@@ -140,7 +185,7 @@ namespace python_multi_array
140185
}
141186
}
142187

143-
template<class T, size_t N>
188+
template<class T, size_t N>
144189
T getitem(shared_ptr<multi_array<T, N>> t, python::object idx)
145190
{
146191
if (N == 1)
@@ -160,7 +205,7 @@ namespace python_multi_array
160205
return impl::getitem_impl(t, s);
161206
}
162207

163-
template<class T, size_t N>
208+
template<class T, size_t N>
164209
void setitem(shared_ptr<multi_array<T, N>> t, python::object idx, T value)
165210
{
166211
if (N == 1)
@@ -187,7 +232,7 @@ namespace python_multi_array
187232
//
188233
// This function resets every element of x with zero.
189234
//
190-
template<class T, size_t N>
235+
template<class T, size_t N>
191236
void reset(shared_ptr<multi_array<T, N>> t)
192237
{
193238
std::fill(t->origin(), t->origin() + t->num_elements(), 0);
@@ -201,7 +246,7 @@ namespace python_multi_array
201246
// uint16, uint32, uint64, int8, int16, int32, int64, float32,
202247
// float64, all defined in numpy.
203248
//
204-
template<class T, size_t N>
249+
template<class T, size_t N>
205250
python::object element(shared_ptr<multi_array<T, N>> t)
206251
{
207252
return python::numpy::dtype::get_builtin<T>();
@@ -213,21 +258,30 @@ namespace python_multi_array
213258
//
214259
// return: the shape of the array.
215260
//
216-
template<class T, size_t N>
261+
template<class T, size_t N>
217262
python::object shape(shared_ptr<multi_array<T, N>> t)
218263
{
219264
const size_t* s = t->shape();
220265
switch (N)
221266
{
222-
case 1: return python::make_tuple(s[0]);
223-
case 2: return python::make_tuple(s[0], s[1]);
224-
case 3: return python::make_tuple(s[0], s[1], s[2]);
225-
case 4: return python::make_tuple(s[0], s[1], s[2], s[3]);
226-
case 5: return python::make_tuple(s[0], s[1], s[2], s[3], s[4]);
227-
case 6: return python::make_tuple(s[0], s[1], s[2], s[3], s[4], s[5]);
228-
case 7: return python::make_tuple(s[0], s[1], s[2], s[3], s[4], s[5], s[6]);
229-
case 8: return python::make_tuple(s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7]);
230-
default: throw std::invalid_argument("this");
267+
case 1:
268+
return python::make_tuple(s[0]);
269+
case 2:
270+
return python::make_tuple(s[0], s[1]);
271+
case 3:
272+
return python::make_tuple(s[0], s[1], s[2]);
273+
case 4:
274+
return python::make_tuple(s[0], s[1], s[2], s[3]);
275+
case 5:
276+
return python::make_tuple(s[0], s[1], s[2], s[3], s[4]);
277+
case 6:
278+
return python::make_tuple(s[0], s[1], s[2], s[3], s[4], s[5]);
279+
case 7:
280+
return python::make_tuple(s[0], s[1], s[2], s[3], s[4], s[5], s[6]);
281+
case 8:
282+
return python::make_tuple(s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7]);
283+
default:
284+
throw std::invalid_argument("this");
231285
}
232286
}
233287

@@ -237,7 +291,7 @@ namespace python_multi_array
237291
//
238292
// return: the number of dimensions of the array.
239293
//
240-
template<class T, size_t N>
294+
template<class T, size_t N>
241295
size_t num_dimensions(shared_ptr<multi_array<T, N>>)
242296
{
243297
return N;
@@ -251,7 +305,7 @@ namespace python_multi_array
251305
// example:
252306
// It returns 8 for an array with shape (2, 4).
253307
//
254-
template<class T, size_t N>
308+
template<class T, size_t N>
255309
size_t num_elements(shared_ptr<multi_array<T, N>> t)
256310
{
257311
size_t n = 1;
@@ -268,7 +322,7 @@ namespace python_multi_array
268322
//
269323
// return: a copy of the array stored in numpy.ndarray.
270324
//
271-
template<class T, size_t N>
325+
template<class T, size_t N>
272326
python::object get(shared_ptr<multi_array<T, N>> t)
273327
{
274328
size_t s[N];
@@ -278,14 +332,22 @@ namespace python_multi_array
278332
auto make_tuple_from_array = [](const size_t* a) {
279333
switch (N)
280334
{
281-
case 1: return python::make_tuple(a[0]);
282-
case 2: return python::make_tuple(a[0], a[1]);
283-
case 3: return python::make_tuple(a[0], a[1], a[2]);
284-
case 4: return python::make_tuple(a[0], a[1], a[2], a[3]);
285-
case 5: return python::make_tuple(a[0], a[1], a[2], a[3], a[4]);
286-
case 6: return python::make_tuple(a[0], a[1], a[2], a[3], a[4], a[5]);
287-
case 7: return python::make_tuple(a[0], a[1], a[2], a[3], a[4], a[5], a[6]);
288-
case 8: return python::make_tuple(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7]);
335+
case 1:
336+
return python::make_tuple(a[0]);
337+
case 2:
338+
return python::make_tuple(a[0], a[1]);
339+
case 3:
340+
return python::make_tuple(a[0], a[1], a[2]);
341+
case 4:
342+
return python::make_tuple(a[0], a[1], a[2], a[3]);
343+
case 5:
344+
return python::make_tuple(a[0], a[1], a[2], a[3], a[4]);
345+
case 6:
346+
return python::make_tuple(a[0], a[1], a[2], a[3], a[4], a[5]);
347+
case 7:
348+
return python::make_tuple(a[0], a[1], a[2], a[3], a[4], a[5], a[6]);
349+
case 8:
350+
return python::make_tuple(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7]);
289351
}
290352
throw std::invalid_argument("this");
291353
};
@@ -303,12 +365,12 @@ namespace python_multi_array
303365
// nd.dtype may be different from x.element() but the values are implicitly
304366
// converted to x.element().
305367
//
306-
template<class T, size_t N>
368+
template<class T, size_t N>
307369
void set(shared_ptr<multi_array<T, N>> t, python::numpy::ndarray nd);
308370

309371
namespace impl
310372
{
311-
template<class T, size_t N, class S>
373+
template<class T, size_t N, class S>
312374
void set_typed(shared_ptr<multi_array<T, N>> t, python::numpy::ndarray nd)
313375
{
314376
if (N != nd.get_nd())
@@ -394,22 +456,58 @@ namespace python_multi_array
394456
}
395457
}
396458

397-
template<class T, size_t N>
459+
template<class T, size_t N>
398460
void set(shared_ptr<multi_array<T, N>> t, python::numpy::ndarray nd)
399461
{
400462
python::numpy::dtype dt = nd.get_dtype();
401-
if (dt == bool8) { impl::set_typed<T, N, bool>(t, nd); }
402-
else if (dt == uint8) { impl::set_typed<T, N, uint8_t>(t, nd); }
403-
else if (dt == uint16) { impl::set_typed<T, N, uint16_t>(t, nd); }
404-
else if (dt == uint32) { impl::set_typed<T, N, uint32_t>(t, nd); }
405-
else if (dt == uint64) { impl::set_typed<T, N, uint64_t>(t, nd); }
406-
else if (dt == int8) { impl::set_typed<T, N, int8_t>(t, nd); }
407-
else if (dt == int16) { impl::set_typed<T, N, int16_t>(t, nd); }
408-
else if (dt == int32) { impl::set_typed<T, N, int32_t>(t, nd); }
409-
else if (dt == int64) { impl::set_typed<T, N, int64_t>(t, nd); }
410-
else if (dt == float32) { impl::set_typed<T, N, float>(t, nd); }
411-
else if (dt == float64) { impl::set_typed<T, N, double>(t, nd); }
412-
else { throw std::invalid_argument("nd"); }
463+
if (dt == bool8)
464+
{
465+
impl::set_typed<T, N, bool>(t, nd);
466+
}
467+
else if (dt == uint8)
468+
{
469+
impl::set_typed<T, N, uint8_t>(t, nd);
470+
}
471+
else if (dt == uint16)
472+
{
473+
impl::set_typed<T, N, uint16_t>(t, nd);
474+
}
475+
else if (dt == uint32)
476+
{
477+
impl::set_typed<T, N, uint32_t>(t, nd);
478+
}
479+
else if (dt == uint64)
480+
{
481+
impl::set_typed<T, N, uint64_t>(t, nd);
482+
}
483+
else if (dt == int8)
484+
{
485+
impl::set_typed<T, N, int8_t>(t, nd);
486+
}
487+
else if (dt == int16)
488+
{
489+
impl::set_typed<T, N, int16_t>(t, nd);
490+
}
491+
else if (dt == int32)
492+
{
493+
impl::set_typed<T, N, int32_t>(t, nd);
494+
}
495+
else if (dt == int64)
496+
{
497+
impl::set_typed<T, N, int64_t>(t, nd);
498+
}
499+
else if (dt == float32)
500+
{
501+
impl::set_typed<T, N, float>(t, nd);
502+
}
503+
else if (dt == float64)
504+
{
505+
impl::set_typed<T, N, double>(t, nd);
506+
}
507+
else
508+
{
509+
throw std::invalid_argument("nd");
510+
}
413511
}
414512

415513
//
@@ -419,7 +517,7 @@ namespace python_multi_array
419517
class array_template
420518
{
421519
public:
422-
template<class T, size_t N>
520+
template<class T, size_t N>
423521
static void declare(const char* name)
424522
{
425523
python::class_<shared_ptr<multi_array<T, N>>>(name)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /