Commit dd2ff59b authored by Xavier Thompson's avatar Xavier Thompson

Add builtin cypclass list and associated unit tests

parent 578c76d8
This diff is collapsed.
cdef extern from * nogil:
"""
template<typename urng_t, typename base_iterator_t, typename reference_t, reference_t (*getter_t)(base_iterator_t)>
struct cy_iterator_t : base_iterator_t
{
using base = base_iterator_t;
using reference = reference_t;
urng_t urange = NULL;
friend void swap(cy_iterator_t & first, cy_iterator_t & second)
{
using std::swap;
swap(first.urange, second.urange);
swap(static_cast<base&>(first), static_cast<base&>(second));
}
cy_iterator_t() = default;
cy_iterator_t(cy_iterator_t const & rhs) : urange(rhs.urange)
{
if (urange != NULL)
{
urange->CyObject_INCREF();
urange->_active_iterators++;
}
}
cy_iterator_t(cy_iterator_t && rhs) : cy_iterator_t()
{
std::swap(*this, rhs);
}
cy_iterator_t & operator=(cy_iterator_t rhs)
{
swap(*this, rhs);
return *this;
}
cy_iterator_t & operator=(base_iterator_t rhs)
{
swap(static_cast<base&>(*this), rhs);
if (urange != NULL) {
urange->_active_iterators--;
urange->CyObject_DECREF();
urange = NULL;
}
return *this;
}
~cy_iterator_t()
{
if (urange != NULL) {
urange->_active_iterators--;
urange->CyObject_DECREF();
urange = NULL;
}
}
cy_iterator_t(base const & b) : base{b} {}
cy_iterator_t(base const & b, urng_t urange) : base{b}, urange{urange}
{
if (urange != NULL) {
urange->CyObject_INCREF();
urange->_active_iterators++;
}
}
cy_iterator_t operator++(int)
{
return static_cast<base&>(*this)++;
}
cy_iterator_t & operator++()
{
++static_cast<base&>(*this);
return (*this);
}
reference operator*() const
{
return getter_t(static_cast<base>(*this));
}
};
"""
from libcpp.vector cimport vector
from libcpp.atomic cimport atomic
from cython.operator cimport dereference
from libcythonplus.iterator cimport *
cdef extern from * nogil:
"""
template<typename base_iterator_t, typename reference_t>
constexpr reference_t list_value_getter_t(base_iterator_t iter)
{
return *iter;
}
template<typename list_t, typename base_iterator_t, typename reference_t>
using list_iterator_t = cy_iterator_t<list_t, base_iterator_t, reference_t, list_value_getter_t<base_iterator_t, reference_t>>;
"""
cdef cppclass list_iterator_t[list_t, base_iterator_t, reference_t]:
list_iterator_t()
list_iterator_t(base_iterator_t)
list_iterator_t(base_iterator_t, const list_t)
reference_t operator*()
list_iterator_t operator++()
bint operator!=(base_iterator_t)
cdef cypclass cyplist[V]:
ctypedef V value_type
ctypedef vector[value_type].size_type size_type
ctypedef list_iterator_t[cyplist[V], vector[value_type].iterator, value_type] iterator
vector[value_type] _elements
atomic[int] _active_iterators
__init__(self):
self._active_iterators.store(0)
__dealloc__(self):
for value in self._elements:
Cy_DECREF(value)
V __getitem__(self, const size_type index) except ~ const:
if index < self._elements.size():
return self._elements[index]
else:
with gil:
raise IndexError("Getting list index out of range")
void __setitem__(self, size_type index, const value_type value) except ~:
if index < self._elements.size():
Cy_INCREF(value)
Cy_DECREF(self._elements[index])
self._elements[index] = value
else:
with gil:
raise IndexError("Setting list index out of range")
void __delitem__(self, size_type index) except ~:
if index < self._elements.size():
if self._active_iterators == 0:
it = self._elements.begin() + index
Cy_DECREF(dereference(it))
self._elements.erase(it)
else:
with gil:
raise RuntimeError("Modifying a list with active iterators")
else:
with gil:
raise IndexError("Deleting list index out of range")
void append(self, const value_type value) except ~:
if self._active_iterators == 0:
Cy_INCREF(value)
self._elements.push_back(value)
else:
with gil:
raise RuntimeError("Modifying a list with active iterators")
void insert(self, size_type index, const value_type value) except ~:
if self._active_iterators == 0:
if index <= self._elements.size():
it = self._elements.begin() + index
Cy_INCREF(value)
self._elements.insert(it, value)
else:
with gil:
raise IndexError("Inserting list index out of range")
else:
with gil:
raise RuntimeError("Modifying a list with active iterators")
void clear(self) except ~:
if self._active_iterators == 0:
for value in self._elements:
Cy_DECREF(value)
self._elements.clear()
else:
with gil:
raise RuntimeError("Modifying a list with active iterators")
list_iterator_t[cyplist[V], vector[value_type].iterator, value_type] begin(self) const:
return list_iterator_t[cyplist[V], vector[value_type].iterator, value_type](self._elements.begin(), self)
vector[value_type].iterator end(self) const:
return self._elements.end()
size_type __len__(self) const:
return self._elements.size()
bint __contains__(self, const value_type value):
for v in self._elements:
if value is v:
return 1
return 0
# mode: run
# tag: cpp, cpp11, pthread
# cython: experimental_cpp_class_def=True, language_level=2
from libcythonplus.list cimport cyplist
cdef cypclass Value:
int value
__init__(self, int i):
self.value = i
def test_append_and_comp_iteration():
"""
>>> test_append_and_comp_iteration()
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
"""
l = cyplist[Value]()
for i in range(10):
l.append(Value(i))
return [v.value for v in l]
def test_nogil_append_and_iteration():
"""
>>> test_nogil_append_and_iteration()
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
"""
indices = []
with nogil:
l = cyplist[Value]()
for i in range(10):
l.append(Value(i))
for v in l:
with gil:
indices.append(v.value)
return indices
def test_nogil_insert_and_iteration():
"""
>>> test_nogil_insert_and_iteration()
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
"""
indices = []
with nogil:
l = cyplist[Value]()
for i in range(10):
l.insert(0, Value(i))
for v in l:
with gil:
indices.append(v.value)
return indices
def test_len():
"""
>>> test_len()
1
"""
l = cyplist[Value]()
cdef long unsigned int nb_elements = 0
for i in range(10):
l.append(Value(i))
for v in l:
nb_elements += 1
if l.__len__() != nb_elements:
return 0
if nb_elements != 10:
return 0
return 1
def test_clear():
"""
>>> test_clear()
1
"""
l = cyplist[Value]()
for i in range(10):
l.append(Value(i))
if l.__len__() != 10:
return -1
l.clear()
if l.__len__() != 0:
return 0
return 1
def test_contains():
"""
>>> test_clear()
1
"""
l = cyplist[Value]()
for i in range(10):
value = Value(i)
if value in l:
return 0
l.append(value)
if value not in l:
return 0
return 1
def test_getitem_out_of_range():
"""
>>> test_getitem_out_of_range()
Getting list index out of range
1
"""
l = cyplist[Value]()
try:
with nogil:
v = l[0]
with gil:
return 0
except IndexError as e:
print(e)
return 1
def test_setitem_out_of_range():
"""
>>> test_setitem_out_of_range()
Setting list index out of range
1
"""
l = cyplist[Value]()
try:
with nogil:
l[0] = Value(0)
with gil:
return 0
except IndexError as e:
print(e)
return 1
def test_delitem_out_of_range():
"""
>>> test_delitem_out_of_range()
Deleting list index out of range
1
"""
l = cyplist[Value]()
try:
with nogil:
del l[0]
with gil:
return 0
except IndexError as e:
print(e)
return 1
def test_append_iterator_invalidation():
"""
>>> test_append_iterator_invalidation()
Modifying a list with active iterators
1
"""
l = cyplist[Value]()
iterator = l.begin()
try:
with nogil:
l.append(Value(1))
with gil:
return 0
except RuntimeError as e:
print(e)
return 1
def test_insert_iterator_invalidation():
"""
>>> test_insert_iterator_invalidation()
Modifying a list with active iterators
1
"""
l = cyplist[Value]()
iterator = l.begin()
try:
with nogil:
l.insert(0, Value(1))
with gil:
return 0
except RuntimeError as e:
print(e)
return 1
def test_del_iterator_invalidation():
"""
>>> test_del_iterator_invalidation()
Modifying a list with active iterators
1
"""
l = cyplist[Value]()
l.append(Value(0))
iterator = l.begin()
try:
with nogil:
del l[0]
with gil:
return 0
except RuntimeError as e:
print(e)
return 1
def test_clear_iterator_invalidation():
"""
>>> test_clear_iterator_invalidation()
Modifying a list with active iterators
1
"""
l = cyplist[Value]()
iterator = l.begin()
try:
with nogil:
l.clear()
with gil:
return 0
except RuntimeError as e:
print(e)
return 1
def test_modification_after_iteration():
"""
>>> test_modification_after_iteration()
1
"""
l = cyplist[Value]()
for value in l:
pass
try:
with nogil:
l.append(Value(1))
l.insert(0, Value(0))
del l[0]
l.clear()
with gil:
return 1
except RuntimeError as e:
print(e)
return 0
def test_scalar_types_list():
"""
>>> test_scalar_types_list()
[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
"""
l = cyplist[double]()
for i in range(10):
value = <double> i
l.append(value)
return [value for value in l]
cdef cypclass DestroyCheckValue(Value):
__dealloc__(self) with gil:
print("destroyed value", self.value)
def test_values_destroyed():
"""
>>> test_values_destroyed()
('destroyed value', 0)
('destroyed value', 1)
('destroyed value', 2)
('destroyed value', 3)
('destroyed value', 4)
('destroyed value', 5)
('destroyed value', 6)
('destroyed value', 7)
('destroyed value', 8)
('destroyed value', 9)
"""
l = cyplist[DestroyCheckValue]()
for i in range(10):
l.append(DestroyCheckValue(i))
def test_values_refcount():
"""
>>> test_values_refcount()
1
"""
l = cyplist[Value]()
value = Value()
if Cy_GETREF(value) != 2:
return 0
l.append(value)
if Cy_GETREF(value) != 3:
return 0
l.insert(0, value)
if Cy_GETREF(value) != 4:
return 0
del l[0]
if Cy_GETREF(value) != 3:
return 0
l.clear()
if Cy_GETREF(value) != 2:
return 0
l.append(value)
if Cy_GETREF(value) != 3:
return 0
del l
if Cy_GETREF(value) != 2:
return 0
return 1
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment