Skip to content

Commit 6be330e

Browse files
committed
Make sharing map iteration independent of tree depth
Previously the depth of a node was used to determine its type. This changes the iterate() method to use is_internal() or is_container() to determine the node type instead.
1 parent 22b6eb6 commit 6be330e

File tree

1 file changed

+20
-27
lines changed

1 file changed

+20
-27
lines changed

src/util/sharing_map.h

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ class sharing_mapt
453453
if(empty())
454454
return;
455455

456-
iterate(map, 0, f);
456+
iterate(map, f);
457457
}
458458

459459
#if !defined(_MSC_VER)
@@ -525,12 +525,10 @@ class sharing_mapt
525525
}
526526

527527
void iterate(
528-
const baset &n,
529-
const unsigned start_depth,
528+
const innert &n,
530529
std::function<void(const key_type &k, const mapped_type &m)> f) const;
531530

532-
void gather_all(const baset &n, const unsigned depth, delta_viewt &delta_view)
533-
const;
531+
void gather_all(const innert &n, delta_viewt &delta_view) const;
534532

535533
std::size_t count_unmarked_nodes(
536534
bool leafs_only,
@@ -559,42 +557,37 @@ class sharing_mapt
559557

560558
SHARING_MAPT(void)
561559
::iterate(
562-
const baset &n,
563-
unsigned start_depth,
560+
const innert &n,
564561
std::function<void(const key_type &k, const mapped_type &m)> f) const
565562
{
566-
typedef std::pair<unsigned, const baset *> stack_itemt;
563+
SM_ASSERT(!n.empty());
567564

568-
std::stack<stack_itemt> stack;
569-
stack.push({start_depth, &n});
565+
std::stack<const innert *> stack;
566+
stack.push(&n);
570567

571568
do
572569
{
573-
const stack_itemt &si = stack.top();
574-
575-
const unsigned depth = si.first;
576-
const baset *bp = si.second;
577-
570+
const innert *ip = stack.top();
578571
stack.pop();
579572

580-
if(depth < steps) // internal
573+
SM_ASSERT(!ip->empty());
574+
575+
if(ip->is_internal())
581576
{
582-
const innert *ip = static_cast<const innert *>(bp);
583577
const to_mapt &m = ip->get_to_map();
584578
SM_ASSERT(!m.empty());
585579

586580
for(const auto &item : m)
587581
{
588582
const innert *i = &item.second;
589-
stack.push({depth + 1, i});
583+
stack.push(i);
590584
}
591585
}
592-
else // container
586+
else
593587
{
594-
SM_ASSERT(depth == steps);
588+
SM_ASSERT(ip->is_container());
595589

596-
const innert *cp = static_cast<const innert *>(bp);
597-
const leaf_listt &ll = cp->get_container();
590+
const leaf_listt &ll = ip->get_container();
598591
SM_ASSERT(!ll.empty());
599592

600593
for(const auto &l : ll)
@@ -766,17 +759,17 @@ SHARING_MAPT(void)::get_view(viewt &view) const
766759
view.push_back(view_itemt(k, m));
767760
};
768761

769-
iterate(map, 0, f);
762+
iterate(map, f);
770763
}
771764

772765
SHARING_MAPT(void)
773-
::gather_all(const baset &n, unsigned depth, delta_viewt &delta_view) const
766+
::gather_all(const innert &n, delta_viewt &delta_view) const
774767
{
775768
auto f = [&delta_view](const key_type &k, const mapped_type &m) {
776769
delta_view.push_back(delta_view_itemt(false, k, m, dummy));
777770
};
778771

779-
iterate(n, depth, f);
772+
iterate(n, f);
780773
}
781774

782775
SHARING_MAPT(void)
@@ -794,7 +787,7 @@ ::get_delta_view(
794787
{
795788
if(!only_common)
796789
{
797-
gather_all(map, 0, delta_view);
790+
gather_all(map, delta_view);
798791
}
799792

800793
return;
@@ -838,7 +831,7 @@ ::get_delta_view(
838831
{
839832
if(!only_common)
840833
{
841-
gather_all(item.second, depth + 1, delta_view);
834+
gather_all(item.second, delta_view);
842835
}
843836
}
844837
else if(!item.second.shares_with(*p))

0 commit comments

Comments
 (0)