From 65891e5e4b49258c00ce080dcf33d1e106137b56 Mon Sep 17 00:00:00 2001 From: "Ralph J. Steinhagen" Date: Tue, 2 Jan 2024 18:03:54 +0100 Subject: [PATCH] added transparent comparator to pmtv::map_t == std::map> Signed-off-by: Ralph J. Steinhagen --- bench/bm_pmt_dict_pack_unpack.cpp | 2 +- bench/bm_pmt_dict_ref.cpp | 2 +- include/pmtv/pmt.hpp | 6 +++--- include/pmtv/type_helpers.hpp | 4 ++-- python/pmtv/bindings/pmt_python.cc | 2 +- test/qa_map.cpp | 29 ++++++++++++++++++++++++----- test/qa_uniform_vector.cpp | 2 +- 7 files changed, 33 insertions(+), 14 deletions(-) diff --git a/bench/bm_pmt_dict_pack_unpack.cpp b/bench/bm_pmt_dict_pack_unpack.cpp index 6fd3d60..670ddb0 100644 --- a/bench/bm_pmt_dict_pack_unpack.cpp +++ b/bench/bm_pmt_dict_pack_unpack.cpp @@ -16,7 +16,7 @@ bool run_test(const int32_t times, int32_t nitems) bool valid = true; for (int32_t i = 0; i < times; i++) { // Create the dictionary - std::map starting_map; + pmtv::map_t starting_map; #if 1 for (int32_t k = 0; k < nitems; k++) { diff --git a/bench/bm_pmt_dict_ref.cpp b/bench/bm_pmt_dict_ref.cpp index 6c7788e..f30f534 100644 --- a/bench/bm_pmt_dict_ref.cpp +++ b/bench/bm_pmt_dict_ref.cpp @@ -50,7 +50,7 @@ int main(int argc, char* argv[]) { // Create the dictionary - std::map starting_map; + pmtv::map_t starting_map; for (int32_t k = 0; k < items; k++) { // auto key = std::string("key" + std::to_string(k)); // auto value = scalar(k); diff --git a/include/pmtv/pmt.hpp b/include/pmtv/pmt.hpp index 78285c4..73ce0c6 100644 --- a/include/pmtv/pmt.hpp +++ b/include/pmtv/pmt.hpp @@ -29,7 +29,7 @@ namespace pmtv { using pmt = pmt_var_t; -using map_t = std::map; +using map_t = std::map>; template inline constexpr std::in_place_type_t> vec_t{}; @@ -197,7 +197,7 @@ constexpr uint8_t pmtTypeIndex() return 5; else if constexpr (std::same_as) return 6; - else if constexpr (std::same_as>) + else if constexpr (std::same_as>>) return 7; else if constexpr (std::same_as>) return 8; @@ -540,7 +540,7 @@ T cast(const P& value) } } // else if constexpr (PmtMap && PmtMap) { - // return std::get>(arg); + // return std::get>>(arg); // } else throw std::runtime_error(fmt::format( diff --git a/include/pmtv/type_helpers.hpp b/include/pmtv/type_helpers.hpp index d9cddf1..20afea4 100644 --- a/include/pmtv/type_helpers.hpp +++ b/include/pmtv/type_helpers.hpp @@ -23,7 +23,7 @@ struct as_pmt { std::string, std::vector, std::vector, - std::map + std::map> >; }; @@ -85,7 +85,7 @@ concept UniformStringVector = std::ranges::range && std::same_as; template -concept PmtMap = std::is_same_v>; +concept PmtMap = std::is_same_v>>; template concept String = std::is_same_v; diff --git a/python/pmtv/bindings/pmt_python.cc b/python/pmtv/bindings/pmt_python.cc index 623e6cc..d59b24a 100644 --- a/python/pmtv/bindings/pmt_python.cc +++ b/python/pmtv/bindings/pmt_python.cc @@ -203,7 +203,7 @@ void bind_pmt(py::module& m) // return pmtv::pmt(); }), py::arg{}.noconvert()) // Map constructor .def(py::init( - [](const std::map& mm) { return pmtv::pmt(mm); })) + [](const pmtv::map_t& mm) { return pmtv::pmt(mm); })) // Fallback for types not directly mapped to pybind types // For supporting e.g. numpy.float32 scalar diff --git a/test/qa_map.cpp b/test/qa_map.cpp index 52301f0..6b5c899 100644 --- a/test/qa_map.cpp +++ b/test/qa_map.cpp @@ -7,6 +7,8 @@ */ #include #include +#include +#include #include @@ -18,6 +20,23 @@ TEST(PmtMap, EmptyMap) auto v = get_map(empty); v["abc"] = pmt(uint64_t(4)); v["xyz"] = pmt(std::vector{ 1, 2, 3, 4, 5 }); + + using namespace std::literals; + using namespace std::string_literals; + using namespace std::string_view_literals; + auto keyStringLiteral = "abc"; + std::string keyString = keyStringLiteral; + std::string_view keyStringView = keyStringLiteral; + EXPECT_TRUE(std::get(v.at(keyString)) == uint64_t(4)); + EXPECT_TRUE(std::get(v.find(keyString)->second) == uint64_t(4)); + EXPECT_TRUE(std::get(v.find(keyStringView)->second) == uint64_t(4)); + EXPECT_TRUE(std::get(v.find(keyStringLiteral)->second) == uint64_t(4)); + EXPECT_TRUE(std::get(v.at("abc"s)) == uint64_t(4)); + EXPECT_TRUE(std::get(v.at(keyStringLiteral)) == uint64_t(4)); + // EXPECT_TRUE(v.at("abc"sv) == pmt(uint64_t(4))) -- not allowed by the ISO-C++ standard + // map::at(T key) checks for exact key and its type 'T' + // wrapping of std::string_view with std::string is required by ISO-C++ design of map::at(..) + EXPECT_TRUE(v.at(std::string(keyStringView)) == pmt(uint64_t(4))); // <- this is OK } @@ -27,7 +46,7 @@ TEST(PmtMap, PmtMapTests) std::vector val2{ 44, 34563, -255729, 4402 }; // Create the PMT map - std::map input_map({ + pmtv::map_t input_map({ { "key1", val1 }, { "key2", val2 }, }); @@ -72,13 +91,13 @@ TEST(PmtMap, get_as) std::vector val2{ 44, 34563, -255729, 4402 }; // Create the PMT map - std::map input_map({ + pmtv::map_t input_map({ { "key1", val1 }, { "key2", val2 }, }); auto x = pmt(input_map); // Make sure that we can get the value back out - // auto y = std::map(x); + // auto y = std::map>(x); auto y = get_map(x); EXPECT_EQ(x == y, true); @@ -92,7 +111,7 @@ TEST(PmtMap, base64) std::vector val2{ 44, 34563, -255729, 4402 }; // Create the PMT map - std::map input_map({ + pmtv::map_t input_map({ { "key1", val1 }, { "key2", val2 }, }); @@ -111,7 +130,7 @@ TEST(PmtMap, fmt) std::vector val2{ 44, 34563, -255729, 4402 }; // Create the PMT map - std::map input_map({ + pmtv::map_t input_map({ { "key1", val1 }, { "key2", val2 }, }); diff --git a/test/qa_uniform_vector.cpp b/test/qa_uniform_vector.cpp index 2ccd8e0..f79dbff 100644 --- a/test/qa_uniform_vector.cpp +++ b/test/qa_uniform_vector.cpp @@ -221,7 +221,7 @@ TYPED_TEST(PmtVectorFixture, get_as) // else // EXPECT_THROW(std::vector(x), ConversionError); - // using mtype = std::map; + // using mtype = std::map>; // EXPECT_THROW(mtype(x), ConversionError); }