[12] | 1 | // Copyright David Abrahams 2002. |
---|
| 2 | // Distributed under the Boost Software License, Version 1.0. (See |
---|
| 3 | // accompanying file LICENSE_1_0.txt or copy at |
---|
| 4 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
| 5 | #ifndef NUMARRAY_DWA2002922_HPP |
---|
| 6 | # define NUMARRAY_DWA2002922_HPP |
---|
| 7 | |
---|
| 8 | # include <boost/python/detail/prefix.hpp> |
---|
| 9 | |
---|
| 10 | # include <boost/python/tuple.hpp> |
---|
| 11 | # include <boost/python/str.hpp> |
---|
| 12 | # include <boost/preprocessor/iteration/local.hpp> |
---|
| 13 | # include <boost/preprocessor/cat.hpp> |
---|
| 14 | # include <boost/preprocessor/repetition/enum.hpp> |
---|
| 15 | # include <boost/preprocessor/repetition/enum_params.hpp> |
---|
| 16 | # include <boost/preprocessor/repetition/enum_binary_params.hpp> |
---|
| 17 | |
---|
| 18 | namespace boost { namespace python { namespace numeric { |
---|
| 19 | |
---|
| 20 | namespace aux |
---|
| 21 | { |
---|
| 22 | struct BOOST_PYTHON_DECL array_base : object |
---|
| 23 | { |
---|
| 24 | # define BOOST_PP_LOCAL_MACRO(n) \ |
---|
| 25 | array_base(BOOST_PP_ENUM_PARAMS_Z(1, n, object const& x)); |
---|
| 26 | # define BOOST_PP_LOCAL_LIMITS (1, 7) |
---|
| 27 | # include BOOST_PP_LOCAL_ITERATE() |
---|
| 28 | |
---|
| 29 | object argmax(long axis=-1); |
---|
| 30 | object argmin(long axis=-1); |
---|
| 31 | object argsort(long axis=-1); |
---|
| 32 | object astype(object const& type = object()); |
---|
| 33 | void byteswap(); |
---|
| 34 | object copy() const; |
---|
| 35 | object diagonal(long offset = 0, long axis1 = 0, long axis2 = 1) const; |
---|
| 36 | void info() const; |
---|
| 37 | bool is_c_array() const; |
---|
| 38 | bool isbyteswapped() const; |
---|
| 39 | object new_(object type) const; |
---|
| 40 | void sort(); |
---|
| 41 | object trace(long offset = 0, long axis1 = 0, long axis2 = 1) const; |
---|
| 42 | object type() const; |
---|
| 43 | char typecode() const; |
---|
| 44 | |
---|
| 45 | object factory(object const& buffer=object() |
---|
| 46 | , object const& type=object() |
---|
| 47 | , object const& shape=object() |
---|
| 48 | , bool copy = true |
---|
| 49 | , bool savespace = false |
---|
| 50 | , object typecode = object()); |
---|
| 51 | |
---|
| 52 | object getflat() const; |
---|
| 53 | long getrank() const; |
---|
| 54 | object getshape() const; |
---|
| 55 | bool isaligned() const; |
---|
| 56 | bool iscontiguous() const; |
---|
| 57 | long itemsize() const; |
---|
| 58 | long nelements() const; |
---|
| 59 | object nonzero() const; |
---|
| 60 | |
---|
| 61 | void put(object const& indices, object const& values); |
---|
| 62 | |
---|
| 63 | void ravel(); |
---|
| 64 | |
---|
| 65 | object repeat(object const& repeats, long axis=0); |
---|
| 66 | |
---|
| 67 | void resize(object const& shape); |
---|
| 68 | |
---|
| 69 | void setflat(object const& flat); |
---|
| 70 | void setshape(object const& shape); |
---|
| 71 | |
---|
| 72 | void swapaxes(long axis1, long axis2); |
---|
| 73 | |
---|
| 74 | object take(object const& sequence, long axis = 0) const; |
---|
| 75 | |
---|
| 76 | void tofile(object const& file) const; |
---|
| 77 | |
---|
| 78 | str tostring() const; |
---|
| 79 | |
---|
| 80 | void transpose(object const& axes = object()); |
---|
| 81 | |
---|
| 82 | object view() const; |
---|
| 83 | |
---|
| 84 | public: // implementation detail - do not touch. |
---|
| 85 | BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(array_base, object); |
---|
| 86 | }; |
---|
| 87 | |
---|
| 88 | struct BOOST_PYTHON_DECL array_object_manager_traits |
---|
| 89 | { |
---|
| 90 | static bool check(PyObject* obj); |
---|
| 91 | static detail::new_non_null_reference adopt(PyObject* obj); |
---|
| 92 | }; |
---|
| 93 | } // namespace aux |
---|
| 94 | |
---|
| 95 | class array : public aux::array_base |
---|
| 96 | { |
---|
| 97 | typedef aux::array_base base; |
---|
| 98 | public: |
---|
| 99 | |
---|
| 100 | object astype() { return base::astype(); } |
---|
| 101 | |
---|
| 102 | template <class Type> |
---|
| 103 | object astype(Type const& type_) |
---|
| 104 | { |
---|
| 105 | return base::astype(object(type_)); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | template <class Type> |
---|
| 109 | object new_(Type const& type_) const |
---|
| 110 | { |
---|
| 111 | return base::new_(object(type_)); |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | template <class Sequence> |
---|
| 115 | void resize(Sequence const& x) |
---|
| 116 | { |
---|
| 117 | base::resize(object(x)); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | # define BOOST_PP_LOCAL_MACRO(n) \ |
---|
| 121 | void resize(BOOST_PP_ENUM_PARAMS_Z(1, n, long x)) \ |
---|
| 122 | { \ |
---|
| 123 | resize(make_tuple(BOOST_PP_ENUM_PARAMS_Z(1, n, x))); \ |
---|
| 124 | } |
---|
| 125 | # define BOOST_PP_LOCAL_LIMITS (1, BOOST_PYTHON_MAX_ARITY) |
---|
| 126 | # include BOOST_PP_LOCAL_ITERATE() |
---|
| 127 | |
---|
| 128 | template <class Sequence> |
---|
| 129 | void setshape(Sequence const& x) |
---|
| 130 | { |
---|
| 131 | base::setshape(object(x)); |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | # define BOOST_PP_LOCAL_MACRO(n) \ |
---|
| 135 | void setshape(BOOST_PP_ENUM_PARAMS_Z(1, n, long x)) \ |
---|
| 136 | { \ |
---|
| 137 | setshape(make_tuple(BOOST_PP_ENUM_PARAMS_Z(1, n, x))); \ |
---|
| 138 | } |
---|
| 139 | # define BOOST_PP_LOCAL_LIMITS (1, BOOST_PYTHON_MAX_ARITY) |
---|
| 140 | # include BOOST_PP_LOCAL_ITERATE() |
---|
| 141 | |
---|
| 142 | template <class Indices, class Values> |
---|
| 143 | void put(Indices const& indices, Values const& values) |
---|
| 144 | { |
---|
| 145 | base::put(object(indices), object(values)); |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | template <class Sequence> |
---|
| 149 | object take(Sequence const& sequence, long axis = 0) |
---|
| 150 | { |
---|
| 151 | return base::take(object(sequence), axis); |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | template <class File> |
---|
| 155 | void tofile(File const& f) const |
---|
| 156 | { |
---|
| 157 | base::tofile(object(f)); |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | object factory() |
---|
| 161 | { |
---|
| 162 | return base::factory(); |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | template <class Buffer> |
---|
| 166 | object factory(Buffer const& buffer) |
---|
| 167 | { |
---|
| 168 | return base::factory(object(buffer)); |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | template <class Buffer, class Type> |
---|
| 172 | object factory( |
---|
| 173 | Buffer const& buffer |
---|
| 174 | , Type const& type_) |
---|
| 175 | { |
---|
| 176 | return base::factory(object(buffer), object(type_)); |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | template <class Buffer, class Type, class Shape> |
---|
| 180 | object factory( |
---|
| 181 | Buffer const& buffer |
---|
| 182 | , Type const& type_ |
---|
| 183 | , Shape const& shape |
---|
| 184 | , bool copy = true |
---|
| 185 | , bool savespace = false) |
---|
| 186 | { |
---|
| 187 | return base::factory(object(buffer), object(type_), object(shape), copy, savespace); |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | template <class Buffer, class Type, class Shape> |
---|
| 191 | object factory( |
---|
| 192 | Buffer const& buffer |
---|
| 193 | , Type const& type_ |
---|
| 194 | , Shape const& shape |
---|
| 195 | , bool copy |
---|
| 196 | , bool savespace |
---|
| 197 | , char typecode) |
---|
| 198 | { |
---|
| 199 | return base::factory(object(buffer), object(type_), object(shape), copy, savespace, object(typecode)); |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | # define BOOST_PYTHON_ENUM_AS_OBJECT(z, n, x) object(BOOST_PP_CAT(x,n)) |
---|
| 203 | # define BOOST_PP_LOCAL_MACRO(n) \ |
---|
| 204 | template <BOOST_PP_ENUM_PARAMS_Z(1, n, class T)> \ |
---|
| 205 | explicit array(BOOST_PP_ENUM_BINARY_PARAMS_Z(1, n, T, const& x)) \ |
---|
| 206 | : base(BOOST_PP_ENUM_1(n, BOOST_PYTHON_ENUM_AS_OBJECT, x)) \ |
---|
| 207 | {} |
---|
| 208 | # define BOOST_PP_LOCAL_LIMITS (1, 7) |
---|
| 209 | # include BOOST_PP_LOCAL_ITERATE() |
---|
| 210 | # undef BOOST_PYTHON_AS_OBJECT |
---|
| 211 | |
---|
| 212 | static BOOST_PYTHON_DECL void set_module_and_type(char const* package_name = 0, char const* type_attribute_name = 0); |
---|
| 213 | |
---|
| 214 | public: // implementation detail -- for internal use only |
---|
| 215 | BOOST_PYTHON_FORWARD_OBJECT_CONSTRUCTORS(array, base); |
---|
| 216 | }; |
---|
| 217 | |
---|
| 218 | } // namespace boost::python::numeric |
---|
| 219 | |
---|
| 220 | namespace converter |
---|
| 221 | { |
---|
| 222 | template <> |
---|
| 223 | struct object_manager_traits< numeric::array > |
---|
| 224 | : numeric::aux::array_object_manager_traits |
---|
| 225 | { |
---|
| 226 | BOOST_STATIC_CONSTANT(bool, is_specialized = true); |
---|
| 227 | }; |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | }} // namespace boost::python |
---|
| 231 | |
---|
| 232 | #endif // NUMARRAY_DWA2002922_HPP |
---|