Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed Sep 12, 2023
1 parent def5161 commit 03ba854
Showing 1 changed file with 28 additions and 40 deletions.
68 changes: 28 additions & 40 deletions test/state/mpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct Path

[[nodiscard]] Path tail(size_t pos) const noexcept
{
assert(pos > 0 && pos <= length); // MPT never requests whole path copy (pos == 0).
assert(pos <= length); // MPT never requests whole path copy (pos == 0).
Path p;
p.length = length - pos;
std::copy_n(&nibbles[pos], p.length, p.nibbles);
Expand Down Expand Up @@ -144,50 +144,38 @@ void MPTNode::insert(const Path& path, bytes&& value) // NOLINT(misc-no-recursi
{
case Kind::branch:
{
if (m_path.length == 0) // Branch has no path.
const auto mismatch_pos = mismatch(m_path, path);

if (mismatch_pos == m_path.length) // Paths match: go into the child.
{
const auto idx = path.nibbles[0];
const auto sub_path = path.tail(mismatch_pos);
const auto idx = sub_path.nibbles[0];
auto& child = m_children[idx];
if (!child)
child = leaf(path.tail(1), std::move(value));
child = leaf(sub_path.tail(1), std::move(value));
else
child->insert(path.tail(1), std::move(value));
}
else
{
const auto mismatch_pos = mismatch(m_path, path);

if (mismatch_pos == m_path.length) // Paths match: go into the child.
{
const auto sub_path = path.tail(mismatch_pos);
const auto idx = sub_path.nibbles[0];
auto& child = m_children[idx];
if (!child)
child = leaf(sub_path.tail(1), std::move(value));
else
child->insert(sub_path.tail(1), std::move(value));
return;
}

const auto orig_idx = m_path.nibbles[mismatch_pos];
const auto new_idx = path.nibbles[mismatch_pos];

// The original branch node must be pushed down, possible extended with
// the adjusted extended node if the path split point is not directly at the branch
// node. Clang Analyzer bug: https://github.com/llvm/llvm-project/issues/47814
// NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)

auto down_branch = std::make_unique<MPTNode>();
down_branch->m_kind = Kind::branch;
// optional_ext(m_path.tail(mismatch_pos + 1), *this);
down_branch->m_path = m_path.tail(mismatch_pos + 1);
for (size_t i = 0; i < num_children; ++i)
down_branch->m_children[i] = std::move(m_children[i]);

auto new_leaf = leaf(path.tail(mismatch_pos + 1), std::move(value));
*this = ext_branch(m_path.head(mismatch_pos), orig_idx, std::move(down_branch), new_idx,
std::move(new_leaf));
child->insert(sub_path.tail(1), std::move(value));
return;
}

const auto orig_idx = m_path.nibbles[mismatch_pos];
const auto new_idx = path.nibbles[mismatch_pos];

// The original branch node must be pushed down, possible extended with
// the adjusted extended node if the path split point is not directly at the branch
// node. Clang Analyzer bug: https://github.com/llvm/llvm-project/issues/47814
// NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)

auto down_branch = std::make_unique<MPTNode>();
down_branch->m_kind = Kind::branch;
// optional_ext(m_path.tail(mismatch_pos + 1), *this);
down_branch->m_path = m_path.tail(mismatch_pos + 1);
for (size_t i = 0; i < num_children; ++i)
down_branch->m_children[i] = std::move(m_children[i]);

auto new_leaf = leaf(path.tail(mismatch_pos + 1), std::move(value));
*this = ext_branch(m_path.head(mismatch_pos), orig_idx, std::move(down_branch), new_idx,
std::move(new_leaf));
break;
}

Expand Down

0 comments on commit 03ba854

Please sign in to comment.