Commit bd0a2c6b authored by scoder's avatar scoder Committed by GitHub

Merge pull request #2207 from vallsv/improve-cpp11-containers

Improve C++11 containers
parents cd6113fc b213375c
...@@ -81,3 +81,6 @@ cdef extern from "<deque>" namespace "std" nogil: ...@@ -81,3 +81,6 @@ cdef extern from "<deque>" namespace "std" nogil:
void resize(size_t, T&) void resize(size_t, T&)
size_t size() size_t size()
void swap(deque&) void swap(deque&)
# C++11 methods
void shrink_to_fit()
...@@ -13,18 +13,14 @@ cdef extern from "<map>" namespace "std" nogil: ...@@ -13,18 +13,14 @@ cdef extern from "<map>" namespace "std" nogil:
iterator operator--() iterator operator--()
bint operator==(iterator) bint operator==(iterator)
bint operator!=(iterator) bint operator!=(iterator)
cppclass const_iterator:
pair[const T, U]& operator*()
const_iterator operator++()
const_iterator operator--()
bint operator==(const_iterator)
bint operator!=(const_iterator)
cppclass reverse_iterator: cppclass reverse_iterator:
pair[T, U]& operator*() pair[T, U]& operator*()
iterator operator++() iterator operator++()
iterator operator--() iterator operator--()
bint operator==(reverse_iterator) bint operator==(reverse_iterator)
bint operator!=(reverse_iterator) bint operator!=(reverse_iterator)
cppclass const_iterator(iterator):
pass
cppclass const_reverse_iterator(reverse_iterator): cppclass const_reverse_iterator(reverse_iterator):
pass pass
map() except + map() except +
...@@ -39,6 +35,7 @@ cdef extern from "<map>" namespace "std" nogil: ...@@ -39,6 +35,7 @@ cdef extern from "<map>" namespace "std" nogil:
bint operator<=(map&, map&) bint operator<=(map&, map&)
bint operator>=(map&, map&) bint operator>=(map&, map&)
U& at(const T&) except + U& at(const T&) except +
const U& const_at "at"(const T&) except +
iterator begin() iterator begin()
const_iterator const_begin "begin" () const_iterator const_begin "begin" ()
void clear() void clear()
......
...@@ -9,6 +9,9 @@ cdef extern from "<queue>" namespace "std" nogil: ...@@ -9,6 +9,9 @@ cdef extern from "<queue>" namespace "std" nogil:
void pop() void pop()
void push(T&) void push(T&)
size_t size() size_t size()
# C++11 methods
void swap(queue&)
cdef cppclass priority_queue[T]: cdef cppclass priority_queue[T]:
priority_queue() except + priority_queue() except +
priority_queue(priority_queue&) except + priority_queue(priority_queue&) except +
...@@ -18,3 +21,5 @@ cdef extern from "<queue>" namespace "std" nogil: ...@@ -18,3 +21,5 @@ cdef extern from "<queue>" namespace "std" nogil:
void push(T&) void push(T&)
size_t size() size_t size()
T& top() T& top()
# C++11 methods
void swap(priority_queue&)
...@@ -38,14 +38,14 @@ cdef extern from "<set>" namespace "std" nogil: ...@@ -38,14 +38,14 @@ cdef extern from "<set>" namespace "std" nogil:
const_iterator const_end "end"() const_iterator const_end "end"()
pair[iterator, iterator] equal_range(const T&) pair[iterator, iterator] equal_range(const T&)
#pair[const_iterator, const_iterator] equal_range(T&) #pair[const_iterator, const_iterator] equal_range(T&)
void erase(iterator) iterator erase(iterator)
void erase(iterator, iterator) iterator erase(iterator, iterator)
size_t erase(T&) size_t erase(T&)
iterator find(T&) iterator find(T&)
const_iterator const_find "find"(T&) const_iterator const_find "find"(T&)
pair[iterator, bint] insert(const T&) except + pair[iterator, bint] insert(const T&) except +
iterator insert(iterator, const T&) except + iterator insert(iterator, const T&) except +
#void insert(input_iterator, input_iterator) void insert(iterator, iterator) except +
#key_compare key_comp() #key_compare key_comp()
iterator lower_bound(T&) iterator lower_bound(T&)
const_iterator const_lower_bound "lower_bound"(T&) const_iterator const_lower_bound "lower_bound"(T&)
......
...@@ -32,7 +32,8 @@ cdef extern from "<unordered_map>" namespace "std" nogil: ...@@ -32,7 +32,8 @@ cdef extern from "<unordered_map>" namespace "std" nogil:
bint operator>(unordered_map&, unordered_map&) bint operator>(unordered_map&, unordered_map&)
bint operator<=(unordered_map&, unordered_map&) bint operator<=(unordered_map&, unordered_map&)
bint operator>=(unordered_map&, unordered_map&) bint operator>=(unordered_map&, unordered_map&)
U& at(T&) U& at(const T&)
const U& const_at "at"(const T&)
iterator begin() iterator begin()
const_iterator const_begin "begin"() const_iterator const_begin "begin"()
void clear() void clear()
...@@ -41,7 +42,7 @@ cdef extern from "<unordered_map>" namespace "std" nogil: ...@@ -41,7 +42,7 @@ cdef extern from "<unordered_map>" namespace "std" nogil:
iterator end() iterator end()
const_iterator const_end "end"() const_iterator const_end "end"()
pair[iterator, iterator] equal_range(T&) pair[iterator, iterator] equal_range(T&)
#pair[const_iterator, const_iterator] equal_range(key_type&) pair[const_iterator, const_iterator] const_equal_range "equal_range"(const T&)
iterator erase(iterator) iterator erase(iterator)
iterator erase(iterator, iterator) iterator erase(iterator, iterator)
size_t erase(T&) size_t erase(T&)
...@@ -49,7 +50,7 @@ cdef extern from "<unordered_map>" namespace "std" nogil: ...@@ -49,7 +50,7 @@ cdef extern from "<unordered_map>" namespace "std" nogil:
const_iterator const_find "find"(T&) const_iterator const_find "find"(T&)
pair[iterator, bint] insert(pair[T, U]) # XXX pair[T,U]& pair[iterator, bint] insert(pair[T, U]) # XXX pair[T,U]&
iterator insert(iterator, pair[T, U]) # XXX pair[T,U]& iterator insert(iterator, pair[T, U]) # XXX pair[T,U]&
#void insert(input_iterator, input_iterator) iterator insert(iterator, iterator)
#key_compare key_comp() #key_compare key_comp()
iterator lower_bound(T&) iterator lower_bound(T&)
const_iterator const_lower_bound "lower_bound"(T&) const_iterator const_lower_bound "lower_bound"(T&)
...@@ -65,5 +66,9 @@ cdef extern from "<unordered_map>" namespace "std" nogil: ...@@ -65,5 +66,9 @@ cdef extern from "<unordered_map>" namespace "std" nogil:
#value_compare value_comp() #value_compare value_comp()
void max_load_factor(float) void max_load_factor(float)
float max_load_factor() float max_load_factor()
void rehash(size_t)
void reserve(size_t) void reserve(size_t)
size_t bucket_count() size_t bucket_count()
size_t max_bucket_count()
size_t bucket_size(size_t)
size_t bucket(const T&)
...@@ -37,16 +37,16 @@ cdef extern from "<unordered_set>" namespace "std" nogil: ...@@ -37,16 +37,16 @@ cdef extern from "<unordered_set>" namespace "std" nogil:
iterator end() iterator end()
const_iterator const_end "end"() const_iterator const_end "end"()
pair[iterator, iterator] equal_range(T&) pair[iterator, iterator] equal_range(T&)
#pair[const_iterator, const_iterator] equal_range(T&) pair[const_iterator, const_iterator] const_equal_range "equal_range"(T&)
void erase(iterator) iterator erase(iterator)
void erase(iterator, iterator) iterator erase(iterator, iterator)
size_t erase(T&) size_t erase(T&)
iterator find(T&) iterator find(T&)
const_iterator const_find "find"(T&) const_iterator const_find "find"(T&)
pair[iterator, bint] insert(T&) pair[iterator, bint] insert(T&)
iterator insert(iterator, T&) iterator insert(iterator, T&)
#void insert(input_iterator, input_iterator)
#key_compare key_comp() #key_compare key_comp()
iterator insert(iterator, iterator)
iterator lower_bound(T&) iterator lower_bound(T&)
const_iterator const_lower_bound "lower_bound"(T&) const_iterator const_lower_bound "lower_bound"(T&)
size_t max_size() size_t max_size()
...@@ -59,3 +59,11 @@ cdef extern from "<unordered_set>" namespace "std" nogil: ...@@ -59,3 +59,11 @@ cdef extern from "<unordered_set>" namespace "std" nogil:
iterator upper_bound(T&) iterator upper_bound(T&)
const_iterator const_upper_bound "upper_bound"(T&) const_iterator const_upper_bound "upper_bound"(T&)
#value_compare value_comp() #value_compare value_comp()
void max_load_factor(float)
float max_load_factor()
void rehash(size_t)
void reserve(size_t)
size_t bucket_count()
size_t max_bucket_count()
size_t bucket_size(size_t)
size_t bucket(const T&)
...@@ -84,4 +84,5 @@ cdef extern from "<vector>" namespace "std" nogil: ...@@ -84,4 +84,5 @@ cdef extern from "<vector>" namespace "std" nogil:
# C++11 methods # C++11 methods
T* data() T* data()
const T* const_data "data"()
void shrink_to_fit() void shrink_to_fit()
...@@ -3,7 +3,141 @@ ...@@ -3,7 +3,141 @@
import sys import sys
from libcpp.unordered_map cimport unordered_map from libcpp.unordered_map cimport unordered_map
from libcpp.unordered_set cimport unordered_set
from libcpp.vector cimport vector
from libcpp.queue cimport queue
from libcpp.queue cimport priority_queue
from libcpp.vector cimport vector
from libcpp.pair cimport pair from libcpp.pair cimport pair
from libcpp.map cimport map
from libcpp.set cimport set
from libcpp.deque cimport deque
def test_vector_functionality():
"""
>>> test_vector_functionality()
'pass'
"""
cdef:
vector[int] int_vector = vector[int]()
int* data
const int* const_data
int_vector.push_back(77)
data = int_vector.data()
const_data = int_vector.const_data()
assert data[0] == 77
assert const_data[0] == 77
return "pass"
def test_queue_functionality():
"""
>>> test_queue_functionality()
'pass'
"""
cdef:
queue[int] int_queue = queue[int]()
queue[int] int_queue2 = queue[int]()
int_queue.push(77)
int_queue.swap(int_queue2)
assert int_queue.size() == 0
assert int_queue2.size() == 1
return "pass"
def test_deque_functionality():
"""
>>> test_deque_functionality()
'pass'
"""
cdef:
deque[int] int_deque = deque[int]()
int_deque.push_back(77)
int_deque.shrink_to_fit()
return "pass"
def test_priority_queue_functionality():
"""
>>> test_priority_queue_functionality()
'pass'
"""
cdef:
priority_queue[int] int_queue = priority_queue[int]()
priority_queue[int] int_queue2 = priority_queue[int]()
int_queue.push(77)
int_queue.swap(int_queue2)
assert int_queue.size() == 0
assert int_queue2.size() == 1
return "pass"
def test_set_functionality():
"""
>>> test_set_functionality()
'pass'
"""
cdef:
set[int] int_set
set[int] int_set2
int_set2.insert(77)
int_set2.insert(66)
int_set.insert(int_set2.const_begin(), int_set2.const_end())
assert int_set.size() == 2
assert int_set.erase(int_set.const_begin(), int_set.const_end()) == int_set.end()
return "pass"
def test_map_functionality():
"""
>>> test_map_functionality()
'pass'
"""
cdef:
map[int, const void*] int_map
const void* data
int_map[77] = NULL
data = int_map.const_at(77)
return "pass"
def test_unordered_set_functionality():
"""
>>> test_unordered_set_functionality()
'pass'
"""
cdef:
unordered_set[int] int_set = unordered_set[int]()
unordered_set[int] int_set2
unordered_set[int].iterator iterator = int_set.begin()
int_set.insert(1)
assert int_set.size() == 1
int_set.erase(int_set.begin(), int_set.end())
assert int_set.size() == 0
int_set.insert(1)
assert int_set.erase(1) == 1 # returns number of elements erased
assert int_set.size() == 0
int_set.insert(1)
iterator = int_set.find(1)
assert int_set.erase(iterator) == int_set.end()
int_set2.insert(3)
int_set2.insert(5)
int_set.insert(int_set2.begin(), int_set2.end())
assert int_set.size() == 2
int_set.max_load_factor(0.5)
assert int_set.max_load_factor() == 0.5
int_set.rehash(20)
int_set.reserve(20)
int_set.bucket_size(0)
int_set.bucket_count()
int_set.max_bucket_count()
int_set.bucket(3)
return "pass"
def test_unordered_map_functionality(): def test_unordered_map_functionality():
""" """
...@@ -15,6 +149,9 @@ def test_unordered_map_functionality(): ...@@ -15,6 +149,9 @@ def test_unordered_map_functionality():
pair[int, int] pair_insert = pair[int, int](1, 2) pair[int, int] pair_insert = pair[int, int](1, 2)
unordered_map[int,int].iterator iterator = int_map.begin() unordered_map[int,int].iterator iterator = int_map.begin()
pair[unordered_map[int,int].iterator, bint] pair_iter = int_map.insert(pair_insert) pair[unordered_map[int,int].iterator, bint] pair_iter = int_map.insert(pair_insert)
unordered_map[int, int] int_map2
unordered_map[int, int*] intptr_map
const int* intptr
assert int_map[1] == 2 assert int_map[1] == 2
assert int_map.size() == 1 assert int_map.size() == 1
assert int_map.erase(1) == 1 # returns number of elements erased assert int_map.erase(1) == 1 # returns number of elements erased
...@@ -24,6 +161,25 @@ def test_unordered_map_functionality(): ...@@ -24,6 +161,25 @@ def test_unordered_map_functionality():
assert int_map[1] == 2 assert int_map[1] == 2
iterator = int_map.find(1) iterator = int_map.find(1)
assert int_map.erase(iterator) == int_map.end() assert int_map.erase(iterator) == int_map.end()
return "pass"
int_map2[1] = 2
int_map2[3] = 3
int_map.clear()
int_map.insert(int_map2.begin(), int_map2.end())
assert int_map.size() == 2
assert int_map.erase(int_map.begin(), int_map.end()) == int_map.end()
int_map.max_load_factor(0.5)
assert int_map.max_load_factor() == 0.5
int_map.rehash(20)
int_map.reserve(20)
int_map[3] = 3
int_map.bucket_size(0)
int_map.bucket_count()
int_map.max_bucket_count()
int_map.bucket(3)
intptr_map[0] = NULL
intptr = intptr_map.const_at(0)
return "pass"
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