Skip to content

Commit

Permalink
Adds support for custom container instead of only std::vector.
Browse files Browse the repository at this point in the history
Adds a test that makes use of boost::interprocess. This also uses
fancy pointers, so there are some updates needed to bucket handling.
  • Loading branch information
martinus committed Aug 15, 2022
1 parent 9415645 commit 565a486
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 65 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: sudo apt-get install -yq libboost-dev
- uses: hendrikmuhs/[email protected]
- uses: actions/setup-python@v4
with:
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.12)
project("unordered_dense"
VERSION 1.1.0
VERSION 1.2.0
DESCRIPTION "A fast & densely stored hashmap and hashset based on robin-hood backward shift deletion"
HOMEPAGE_URL "https://github.com/martinus/unordered_dense")

Expand Down
143 changes: 80 additions & 63 deletions include/ankerl/unordered_dense.h

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#

project('unordered_dense', 'cpp',
version: '1.1.0',
version: '1.2.0',
license: 'MIT',
default_options : ['cpp_std=c++17', 'warning_level=3', 'werror=true'])

Expand Down
16 changes: 16 additions & 0 deletions test/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ test_sources = [
'app/ui/Periodic.cpp',
'app/ui/ProgressBar.cpp',
'app/unordered_dense.cpp',

'bench/copy.cpp',
'bench/find_random.cpp',
'bench/quick_overall_map.cpp',
'bench/swap.cpp',

'fuzz/api.cpp',
'fuzz/insert_erase.cpp',
'fuzz/string.cpp',

'unit/assign_to_move.cpp',
'unit/assignment_combinations.cpp',
'unit/at.cpp',
Expand All @@ -21,6 +24,8 @@ test_sources = [
'unit/copyassignment.cpp',
'unit/count.cpp',
'unit/ctors.cpp',
'unit/custom_container_boost.cpp',
'unit/custom_container.cpp',
'unit/deduction_guides.cpp',
'unit/diamond.cpp',
'unit/empty.cpp',
Expand Down Expand Up @@ -104,17 +109,28 @@ if get_option('cpp_args').contains('-m32')
fmt_method = 'builtin'
endif

opt_boost = dependency('boost', required: false)
link_args = []
if opt_boost.found()
add_global_arguments('-DANKERL_UNORDERED_DENSE_HAS_BOOST=1', language: 'cpp')
link_args += ['-lrt']
else
add_global_arguments('-DANKERL_UNORDERED_DENSE_HAS_BOOST=0', language: 'cpp')
endif

test_exe = executable(
'udm',
test_sources,
include_directories: incdir,
cpp_args: cpp_args,
link_args: link_args,
dependencies: [
dependency('threads'), # add dependency for threads (-lpthread, see https://mesonbuild.com/howtox.html),

# see what's in the [provide] sections for the dependency names
dependency('doctest'),
dependency('fmt', method: fmt_method),
opt_boost, # boost might not be found
],
)

Expand Down
25 changes: 25 additions & 0 deletions test/unit/custom_container.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <ankerl/unordered_dense.h>

#include <doctest.h>

#include <deque>

static_assert(
ankerl::unordered_dense::detail::is_detected_v<ankerl::unordered_dense::detail::detect_iterator, std::deque<int>>);

static_assert(
!ankerl::unordered_dense::detail::is_detected_v<ankerl::unordered_dense::detail::detect_iterator, std::allocator<int>>);

TEST_CASE("custom_container") {
using Map = ankerl::unordered_dense::
map<int, std::string, ankerl::unordered_dense::hash<int>, std::equal_to<int>, std::deque<std::pair<int, std::string>>>;

auto map = Map();

for (int i = 0; i < 10; ++i) {
map[i] = std::to_string(i);
}

REQUIRE(std::is_same_v<std::deque<std::pair<int, std::string>>, typename Map::value_container_type>);
std::deque<std::pair<int, std::string>> container = std::move(map).extract();
}
59 changes: 59 additions & 0 deletions test/unit/custom_container_boost.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#if ANKERL_UNORDERED_DENSE_HAS_BOOST

# include <ankerl/unordered_dense.h>

# include <boost/container/vector.hpp>
# include <boost/interprocess/allocators/allocator.hpp>
# include <boost/interprocess/allocators/node_allocator.hpp>
# include <boost/interprocess/containers/vector.hpp>
# include <boost/interprocess/managed_shared_memory.hpp>
# include <doctest.h>

# include <deque>

// See https://www.boost.org/doc/libs/1_80_0/doc/html/interprocess/allocators_containers.html
TEST_CASE("boost_container_vector") {
// Remove shared memory on construction and destruction
struct ShmRemove {
ShmRemove() {
boost::interprocess::shared_memory_object::remove("MySharedMemory");
}
~ShmRemove() {
boost::interprocess::shared_memory_object::remove("MySharedMemory");
}
} remover;

// Create shared memory
auto segment = boost::interprocess::managed_shared_memory(boost::interprocess::create_only, "MySharedMemory", 65536);

// Alias an STL-like allocator of ints that allocates ints from the segment
using ShmemAllocator = boost::interprocess::allocator<std::pair<int, std::string>,
boost::interprocess::managed_shared_memory::segment_manager>;
using ShmemVector = boost::interprocess::vector<std::pair<int, std::string>, ShmemAllocator>;

using Map =
ankerl::unordered_dense::map<int, std::string, ankerl::unordered_dense::hash<int>, std::equal_to<int>, ShmemVector>;

auto map = Map{ShmemVector{ShmemAllocator{segment.get_segment_manager()}}};

for (int i = 0; i < 100; ++i) {
map.try_emplace(i, std::to_string(i));
}

REQUIRE(map.size() == 100);
for (int i = 0; i < 100; ++i) {
auto it = map.find(i);
REQUIRE(it != map.end());
REQUIRE(it->first == i);
REQUIRE(it->second == std::to_string(i));
}

map.erase(123);
REQUIRE(map.size() == 100);
map.erase(29);
REQUIRE(map.size() == 99);

map.emplace(std::pair<int, std::string>(9999, "hello"));
REQUIRE(map.size() == 100);
}
#endif

0 comments on commit 565a486

Please sign in to comment.