2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-22 17:32:55 +00:00

added __iter__ and __contains__

[SVN r19311]
This commit is contained in:
Joel de Guzman
2003-07-26 01:50:35 +00:00
parent 7aae525587
commit 2dece7ecaf
4 changed files with 65 additions and 4 deletions

View File

@@ -52,6 +52,10 @@ namespace boost { namespace python {
//
// 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);
@@ -90,6 +94,7 @@ namespace boost { namespace python {
, class DerivedPolicies
, bool NoProxy = false
, class Element = typename Container::value_type
, class Key = typename Container::value_type
, class Index = typename Container::size_type
>
class indexing_suite
@@ -99,6 +104,7 @@ namespace boost { namespace python {
, DerivedPolicies
, NoProxy
, Element
, Key
, Index
> >
{
@@ -111,13 +117,18 @@ namespace boost { namespace python {
void visit(Class& cl) const
{
// Hook into the class_ generic visitation .def function
register_ptr_to_python<container_element_t>();
register_ptr_to_python<container_element_t>();
Container::iterator(Container::*begin_)() = &Container::begin;
Container::iterator(Container::*end_)() = &Container::end;
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__", boost::python::range(begin_, end_))
;
}
@@ -306,14 +317,14 @@ namespace boost { namespace python {
{
object elem = l[i];
extract<Element const&> x(elem);
// try if elem is an exact element_type
// try if elem is an exact Element type
if (x.check())
{
temp.push_back(x());
}
else
{
// try to convert elem to element_type
// try to convert elem to Element type
extract<Element> x(elem);
if (x.check())
{
@@ -365,6 +376,26 @@ namespace boost { namespace python {
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;
}
}
static object
get_slice(Container& container, Index from, Index to)
{

View File

@@ -8,6 +8,7 @@
# define VECTOR_INDEXING_SUITE_JDG20036_HPP
# include <boost/python/indexing/indexing_suite.hpp>
# include <boost/python/iterator.hpp>
namespace boost { namespace python {
@@ -49,6 +50,7 @@ namespace boost { namespace python {
public:
typedef typename Container::value_type element_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;
@@ -106,6 +108,13 @@ namespace boost { namespace python {
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
convert_index(Container& container, PyObject* i_)
{

View File

@@ -13,6 +13,7 @@ struct X // a container element
std::string repr() const { return s; }
void reset() { s = "reset"; }
void foo() { s = "foo"; }
bool operator==(X const& x) const { return s == x.s; }
};
std::string x_value(X const& x)

View File

@@ -21,7 +21,7 @@ foo
'gotya bochi bochi'
#####################################################################
# Utility
# Iteration
#####################################################################
>>> def print_xvec(xvec):
... s = '[ '
@@ -280,6 +280,26 @@ d
>>> z4 # proxy index adjusted
e
#####################################################################
# Contains
#####################################################################
>>> v[:] = ['a','b','c','d','e'] # reset again
>>> 'a' in v
1
>>> 'b' in v
1
>>> 'c' in v
1
>>> 'd' in v
1
>>> 'e' in v
1
>>> 'X' in v
0
>>> 12345 in v
0
#####################################################################
# END....
#####################################################################