166 lines
4.6 KiB
C++
166 lines
4.6 KiB
C++
#include <print>
|
|
|
|
#include <snipplib/containers/snplib_hmap.h>
|
|
|
|
|
|
using namespace snplib;
|
|
|
|
int main()
|
|
{
|
|
int i = 10;
|
|
double d = 7.7;
|
|
std::string s = "klewjdl";
|
|
|
|
HeterogenMap<std::string_view> hmap;
|
|
|
|
bool ok = hmap.push("int_key", i, std::tuple<double, float, long>{});
|
|
if (!ok) {
|
|
std::println("cannot insert integer value {}", i);
|
|
}
|
|
|
|
ok = hmap.push("str_key", s);
|
|
if (!ok) {
|
|
std::println("cannot insert string value {}", s);
|
|
}
|
|
|
|
ok = hmap.push("dbl_key", d, std::tuple<double, float, long, int>{});
|
|
if (!ok) {
|
|
std::println("cannot insert double value {}", d);
|
|
}
|
|
|
|
auto rd = hmap.get<double>("int_key");
|
|
if (rd) {
|
|
std::println("hmap[int_key] = {}", rd.value());
|
|
} else {
|
|
std::println("cannot convert to double value with key 'int_key'");
|
|
}
|
|
|
|
auto rl = hmap.get<long>("dbl_key");
|
|
if (rl) {
|
|
std::println("hmap[dbl_key] = {}", rl.value());
|
|
} else {
|
|
std::println("cannot convert to long value with key 'dbl_key'");
|
|
}
|
|
|
|
std::println("\n{:*^80}", " copy ctor ");
|
|
|
|
HeterogenMap<std::string_view> hmap_copy(hmap);
|
|
|
|
auto rs = hmap.get<std::string>("str_key");
|
|
if (rs) {
|
|
std::println("hmap['str_key'] = {}", rs.value());
|
|
} else {
|
|
std::println("cannot get hmap['str_key']");
|
|
}
|
|
|
|
rs = hmap_copy.get<std::string>("str_key");
|
|
if (rs) {
|
|
std::println("hmap_copy['str_key'] = {}", rs.value());
|
|
} else {
|
|
std::println("cannot get hmap_copy['str_key']");
|
|
}
|
|
|
|
std::println("\n{:*^80}", " move ctor ");
|
|
|
|
HeterogenMap<std::string_view> hmap_move(std::move(hmap));
|
|
std::println("contains hmap_move['str_key']: {}", hmap_move.contains("str_key"));
|
|
|
|
rs = hmap_move.get<std::string>("str_key");
|
|
if (rs) {
|
|
std::println("hmap_move['str_key'] = {}", rs.value());
|
|
} else {
|
|
std::println("cannot get hmap_move['str_key']");
|
|
}
|
|
|
|
rs = hmap.get<std::string>("str_key");
|
|
if (rs) {
|
|
std::println("hmap['str_key'] = {}", rs.value());
|
|
} else {
|
|
std::println("cannot get hmap['str_key']");
|
|
std::println("contains hmap['str_key']: {}", hmap.contains("str_key"));
|
|
}
|
|
|
|
|
|
std::println("\n{:*^80}", " set value ");
|
|
|
|
double dd = 123.456789;
|
|
std::println("set hmap_copy['int_key'] to {} ...", dd);
|
|
if (hmap_copy.set("int_key", dd)) {
|
|
std::println("cannot set hmap_copy['int_key'] to {}", dd);
|
|
} else {
|
|
auto ri = hmap_copy.get<int>("int_key");
|
|
if (ri) {
|
|
std::println("hmap_copy['int_key'] = {}", ri.value());
|
|
} else {
|
|
std::println("cannot get value of hmap_copy['int_key']");
|
|
}
|
|
}
|
|
|
|
std::println("\n{:*^80}", " delete key ");
|
|
|
|
std::println("erase node with key 'int_key'");
|
|
if (hmap_move.erase("int_key")) {
|
|
auto ri = hmap_move.get<int>("int_key");
|
|
if (ri) { // should not be
|
|
std::println("hmap_move['int_key'] = {}", ri.value());
|
|
} else {
|
|
std::println("cannot get value of hmap_move['int_key'] as int");
|
|
}
|
|
|
|
auto rd = hmap_move.get<double>("int_key");
|
|
if (rd) { // should not be
|
|
std::println("hmap_move['int_key'] = {}", rd.value());
|
|
} else {
|
|
std::println("cannot get value of hmap_move['int_key'] as double");
|
|
}
|
|
|
|
ri = hmap_copy.get<int>("int_key");
|
|
if (ri) {
|
|
std::println("hmap_copy['int_key'] = {}", ri.value());
|
|
} else {
|
|
std::println("cannot get value of hmap_copy['int_key']");
|
|
}
|
|
}
|
|
|
|
std::println("\n{:*^80}", " hmap size ");
|
|
|
|
std::println("hmap.size() = {}", hmap.size());
|
|
std::println("hmap_move.size() = {}", hmap_move.size());
|
|
std::println("hmap_copy.size() = {}", hmap_copy.size());
|
|
|
|
std::println("\n{:*^80}", " hmap with custom conv funcs ");
|
|
|
|
hmap.pushWithCnv(
|
|
"str_cst", std::string{"ABC"},
|
|
[&s](std::string const& v) {
|
|
return v.c_str();
|
|
// return s.c_str();
|
|
},
|
|
[&s](const char* p) {
|
|
s = p;
|
|
return s;
|
|
});
|
|
|
|
auto ptr = hmap.get<const char*>("str_cst");
|
|
if (ptr) {
|
|
std::println("ptr = {}", ptr.value());
|
|
} else {
|
|
std::println("cannot get PTR");
|
|
}
|
|
|
|
// std::string pp = "EEEE";
|
|
// auto err = hmap.set("str_cst", pp.c_str());
|
|
const char* pp = "EEEE";
|
|
auto err = hmap.set("str_cst", pp);
|
|
if (err) {
|
|
std::println("cannot set 'str_cst'");
|
|
} else {
|
|
std::println("s = {}", s);
|
|
auto rs = hmap.get<std::string>("str_cst");
|
|
if (rs) {
|
|
std::println("s = {} (inner)", rs.value());
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
} |