Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
kwant
tinyarray
Commits
e2a5e676
Commit
e2a5e676
authored
Nov 30, 2015
by
Christoph Groth
Browse files
finish
33c3a965
by removing unused code and renaming functions
parent
eddb7e15
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/array.cc
View file @
e2a5e676
...
...
@@ -165,22 +165,23 @@ const char *seq_err_msg =
"A sequence does not support sequence protocol - "
"this is probably due to a bug in numpy for 0-d arrays."
;
// This function determines the shape of an array
like sequence (
or sequence
of
// sequences
or number
) given to it as first parameter. `dtype_guess' is the
//
dtype of
the first element of the sequence.
// This function determines the shape of an array
-
like sequence (of
// sequences
...
) given to it as first parameter. `dtype_guess' is the
dtype of
// the first element of the sequence.
//
// All four arguments after the first one are written to. `shape' and `seqs'
// must have space for at least `max_ndim' elements.
//
// After successful execution `seqs' will contain `ndim' new references
// returned by PySequence_Fast.
int
examine_
arraylik
e
(
PyObject
*
arraylike
,
int
*
ndim
,
size_t
*
shape
,
PyObject
**
seqs
,
Dtype
*
dtype_guess
)
int
examine_
sequenc
e
(
PyObject
*
arraylike
,
int
*
ndim
,
size_t
*
shape
,
PyObject
**
seqs
,
Dtype
*
dtype_guess
)
{
PyObject
*
p
=
arraylike
;
int
d
=
-
1
;
while
(
true
)
{
if
(
PySequence_Check
(
p
))
{
assert
(
PySequence_Check
(
p
));
for
(
bool
is_sequence
=
true
;
;
is_sequence
=
PySequence_Check
(
p
))
{
if
(
is_sequence
)
{
++
d
;
if
(
d
==
ptrdiff_t
(
max_ndim
))
{
// Strings are, in a way, infinitely nested sequences because
...
...
@@ -225,22 +226,13 @@ fail:
return
-
1
;
}
// This function is designed to be run after examine_
arraylik
e. It takes care
// This function is designed to be run after examine_
sequenc
e. It takes care
// of releasing the references passed to it in seqs.
template
<
typename
T
>
int
readin_
arraylike
(
T
*
dest
,
int
ndim
,
const
size_t
*
shape
,
PyObject
*
arraylike
,
PyObject
**
seqs
,
bool
exact
)
int
readin_
seqs
(
PyObject
**
seqs
,
T
*
dest
,
int
ndim
,
const
size_t
*
shape
,
bool
exact
)
{
if
(
ndim
==
0
)
{
T
value
;
if
(
exact
)
value
=
number_from_pyobject_exact
<
T
>
(
arraylike
);
else
value
=
number_from_pyobject
<
T
>
(
arraylike
);
if
(
value
==
T
(
-
1
)
&&
PyErr_Occurred
())
return
-
1
;
*
dest
++
=
value
;
return
0
;
}
assert
(
ndim
>
0
);
// seqs is the stack of sequences being processed, all returned by
// PySequence_Fast. ps[d] and es[d] are the begin and end of the elements
...
...
@@ -323,9 +315,8 @@ fail:
}
template
<
typename
T
>
PyObject
*
make_and_readin_array
(
PyObject
*
in
,
int
ndim_in
,
int
ndim_out
,
const
size_t
*
shape_out
,
PyObject
**
seqs
,
bool
exact
)
PyObject
*
readin_seqs_into_new
(
PyObject
**
seqs
,
int
ndim_in
,
int
ndim_out
,
const
size_t
*
shape_out
,
bool
exact
)
{
Array
<
T
>
*
result
=
Array
<
T
>::
make
(
ndim_out
,
shape_out
);
assert
(
ndim_out
>=
ndim_in
);
...
...
@@ -334,8 +325,8 @@ PyObject *make_and_readin_array(PyObject *in, int ndim_in, int ndim_out,
assert
(
shape_out
[
d
]
==
1
);
#endif
if
(
result
==
0
)
return
0
;
if
(
readin_
arraylike
<
T
>
(
result
->
data
(),
ndim_in
,
shape_out
+
ndim_out
-
ndim_in
,
in
,
seqs
,
exact
)
if
(
readin_
seqs
<
T
>
(
seqs
,
result
->
data
(),
ndim_in
,
shape_out
+
ndim_out
-
ndim_in
,
exact
)
==
-
1
)
{
Py_DECREF
(
result
);
return
0
;
...
...
@@ -343,12 +334,12 @@ PyObject *make_and_readin_array(PyObject *in, int ndim_in, int ndim_out,
return
(
PyObject
*
)
result
;
}
PyObject
*
(
*
make_and_readin_array
_dtable
[])(
PyObject
*
,
int
,
int
,
const
size_t
*
,
PyObject
**
,
bool
)
=
DTYPE_DISPATCH
(
make_and_readin_array
);
PyObject
*
(
*
readin_seqs_into_new
_dtable
[])(
PyObject
*
*
,
int
,
int
,
const
size_t
*
,
bool
)
=
DTYPE_DISPATCH
(
readin_seqs_into_new
);
template
<
typename
T
>
PyObject
*
make_and_
readin_scalar
(
PyObject
*
in
,
bool
exact
,
int
ndim
=
0
)
PyObject
*
readin_scalar
_into_new
(
PyObject
*
in
,
bool
exact
,
int
ndim
=
0
)
{
T
value
;
if
(
exact
)
...
...
@@ -368,8 +359,8 @@ PyObject *make_and_readin_scalar(PyObject *in, bool exact, int ndim = 0)
return
(
PyObject
*
)
result
;
}
PyObject
*
(
*
make_and_
readin_scalar_dtable
[])(
PyObject
*
,
bool
,
int
)
=
DTYPE_DISPATCH
(
make_and_
readin_scalar
);
PyObject
*
(
*
readin_scalar_
into_new_
dtable
[])(
PyObject
*
,
bool
,
int
)
=
DTYPE_DISPATCH
(
readin_scalar
_into_new
);
int
examine_buffer
(
PyObject
*
in
,
Py_buffer
*
view
,
Dtype
*
dtype
)
{
...
...
@@ -1383,11 +1374,11 @@ PyObject *array_from_arraylike(PyObject *in, Dtype *dtype, Dtype dtype_min,
*
dtype
=
dt
;
return
result
;
}
else
if
(
PySequence_Check
(
in
))
{
// `in` is not an array, but is a sequence
// `in` is not an array, but is a sequence
.
bool
find_type
=
(
dt
==
NONE
);
// Try if buffer interface is supported
// Try if buffer interface is supported
.
Py_buffer
view
;
if
(
examine_buffer
(
in
,
&
view
,
find_type
?
&
dt
:
0
)
==
0
)
{
if
(
find_type
&&
int
(
dt
)
<
int
(
dtype_min
))
dt
=
dtype_min
;
...
...
@@ -1410,8 +1401,8 @@ PyObject *array_from_arraylike(PyObject *in, Dtype *dtype, Dtype dtype_min,
return
result
;
}
if
(
examine_
arraylik
e
(
in
,
&
ndim
,
shape
,
seqs
,
find_type
?
&
dt
:
0
)
==
0
)
{
if
(
examine_
sequenc
e
(
in
,
&
ndim
,
shape
,
seqs
,
find_type
?
&
dt
:
0
)
==
0
)
{
if
(
as_matrix
&&
ndim
!=
2
)
{
if
(
ndim
>
2
)
{
PyErr_SetString
(
PyExc_ValueError
,
...
...
@@ -1431,8 +1422,8 @@ PyObject *array_from_arraylike(PyObject *in, Dtype *dtype, Dtype dtype_min,
}
if
(
int
(
dt
)
<
int
(
dtype_min
))
dt
=
dtype_min
;
while
(
true
)
{
result
=
make_and_readin_array
_dtable
[
int
(
dt
)](
in
,
ndim
,
(
as_matrix
?
2
:
ndim
),
shape
,
seqs
,
true
);
result
=
readin_seqs_into_new
_dtable
[
int
(
dt
)](
seqs
,
ndim
,
(
as_matrix
?
2
:
ndim
),
shape
,
true
);
if
(
result
)
break
;
dt
=
Dtype
(
int
(
dt
)
+
1
);
if
(
dt
==
NONE
)
{
...
...
@@ -1446,30 +1437,31 @@ PyObject *array_from_arraylike(PyObject *in, Dtype *dtype, Dtype dtype_min,
for
(
int
d
=
0
;
d
<
ndim
;
++
d
)
Py_DECREF
(
seqs_copy
[
d
]);
}
else
{
// A specific dtype has been requested.
result
=
make_and_readin_array
_dtable
[
int
(
dt
)](
in
,
ndim
,
(
as_matrix
?
2
:
ndim
),
shape
,
seqs
,
false
);
result
=
readin_seqs_into_new
_dtable
[
int
(
dt
)](
seqs
,
ndim
,
(
as_matrix
?
2
:
ndim
),
shape
,
false
);
}
*
dtype
=
dt
;
return
result
;
}
}
else
{
// `in` is a scalar
// `in` is a scalar
, or an invalid input.
dtype_in
=
dtype_of_scalar
(
in
);
bool
find_type
=
(
dt
==
NONE
);
if
(
dtype_in
==
NONE
)
{
PyErr_SetString
(
PyExc_TypeError
,
"Expecting a number."
);
PyErr_SetString
(
PyExc_TypeError
,
"Expecting an arraylike object or a scalar."
);
return
0
;
}
if
(
find_type
)
{
dt
=
Dtype
(
std
::
max
(
int
(
dtype_in
),
int
(
dtype_min
)));
result
=
make_and_
readin_scalar_dtable
[
int
(
dt
)](
result
=
readin_scalar_
into_new_
dtable
[
int
(
dt
)](
in
,
true
,
(
as_matrix
?
2
:
0
));
}
else
{
result
=
make_and_
readin_scalar_dtable
[
int
(
dt
)](
result
=
readin_scalar_
into_new_
dtable
[
int
(
dt
)](
in
,
false
,
(
as_matrix
?
2
:
0
));
}
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment