Upgraded boost
This commit is contained in:
1 parent
10da2cde64
commit
a737c4ecf6
15332 files changed
+3057188
-2380045
No files matched your search
@@ -1,57 +1,57 @@
|
||||
|
||||
// (C) Copyright Joel de Guzman 2003.
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef PY_CONTAINER_UTILS_JDG20038_HPP
|
||||
# define PY_CONTAINER_UTILS_JDG20038_HPP
|
||||
|
||||
# include <utility>
|
||||
# include <boost/foreach.hpp>
|
||||
# include <boost/python/object.hpp>
|
||||
# include <boost/python/handle.hpp>
|
||||
# include <boost/python/extract.hpp>
|
||||
# include <boost/python/stl_iterator.hpp>
|
||||
|
||||
namespace boost { namespace python { namespace container_utils {
|
||||
|
||||
template <typename Container>
|
||||
void
|
||||
extend_container(Container& container, object l)
|
||||
{
|
||||
typedef typename Container::value_type data_type;
|
||||
|
||||
// l must be iterable
|
||||
BOOST_FOREACH(object elem,
|
||||
std::make_pair(
|
||||
boost::python::stl_input_iterator<object>(l),
|
||||
boost::python::stl_input_iterator<object>()
|
||||
))
|
||||
{
|
||||
extract<data_type const&> x(elem);
|
||||
// try if elem is an exact data_type type
|
||||
if (x.check())
|
||||
{
|
||||
container.push_back(x());
|
||||
}
|
||||
else
|
||||
{
|
||||
// try to convert elem to data_type type
|
||||
extract<data_type> x(elem);
|
||||
if (x.check())
|
||||
{
|
||||
container.push_back(x());
|
||||
}
|
||||
else
|
||||
{
|
||||
PyErr_SetString(PyExc_TypeError, "Incompatible Data Type");
|
||||
throw_error_already_set();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}}} // namespace boost::python::container_utils
|
||||
|
||||
#endif
|
||||
|
||||
// (C) Copyright Joel de Guzman 2003.
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef PY_CONTAINER_UTILS_JDG20038_HPP
|
||||
# define PY_CONTAINER_UTILS_JDG20038_HPP
|
||||
|
||||
# include <utility>
|
||||
# include <boost/foreach.hpp>
|
||||
# include <boost/python/object.hpp>
|
||||
# include <boost/python/handle.hpp>
|
||||
# include <boost/python/extract.hpp>
|
||||
# include <boost/python/stl_iterator.hpp>
|
||||
|
||||
namespace boost { namespace python { namespace container_utils {
|
||||
|
||||
template <typename Container>
|
||||
void
|
||||
extend_container(Container& container, object l)
|
||||
{
|
||||
typedef typename Container::value_type data_type;
|
||||
|
||||
// l must be iterable
|
||||
BOOST_FOREACH(object elem,
|
||||
std::make_pair(
|
||||
boost::python::stl_input_iterator<object>(l),
|
||||
boost::python::stl_input_iterator<object>()
|
||||
))
|
||||
{
|
||||
extract<data_type const&> x(elem);
|
||||
// try if elem is an exact data_type type
|
||||
if (x.check())
|
||||
{
|
||||
container.push_back(x());
|
||||
}
|
||||
else
|
||||
{
|
||||
// try to convert elem to data_type type
|
||||
extract<data_type> x(elem);
|
||||
if (x.check())
|
||||
{
|
||||
container.push_back(x());
|
||||
}
|
||||
else
|
||||
{
|
||||
PyErr_SetString(PyExc_TypeError, "Incompatible Data Type");
|
||||
throw_error_already_set();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}}} // namespace boost::python::container_utils
|
||||
|
||||
#endif
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,295 +1,295 @@
|
||||
// (C) Copyright Joel de Guzman 2003.
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef INDEXING_SUITE_JDG20036_HPP
|
||||
# define INDEXING_SUITE_JDG20036_HPP
|
||||
|
||||
# include <boost/python/class.hpp>
|
||||
# include <boost/python/def_visitor.hpp>
|
||||
# include <boost/python/register_ptr_to_python.hpp>
|
||||
# include <boost/python/suite/indexing/detail/indexing_suite_detail.hpp>
|
||||
# include <boost/python/return_internal_reference.hpp>
|
||||
# include <boost/python/iterator.hpp>
|
||||
# include <boost/mpl/or.hpp>
|
||||
# include <boost/mpl/not.hpp>
|
||||
# include <boost/type_traits/is_same.hpp>
|
||||
|
||||
namespace boost { namespace python {
|
||||
|
||||
// indexing_suite class. This class is the facade class for
|
||||
// the management of C++ containers intended to be integrated
|
||||
// to Python. The objective is make a C++ container look and
|
||||
// feel and behave exactly as we'd expect a Python container.
|
||||
// By default indexed elements are returned by proxy. This can be
|
||||
// disabled by supplying *true* in the NoProxy template parameter.
|
||||
//
|
||||
// Derived classes provide the hooks needed by the indexing_suite
|
||||
// to do its job:
|
||||
//
|
||||
// static data_type&
|
||||
// get_item(Container& container, index_type i);
|
||||
//
|
||||
// static object
|
||||
// get_slice(Container& container, index_type from, index_type to);
|
||||
//
|
||||
// static void
|
||||
// set_item(Container& container, index_type i, data_type const& v);
|
||||
//
|
||||
// static void
|
||||
// set_slice(
|
||||
// Container& container, index_type from,
|
||||
// index_type to, data_type const& v
|
||||
// );
|
||||
//
|
||||
// template <class Iter>
|
||||
// static void
|
||||
// set_slice(Container& container, index_type from,
|
||||
// index_type to, Iter first, Iter last
|
||||
// );
|
||||
//
|
||||
// static void
|
||||
// delete_item(Container& container, index_type i);
|
||||
//
|
||||
// static void
|
||||
// delete_slice(Container& container, index_type from, index_type to);
|
||||
//
|
||||
// static size_t
|
||||
// size(Container& container);
|
||||
//
|
||||
// template <class T>
|
||||
// static bool
|
||||
// contains(Container& container, T const& val);
|
||||
//
|
||||
// static index_type
|
||||
// convert_index(Container& container, PyObject* i);
|
||||
//
|
||||
// static index_type
|
||||
// adjust_index(index_type current, index_type from,
|
||||
// index_type to, size_type len
|
||||
// );
|
||||
//
|
||||
// Most of these policies are self explanatory. convert_index and
|
||||
// adjust_index, however, deserves some explanation.
|
||||
//
|
||||
// convert_index converts an Python index into a C++ index that the
|
||||
// container can handle. For instance, negative indexes in Python, by
|
||||
// convention, indexes from the right (e.g. C[-1] indexes the rightmost
|
||||
// element in C). convert_index should handle the necessary conversion
|
||||
// for the C++ container (e.g. convert -1 to C.size()-1). convert_index
|
||||
// should also be able to convert the type of the index (A dynamic Python
|
||||
// type) to the actual type that the C++ container expects.
|
||||
//
|
||||
// When a container expands or contracts, held indexes to its elements
|
||||
// must be adjusted to follow the movement of data. For instance, if
|
||||
// we erase 3 elements, starting from index 0 from a 5 element vector,
|
||||
// what used to be at index 4 will now be at index 1:
|
||||
//
|
||||
// [a][b][c][d][e] ---> [d][e]
|
||||
// ^ ^
|
||||
// 4 1
|
||||
//
|
||||
// adjust_index takes care of the adjustment. Given a current index,
|
||||
// the function should return the adjusted index when data in the
|
||||
// container at index from..to is replaced by *len* elements.
|
||||
//
|
||||
|
||||
template <
|
||||
class Container
|
||||
, class DerivedPolicies
|
||||
, bool NoProxy = false
|
||||
, bool NoSlice = false
|
||||
, class Data = typename Container::value_type
|
||||
, class Index = typename Container::size_type
|
||||
, class Key = typename Container::value_type
|
||||
>
|
||||
class indexing_suite
|
||||
: public def_visitor<
|
||||
indexing_suite<
|
||||
Container
|
||||
, DerivedPolicies
|
||||
, NoProxy
|
||||
, NoSlice
|
||||
, Data
|
||||
, Index
|
||||
, Key
|
||||
> >
|
||||
{
|
||||
private:
|
||||
|
||||
typedef mpl::or_<
|
||||
mpl::bool_<NoProxy>
|
||||
, mpl::not_<is_class<Data> >
|
||||
, typename mpl::or_<
|
||||
is_same<Data, std::string>
|
||||
, is_same<Data, std::complex<float> >
|
||||
, is_same<Data, std::complex<double> >
|
||||
, is_same<Data, std::complex<long double> > >::type>
|
||||
no_proxy;
|
||||
|
||||
typedef detail::container_element<Container, Index, DerivedPolicies>
|
||||
container_element_t;
|
||||
|
||||
typedef return_internal_reference<> return_policy;
|
||||
|
||||
typedef typename mpl::if_<
|
||||
no_proxy
|
||||
, iterator<Container>
|
||||
, iterator<Container, return_policy> >::type
|
||||
def_iterator;
|
||||
|
||||
typedef typename mpl::if_<
|
||||
no_proxy
|
||||
, detail::no_proxy_helper<
|
||||
Container
|
||||
, DerivedPolicies
|
||||
, container_element_t
|
||||
, Index>
|
||||
, detail::proxy_helper<
|
||||
Container
|
||||
, DerivedPolicies
|
||||
, container_element_t
|
||||
, Index> >::type
|
||||
proxy_handler;
|
||||
|
||||
typedef typename mpl::if_<
|
||||
mpl::bool_<NoSlice>
|
||||
, detail::no_slice_helper<
|
||||
Container
|
||||
, DerivedPolicies
|
||||
, proxy_handler
|
||||
, Data
|
||||
, Index>
|
||||
, detail::slice_helper<
|
||||
Container
|
||||
, DerivedPolicies
|
||||
, proxy_handler
|
||||
, Data
|
||||
, Index> >::type
|
||||
slice_handler;
|
||||
|
||||
public:
|
||||
|
||||
template <class Class>
|
||||
void visit(Class& cl) const
|
||||
{
|
||||
// Hook into the class_ generic visitation .def function
|
||||
proxy_handler::register_container_element();
|
||||
|
||||
cl
|
||||
.def("__len__", base_size)
|
||||
.def("__setitem__", &base_set_item)
|
||||
.def("__delitem__", &base_delete_item)
|
||||
.def("__getitem__", &base_get_item)
|
||||
.def("__contains__", &base_contains)
|
||||
.def("__iter__", def_iterator())
|
||||
;
|
||||
|
||||
DerivedPolicies::extension_def(cl);
|
||||
}
|
||||
|
||||
template <class Class>
|
||||
static void
|
||||
extension_def(Class& cl)
|
||||
{
|
||||
// default.
|
||||
// no more extensions
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
static object
|
||||
base_get_item(back_reference<Container&> container, PyObject* i)
|
||||
{
|
||||
if (PySlice_Check(i))
|
||||
return slice_handler::base_get_slice(
|
||||
container.get(), static_cast<PySliceObject*>(static_cast<void*>(i)));
|
||||
|
||||
return proxy_handler::base_get_item_(container, i);
|
||||
}
|
||||
|
||||
static void
|
||||
base_set_item(Container& container, PyObject* i, PyObject* v)
|
||||
{
|
||||
if (PySlice_Check(i))
|
||||
{
|
||||
slice_handler::base_set_slice(container,
|
||||
static_cast<PySliceObject*>(static_cast<void*>(i)), v);
|
||||
}
|
||||
else
|
||||
{
|
||||
extract<Data&> elem(v);
|
||||
// try if elem is an exact Data
|
||||
if (elem.check())
|
||||
{
|
||||
DerivedPolicies::
|
||||
set_item(container,
|
||||
DerivedPolicies::
|
||||
convert_index(container, i), elem());
|
||||
}
|
||||
else
|
||||
{
|
||||
// try to convert elem to Data
|
||||
extract<Data> elem(v);
|
||||
if (elem.check())
|
||||
{
|
||||
DerivedPolicies::
|
||||
set_item(container,
|
||||
DerivedPolicies::
|
||||
convert_index(container, i), elem());
|
||||
}
|
||||
else
|
||||
{
|
||||
PyErr_SetString(PyExc_TypeError, "Invalid assignment");
|
||||
throw_error_already_set();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
base_delete_item(Container& container, PyObject* i)
|
||||
{
|
||||
if (PySlice_Check(i))
|
||||
{
|
||||
slice_handler::base_delete_slice(
|
||||
container, static_cast<PySliceObject*>(static_cast<void*>(i)));
|
||||
return;
|
||||
}
|
||||
|
||||
Index index = DerivedPolicies::convert_index(container, i);
|
||||
proxy_handler::base_erase_index(container, index, mpl::bool_<NoSlice>());
|
||||
DerivedPolicies::delete_item(container, index);
|
||||
}
|
||||
|
||||
static size_t
|
||||
base_size(Container& container)
|
||||
{
|
||||
return DerivedPolicies::size(container);
|
||||
}
|
||||
|
||||
static bool
|
||||
base_contains(Container& container, PyObject* key)
|
||||
{
|
||||
extract<Key const&> x(key);
|
||||
// try if key is an exact Key type
|
||||
if (x.check())
|
||||
{
|
||||
return DerivedPolicies::contains(container, x());
|
||||
}
|
||||
else
|
||||
{
|
||||
// try to convert key to Key type
|
||||
extract<Key> x(key);
|
||||
if (x.check())
|
||||
return DerivedPolicies::contains(container, x());
|
||||
else
|
||||
return false;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}} // namespace boost::python
|
||||
|
||||
#endif // INDEXING_SUITE_JDG20036_HPP
|
||||
// (C) Copyright Joel de Guzman 2003.
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef INDEXING_SUITE_JDG20036_HPP
|
||||
# define INDEXING_SUITE_JDG20036_HPP
|
||||
|
||||
# include <boost/python/class.hpp>
|
||||
# include <boost/python/def_visitor.hpp>
|
||||
# include <boost/python/register_ptr_to_python.hpp>
|
||||
# include <boost/python/suite/indexing/detail/indexing_suite_detail.hpp>
|
||||
# include <boost/python/return_internal_reference.hpp>
|
||||
# include <boost/python/iterator.hpp>
|
||||
# include <boost/mpl/or.hpp>
|
||||
# include <boost/mpl/not.hpp>
|
||||
# include <boost/python/detail/type_traits.hpp>
|
||||
|
||||
namespace boost { namespace python {
|
||||
|
||||
// indexing_suite class. This class is the facade class for
|
||||
// the management of C++ containers intended to be integrated
|
||||
// to Python. The objective is make a C++ container look and
|
||||
// feel and behave exactly as we'd expect a Python container.
|
||||
// By default indexed elements are returned by proxy. This can be
|
||||
// disabled by supplying *true* in the NoProxy template parameter.
|
||||
//
|
||||
// Derived classes provide the hooks needed by the indexing_suite
|
||||
// to do its job:
|
||||
//
|
||||
// static data_type&
|
||||
// get_item(Container& container, index_type i);
|
||||
//
|
||||
// static object
|
||||
// get_slice(Container& container, index_type from, index_type to);
|
||||
//
|
||||
// static void
|
||||
// set_item(Container& container, index_type i, data_type const& v);
|
||||
//
|
||||
// static void
|
||||
// set_slice(
|
||||
// Container& container, index_type from,
|
||||
// index_type to, data_type const& v
|
||||
// );
|
||||
//
|
||||
// template <class Iter>
|
||||
// static void
|
||||
// set_slice(Container& container, index_type from,
|
||||
// index_type to, Iter first, Iter last
|
||||
// );
|
||||
//
|
||||
// static void
|
||||
// delete_item(Container& container, index_type i);
|
||||
//
|
||||
// static void
|
||||
// delete_slice(Container& container, index_type from, index_type to);
|
||||
//
|
||||
// static size_t
|
||||
// size(Container& container);
|
||||
//
|
||||
// template <class T>
|
||||
// static bool
|
||||
// contains(Container& container, T const& val);
|
||||
//
|
||||
// static index_type
|
||||
// convert_index(Container& container, PyObject* i);
|
||||
//
|
||||
// static index_type
|
||||
// adjust_index(index_type current, index_type from,
|
||||
// index_type to, size_type len
|
||||
// );
|
||||
//
|
||||
// Most of these policies are self explanatory. convert_index and
|
||||
// adjust_index, however, deserves some explanation.
|
||||
//
|
||||
// convert_index converts an Python index into a C++ index that the
|
||||
// container can handle. For instance, negative indexes in Python, by
|
||||
// convention, indexes from the right (e.g. C[-1] indexes the rightmost
|
||||
// element in C). convert_index should handle the necessary conversion
|
||||
// for the C++ container (e.g. convert -1 to C.size()-1). convert_index
|
||||
// should also be able to convert the type of the index (A dynamic Python
|
||||
// type) to the actual type that the C++ container expects.
|
||||
//
|
||||
// When a container expands or contracts, held indexes to its elements
|
||||
// must be adjusted to follow the movement of data. For instance, if
|
||||
// we erase 3 elements, starting from index 0 from a 5 element vector,
|
||||
// what used to be at index 4 will now be at index 1:
|
||||
//
|
||||
// [a][b][c][d][e] ---> [d][e]
|
||||
// ^ ^
|
||||
// 4 1
|
||||
//
|
||||
// adjust_index takes care of the adjustment. Given a current index,
|
||||
// the function should return the adjusted index when data in the
|
||||
// container at index from..to is replaced by *len* elements.
|
||||
//
|
||||
|
||||
template <
|
||||
class Container
|
||||
, class DerivedPolicies
|
||||
, bool NoProxy = false
|
||||
, bool NoSlice = false
|
||||
, class Data = typename Container::value_type
|
||||
, class Index = typename Container::size_type
|
||||
, class Key = typename Container::value_type
|
||||
>
|
||||
class indexing_suite
|
||||
: public def_visitor<
|
||||
indexing_suite<
|
||||
Container
|
||||
, DerivedPolicies
|
||||
, NoProxy
|
||||
, NoSlice
|
||||
, Data
|
||||
, Index
|
||||
, Key
|
||||
> >
|
||||
{
|
||||
private:
|
||||
|
||||
typedef mpl::or_<
|
||||
mpl::bool_<NoProxy>
|
||||
, mpl::not_<is_class<Data> >
|
||||
, typename mpl::or_<
|
||||
detail::is_same<Data, std::string>
|
||||
, detail::is_same<Data, std::complex<float> >
|
||||
, detail::is_same<Data, std::complex<double> >
|
||||
, detail::is_same<Data, std::complex<long double> > >::type>
|
||||
no_proxy;
|
||||
|
||||
typedef detail::container_element<Container, Index, DerivedPolicies>
|
||||
container_element_t;
|
||||
|
||||
typedef return_internal_reference<> return_policy;
|
||||
|
||||
typedef typename mpl::if_<
|
||||
no_proxy
|
||||
, iterator<Container>
|
||||
, iterator<Container, return_policy> >::type
|
||||
def_iterator;
|
||||
|
||||
typedef typename mpl::if_<
|
||||
no_proxy
|
||||
, detail::no_proxy_helper<
|
||||
Container
|
||||
, DerivedPolicies
|
||||
, container_element_t
|
||||
, Index>
|
||||
, detail::proxy_helper<
|
||||
Container
|
||||
, DerivedPolicies
|
||||
, container_element_t
|
||||
, Index> >::type
|
||||
proxy_handler;
|
||||
|
||||
typedef typename mpl::if_<
|
||||
mpl::bool_<NoSlice>
|
||||
, detail::no_slice_helper<
|
||||
Container
|
||||
, DerivedPolicies
|
||||
, proxy_handler
|
||||
, Data
|
||||
, Index>
|
||||
, detail::slice_helper<
|
||||
Container
|
||||
, DerivedPolicies
|
||||
, proxy_handler
|
||||
, Data
|
||||
, Index> >::type
|
||||
slice_handler;
|
||||
|
||||
public:
|
||||
|
||||
template <class Class>
|
||||
void visit(Class& cl) const
|
||||
{
|
||||
// Hook into the class_ generic visitation .def function
|
||||
proxy_handler::register_container_element();
|
||||
|
||||
cl
|
||||
.def("__len__", base_size)
|
||||
.def("__setitem__", &base_set_item)
|
||||
.def("__delitem__", &base_delete_item)
|
||||
.def("__getitem__", &base_get_item)
|
||||
.def("__contains__", &base_contains)
|
||||
.def("__iter__", def_iterator())
|
||||
;
|
||||
|
||||
DerivedPolicies::extension_def(cl);
|
||||
}
|
||||
|
||||
template <class Class>
|
||||
static void
|
||||
extension_def(Class& cl)
|
||||
{
|
||||
// default.
|
||||
// no more extensions
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
static object
|
||||
base_get_item(back_reference<Container&> container, PyObject* i)
|
||||
{
|
||||
if (PySlice_Check(i))
|
||||
return slice_handler::base_get_slice(
|
||||
container.get(), static_cast<PySliceObject*>(static_cast<void*>(i)));
|
||||
|
||||
return proxy_handler::base_get_item_(container, i);
|
||||
}
|
||||
|
||||
static void
|
||||
base_set_item(Container& container, PyObject* i, PyObject* v)
|
||||
{
|
||||
if (PySlice_Check(i))
|
||||
{
|
||||
slice_handler::base_set_slice(container,
|
||||
static_cast<PySliceObject*>(static_cast<void*>(i)), v);
|
||||
}
|
||||
else
|
||||
{
|
||||
extract<Data&> elem(v);
|
||||
// try if elem is an exact Data
|
||||
if (elem.check())
|
||||
{
|
||||
DerivedPolicies::
|
||||
set_item(container,
|
||||
DerivedPolicies::
|
||||
convert_index(container, i), elem());
|
||||
}
|
||||
else
|
||||
{
|
||||
// try to convert elem to Data
|
||||
extract<Data> elem(v);
|
||||
if (elem.check())
|
||||
{
|
||||
DerivedPolicies::
|
||||
set_item(container,
|
||||
DerivedPolicies::
|
||||
convert_index(container, i), elem());
|
||||
}
|
||||
else
|
||||
{
|
||||
PyErr_SetString(PyExc_TypeError, "Invalid assignment");
|
||||
throw_error_already_set();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
base_delete_item(Container& container, PyObject* i)
|
||||
{
|
||||
if (PySlice_Check(i))
|
||||
{
|
||||
slice_handler::base_delete_slice(
|
||||
container, static_cast<PySliceObject*>(static_cast<void*>(i)));
|
||||
return;
|
||||
}
|
||||
|
||||
Index index = DerivedPolicies::convert_index(container, i);
|
||||
proxy_handler::base_erase_index(container, index, mpl::bool_<NoSlice>());
|
||||
DerivedPolicies::delete_item(container, index);
|
||||
}
|
||||
|
||||
static size_t
|
||||
base_size(Container& container)
|
||||
{
|
||||
return DerivedPolicies::size(container);
|
||||
}
|
||||
|
||||
static bool
|
||||
base_contains(Container& container, PyObject* key)
|
||||
{
|
||||
extract<Key const&> x(key);
|
||||
// try if key is an exact Key type
|
||||
if (x.check())
|
||||
{
|
||||
return DerivedPolicies::contains(container, x());
|
||||
}
|
||||
else
|
||||
{
|
||||
// try to convert key to Key type
|
||||
extract<Key> x(key);
|
||||
if (x.check())
|
||||
return DerivedPolicies::contains(container, x());
|
||||
else
|
||||
return false;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
}} // namespace boost::python
|
||||
|
||||
#endif // INDEXING_SUITE_JDG20036_HPP
|
||||
@@ -1,181 +1,181 @@
|
||||
// (C) Copyright Joel de Guzman 2003.
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef MAP_INDEXING_SUITE_JDG20038_HPP
|
||||
# define MAP_INDEXING_SUITE_JDG20038_HPP
|
||||
|
||||
# include <boost/python/suite/indexing/indexing_suite.hpp>
|
||||
# include <boost/python/iterator.hpp>
|
||||
# include <boost/python/call_method.hpp>
|
||||
# include <boost/python/tuple.hpp>
|
||||
|
||||
namespace boost { namespace python {
|
||||
|
||||
// Forward declaration
|
||||
template <class Container, bool NoProxy, class DerivedPolicies>
|
||||
class map_indexing_suite;
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template <class Container, bool NoProxy>
|
||||
class final_map_derived_policies
|
||||
: public map_indexing_suite<Container,
|
||||
NoProxy, final_map_derived_policies<Container, NoProxy> > {};
|
||||
}
|
||||
|
||||
// The map_indexing_suite class is a predefined indexing_suite derived
|
||||
// class for wrapping std::map (and std::map like) classes. It provides
|
||||
// all the policies required by the indexing_suite (see indexing_suite).
|
||||
// Example usage:
|
||||
//
|
||||
// class X {...};
|
||||
//
|
||||
// ...
|
||||
//
|
||||
// class_<std::map<std::string, X> >("XMap")
|
||||
// .def(map_indexing_suite<std::map<std::string, X> >())
|
||||
// ;
|
||||
//
|
||||
// By default indexed elements are returned by proxy. This can be
|
||||
// disabled by supplying *true* in the NoProxy template parameter.
|
||||
//
|
||||
template <
|
||||
class Container,
|
||||
bool NoProxy = false,
|
||||
class DerivedPolicies
|
||||
= detail::final_map_derived_policies<Container, NoProxy> >
|
||||
class map_indexing_suite
|
||||
: public indexing_suite<
|
||||
Container
|
||||
, DerivedPolicies
|
||||
, NoProxy
|
||||
, true
|
||||
, typename Container::value_type::second_type
|
||||
, typename Container::key_type
|
||||
, typename Container::key_type
|
||||
>
|
||||
{
|
||||
public:
|
||||
|
||||
typedef typename Container::value_type value_type;
|
||||
typedef typename Container::value_type::second_type data_type;
|
||||
typedef typename Container::key_type key_type;
|
||||
typedef typename Container::key_type index_type;
|
||||
typedef typename Container::size_type size_type;
|
||||
typedef typename Container::difference_type difference_type;
|
||||
|
||||
template <class Class>
|
||||
static void
|
||||
extension_def(Class& cl)
|
||||
{
|
||||
// Wrap the map's element (value_type)
|
||||
std::string elem_name = "map_indexing_suite_";
|
||||
object class_name(cl.attr("__name__"));
|
||||
extract<std::string> class_name_extractor(class_name);
|
||||
elem_name += class_name_extractor();
|
||||
elem_name += "_entry";
|
||||
|
||||
typedef typename mpl::if_<
|
||||
mpl::and_<is_class<data_type>, mpl::bool_<!NoProxy> >
|
||||
, return_internal_reference<>
|
||||
, default_call_policies
|
||||
>::type get_data_return_policy;
|
||||
|
||||
class_<value_type>(elem_name.c_str())
|
||||
.def("__repr__", &DerivedPolicies::print_elem)
|
||||
.def("data", &DerivedPolicies::get_data, get_data_return_policy())
|
||||
.def("key", &DerivedPolicies::get_key)
|
||||
;
|
||||
}
|
||||
|
||||
static object
|
||||
print_elem(typename Container::value_type const& e)
|
||||
{
|
||||
return "(%s, %s)" % python::make_tuple(e.first, e.second);
|
||||
}
|
||||
|
||||
static
|
||||
typename mpl::if_<
|
||||
mpl::and_<is_class<data_type>, mpl::bool_<!NoProxy> >
|
||||
, data_type&
|
||||
, data_type
|
||||
>::type
|
||||
get_data(typename Container::value_type& e)
|
||||
{
|
||||
return e.second;
|
||||
}
|
||||
|
||||
static typename Container::key_type
|
||||
get_key(typename Container::value_type& e)
|
||||
{
|
||||
return e.first;
|
||||
}
|
||||
|
||||
static data_type&
|
||||
get_item(Container& container, index_type i_)
|
||||
{
|
||||
typename Container::iterator i = container.find(i_);
|
||||
if (i == container.end())
|
||||
{
|
||||
PyErr_SetString(PyExc_KeyError, "Invalid key");
|
||||
throw_error_already_set();
|
||||
}
|
||||
return i->second;
|
||||
}
|
||||
|
||||
static void
|
||||
set_item(Container& container, index_type i, data_type const& v)
|
||||
{
|
||||
container[i] = v;
|
||||
}
|
||||
|
||||
static void
|
||||
delete_item(Container& container, index_type i)
|
||||
{
|
||||
container.erase(i);
|
||||
}
|
||||
|
||||
static size_t
|
||||
size(Container& container)
|
||||
{
|
||||
return container.size();
|
||||
}
|
||||
|
||||
static bool
|
||||
contains(Container& container, key_type const& key)
|
||||
{
|
||||
return container.find(key) != container.end();
|
||||
}
|
||||
|
||||
static bool
|
||||
compare_index(Container& container, index_type a, index_type b)
|
||||
{
|
||||
return container.key_comp()(a, b);
|
||||
}
|
||||
|
||||
static index_type
|
||||
convert_index(Container& /*container*/, PyObject* i_)
|
||||
{
|
||||
extract<key_type const&> i(i_);
|
||||
if (i.check())
|
||||
{
|
||||
return i();
|
||||
}
|
||||
else
|
||||
{
|
||||
extract<key_type> i(i_);
|
||||
if (i.check())
|
||||
return i();
|
||||
}
|
||||
|
||||
PyErr_SetString(PyExc_TypeError, "Invalid index type");
|
||||
throw_error_already_set();
|
||||
return index_type();
|
||||
}
|
||||
};
|
||||
|
||||
}} // namespace boost::python
|
||||
|
||||
#endif // MAP_INDEXING_SUITE_JDG20038_HPP
|
||||
// (C) Copyright Joel de Guzman 2003.
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef MAP_INDEXING_SUITE_JDG20038_HPP
|
||||
# define MAP_INDEXING_SUITE_JDG20038_HPP
|
||||
|
||||
# include <boost/python/suite/indexing/indexing_suite.hpp>
|
||||
# include <boost/python/iterator.hpp>
|
||||
# include <boost/python/call_method.hpp>
|
||||
# include <boost/python/tuple.hpp>
|
||||
|
||||
namespace boost { namespace python {
|
||||
|
||||
// Forward declaration
|
||||
template <class Container, bool NoProxy, class DerivedPolicies>
|
||||
class map_indexing_suite;
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template <class Container, bool NoProxy>
|
||||
class final_map_derived_policies
|
||||
: public map_indexing_suite<Container,
|
||||
NoProxy, final_map_derived_policies<Container, NoProxy> > {};
|
||||
}
|
||||
|
||||
// The map_indexing_suite class is a predefined indexing_suite derived
|
||||
// class for wrapping std::map (and std::map like) classes. It provides
|
||||
// all the policies required by the indexing_suite (see indexing_suite).
|
||||
// Example usage:
|
||||
//
|
||||
// class X {...};
|
||||
//
|
||||
// ...
|
||||
//
|
||||
// class_<std::map<std::string, X> >("XMap")
|
||||
// .def(map_indexing_suite<std::map<std::string, X> >())
|
||||
// ;
|
||||
//
|
||||
// By default indexed elements are returned by proxy. This can be
|
||||
// disabled by supplying *true* in the NoProxy template parameter.
|
||||
//
|
||||
template <
|
||||
class Container,
|
||||
bool NoProxy = false,
|
||||
class DerivedPolicies
|
||||
= detail::final_map_derived_policies<Container, NoProxy> >
|
||||
class map_indexing_suite
|
||||
: public indexing_suite<
|
||||
Container
|
||||
, DerivedPolicies
|
||||
, NoProxy
|
||||
, true
|
||||
, typename Container::value_type::second_type
|
||||
, typename Container::key_type
|
||||
, typename Container::key_type
|
||||
>
|
||||
{
|
||||
public:
|
||||
|
||||
typedef typename Container::value_type value_type;
|
||||
typedef typename Container::value_type::second_type data_type;
|
||||
typedef typename Container::key_type key_type;
|
||||
typedef typename Container::key_type index_type;
|
||||
typedef typename Container::size_type size_type;
|
||||
typedef typename Container::difference_type difference_type;
|
||||
|
||||
template <class Class>
|
||||
static void
|
||||
extension_def(Class& cl)
|
||||
{
|
||||
// Wrap the map's element (value_type)
|
||||
std::string elem_name = "map_indexing_suite_";
|
||||
object class_name(cl.attr("__name__"));
|
||||
extract<std::string> class_name_extractor(class_name);
|
||||
elem_name += class_name_extractor();
|
||||
elem_name += "_entry";
|
||||
|
||||
typedef typename mpl::if_<
|
||||
mpl::and_<is_class<data_type>, mpl::bool_<!NoProxy> >
|
||||
, return_internal_reference<>
|
||||
, default_call_policies
|
||||
>::type get_data_return_policy;
|
||||
|
||||
class_<value_type>(elem_name.c_str())
|
||||
.def("__repr__", &DerivedPolicies::print_elem)
|
||||
.def("data", &DerivedPolicies::get_data, get_data_return_policy())
|
||||
.def("key", &DerivedPolicies::get_key)
|
||||
;
|
||||
}
|
||||
|
||||
static object
|
||||
print_elem(typename Container::value_type const& e)
|
||||
{
|
||||
return "(%s, %s)" % python::make_tuple(e.first, e.second);
|
||||
}
|
||||
|
||||
static
|
||||
typename mpl::if_<
|
||||
mpl::and_<is_class<data_type>, mpl::bool_<!NoProxy> >
|
||||
, data_type&
|
||||
, data_type
|
||||
>::type
|
||||
get_data(typename Container::value_type& e)
|
||||
{
|
||||
return e.second;
|
||||
}
|
||||
|
||||
static typename Container::key_type
|
||||
get_key(typename Container::value_type& e)
|
||||
{
|
||||
return e.first;
|
||||
}
|
||||
|
||||
static data_type&
|
||||
get_item(Container& container, index_type i_)
|
||||
{
|
||||
typename Container::iterator i = container.find(i_);
|
||||
if (i == container.end())
|
||||
{
|
||||
PyErr_SetString(PyExc_KeyError, "Invalid key");
|
||||
throw_error_already_set();
|
||||
}
|
||||
return i->second;
|
||||
}
|
||||
|
||||
static void
|
||||
set_item(Container& container, index_type i, data_type const& v)
|
||||
{
|
||||
container[i] = v;
|
||||
}
|
||||
|
||||
static void
|
||||
delete_item(Container& container, index_type i)
|
||||
{
|
||||
container.erase(i);
|
||||
}
|
||||
|
||||
static size_t
|
||||
size(Container& container)
|
||||
{
|
||||
return container.size();
|
||||
}
|
||||
|
||||
static bool
|
||||
contains(Container& container, key_type const& key)
|
||||
{
|
||||
return container.find(key) != container.end();
|
||||
}
|
||||
|
||||
static bool
|
||||
compare_index(Container& container, index_type a, index_type b)
|
||||
{
|
||||
return container.key_comp()(a, b);
|
||||
}
|
||||
|
||||
static index_type
|
||||
convert_index(Container& /*container*/, PyObject* i_)
|
||||
{
|
||||
extract<key_type const&> i(i_);
|
||||
if (i.check())
|
||||
{
|
||||
return i();
|
||||
}
|
||||
else
|
||||
{
|
||||
extract<key_type> i(i_);
|
||||
if (i.check())
|
||||
return i();
|
||||
}
|
||||
|
||||
PyErr_SetString(PyExc_TypeError, "Invalid index type");
|
||||
throw_error_already_set();
|
||||
return index_type();
|
||||
}
|
||||
};
|
||||
|
||||
}} // namespace boost::python
|
||||
|
||||
#endif // MAP_INDEXING_SUITE_JDG20038_HPP
|
||||
@@ -1,242 +1,242 @@
|
||||
// (C) Copyright Joel de Guzman 2003.
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef VECTOR_INDEXING_SUITE_JDG20036_HPP
|
||||
# define VECTOR_INDEXING_SUITE_JDG20036_HPP
|
||||
|
||||
# include <boost/python/suite/indexing/indexing_suite.hpp>
|
||||
# include <boost/python/suite/indexing/container_utils.hpp>
|
||||
# include <boost/python/iterator.hpp>
|
||||
|
||||
namespace boost { namespace python {
|
||||
|
||||
// Forward declaration
|
||||
template <class Container, bool NoProxy, class DerivedPolicies>
|
||||
class vector_indexing_suite;
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template <class Container, bool NoProxy>
|
||||
class final_vector_derived_policies
|
||||
: public vector_indexing_suite<Container,
|
||||
NoProxy, final_vector_derived_policies<Container, NoProxy> > {};
|
||||
}
|
||||
|
||||
// The vector_indexing_suite class is a predefined indexing_suite derived
|
||||
// class for wrapping std::vector (and std::vector like) classes. It provides
|
||||
// all the policies required by the indexing_suite (see indexing_suite).
|
||||
// Example usage:
|
||||
//
|
||||
// class X {...};
|
||||
//
|
||||
// ...
|
||||
//
|
||||
// class_<std::vector<X> >("XVec")
|
||||
// .def(vector_indexing_suite<std::vector<X> >())
|
||||
// ;
|
||||
//
|
||||
// By default indexed elements are returned by proxy. This can be
|
||||
// disabled by supplying *true* in the NoProxy template parameter.
|
||||
//
|
||||
template <
|
||||
class Container,
|
||||
bool NoProxy = false,
|
||||
class DerivedPolicies
|
||||
= detail::final_vector_derived_policies<Container, NoProxy> >
|
||||
class vector_indexing_suite
|
||||
: public indexing_suite<Container, DerivedPolicies, NoProxy>
|
||||
{
|
||||
public:
|
||||
|
||||
typedef typename Container::value_type data_type;
|
||||
typedef typename Container::value_type key_type;
|
||||
typedef typename Container::size_type index_type;
|
||||
typedef typename Container::size_type size_type;
|
||||
typedef typename Container::difference_type difference_type;
|
||||
|
||||
template <class Class>
|
||||
static void
|
||||
extension_def(Class& cl)
|
||||
{
|
||||
cl
|
||||
.def("append", &base_append)
|
||||
.def("extend", &base_extend)
|
||||
;
|
||||
}
|
||||
|
||||
static
|
||||
typename mpl::if_<
|
||||
is_class<data_type>
|
||||
, data_type&
|
||||
, data_type
|
||||
>::type
|
||||
get_item(Container& container, index_type i)
|
||||
{
|
||||
return container[i];
|
||||
}
|
||||
|
||||
static object
|
||||
get_slice(Container& container, index_type from, index_type to)
|
||||
{
|
||||
if (from > to)
|
||||
return object(Container());
|
||||
return object(Container(container.begin()+from, container.begin()+to));
|
||||
}
|
||||
|
||||
static void
|
||||
set_item(Container& container, index_type i, data_type const& v)
|
||||
{
|
||||
container[i] = v;
|
||||
}
|
||||
|
||||
static void
|
||||
set_slice(Container& container, index_type from,
|
||||
index_type to, data_type const& v)
|
||||
{
|
||||
if (from > to) {
|
||||
return;
|
||||
}
|
||||
else {
|
||||
container.erase(container.begin()+from, container.begin()+to);
|
||||
container.insert(container.begin()+from, v);
|
||||
}
|
||||
}
|
||||
|
||||
template <class Iter>
|
||||
static void
|
||||
set_slice(Container& container, index_type from,
|
||||
index_type to, Iter first, Iter last)
|
||||
{
|
||||
if (from > to) {
|
||||
container.insert(container.begin()+from, first, last);
|
||||
}
|
||||
else {
|
||||
container.erase(container.begin()+from, container.begin()+to);
|
||||
container.insert(container.begin()+from, first, last);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
delete_item(Container& container, index_type i)
|
||||
{
|
||||
container.erase(container.begin()+i);
|
||||
}
|
||||
|
||||
static void
|
||||
delete_slice(Container& container, index_type from, index_type to)
|
||||
{
|
||||
if (from > to) {
|
||||
// A null-op.
|
||||
return;
|
||||
}
|
||||
container.erase(container.begin()+from, container.begin()+to);
|
||||
}
|
||||
|
||||
static size_t
|
||||
size(Container& container)
|
||||
{
|
||||
return container.size();
|
||||
}
|
||||
|
||||
static bool
|
||||
contains(Container& container, key_type const& key)
|
||||
{
|
||||
return std::find(container.begin(), container.end(), key)
|
||||
!= container.end();
|
||||
}
|
||||
|
||||
static index_type
|
||||
get_min_index(Container& /*container*/)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static index_type
|
||||
get_max_index(Container& container)
|
||||
{
|
||||
return container.size();
|
||||
}
|
||||
|
||||
static bool
|
||||
compare_index(Container& /*container*/, index_type a, index_type b)
|
||||
{
|
||||
return a < b;
|
||||
}
|
||||
|
||||
static index_type
|
||||
convert_index(Container& container, PyObject* i_)
|
||||
{
|
||||
extract<long> i(i_);
|
||||
if (i.check())
|
||||
{
|
||||
long index = i();
|
||||
if (index < 0)
|
||||
index += DerivedPolicies::size(container);
|
||||
if (index >= long(container.size()) || index < 0)
|
||||
{
|
||||
PyErr_SetString(PyExc_IndexError, "Index out of range");
|
||||
throw_error_already_set();
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
PyErr_SetString(PyExc_TypeError, "Invalid index type");
|
||||
throw_error_already_set();
|
||||
return index_type();
|
||||
}
|
||||
|
||||
static void
|
||||
append(Container& container, data_type const& v)
|
||||
{
|
||||
container.push_back(v);
|
||||
}
|
||||
|
||||
template <class Iter>
|
||||
static void
|
||||
extend(Container& container, Iter first, Iter last)
|
||||
{
|
||||
container.insert(container.end(), first, last);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
static void
|
||||
base_append(Container& container, object v)
|
||||
{
|
||||
extract<data_type&> elem(v);
|
||||
// try if elem is an exact Data
|
||||
if (elem.check())
|
||||
{
|
||||
DerivedPolicies::append(container, elem());
|
||||
}
|
||||
else
|
||||
{
|
||||
// try to convert elem to data_type
|
||||
extract<data_type> elem(v);
|
||||
if (elem.check())
|
||||
{
|
||||
DerivedPolicies::append(container, elem());
|
||||
}
|
||||
else
|
||||
{
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
"Attempting to append an invalid type");
|
||||
throw_error_already_set();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
base_extend(Container& container, object v)
|
||||
{
|
||||
std::vector<data_type> temp;
|
||||
container_utils::extend_container(temp, v);
|
||||
DerivedPolicies::extend(container, temp.begin(), temp.end());
|
||||
}
|
||||
};
|
||||
|
||||
}} // namespace boost::python
|
||||
|
||||
#endif // VECTOR_INDEXING_SUITE_JDG20036_HPP
|
||||
// (C) Copyright Joel de Guzman 2003.
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#ifndef VECTOR_INDEXING_SUITE_JDG20036_HPP
|
||||
# define VECTOR_INDEXING_SUITE_JDG20036_HPP
|
||||
|
||||
# include <boost/python/suite/indexing/indexing_suite.hpp>
|
||||
# include <boost/python/suite/indexing/container_utils.hpp>
|
||||
# include <boost/python/iterator.hpp>
|
||||
|
||||
namespace boost { namespace python {
|
||||
|
||||
// Forward declaration
|
||||
template <class Container, bool NoProxy, class DerivedPolicies>
|
||||
class vector_indexing_suite;
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template <class Container, bool NoProxy>
|
||||
class final_vector_derived_policies
|
||||
: public vector_indexing_suite<Container,
|
||||
NoProxy, final_vector_derived_policies<Container, NoProxy> > {};
|
||||
}
|
||||
|
||||
// The vector_indexing_suite class is a predefined indexing_suite derived
|
||||
// class for wrapping std::vector (and std::vector like) classes. It provides
|
||||
// all the policies required by the indexing_suite (see indexing_suite).
|
||||
// Example usage:
|
||||
//
|
||||
// class X {...};
|
||||
//
|
||||
// ...
|
||||
//
|
||||
// class_<std::vector<X> >("XVec")
|
||||
// .def(vector_indexing_suite<std::vector<X> >())
|
||||
// ;
|
||||
//
|
||||
// By default indexed elements are returned by proxy. This can be
|
||||
// disabled by supplying *true* in the NoProxy template parameter.
|
||||
//
|
||||
template <
|
||||
class Container,
|
||||
bool NoProxy = false,
|
||||
class DerivedPolicies
|
||||
= detail::final_vector_derived_policies<Container, NoProxy> >
|
||||
class vector_indexing_suite
|
||||
: public indexing_suite<Container, DerivedPolicies, NoProxy>
|
||||
{
|
||||
public:
|
||||
|
||||
typedef typename Container::value_type data_type;
|
||||
typedef typename Container::value_type key_type;
|
||||
typedef typename Container::size_type index_type;
|
||||
typedef typename Container::size_type size_type;
|
||||
typedef typename Container::difference_type difference_type;
|
||||
|
||||
template <class Class>
|
||||
static void
|
||||
extension_def(Class& cl)
|
||||
{
|
||||
cl
|
||||
.def("append", &base_append)
|
||||
.def("extend", &base_extend)
|
||||
;
|
||||
}
|
||||
|
||||
static
|
||||
typename mpl::if_<
|
||||
is_class<data_type>
|
||||
, data_type&
|
||||
, data_type
|
||||
>::type
|
||||
get_item(Container& container, index_type i)
|
||||
{
|
||||
return container[i];
|
||||
}
|
||||
|
||||
static object
|
||||
get_slice(Container& container, index_type from, index_type to)
|
||||
{
|
||||
if (from > to)
|
||||
return object(Container());
|
||||
return object(Container(container.begin()+from, container.begin()+to));
|
||||
}
|
||||
|
||||
static void
|
||||
set_item(Container& container, index_type i, data_type const& v)
|
||||
{
|
||||
container[i] = v;
|
||||
}
|
||||
|
||||
static void
|
||||
set_slice(Container& container, index_type from,
|
||||
index_type to, data_type const& v)
|
||||
{
|
||||
if (from > to) {
|
||||
return;
|
||||
}
|
||||
else {
|
||||
container.erase(container.begin()+from, container.begin()+to);
|
||||
container.insert(container.begin()+from, v);
|
||||
}
|
||||
}
|
||||
|
||||
template <class Iter>
|
||||
static void
|
||||
set_slice(Container& container, index_type from,
|
||||
index_type to, Iter first, Iter last)
|
||||
{
|
||||
if (from > to) {
|
||||
container.insert(container.begin()+from, first, last);
|
||||
}
|
||||
else {
|
||||
container.erase(container.begin()+from, container.begin()+to);
|
||||
container.insert(container.begin()+from, first, last);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
delete_item(Container& container, index_type i)
|
||||
{
|
||||
container.erase(container.begin()+i);
|
||||
}
|
||||
|
||||
static void
|
||||
delete_slice(Container& container, index_type from, index_type to)
|
||||
{
|
||||
if (from > to) {
|
||||
// A null-op.
|
||||
return;
|
||||
}
|
||||
container.erase(container.begin()+from, container.begin()+to);
|
||||
}
|
||||
|
||||
static size_t
|
||||
size(Container& container)
|
||||
{
|
||||
return container.size();
|
||||
}
|
||||
|
||||
static bool
|
||||
contains(Container& container, key_type const& key)
|
||||
{
|
||||
return std::find(container.begin(), container.end(), key)
|
||||
!= container.end();
|
||||
}
|
||||
|
||||
static index_type
|
||||
get_min_index(Container& /*container*/)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static index_type
|
||||
get_max_index(Container& container)
|
||||
{
|
||||
return container.size();
|
||||
}
|
||||
|
||||
static bool
|
||||
compare_index(Container& /*container*/, index_type a, index_type b)
|
||||
{
|
||||
return a < b;
|
||||
}
|
||||
|
||||
static index_type
|
||||
convert_index(Container& container, PyObject* i_)
|
||||
{
|
||||
extract<long> i(i_);
|
||||
if (i.check())
|
||||
{
|
||||
long index = i();
|
||||
if (index < 0)
|
||||
index += DerivedPolicies::size(container);
|
||||
if (index >= long(container.size()) || index < 0)
|
||||
{
|
||||
PyErr_SetString(PyExc_IndexError, "Index out of range");
|
||||
throw_error_already_set();
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
PyErr_SetString(PyExc_TypeError, "Invalid index type");
|
||||
throw_error_already_set();
|
||||
return index_type();
|
||||
}
|
||||
|
||||
static void
|
||||
append(Container& container, data_type const& v)
|
||||
{
|
||||
container.push_back(v);
|
||||
}
|
||||
|
||||
template <class Iter>
|
||||
static void
|
||||
extend(Container& container, Iter first, Iter last)
|
||||
{
|
||||
container.insert(container.end(), first, last);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
static void
|
||||
base_append(Container& container, object v)
|
||||
{
|
||||
extract<data_type&> elem(v);
|
||||
// try if elem is an exact Data
|
||||
if (elem.check())
|
||||
{
|
||||
DerivedPolicies::append(container, elem());
|
||||
}
|
||||
else
|
||||
{
|
||||
// try to convert elem to data_type
|
||||
extract<data_type> elem(v);
|
||||
if (elem.check())
|
||||
{
|
||||
DerivedPolicies::append(container, elem());
|
||||
}
|
||||
else
|
||||
{
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
"Attempting to append an invalid type");
|
||||
throw_error_already_set();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
base_extend(Container& container, object v)
|
||||
{
|
||||
std::vector<data_type> temp;
|
||||
container_utils::extend_container(temp, v);
|
||||
DerivedPolicies::extend(container, temp.begin(), temp.end());
|
||||
}
|
||||
};
|
||||
|
||||
}} // namespace boost::python
|
||||
|
||||
#endif // VECTOR_INDEXING_SUITE_JDG20036_HPP
|
||||
Reference in new issue
Block a user