Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

champ for_each_chunk_p #270

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions immer/detail/hamts/champ.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <immer/detail/hamts/node.hpp>

#include <algorithm>
#include <array>
#include <cassert>

namespace immer {
namespace detail {
Expand Down Expand Up @@ -328,6 +330,61 @@ struct champ
}
}

template <typename Fn>
bool for_each_chunk_p(Fn&& fn) const
{
struct State
{
const node_t* const* fst{};
const node_t* const* lst{};
};

std::array<State, max_depth<B> + 1> stack{};
stack[0] = {&root, &root + 1};
size_t depth = 0;

while (true) {
auto fst = stack[depth].fst;
auto lst = stack[depth].lst;

if (fst == lst) {
if (!depth)
return true;

depth--;
continue;
}

const node_t* node = *fst;

if (depth < max_depth<B>) {
auto datamap = node->datamap();
if (datamap &&
!fn(node->values(), node->values() + node->data_count())) {
return false;
}

auto nodemap = node->nodemap();
stack[depth].fst++;

if (nodemap) {
auto childFst = node->children();
depth++;
stack[depth].fst = childFst;
stack[depth].lst = childFst + node->children_count();
}
continue;
}

if (!fn(node->collisions(),
node->collisions() + node->collision_count())) {
return false;
}
--depth;
}
return true;
}

template <typename EqualValue, typename Differ>
void diff(const champ& new_champ, Differ&& differ) const
{
Expand Down
7 changes: 3 additions & 4 deletions test/algorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,9 @@ TEST_CASE("all_of")
do_check(immer::vector<int>{});
do_check(immer::flex_vector<int>{});
do_check(immer::array<int>{});
// not supported
// do_check(immer::map<int, int>{});
// do_check(immer::set<int>{});
// do_check(immer::table<thing>{});
do_check(immer::map<int, int>{});
do_check(immer::set<int>{});
do_check(immer::table<thing>{});
}

TEST_CASE("update vectors")
Expand Down
49 changes: 49 additions & 0 deletions test/map/generic.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,55 @@ TEST_CASE("accumulate")
}
}

TEST_CASE("all_of")
{
static const auto n = 666u;
auto v = make_test_map(n);

static const auto all_identity = [](const auto& keyval) {
return keyval.first == keyval.second;
};
static const auto all_less_n = [](const auto& keyval) {
return keyval.second < n;
};
static const auto all_less_n2 = [](const auto& keyval) {
return keyval.second < n / 2;
};

SECTION("All identity (true)")
{
bool result = immer::all_of(v, all_identity);
CHECK(result);
}

SECTION("Empty (true)")
{
bool result = immer::all_of(make_test_map(0), all_identity);
CHECK(result);
}

SECTION("All less n/2 (false)")
{
bool result = immer::all_of(v, all_less_n2);
CHECK(!result);
}

SECTION("All less n w/ collisions (true)")
{
auto vals = make_values_with_collisions(n);
auto s = make_test_map(vals);
bool result = immer::all_of(v, all_less_n);
CHECK(result);
}
SECTION("All less n/2 w/ collisions (false)")
{
auto vals = make_values_with_collisions(n);
auto s = make_test_map(vals);
bool result = immer::all_of(v, all_less_n2);
CHECK(!result);
}
}

TEST_CASE("update a lot")
{
auto v = make_test_map(666u);
Expand Down