From 174ca03ad1d357e335a59f0156c7bc21ccc5e0de Mon Sep 17 00:00:00 2001 From: account-login Date: Wed, 20 Oct 2021 17:03:20 +0800 Subject: Add some missing functions to libcpp maps and string (GH-4395) * add swap() to libcpp.string * add load_factor() to libcpp.unordered_map and libcpp.unordered_set --- Cython/Includes/libcpp/string.pxd | 1 + Cython/Includes/libcpp/unordered_map.pxd | 1 + Cython/Includes/libcpp/unordered_set.pxd | 2 ++ tests/run/cpp_stl_cpp11.pyx | 2 ++ tests/run/cpp_stl_multiset.pyx | 9 +++++++++ tests/run/cpp_stl_string.pyx | 10 ++++++++++ 6 files changed, 25 insertions(+) diff --git a/Cython/Includes/libcpp/string.pxd b/Cython/Includes/libcpp/string.pxd index a894144f1..73128324b 100644 --- a/Cython/Includes/libcpp/string.pxd +++ b/Cython/Includes/libcpp/string.pxd @@ -62,6 +62,7 @@ cdef extern from "" namespace "std" nogil: void resize(size_t) except + void resize(size_t, char) except + void shrink_to_fit() except + + void swap(string& other) size_t capacity() void reserve(size_t) except + void clear() diff --git a/Cython/Includes/libcpp/unordered_map.pxd b/Cython/Includes/libcpp/unordered_map.pxd index a00fbbed2..9da0fffd5 100644 --- a/Cython/Includes/libcpp/unordered_map.pxd +++ b/Cython/Includes/libcpp/unordered_map.pxd @@ -66,6 +66,7 @@ cdef extern from "" namespace "std" nogil: #value_compare value_comp() void max_load_factor(float) float max_load_factor() + float load_factor() void rehash(size_t) void reserve(size_t) size_t bucket_count() diff --git a/Cython/Includes/libcpp/unordered_set.pxd b/Cython/Includes/libcpp/unordered_set.pxd index 0b9931925..1418f9e28 100644 --- a/Cython/Includes/libcpp/unordered_set.pxd +++ b/Cython/Includes/libcpp/unordered_set.pxd @@ -51,6 +51,7 @@ cdef extern from "" namespace "std" nogil: #value_compare value_comp() void max_load_factor(float) float max_load_factor() + float load_factor() void rehash(size_t) void reserve(size_t) size_t bucket_count() @@ -110,6 +111,7 @@ cdef extern from "" namespace "std" nogil: #value_compare value_comp() void max_load_factor(float) float max_load_factor() + float load_factor() void rehash(size_t) void reserve(size_t) size_t bucket_count() diff --git a/tests/run/cpp_stl_cpp11.pyx b/tests/run/cpp_stl_cpp11.pyx index be0b72e33..ffeafdb3c 100644 --- a/tests/run/cpp_stl_cpp11.pyx +++ b/tests/run/cpp_stl_cpp11.pyx @@ -137,6 +137,7 @@ def test_unordered_set_functionality(): int_set.bucket_count() int_set.max_bucket_count() int_set.bucket(3) + assert int_set.load_factor() > 0 return "pass" @@ -187,6 +188,7 @@ def test_unordered_map_functionality(): int_map.bucket_count() int_map.max_bucket_count() int_map.bucket(3) + assert int_map.load_factor() > 0 intptr_map[0] = NULL intptr = intptr_map.const_at(0) diff --git a/tests/run/cpp_stl_multiset.pyx b/tests/run/cpp_stl_multiset.pyx index 26f3bbe61..66842e04e 100644 --- a/tests/run/cpp_stl_multiset.pyx +++ b/tests/run/cpp_stl_multiset.pyx @@ -104,3 +104,12 @@ def test_unordered_multiset_find_erase(vals, to_remove): it = ms.find(to_remove) ms.erase(it) return sorted([ item for item in ms ]) + + +def test_unordered_multiset_misc(): + """ + >>> test_unordered_multiset_misc() + """ + cdef unordered_multiset[int] ms = unordered_multiset[int]() + ms.insert(1) + assert ms.load_factor() > 0 diff --git a/tests/run/cpp_stl_string.pyx b/tests/run/cpp_stl_string.pyx index ff7757b08..6af0af303 100644 --- a/tests/run/cpp_stl_string.pyx +++ b/tests/run/cpp_stl_string.pyx @@ -438,6 +438,16 @@ def test_stof(char *a): return stof(s) +def test_swap(): + """ + >>> test_swap() + """ + cdef string s1 = b_asdf, s_asdf = b_asdf + cdef string s2 = b_asdg, s_asdg = b_asdg + s1.swap(s2) + assert s1 == s_asdg and s2 == s_asdf + + _WARNINGS = """ 21:31: Cannot pass Python object as C++ data structure reference (string &), will pass by copy. """ -- cgit v1.2.1