Metall  v0.29
A persistent memory allocator for data-centric analytics
string_map.cpp

This is an example of how to use a STL map container of which key is STL string.

// Copyright 2019 Lawrence Livermore National Security, LLC and other Metall
// Project Developers. See the top-level COPYRIGHT file for details.
//
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
#include <iostream>
#include <boost/container/string.hpp>
#include <boost/container/map.hpp>
// String with Metall
using persistent_string =
boost::container::basic_string<char, std::char_traits<char>,
// Example of string-int map
void string_int_map() {
using value_type = std::pair<const persistent_string, // Key type
int>; // Mapped type
using string_int_map =
boost::container::map<persistent_string, int, std::less<>,
{
metall::manager manager(metall::create_only, "/tmp/datastore");
auto pmap = manager.construct<string_int_map>("string-int-map")(
manager.get_allocator<>());
pmap->insert(
value_type(persistent_string("zero", manager.get_allocator<>()), 0));
(*pmap)[persistent_string("one", manager.get_allocator<>())] = 1;
}
{
metall::manager manager(metall::open_only, "/tmp/datastore");
auto pmap = manager.find<string_int_map>("string-int-map").first;
std::cout << pmap->at(persistent_string("zero", manager.get_allocator<>()))
<< std::endl; // Will print "0"
std::cout << pmap->at(persistent_string("one", manager.get_allocator<>()))
<< std::endl; // Will print "1"
}
}
// Example of int-string map
// This is also considered as an example of a container of containers
void int_string_map() {
using value_type = std::pair<const int, // Key type
persistent_string>; // Mapped type
using int_string_map = boost::container::map<int, persistent_string,
std::less<>, map_allocator_type>;
{
metall::manager manager(metall::create_only, "/tmp/datastore");
auto pmap = manager.construct<int_string_map>("int-string-map")(
manager.get_allocator<>());
pmap->insert(
value_type(0, persistent_string("zero", manager.get_allocator<>())));
(*pmap)[1] = persistent_string("one", manager.get_allocator<>());
// Mapped objects (persistent_string) use an allocator stored in pmap
// automatically thanks to scoped allocator
(*pmap)[2].push_back('t');
(*pmap)[2].push_back('w');
(*pmap)[2].push_back('o');
}
{
metall::manager manager(metall::open_only, "/tmp/datastore");
auto pmap = manager.find<int_string_map>("int-string-map").first;
std::cout << pmap->at(0) << std::endl; // Will print "zero"
std::cout << pmap->at(1) << std::endl; // Will print "one"
std::cout << pmap->at(2) << std::endl; // Will print "two"
}
}
int main() {
string_int_map();
int_string_map();
return 0;
}
A generalized Metall manager class.
Definition: basic_manager.hpp:40
container::scoped_allocator_adaptor< allocator_type< OuterT >, allocator_type< InnerT >... > scoped_allocator_type
Allocator type wrapped by scoped_allocator_adaptor.
Definition: basic_manager.hpp:71
A STL compatible allocator.
Definition: stl_allocator.hpp:34
int main()
Definition: jgraph.cpp:24
boost::container::map< Key, T, Compare, Allocator > map
A map container that uses Metall as its default allocator.
Definition: map.hpp:20
basic_manager<> manager
Default Metall manager class which is an alias of basic_manager with the default template parameters.
Definition: metall.hpp:34