Skip to content

Commit 437fe66

Browse files
committed
feat(vector_indexing_suite): implement count
1 parent 5d14c1d commit 437fe66

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ namespace boost { namespace python {
6363
cl
6464
.def("__iadd__", &base_iadd)
6565
.def("append", &base_append)
66+
.def("count", &base_count)
6667
.def("extend", &base_extend)
6768
;
6869
}
@@ -208,6 +209,21 @@ namespace boost { namespace python {
208209
}
209210

210211
private:
212+
213+
static size_t
214+
base_count(Container& container, object v)
215+
{
216+
extract<data_type&> elem(v);
217+
if (elem.check()) {
218+
return std::count(container.begin(), container.end(), elem());
219+
} else {
220+
extract<data_type> elem(v);
221+
if (!elem.check()) {
222+
return 0;
223+
}
224+
return std::count(container.begin(), container.end(), elem());
225+
}
226+
}
211227

212228
static void
213229
base_append(Container& container, object v)

test/vector_indexing_suite.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,13 @@
297297
>>> assert not 12345 in v
298298
299299
#####################################################################
300+
# Count
301+
#####################################################################
302+
>>> v.count('a')
303+
1
304+
>>> v.count(12345)
305+
0
306+
300307
# Show that iteration allows mutable access to the elements
301308
#####################################################################
302309
>>> v[:] = ['a','b','c','d','e'] # reset again

0 commit comments

Comments
 (0)