From 21c348357ef1d1eb69d48d9757f6693dd9ed4da2 Mon Sep 17 00:00:00 2001 From: will Date: Tue, 7 Jul 2020 12:29:34 -0700 Subject: Add missing unordered_map template defaults (GH-3686) --- Cython/Includes/libcpp/unordered_map.pxd | 2 +- tests/run/cpp_stl_cpp11.pyx | 15 +++++++++++++++ tests/run/cpp_unordered_map_helper.h | 13 +++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 tests/run/cpp_unordered_map_helper.h 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 "" 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 +#include + +struct IntVectorHash { + size_t operator()(const std::vector& v) const { + std::hash hasher; + size_t seed = 0; + for (int i : v) { + seed ^= hasher(i) + 0x9e3779b9 + (seed << 6) + (seed >> 2); + } + return seed; + } +}; -- cgit v1.2.1