Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/boost/python/suite/indexing/indexing_suite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -288,6 +289,12 @@ namespace boost { namespace python {
return false;
}
}

static void
base_clear(Container& container)
{
return DerivedPolicies::clear(container);
}
};

}} // namespace boost::python
Expand Down
6 changes: 6 additions & 0 deletions include/boost/python/suite/indexing/map_indexing_suite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ namespace boost { namespace python {
container.erase(i);
}

static void
clear(Container& container)
{
container.clear();
}

static size_t
size(Container& container)
{
Expand Down
77 changes: 77 additions & 0 deletions include/boost/python/suite/indexing/vector_indexing_suite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
;
}

Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -201,6 +211,21 @@ namespace boost { namespace python {
}

private:

static size_t
base_count(Container& container, object v)
{
extract<data_type&> elem(v);
if (elem.check()) {
return std::count(container.begin(), container.end(), elem());
} else {
extract<data_type> elem(v);
if (!elem.check()) {
return 0;
}
return std::count(container.begin(), container.end(), elem());
}
}

static void
base_append(Container& container, object v)
Expand Down Expand Up @@ -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<data_type&> 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<data_type> 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
Expand Down
4 changes: 4 additions & 0 deletions test/map_indexing_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@
... dom = el.data()
joel kimpo

>>> tm.clear()
>>> print_xmap(tm)
[ ]

#####################################################################
# Test custom converter...
#####################################################################
Expand Down
30 changes: 30 additions & 0 deletions test/vector_indexing_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
#####################################################################
Expand Down Expand Up @@ -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
#####################################################################
Expand All @@ -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
Expand All @@ -334,6 +360,10 @@
>>> print_xvec(v)
[ a b c d e f h i j ]

>>> v.clear()
>>> print_xvec(v)
[ ]

#####################################################################
# vector of strings
#####################################################################
Expand Down
Loading