From a10ea0c8ebf9270438013acd7595eef15ec41042 Mon Sep 17 00:00:00 2001 From: Eisuke Kawashima Date: Sun, 5 Apr 2026 17:36:56 +0900 Subject: [PATCH 1/5] feat(vector_indexing_suite): implement __iadd__ --- .../boost/python/suite/indexing/vector_indexing_suite.hpp | 8 ++++++++ test/vector_indexing_suite.py | 3 +++ 2 files changed, 11 insertions(+) diff --git a/include/boost/python/suite/indexing/vector_indexing_suite.hpp b/include/boost/python/suite/indexing/vector_indexing_suite.hpp index 34c29ecc6f..6ca31896ae 100644 --- a/include/boost/python/suite/indexing/vector_indexing_suite.hpp +++ b/include/boost/python/suite/indexing/vector_indexing_suite.hpp @@ -61,6 +61,7 @@ namespace boost { namespace python { extension_def(Class& cl) { cl + .def("__iadd__", &base_iadd) .def("append", &base_append) .def("extend", &base_extend) ; @@ -235,6 +236,13 @@ 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); + } }; }} // namespace boost::python diff --git a/test/vector_indexing_suite.py b/test/vector_indexing_suite.py index 478cd01516..5e5e8f7a3d 100644 --- a/test/vector_indexing_suite.py +++ b/test/vector_indexing_suite.py @@ -320,6 +320,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 From 5d14c1d85671d379546beb948fafa2049026c586 Mon Sep 17 00:00:00 2001 From: Eisuke Kawashima Date: Sun, 5 Apr 2026 16:20:09 +0900 Subject: [PATCH 2/5] feat(indexing_suite): implement clear --- include/boost/python/suite/indexing/indexing_suite.hpp | 7 +++++++ include/boost/python/suite/indexing/map_indexing_suite.hpp | 6 ++++++ .../boost/python/suite/indexing/vector_indexing_suite.hpp | 6 ++++++ test/map_indexing_suite.py | 4 ++++ test/vector_indexing_suite.py | 4 ++++ 5 files changed, 27 insertions(+) diff --git a/include/boost/python/suite/indexing/indexing_suite.hpp b/include/boost/python/suite/indexing/indexing_suite.hpp index 3469a2a40f..140e3a665c 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 7fbad4cace..567abe4e74 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 6ca31896ae..264eac0133 100644 --- a/include/boost/python/suite/indexing/vector_indexing_suite.hpp +++ b/include/boost/python/suite/indexing/vector_indexing_suite.hpp @@ -135,6 +135,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) { diff --git a/test/map_indexing_suite.py b/test/map_indexing_suite.py index 6d3e57a102..939d984c7b 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 5e5e8f7a3d..24d1caba42 100644 --- a/test/vector_indexing_suite.py +++ b/test/vector_indexing_suite.py @@ -337,6 +337,10 @@ >>> print_xvec(v) [ a b c d e f h i j ] +>>> v.clear() +>>> print_xvec(v) +[ ] + ##################################################################### # vector of strings ##################################################################### From 437fe66d8600ad607fc8c4b0f3d85e495f39f9c6 Mon Sep 17 00:00:00 2001 From: Eisuke Kawashima Date: Sun, 5 Apr 2026 16:13:45 +0900 Subject: [PATCH 3/5] feat(vector_indexing_suite): implement count --- .../suite/indexing/vector_indexing_suite.hpp | 16 ++++++++++++++++ test/vector_indexing_suite.py | 7 +++++++ 2 files changed, 23 insertions(+) diff --git a/include/boost/python/suite/indexing/vector_indexing_suite.hpp b/include/boost/python/suite/indexing/vector_indexing_suite.hpp index 264eac0133..7cbdd2c6e2 100644 --- a/include/boost/python/suite/indexing/vector_indexing_suite.hpp +++ b/include/boost/python/suite/indexing/vector_indexing_suite.hpp @@ -63,6 +63,7 @@ namespace boost { namespace python { cl .def("__iadd__", &base_iadd) .def("append", &base_append) + .def("count", &base_count) .def("extend", &base_extend) ; } @@ -208,6 +209,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) diff --git a/test/vector_indexing_suite.py b/test/vector_indexing_suite.py index 24d1caba42..6abca6140a 100644 --- a/test/vector_indexing_suite.py +++ b/test/vector_indexing_suite.py @@ -297,6 +297,13 @@ >>> assert not 12345 in v ##################################################################### +# Count +##################################################################### +>>> v.count('a') +1 +>>> v.count(12345) +0 + # Show that iteration allows mutable access to the elements ##################################################################### >>> v[:] = ['a','b','c','d','e'] # reset again From fb54967b160bfcc369157d651be48a0a35821bc4 Mon Sep 17 00:00:00 2001 From: Eisuke Kawashima Date: Sun, 5 Apr 2026 17:50:35 +0900 Subject: [PATCH 4/5] feat(vector_indexing_suite): implement remove --- .../suite/indexing/vector_indexing_suite.hpp | 37 +++++++++++++++++++ test/vector_indexing_suite.py | 8 ++++ 2 files changed, 45 insertions(+) diff --git a/include/boost/python/suite/indexing/vector_indexing_suite.hpp b/include/boost/python/suite/indexing/vector_indexing_suite.hpp index 7cbdd2c6e2..fd5d86c406 100644 --- a/include/boost/python/suite/indexing/vector_indexing_suite.hpp +++ b/include/boost/python/suite/indexing/vector_indexing_suite.hpp @@ -65,6 +65,7 @@ namespace boost { namespace python { .def("append", &base_append) .def("count", &base_count) .def("extend", &base_extend) + .def("remove", &base_remove) ; } @@ -265,6 +266,42 @@ namespace boost { namespace python { 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(); + } + } + } }; }} // namespace boost::python diff --git a/test/vector_indexing_suite.py b/test/vector_indexing_suite.py index 6abca6140a..c0b72c6ab9 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 ##################################################################### From 2fc1cb731db89361780241c3d1936602c4a962ef Mon Sep 17 00:00:00 2001 From: Eisuke Kawashima Date: Sun, 5 Apr 2026 16:29:32 +0900 Subject: [PATCH 5/5] feat(vector_indexing_suite): implement reverse --- .../python/suite/indexing/vector_indexing_suite.hpp | 10 ++++++++++ test/vector_indexing_suite.py | 8 ++++++++ 2 files changed, 18 insertions(+) diff --git a/include/boost/python/suite/indexing/vector_indexing_suite.hpp b/include/boost/python/suite/indexing/vector_indexing_suite.hpp index fd5d86c406..f1b27c7381 100644 --- a/include/boost/python/suite/indexing/vector_indexing_suite.hpp +++ b/include/boost/python/suite/indexing/vector_indexing_suite.hpp @@ -66,6 +66,7 @@ namespace boost { namespace python { .def("count", &base_count) .def("extend", &base_extend) .def("remove", &base_remove) + .def("reverse", &base_reverse) ; } @@ -302,6 +303,15 @@ namespace boost { namespace python { } } } + + 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/vector_indexing_suite.py b/test/vector_indexing_suite.py index c0b72c6ab9..fb2b92a1ee 100644 --- a/test/vector_indexing_suite.py +++ b/test/vector_indexing_suite.py @@ -312,6 +312,14 @@ >>> v.count(12345) 0 +##################################################################### +# Reverse +##################################################################### +>>> v.reverse() +>>> print_xvec(v) +[ e d c b a ] + +##################################################################### # Show that iteration allows mutable access to the elements ##################################################################### >>> v[:] = ['a','b','c','d','e'] # reset again