diff --git a/include/boost/python/suite/indexing/indexing_suite.hpp b/include/boost/python/suite/indexing/indexing_suite.hpp index 3469a2a40..140e3a665 100644 --- a/include/boost/python/suite/indexing/indexing_suite.hpp +++ b/include/boost/python/suite/indexing/indexing_suite.hpp @@ -184,6 +184,7 @@ namespace boost { namespace python { .def("__getitem__", &base_get_item) .def("__contains__", &base_contains) .def("__iter__", def_iterator()) + .def("clear", &base_clear) ; DerivedPolicies::extension_def(cl); @@ -288,6 +289,12 @@ namespace boost { namespace python { return false; } } + + static void + base_clear(Container& container) + { + return DerivedPolicies::clear(container); + } }; }} // namespace boost::python diff --git a/include/boost/python/suite/indexing/map_indexing_suite.hpp b/include/boost/python/suite/indexing/map_indexing_suite.hpp index 7fbad4cac..567abe4e7 100644 --- a/include/boost/python/suite/indexing/map_indexing_suite.hpp +++ b/include/boost/python/suite/indexing/map_indexing_suite.hpp @@ -137,6 +137,12 @@ namespace boost { namespace python { container.erase(i); } + static void + clear(Container& container) + { + container.clear(); + } + static size_t size(Container& container) { diff --git a/include/boost/python/suite/indexing/vector_indexing_suite.hpp b/include/boost/python/suite/indexing/vector_indexing_suite.hpp index 34c29ecc6..f1b27c738 100644 --- a/include/boost/python/suite/indexing/vector_indexing_suite.hpp +++ b/include/boost/python/suite/indexing/vector_indexing_suite.hpp @@ -61,8 +61,12 @@ namespace boost { namespace python { extension_def(Class& cl) { cl + .def("__iadd__", &base_iadd) .def("append", &base_append) + .def("count", &base_count) .def("extend", &base_extend) + .def("remove", &base_remove) + .def("reverse", &base_reverse) ; } @@ -134,6 +138,12 @@ namespace boost { namespace python { container.erase(container.begin()+from, container.begin()+to); } + static void + clear(Container& container) + { + container.clear(); + } + static size_t size(Container& container) { @@ -201,6 +211,21 @@ namespace boost { namespace python { } private: + + static size_t + base_count(Container& container, object v) + { + extract elem(v); + if (elem.check()) { + return std::count(container.begin(), container.end(), elem()); + } else { + extract elem(v); + if (!elem.check()) { + return 0; + } + return std::count(container.begin(), container.end(), elem()); + } + } static void base_append(Container& container, object v) @@ -235,6 +260,58 @@ namespace boost { namespace python { container_utils::extend_container(temp, v); DerivedPolicies::extend(container, temp.begin(), temp.end()); } + + static object + base_iadd(Container& container, object v) + { + base_extend(container, v); + return object(container); + } + + static void + base_remove(Container& container, object v) + { + extract key(v); + if (key.check()) + { + auto i = std::find(container.begin(), container.end(), key()); + if (i == container.end()) + { + PyErr_SetString(PyExc_ValueError, "remove(x): x not in vector_indexing_suite"); + throw_error_already_set(); + } + container.erase(i); + } + else + { + extract key(v); + if (key.check()) + { + auto i = std::find(container.begin(), container.end(), key()); + if (i == container.end()) + { + PyErr_SetString(PyExc_ValueError, "remove(x): x not in vector_indexing_suite"); + throw_error_already_set(); + } + container.erase(i); + } + else + { + PyErr_SetString(PyExc_TypeError, + "Attempting to remove an invalid type"); + throw_error_already_set(); + } + } + } + + static void + base_reverse(Container& container) + { + using std::swap; + const unsigned n = size(container); + for (unsigned i = 0; i < n / 2; i++) + swap(container[i], container[n - i - 1]); + } }; }} // namespace boost::python diff --git a/test/map_indexing_suite.py b/test/map_indexing_suite.py index 6d3e57a10..939d984c7 100644 --- a/test/map_indexing_suite.py +++ b/test/map_indexing_suite.py @@ -202,6 +202,10 @@ ... dom = el.data() joel kimpo +>>> tm.clear() +>>> print_xmap(tm) +[ ] + ##################################################################### # Test custom converter... ##################################################################### diff --git a/test/vector_indexing_suite.py b/test/vector_indexing_suite.py index 478cd0151..fb2b92a1e 100644 --- a/test/vector_indexing_suite.py +++ b/test/vector_indexing_suite.py @@ -78,6 +78,14 @@ >>> print_xvec(v) [ yaba c d e ] +>>> v2 = XVec() +>>> v2[:] = [X('b'), X('a'), X('c'), X('b'), X('a')] +>>> try: v2.remove("z") +... except ValueError: pass +>>> v2.remove("a") +>>> print_xvec(v2) +[ b c b a ] + ##################################################################### # Calling a mutating function of a container element ##################################################################### @@ -296,6 +304,21 @@ >>> assert not 'X' in v >>> assert not 12345 in v +##################################################################### +# Count +##################################################################### +>>> v.count('a') +1 +>>> v.count(12345) +0 + +##################################################################### +# Reverse +##################################################################### +>>> v.reverse() +>>> print_xvec(v) +[ e d c b a ] + ##################################################################### # Show that iteration allows mutable access to the elements ##################################################################### @@ -320,6 +343,9 @@ >>> v.extend(['f','g','h','i','j']) >>> print_xvec(v) [ a b c d e f g h i j ] +>>> v += ['k','l','m'] +>>> print_xvec(v) +[ a b c d e f g h i j k l m ] ##################################################################### # extend using a generator expression @@ -334,6 +360,10 @@ >>> print_xvec(v) [ a b c d e f h i j ] +>>> v.clear() +>>> print_xvec(v) +[ ] + ##################################################################### # vector of strings #####################################################################