From 2dece7ecaf5111960b724801d492f41081b95a2e Mon Sep 17 00:00:00 2001 From: Joel de Guzman Date: Sat, 26 Jul 2003 01:50:35 +0000 Subject: [PATCH] added __iter__ and __contains__ [SVN r19311] --- .../boost/python/indexing/indexing_suite.hpp | 37 +++++++++++++++++-- .../python/indexing/vector_indexing_suite.hpp | 9 +++++ test/vector_indexing_suite.cpp | 1 + test/vector_indexing_suite.py | 22 ++++++++++- 4 files changed, 65 insertions(+), 4 deletions(-) diff --git a/include/boost/python/indexing/indexing_suite.hpp b/include/boost/python/indexing/indexing_suite.hpp index f2af6b1c..80da6a38 100644 --- a/include/boost/python/indexing/indexing_suite.hpp +++ b/include/boost/python/indexing/indexing_suite.hpp @@ -52,6 +52,10 @@ namespace boost { namespace python { // // static size_t // size(Container& container); + // + // template + // 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(); + register_ptr_to_python(); + + 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 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 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 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 x(key); + if (x.check()) + return DerivedPolicies::contains(container, x()); + else + return false; + } + } + static object get_slice(Container& container, Index from, Index to) { diff --git a/include/boost/python/indexing/vector_indexing_suite.hpp b/include/boost/python/indexing/vector_indexing_suite.hpp index fffa3da5..cd77e743 100644 --- a/include/boost/python/indexing/vector_indexing_suite.hpp +++ b/include/boost/python/indexing/vector_indexing_suite.hpp @@ -8,6 +8,7 @@ # define VECTOR_INDEXING_SUITE_JDG20036_HPP # include +# include 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_) { diff --git a/test/vector_indexing_suite.cpp b/test/vector_indexing_suite.cpp index 80c3f97d..3e78aeae 100644 --- a/test/vector_indexing_suite.cpp +++ b/test/vector_indexing_suite.cpp @@ -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) diff --git a/test/vector_indexing_suite.py b/test/vector_indexing_suite.py index ce233bc6..39d0dd06 100644 --- a/test/vector_indexing_suite.py +++ b/test/vector_indexing_suite.py @@ -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.... #####################################################################