Skip to content

Commit

Permalink
added transparent comparator to pmtv::map_t == std::map<std::string, …
Browse files Browse the repository at this point in the history
…pmt, std::less<>>

Signed-off-by: Ralph J. Steinhagen <r.steinhagen@gsi.de>
  • Loading branch information
RalphSteinhagen committed Jan 2, 2024
1 parent cde3c43 commit 65891e5
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 14 deletions.
2 changes: 1 addition & 1 deletion bench/bm_pmt_dict_pack_unpack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, pmt> starting_map;
pmtv::map_t starting_map;

#if 1
for (int32_t k = 0; k < nitems; k++) {
Expand Down
2 changes: 1 addition & 1 deletion bench/bm_pmt_dict_ref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ int main(int argc, char* argv[])

{
// Create the dictionary
std::map<std::string, pmt> 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);
Expand Down
6 changes: 3 additions & 3 deletions include/pmtv/pmt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
namespace pmtv {

using pmt = pmt_var_t;
using map_t = std::map<std::string, pmt>;
using map_t = std::map<std::string, pmt, std::less<>>;

template <class T>
inline constexpr std::in_place_type_t<std::vector<T>> vec_t{};
Expand Down Expand Up @@ -197,7 +197,7 @@ constexpr uint8_t pmtTypeIndex()
return 5;
else if constexpr (std::same_as<T, std::string>)
return 6;
else if constexpr (std::same_as<T, std::map<std::string, pmt>>)
else if constexpr (std::same_as<T, std::map<std::string, pmt, std::less<>>>)
return 7;
else if constexpr (std::same_as<T, std::vector<std::string>>)
return 8;
Expand Down Expand Up @@ -540,7 +540,7 @@ T cast(const P& value)
}
}
// else if constexpr (PmtMap<T> && PmtMap<U>) {
// return std::get<std::map<std::string, pmt_var_t>>(arg);
// return std::get<std::map<std::string, pmt_var_t, std::less<>>>(arg);
// }
else
throw std::runtime_error(fmt::format(
Expand Down
4 changes: 2 additions & 2 deletions include/pmtv/type_helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct as_pmt {
std::string,
std::vector<std::string>,
std::vector<rva::self_t>,
std::map<std::string, rva::self_t>
std::map<std::string, rva::self_t, std::less<>>
>;
};

Expand Down Expand Up @@ -85,7 +85,7 @@ concept UniformStringVector =
std::ranges::range<T> && std::same_as<typename T::value_type, std::string>;

template <typename T>
concept PmtMap = std::is_same_v<T, std::map<std::string, pmt_var_t>>;
concept PmtMap = std::is_same_v<T, std::map<std::string, pmt_var_t, std::less<>>>;

template <typename T>
concept String = std::is_same_v<T, std::string>;
Expand Down
2 changes: 1 addition & 1 deletion python/pmtv/bindings/pmt_python.cc
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ void bind_pmt(py::module& m)
// return pmtv::pmt(); }), py::arg{}.noconvert())
// Map constructor
.def(py::init(
[](const std::map<std::string, pmtv::pmt>& 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
Expand Down
29 changes: 24 additions & 5 deletions test/qa_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/
#include <gtest/gtest.h>
#include <complex>
#include <string>
#include <string_view>

#include <pmtv/pmt.hpp>

Expand All @@ -18,6 +20,23 @@ TEST(PmtMap, EmptyMap)
auto v = get_map(empty);
v["abc"] = pmt(uint64_t(4));
v["xyz"] = pmt(std::vector<double>{ 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<uint64_t>(v.at(keyString)) == uint64_t(4));
EXPECT_TRUE(std::get<uint64_t >(v.find(keyString)->second) == uint64_t(4));
EXPECT_TRUE(std::get<uint64_t>(v.find(keyStringView)->second) == uint64_t(4));
EXPECT_TRUE(std::get<uint64_t>(v.find(keyStringLiteral)->second) == uint64_t(4));
EXPECT_TRUE(std::get<uint64_t>(v.at("abc"s)) == uint64_t(4));
EXPECT_TRUE(std::get<uint64_t>(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
}


Expand All @@ -27,7 +46,7 @@ TEST(PmtMap, PmtMapTests)
std::vector<int32_t> val2{ 44, 34563, -255729, 4402 };

// Create the PMT map
std::map<std::string, pmt> input_map({
pmtv::map_t input_map({
{ "key1", val1 },
{ "key2", val2 },
});
Expand Down Expand Up @@ -72,13 +91,13 @@ TEST(PmtMap, get_as)
std::vector<int32_t> val2{ 44, 34563, -255729, 4402 };

// Create the PMT map
std::map<std::string, pmt> 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<std::string, pmt>(x);
// auto y = std::map<std::string, pmt, std::less<>>(x);
auto y = get_map(x);
EXPECT_EQ(x == y, true);

Expand All @@ -92,7 +111,7 @@ TEST(PmtMap, base64)
std::vector<int32_t> val2{ 44, 34563, -255729, 4402 };

// Create the PMT map
std::map<std::string, pmt> input_map({
pmtv::map_t input_map({
{ "key1", val1 },
{ "key2", val2 },
});
Expand All @@ -111,7 +130,7 @@ TEST(PmtMap, fmt)
std::vector<int32_t> val2{ 44, 34563, -255729, 4402 };

// Create the PMT map
std::map<std::string, pmt> input_map({
pmtv::map_t input_map({
{ "key1", val1 },
{ "key2", val2 },
});
Expand Down
2 changes: 1 addition & 1 deletion test/qa_uniform_vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ TYPED_TEST(PmtVectorFixture, get_as)
// else
// EXPECT_THROW(std::vector<int>(x), ConversionError);

// using mtype = std::map<std::string, pmt>;
// using mtype = std::map<std::string, pmt, std::less<>>;
// EXPECT_THROW(mtype(x), ConversionError);
}

Expand Down

0 comments on commit 65891e5

Please sign in to comment.