summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorwill <timwilloney@gmail.com>2020-07-07 12:29:34 -0700
committerStefan Behnel <stefan_ml@behnel.de>2020-07-07 21:30:45 +0200
commit21c348357ef1d1eb69d48d9757f6693dd9ed4da2 (patch)
tree8bc3d0631cd3874eb8658c2202c4fecb5ad5e22c
parent6e3c9562719d514384d0347490dcb01af58ce87b (diff)
downloadcython-21c348357ef1d1eb69d48d9757f6693dd9ed4da2.tar.gz
Add missing unordered_map template defaults (GH-3686)
-rw-r--r--Cython/Includes/libcpp/unordered_map.pxd2
-rw-r--r--tests/run/cpp_stl_cpp11.pyx15
-rw-r--r--tests/run/cpp_unordered_map_helper.h13
3 files changed, 29 insertions, 1 deletions
diff --git a/Cython/Includes/libcpp/unordered_map.pxd b/Cython/Includes/libcpp/unordered_map.pxd
index 329c1cefd..a00fbbed2 100644
--- a/Cython/Includes/libcpp/unordered_map.pxd
+++ b/Cython/Includes/libcpp/unordered_map.pxd
@@ -1,7 +1,7 @@
from .utility cimport pair
cdef extern from "<unordered_map>" namespace "std" nogil:
- cdef cppclass unordered_map[T, U]:
+ cdef cppclass unordered_map[T, U, HASH=*, PRED=*, ALLOCATOR=*]:
ctypedef T key_type
ctypedef U mapped_type
ctypedef pair[const T, U] value_type
diff --git a/tests/run/cpp_stl_cpp11.pyx b/tests/run/cpp_stl_cpp11.pyx
index bf0e048b7..f4fa4d360 100644
--- a/tests/run/cpp_stl_cpp11.pyx
+++ b/tests/run/cpp_stl_cpp11.pyx
@@ -140,6 +140,11 @@ def test_unordered_set_functionality():
return "pass"
+cdef extern from "cpp_unordered_map_helper.h":
+ cdef cppclass IntVectorHash:
+ pass
+
+
def test_unordered_map_functionality():
"""
>>> test_unordered_map_functionality()
@@ -153,6 +158,8 @@ def test_unordered_map_functionality():
unordered_map[int, int] int_map2
unordered_map[int, int*] intptr_map
const int* intptr
+ unordered_map[vector[int], int, IntVectorHash] int_vector_map
+ vector[int] intvec
assert int_map[1] == 2
assert int_map.size() == 1
assert int_map.erase(1) == 1 # returns number of elements erased
@@ -183,4 +190,12 @@ def test_unordered_map_functionality():
intptr_map[0] = NULL
intptr = intptr_map.const_at(0)
+
+ intvec = [1, 2]
+ int_vector_map[intvec] = 3
+ intvec = [4, 5]
+ int_vector_map[intvec] = 6
+ assert int_vector_map[intvec] == 6
+ intvec = [1, 2]
+ assert int_vector_map[intvec] == 3
return "pass"
diff --git a/tests/run/cpp_unordered_map_helper.h b/tests/run/cpp_unordered_map_helper.h
new file mode 100644
index 000000000..de2447306
--- /dev/null
+++ b/tests/run/cpp_unordered_map_helper.h
@@ -0,0 +1,13 @@
+#include <functional>
+#include <vector>
+
+struct IntVectorHash {
+ size_t operator()(const std::vector<int>& v) const {
+ std::hash<int> hasher;
+ size_t seed = 0;
+ for (int i : v) {
+ seed ^= hasher(i) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
+ }
+ return seed;
+ }
+};