Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Xavier Thompson
cython
Commits
dd2ff59b
Commit
dd2ff59b
authored
Sep 01, 2020
by
Xavier Thompson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add builtin cypclass list and associated unit tests
parent
578c76d8
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
561 additions
and
139 deletions
+561
-139
Cython/Includes/libcythonplus/dict.pxd
Cython/Includes/libcythonplus/dict.pxd
+58
-139
Cython/Includes/libcythonplus/iterator.pxd
Cython/Includes/libcythonplus/iterator.pxd
+85
-0
Cython/Includes/libcythonplus/list.pxd
Cython/Includes/libcythonplus/list.pxd
+113
-0
tests/run/cypclass_builtin_list.pyx
tests/run/cypclass_builtin_list.pyx
+305
-0
No files found.
Cython/Includes/libcythonplus/dict.pxd
View file @
dd2ff59b
...
@@ -4,121 +4,40 @@ from libcpp.vector cimport vector
...
@@ -4,121 +4,40 @@ from libcpp.vector cimport vector
from
libcpp.atomic
cimport
atomic
from
libcpp.atomic
cimport
atomic
from
cython.operator
cimport
dereference
from
cython.operator
cimport
dereference
from
libcythonplus.iterator
cimport
*
cdef
extern
from
*
nogil
:
cdef
extern
from
*
nogil
:
"""
"""
template<typename dict_t, typename base_iterator_t, typename reference_t, reference_t (*getter_t)(base_iterator_t)>
struct dict_iterator_t : base_iterator_t
{
using base = base_iterator_t;
using reference = reference_t;
dict_t urange = NULL;
friend void swap(dict_iterator_t & first, dict_iterator_t & second)
{
using std::swap;
swap(first.urange, second.urange);
swap(static_cast<base&>(first), static_cast<base&>(second));
}
dict_iterator_t() = default;
dict_iterator_t(dict_iterator_t const & rhs) : urange(rhs.urange)
{
if (urange != NULL)
{
urange->CyObject_INCREF();
urange->_active_iterators++;
}
}
dict_iterator_t(dict_iterator_t && rhs) : dict_iterator_t()
{
std::swap(*this, rhs);
}
dict_iterator_t & operator=(dict_iterator_t rhs)
{
swap(*this, rhs);
return *this;
}
dict_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;
}
~dict_iterator_t()
{
if (urange != NULL) {
urange->_active_iterators--;
urange->CyObject_DECREF();
urange = NULL;
}
}
dict_iterator_t(base const & b) : base{b} {}
dict_iterator_t(base const & b, dict_t urange) : base{b}, urange{urange}
{
if (urange != NULL) {
urange->CyObject_INCREF();
urange->_active_iterators++;
}
}
dict_iterator_t operator++(int)
{
return static_cast<base&>(*this)++;
}
dict_iterator_t & operator++()
{
++static_cast<base&>(*this);
return (*this);
}
reference operator*() const
{
return getter_t(static_cast<base>(*this));
}
};
template<typename base_iterator_t, typename reference_t>
template<typename base_iterator_t, typename reference_t>
constexpr reference_t key_getter_t(base_iterator_t iter)
constexpr reference_t
dict_
key_getter_t(base_iterator_t iter)
{
{
return iter->first;
return iter->first;
}
}
template<typename base_iterator_t, typename reference_t>
template<typename base_iterator_t, typename reference_t>
constexpr reference_t value_getter_t(base_iterator_t iter)
constexpr reference_t
dict_
value_getter_t(base_iterator_t iter)
{
{
return iter->second;
return iter->second;
}
}
template<typename base_iterator_t, typename reference_t>
template<typename base_iterator_t, typename reference_t>
constexpr reference_t item_getter_t(base_iterator_t iter)
constexpr reference_t
dict_
item_getter_t(base_iterator_t iter)
{
{
return *iter;
return *iter;
}
}
template<typename dict_t, typename base_iterator_t, typename reference_t>
template<typename dict_t, typename base_iterator_t, typename reference_t>
using
key_iterator_t = dict_iterator_t<dict_t, base_iterator_t, reference_t,
key_getter_t<base_iterator_t, reference_t>>;
using
dict_key_iterator_t = cy_iterator_t<dict_t, base_iterator_t, reference_t, dict_
key_getter_t<base_iterator_t, reference_t>>;
template<typename dict_t, typename base_iterator_t, typename reference_t>
template<typename dict_t, typename base_iterator_t, typename reference_t>
using
value_iterator_t = dict_iterator_t<dict_t, base_iterator_t, reference_t,
value_getter_t<base_iterator_t, reference_t>>;
using
dict_value_iterator_t = cy_iterator_t<dict_t, base_iterator_t, reference_t, dict_
value_getter_t<base_iterator_t, reference_t>>;
template<typename dict_t, typename base_iterator_t, typename reference_t>
template<typename dict_t, typename base_iterator_t, typename reference_t>
using
item_iterator_t = dict_iterator_t<dict_t, base_iterator_t, reference_t,
item_getter_t<base_iterator_t, reference_t>>;
using
dict_item_iterator_t = cy_iterator_t<dict_t, base_iterator_t, reference_t, dict_
item_getter_t<base_iterator_t, reference_t>>;
template <typename dict_t, typename iterator_t>
template <typename dict_t, typename iterator_t>
class
view_dict
class
dict_view
{
{
private:
private:
dict_t urange = NULL;
dict_t urange = NULL;
...
@@ -126,14 +45,14 @@ cdef extern from * nogil:
...
@@ -126,14 +45,14 @@ cdef extern from * nogil:
public:
public:
using iterator = iterator_t;
using iterator = iterator_t;
friend void swap(
view_dict & first, view_dict
& second)
friend void swap(
dict_view & first, dict_view
& second)
{
{
using std::swap;
using std::swap;
swap(first.urange, second.urange);
swap(first.urange, second.urange);
}
}
view_dict
() = default;
dict_view
() = default;
view_dict(view_dict
const & rhs) : urange(rhs.urange)
dict_view(dict_view
const & rhs) : urange(rhs.urange)
{
{
if (urange != NULL)
if (urange != NULL)
{
{
...
@@ -141,18 +60,18 @@ cdef extern from * nogil:
...
@@ -141,18 +60,18 @@ cdef extern from * nogil:
}
}
}
}
view_dict(view_dict && rhs) : view_dict
()
dict_view(dict_view && rhs) : dict_view
()
{
{
swap(*this, rhs);
swap(*this, rhs);
}
}
view_dict & operator=(view_dict
rhs)
dict_view & operator=(dict_view
rhs)
{
{
swap(*this, rhs);
swap(*this, rhs);
return *this;
return *this;
}
}
~
view_dict
()
~
dict_view
()
{
{
if (urange != NULL)
if (urange != NULL)
{
{
...
@@ -161,7 +80,7 @@ cdef extern from * nogil:
...
@@ -161,7 +80,7 @@ cdef extern from * nogil:
}
}
}
}
view_dict
(dict_t urange) : urange(urange)
dict_view
(dict_t urange) : urange(urange)
{
{
if (urange != NULL)
if (urange != NULL)
{
{
...
@@ -181,57 +100,57 @@ cdef extern from * nogil:
...
@@ -181,57 +100,57 @@ cdef extern from * nogil:
};
};
template<typename dict_t, typename base_iterator_t, typename reference_t>
template<typename dict_t, typename base_iterator_t, typename reference_t>
using
view_dict_keys = view_dict<dict_t,
key_iterator_t<dict_t, base_iterator_t, reference_t>>;
using
dict_keys_view_t = dict_view<dict_t, dict_
key_iterator_t<dict_t, base_iterator_t, reference_t>>;
template<typename dict_t, typename base_iterator_t, typename reference_t>
template<typename dict_t, typename base_iterator_t, typename reference_t>
using
view_dict_values = view_dict<dict_t,
value_iterator_t<dict_t, base_iterator_t, reference_t>>;
using
dict_values_view_t = dict_view<dict_t, dict_
value_iterator_t<dict_t, base_iterator_t, reference_t>>;
template<typename dict_t, typename base_iterator_t, typename reference_t>
template<typename dict_t, typename base_iterator_t, typename reference_t>
using
view_dict_items = view_dict<dict_t,
item_iterator_t<dict_t, base_iterator_t, reference_t>>;
using
dict_items_view_t = dict_view<dict_t, dict_
item_iterator_t<dict_t, base_iterator_t, reference_t>>;
"""
"""
cdef
cppclass
key_iterator_t
[
dict_t
,
base_iterator_t
,
reference_t
]:
cdef
cppclass
dict_
key_iterator_t
[
dict_t
,
base_iterator_t
,
reference_t
]:
key_iterator_t
()
dict_
key_iterator_t
()
key_iterator_t
(
base_iterator_t
)
dict_
key_iterator_t
(
base_iterator_t
)
key_iterator_t
(
base_iterator_t
,
const
dict_t
)
dict_
key_iterator_t
(
base_iterator_t
,
const
dict_t
)
reference_t
operator
*
()
reference_t
operator
*
()
key_iterator_t
operator
++
()
dict_
key_iterator_t
operator
++
()
bint
operator
!=
(
base_iterator_t
)
bint
operator
!=
(
base_iterator_t
)
cdef
cppclass
value_iterator_t
[
dict_t
,
base_iterator_t
,
reference_t
]:
cdef
cppclass
dict_
value_iterator_t
[
dict_t
,
base_iterator_t
,
reference_t
]:
value_iterator_t
()
dict_
value_iterator_t
()
value_iterator_t
(
base_iterator_t
)
dict_
value_iterator_t
(
base_iterator_t
)
value_iterator_t
(
base_iterator_t
,
const
dict_t
)
dict_
value_iterator_t
(
base_iterator_t
,
const
dict_t
)
reference_t
operator
*
()
reference_t
operator
*
()
value_iterator_t
operator
++
()
dict_
value_iterator_t
operator
++
()
bint
operator
!=
(
base_iterator_t
)
bint
operator
!=
(
base_iterator_t
)
cdef
cppclass
item_iterator_t
[
dict_t
,
base_iterator_t
,
reference_t
]:
cdef
cppclass
dict_
item_iterator_t
[
dict_t
,
base_iterator_t
,
reference_t
]:
item_iterator_t
()
dict_
item_iterator_t
()
item_iterator_t
(
base_iterator_t
)
dict_
item_iterator_t
(
base_iterator_t
)
item_iterator_t
(
base_iterator_t
,
const
dict_t
)
dict_
item_iterator_t
(
base_iterator_t
,
const
dict_t
)
reference_t
operator
*
()
reference_t
operator
*
()
item_iterator_t
operator
++
()
dict_
item_iterator_t
operator
++
()
bint
operator
!=
(
base_iterator_t
)
bint
operator
!=
(
base_iterator_t
)
cdef
cppclass
view_dict_keys
[
dict_t
,
base_iterator_t
,
reference_t
]:
cdef
cppclass
dict_keys_view_t
[
dict_t
,
base_iterator_t
,
reference_t
]:
ctypedef
key_iterator_t
[
dict_t
,
base_iterator_t
,
reference_t
]
iterator
ctypedef
dict_
key_iterator_t
[
dict_t
,
base_iterator_t
,
reference_t
]
iterator
view_dict_keys
()
dict_keys_view_t
()
view_dict_keys
(
const
dict_t
)
dict_keys_view_t
(
const
dict_t
)
key_iterator_t
[
dict_t
,
base_iterator_t
,
reference_t
]
begin
()
dict_
key_iterator_t
[
dict_t
,
base_iterator_t
,
reference_t
]
begin
()
base_iterator_t
end
()
base_iterator_t
end
()
cdef
cppclass
view_dict_values
[
dict_t
,
base_iterator_t
,
reference_t
]:
cdef
cppclass
dict_values_view_t
[
dict_t
,
base_iterator_t
,
reference_t
]:
ctypedef
value_iterator_t
[
dict_t
,
base_iterator_t
,
reference_t
]
iterator
ctypedef
dict_
value_iterator_t
[
dict_t
,
base_iterator_t
,
reference_t
]
iterator
view_dict_values
()
dict_values_view_t
()
view_dict_values
(
const
dict_t
)
dict_values_view_t
(
const
dict_t
)
value_iterator_t
[
dict_t
,
base_iterator_t
,
reference_t
]
begin
()
dict_
value_iterator_t
[
dict_t
,
base_iterator_t
,
reference_t
]
begin
()
base_iterator_t
end
()
base_iterator_t
end
()
cdef
cppclass
view_dict_items
[
dict_t
,
base_iterator_t
,
reference_t
]:
cdef
cppclass
dict_items_view_t
[
dict_t
,
base_iterator_t
,
reference_t
]:
ctypedef
item_iterator_t
[
dict_t
,
base_iterator_t
,
reference_t
]
iterator
ctypedef
dict_
item_iterator_t
[
dict_t
,
base_iterator_t
,
reference_t
]
iterator
view_dict_items
()
dict_items_view_t
()
view_dict_items
(
const
dict_t
)
dict_items_view_t
(
const
dict_t
)
item_iterator_t
[
dict_t
,
base_iterator_t
,
reference_t
]
begin
()
dict_
item_iterator_t
[
dict_t
,
base_iterator_t
,
reference_t
]
begin
()
base_iterator_t
end
()
base_iterator_t
end
()
...
@@ -240,7 +159,7 @@ cdef cypclass cypdict[K, V]:
...
@@ -240,7 +159,7 @@ cdef cypclass cypdict[K, V]:
ctypedef
V
value_type
ctypedef
V
value_type
ctypedef
pair
[
key_type
,
value_type
]
item_type
ctypedef
pair
[
key_type
,
value_type
]
item_type
ctypedef
vector
[
item_type
].
size_type
size_type
ctypedef
vector
[
item_type
].
size_type
size_type
ctypedef
key_iterator_t
[
cypdict
[
K
,
V
],
vector
[
item_type
].
iterator
,
key_type
]
iterator
ctypedef
dict_
key_iterator_t
[
cypdict
[
K
,
V
],
vector
[
item_type
].
iterator
,
key_type
]
iterator
vector
[
item_type
]
_items
vector
[
item_type
]
_items
unordered_map
[
key_type
,
size_type
]
_indices
unordered_map
[
key_type
,
size_type
]
_indices
...
@@ -316,8 +235,8 @@ cdef cypclass cypdict[K, V]:
...
@@ -316,8 +235,8 @@ cdef cypclass cypdict[K, V]:
with
gil
:
with
gil
:
raise
RuntimeError
(
"Modifying a dictionary with active iterators"
)
raise
RuntimeError
(
"Modifying a dictionary with active iterators"
)
key_iterator_t
[
cypdict
[
K
,
V
],
vector
[
item_type
].
iterator
,
key_type
]
begin
(
self
)
const
:
dict_
key_iterator_t
[
cypdict
[
K
,
V
],
vector
[
item_type
].
iterator
,
key_type
]
begin
(
self
)
const
:
return
key_iterator_t
[
cypdict
[
K
,
V
],
vector
[
item_type
].
iterator
,
key_type
](
self
.
_items
.
begin
(),
self
)
return
dict_
key_iterator_t
[
cypdict
[
K
,
V
],
vector
[
item_type
].
iterator
,
key_type
](
self
.
_items
.
begin
(),
self
)
vector
[
item_type
].
iterator
end
(
self
)
const
:
vector
[
item_type
].
iterator
end
(
self
)
const
:
return
self
.
_items
.
end
()
return
self
.
_items
.
end
()
...
@@ -328,11 +247,11 @@ cdef cypclass cypdict[K, V]:
...
@@ -328,11 +247,11 @@ cdef cypclass cypdict[K, V]:
bint
__contains__
(
self
,
const
key_type
key
)
const
:
bint
__contains__
(
self
,
const
key_type
key
)
const
:
return
self
.
_indices
.
count
(
key
)
return
self
.
_indices
.
count
(
key
)
view_dict_keys
[
cypdict
[
K
,
V
],
vector
[
item_type
].
iterator
,
key_type
]
keys
(
self
)
const
:
dict_keys_view_t
[
cypdict
[
K
,
V
],
vector
[
item_type
].
iterator
,
key_type
]
keys
(
self
)
const
:
return
view_dict_keys
[
cypdict
[
K
,
V
],
vector
[
item_type
].
iterator
,
key_type
](
self
)
return
dict_keys_view_t
[
cypdict
[
K
,
V
],
vector
[
item_type
].
iterator
,
key_type
](
self
)
view_dict_values
[
cypdict
[
K
,
V
],
vector
[
item_type
].
iterator
,
value_type
]
values
(
self
)
const
:
dict_values_view_t
[
cypdict
[
K
,
V
],
vector
[
item_type
].
iterator
,
value_type
]
values
(
self
)
const
:
return
view_dict_values
[
cypdict
[
K
,
V
],
vector
[
item_type
].
iterator
,
value_type
](
self
)
return
dict_values_view_t
[
cypdict
[
K
,
V
],
vector
[
item_type
].
iterator
,
value_type
](
self
)
view_dict_items
[
cypdict
[
K
,
V
],
vector
[
item_type
].
iterator
,
item_type
]
items
(
self
)
const
:
dict_items_view_t
[
cypdict
[
K
,
V
],
vector
[
item_type
].
iterator
,
item_type
]
items
(
self
)
const
:
return
view_dict_items
[
cypdict
[
K
,
V
],
vector
[
item_type
].
iterator
,
item_type
](
self
)
return
dict_items_view_t
[
cypdict
[
K
,
V
],
vector
[
item_type
].
iterator
,
item_type
](
self
)
Cython/Includes/libcythonplus/iterator.pxd
0 → 100644
View file @
dd2ff59b
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));
}
};
"""
Cython/Includes/libcythonplus/list.pxd
0 → 100644
View file @
dd2ff59b
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
tests/run/cypclass_builtin_list.pyx
0 → 100644
View file @
dd2ff59b
# 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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment