Files
parallel-hashmap/examples/dump_load.cc
T

184 lines
4.9 KiB
C++
Raw Normal View History

2019-08-13 22:42:37 +08:00
#include <iostream>
#include <string>
#include <parallel_hashmap/phmap.h>
using phmap::flat_hash_map;
using phmap::flat_hash_set;
using phmap::parallel_flat_hash_map;
2019-08-13 22:42:37 +08:00
void dump_load_string_string() {
2019-08-13 22:42:37 +08:00
flat_hash_map<std::string, std::string> mp1;
2019-08-16 21:24:02 +08:00
phmap::BinaryOutputArchive ar_out("./dump.data");
2019-08-13 22:42:37 +08:00
// Add a new entry
mp1["key-1"] = "value-1";
mp1["key-2"] = "value-2";
// Iterate and print keys and values
for (const auto& n : mp1)
std::cout << n.first << "'s value is: " << n.second << "\n";
2019-08-16 21:24:02 +08:00
mp1.dump(ar_out);
2019-08-13 22:42:37 +08:00
flat_hash_map<std::string, std::string> mp2;
2019-08-16 21:24:02 +08:00
phmap::BinaryInputArchive ar_in("./dump.data");
mp2.load(ar_in);
2019-08-13 22:42:37 +08:00
// Iterate and print keys and values g|++
for (const auto& n : mp2)
std::cout << n.first << "'s value is: " << n.second << "\n";
2019-08-14 23:28:33 +08:00
std::remove("./dump.data");
2019-08-13 22:42:37 +08:00
}
void dump_load_uint64_uint32() {
2019-08-13 22:42:37 +08:00
flat_hash_map<uint64_t, uint32_t> mp1;
2019-08-16 21:24:02 +08:00
phmap::BinaryOutputArchive ar_out("./dump.data");
2019-08-13 22:42:37 +08:00
// Add a new entry
mp1[100] = 99;
mp1[300] = 299;
// Iterate and print keys and values
for (const auto& n : mp1)
std::cout << n.first << "'s value is: " << n.second << "\n";
2019-08-16 21:24:02 +08:00
mp1.dump(ar_out);
2019-08-13 22:42:37 +08:00
flat_hash_map<uint64_t, uint32_t> mp2;
2019-08-16 21:24:02 +08:00
phmap::BinaryInputArchive ar_in("./dump.data");
mp2.load(ar_in);
2019-08-13 22:42:37 +08:00
// Iterate and print keys and values g|++
for (const auto& n : mp2)
std::cout << n.first << "'s value is: " << n.second << "\n";
2019-08-14 23:28:33 +08:00
std::remove("./dump.data");
2019-08-13 22:42:37 +08:00
}
void dump_load_string_uint32() {
2019-08-13 22:42:37 +08:00
flat_hash_map<std::string, uint32_t> mp1;
2019-08-16 21:24:02 +08:00
phmap::BinaryOutputArchive ar_out("./dump.data");
2019-08-13 22:42:37 +08:00
// Add a new entry
mp1["key-1"] = 99;
mp1["key-2"] = 299;
// Iterate and print keys and values
for (const auto& n : mp1)
std::cout << n.first << "'s value is: " << n.second << "\n";
2019-08-16 21:24:02 +08:00
mp1.dump(ar_out);
2019-08-13 22:42:37 +08:00
flat_hash_map<std::string, uint32_t> mp2;
2019-08-16 21:24:02 +08:00
phmap::BinaryInputArchive ar_in("./dump.data");
2019-08-13 22:42:37 +08:00
2019-08-16 21:24:02 +08:00
mp2.load(ar_in);
2019-08-13 22:42:37 +08:00
// Iterate and print keys and values g|++
for (const auto& n : mp2)
std::cout << n.first << "'s value is: " << n.second << "\n";
2019-08-14 23:28:33 +08:00
std::remove("./dump.data");
2019-08-13 22:42:37 +08:00
}
void dump_load_uint32_string() {
2019-08-13 22:42:37 +08:00
flat_hash_map<uint32_t, std::string> mp1;
2019-08-16 21:24:02 +08:00
phmap::BinaryOutputArchive ar_out("./dump.data");
2019-08-13 22:42:37 +08:00
// Add a new entry
mp1[100] = "hello";
mp1[299] = "world";
// Iterate and print keys and values
for (const auto& n : mp1)
std::cout << n.first << "'s value is: " << n.second << "\n";
2019-08-16 21:24:02 +08:00
mp1.dump(ar_out);
2019-08-13 22:42:37 +08:00
flat_hash_map<uint32_t, std::string> mp2;
2019-08-16 21:24:02 +08:00
phmap::BinaryInputArchive ar_in("./dump.data");
2019-08-13 22:42:37 +08:00
2019-08-16 21:24:02 +08:00
mp2.load(ar_in);
2019-08-13 22:42:37 +08:00
// Iterate and print keys and values g|++
for (const auto& n : mp2)
std::cout << n.first << "'s value is: " << n.second << "\n";
2019-08-14 23:28:33 +08:00
std::remove("./dump.data");
2019-08-13 22:42:37 +08:00
}
void dump_load_string() {
2019-08-13 22:42:37 +08:00
flat_hash_set<std::string> st1;
2019-08-16 21:24:02 +08:00
phmap::BinaryOutputArchive ar_out("./dump.data");
2019-08-13 22:42:37 +08:00
// Add a new entry
st1.insert("hello");
st1.insert("world");
// Iterate and print
for (const auto& n : st1)
std::cout << "value: " << n << "\n";
2019-08-16 21:24:02 +08:00
st1.dump(ar_out);
2019-08-13 22:42:37 +08:00
flat_hash_set<std::string> st2;
2019-08-16 21:24:02 +08:00
phmap::BinaryInputArchive ar_in("./dump.data");
2019-08-13 22:42:37 +08:00
2019-08-16 21:24:02 +08:00
st2.load(ar_in);
2019-08-13 22:42:37 +08:00
// Iterate and print keys and values g|++
for (const auto& n : st2)
std::cout << "value: " << n << "\n";
2019-08-14 23:28:33 +08:00
std::remove("./dump.data");
2019-08-13 22:42:37 +08:00
}
void dump_load_uint64() {
2019-08-13 22:42:37 +08:00
flat_hash_set<uint64_t> st1;
2019-08-16 21:24:02 +08:00
phmap::BinaryOutputArchive ar_out("./dump.data");
2019-08-13 22:42:37 +08:00
// Add a new entry
st1.insert(878);
st1.insert(1424);
// Iterate and print
for (const auto& n : st1)
std::cout << "value: " << n << "\n";
2019-08-16 21:24:02 +08:00
st1.dump(ar_out);
2019-08-13 22:42:37 +08:00
flat_hash_set<uint64_t> st2;
2019-08-16 21:24:02 +08:00
phmap::BinaryInputArchive ar_in("./dump.data");
2019-08-13 22:42:37 +08:00
2019-08-16 21:24:02 +08:00
st2.load(ar_in);
2019-08-13 22:42:37 +08:00
// Iterate and print keys and values g|++
for (const auto& n : st2)
std::cout << "value: " << n << "\n";
2019-08-14 23:28:33 +08:00
std::remove("./dump.data");
2019-08-13 22:42:37 +08:00
}
void dump_load_parallel_flat_hash_map() {
parallel_flat_hash_map<uint64_t, uint32_t> mp1;
2019-08-16 21:24:02 +08:00
phmap::OutputArchiveWrapper<phmap::BinaryOutputArchive> w_out("./");
// Add a new entry
mp1[100] = 99;
mp1[300] = 299;
mp1[101] = 992;
mp1[1300] = 2991;
mp1[1130] = 299;
mp1[2130] = 1299;
// Iterate and print
for (const auto& n : mp1)
std::cout << "key: " << n.first << ", value: " << n.second << "\n";
2019-08-16 21:24:02 +08:00
mp1.dump(w_out);
parallel_flat_hash_map<uint64_t, uint32_t> mp2;
2019-08-16 21:24:02 +08:00
phmap::InputArchiveWrapper<phmap::BinaryInputArchive> w_in("./");
2019-08-16 21:24:02 +08:00
mp2.load(w_in);
for (const auto& n : mp2)
std::cout << "key: " << n.first << ", value: " << n.second << "\n";
}
2019-08-13 22:42:37 +08:00
int main()
{
dump_load_string_string();
dump_load_uint64_uint32();
dump_load_string_uint32();
dump_load_uint32_string();
dump_load_string();
dump_load_uint64();
dump_load_parallel_flat_hash_map();
2019-08-13 22:42:37 +08:00
return 0;
}