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