Skip to content

Commit fb54967

Browse files
committed
feat(vector_indexing_suite): implement remove
1 parent 437fe66 commit fb54967

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

include/boost/python/suite/indexing/vector_indexing_suite.hpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ namespace boost { namespace python {
6565
.def("append", &base_append)
6666
.def("count", &base_count)
6767
.def("extend", &base_extend)
68+
.def("remove", &base_remove)
6869
;
6970
}
7071

@@ -265,6 +266,42 @@ namespace boost { namespace python {
265266
base_extend(container, v);
266267
return object(container);
267268
}
269+
270+
static void
271+
base_remove(Container& container, object v)
272+
{
273+
extract<data_type&> key(v);
274+
if (key.check())
275+
{
276+
auto i = std::find(container.begin(), container.end(), key());
277+
if (i == container.end())
278+
{
279+
PyErr_SetString(PyExc_ValueError, "remove(x): x not in vector_indexing_suite");
280+
throw_error_already_set();
281+
}
282+
container.erase(i);
283+
}
284+
else
285+
{
286+
extract<data_type> key(v);
287+
if (key.check())
288+
{
289+
auto i = std::find(container.begin(), container.end(), key());
290+
if (i == container.end())
291+
{
292+
PyErr_SetString(PyExc_ValueError, "remove(x): x not in vector_indexing_suite");
293+
throw_error_already_set();
294+
}
295+
container.erase(i);
296+
}
297+
else
298+
{
299+
PyErr_SetString(PyExc_TypeError,
300+
"Attempting to remove an invalid type");
301+
throw_error_already_set();
302+
}
303+
}
304+
}
268305
};
269306

270307
}} // namespace boost::python

test/vector_indexing_suite.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@
7878
>>> print_xvec(v)
7979
[ yaba c d e ]
8080
81+
>>> v2 = XVec()
82+
>>> v2[:] = [X('b'), X('a'), X('c'), X('b'), X('a')]
83+
>>> try: v2.remove("z")
84+
... except ValueError: pass
85+
>>> v2.remove("a")
86+
>>> print_xvec(v2)
87+
[ b c b a ]
88+
8189
#####################################################################
8290
# Calling a mutating function of a container element
8391
#####################################################################

0 commit comments

Comments
 (0)