Skip to content

Iterator arrow operator now returns an interval not a node. #22

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

Merged
merged 1 commit into from
Jul 23, 2024
Merged
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
12 changes: 6 additions & 6 deletions include/interval-tree/draw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace lib_interval_tree
template <typename... List>
std::string iterCaption(typename lib_interval_tree::interval_tree <List...>::const_iterator iter)
{
auto ival = iter->interval();
auto ival = *iter.node()->interval();
std::stringstream sstr;
sstr << '[' << ival.low() << ',' << ival.high() << ']';
return sstr.str();
Expand Down Expand Up @@ -172,7 +172,7 @@ namespace lib_interval_tree
circleY,
circleRadius
};
switch (iter->color())
switch (iter.node()->color())
{
case (rb_color::red):
circle.draw(blackPen, Cairo::Colors::Red);
Expand All @@ -190,15 +190,15 @@ namespace lib_interval_tree

caption.move(circleX - actualCaptionBounds.getWidth() / 2., circleY - actualCaptionBounds.getHeight() / 2. - maxBounds.getHeight());

if (iter->color() != rb_color::black)
if (iter.node()->color() != rb_color::black)
caption.draw(iterCaptionPen);
else
caption.draw(Cairo::Colors::White);

max.move(circleX - maxBounds.getWidth() / 2., circleY - maxBounds.getHeight() / 2. + 10.);
//ptr.move(circleX - ptrBounds.getWidth() / 2., circleY - ptrBounds.getHeight() / 2. + 10. + maxBounds.getHeight() + margin);

if (iter->color() != rb_color::red)
if (iter.node()->color() != rb_color::red)
max.draw(ptrPen);
else
max.draw(Cairo::Colors::Yellow);
Expand Down Expand Up @@ -255,10 +255,10 @@ namespace lib_interval_tree
{
int y = pY;
int x = pX;
if (!iter->is_root())
if (!iter.node()->is_root())
{
++y;
if (iter->is_right())
if (iter.node()->is_right())
x = x + subtreeSize(iter.left()) + 1;
else // is_left
x = x - subtreeSize(iter.right()) - 1;
Expand Down
63 changes: 37 additions & 26 deletions include/interval-tree/interval_tree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,9 @@ namespace lib_interval_tree
{
}

interval_type interval() const
interval_type const* interval() const
{
return interval_;
return &interval_;
}

value_type max() const
Expand Down Expand Up @@ -382,7 +382,12 @@ namespace lib_interval_tree

typename tree_type::interval_type interval() const
{
return node_->interval();
return *node_->interval();
}

node_ptr_t node() const
{
return node_;
}

virtual ~basic_interval_tree_iterator() = default;
Expand Down Expand Up @@ -464,7 +469,7 @@ namespace lib_interval_tree
typename value_type::interval_type operator*() const
{
if (node_)
return node_->interval();
return *node_->interval();
else
throw std::out_of_range("dereferencing interval_tree_iterator out of bounds");
}
Expand Down Expand Up @@ -505,9 +510,12 @@ namespace lib_interval_tree
throw std::out_of_range("interval_tree_iterator out of bounds");
}

value_type const* operator->() const
typename value_type::interval_type* operator->() const
{
return node_;
if (node_)
return node_->interval();
else
throw std::out_of_range("dereferencing interval_tree_iterator out of bounds");
}

private:
Expand Down Expand Up @@ -618,14 +626,17 @@ namespace lib_interval_tree
typename value_type::interval_type operator*() const
{
if (node_)
return node_->interval();
return *node_->interval();
else
throw std::out_of_range("interval_tree_iterator out of bounds");
}

value_type* operator->()
typename value_type::interval_type const* operator->() const
{
return node_;
if (node_)
return node_->interval();
else
throw std::out_of_range("dereferencing interval_tree_iterator out of bounds");
}

private:
Expand Down Expand Up @@ -776,8 +787,8 @@ namespace lib_interval_tree
return insert(ival);
else
{
auto mergeSet = iter->interval().join(ival);
erase(iter);
auto mergeSet = iter.interval().join(ival);
erase(iter);
return insert_merge_set(mergeSet, mergeSetOverlapping);
}
}
Expand Down Expand Up @@ -1061,7 +1072,7 @@ namespace lib_interval_tree
{
if (empty())
return {};
auto min = std::begin(*this)->interval().low();
auto min = std::begin(*this)->interval()->low();
auto max = root_->max_;
return punch({min, max});
}
Expand All @@ -1078,20 +1089,20 @@ namespace lib_interval_tree

interval_tree result;
auto i = std::begin(*this);
if (ival.low() < i->interval().low())
result.insert({ival.low(), i->interval().low()});
if (ival.low() < i->interval()->low())
result.insert({ival.low(), i->interval()->low()});

for (auto e = end(); i != e; ++i)
{
auto next = i; ++next;
if (next != e)
result.insert({i->interval().high(), next->interval().low()});
result.insert({i->interval()->high(), next->interval()->low()});
else
break;
}

if (i != end() && i->interval().high() < ival.high())
result.insert({i->interval().high(), ival.high()});
if (i != end() && i->interval()->high() < ival.high())
result.insert({i->interval()->high(), ival.high()});

return result;
}
Expand Down Expand Up @@ -1162,9 +1173,9 @@ namespace lib_interval_tree
};

template <typename MergeSet>
iterator insert_merge_set(MergeSet const& merge_set, bool mergeSetOverlapping)
iterator insert_merge_set(MergeSet const& merge_set, bool mergeSetOverlapping)
{
if (mergeSetOverlapping)
if (mergeSetOverlapping)
{
for (auto iter = merge_set.begin(), end = merge_set.end(); iter != end;)
{
Expand All @@ -1177,7 +1188,7 @@ namespace lib_interval_tree
}
return end();
}
else
else
{
for (auto iter = merge_set.begin(), end = merge_set.end(); iter != end;)
{
Expand Down Expand Up @@ -1215,7 +1226,7 @@ namespace lib_interval_tree
ComparatorFunctionT const& compare
)
{
if (compare(ptr->interval(), ival))
if (compare(*ptr->interval(), ival))
{
if (!on_find(IteratorT{ptr, self}))
return false;
Expand Down Expand Up @@ -1243,7 +1254,7 @@ namespace lib_interval_tree
template <typename ComparatorFunctionT>
node_type* find_i(node_type* ptr, interval_type const& ival, ComparatorFunctionT const& compare) const
{
if (compare(ptr->interval(), ival))
if (compare(*ptr->interval(), ival))
return ptr;
else
return find_i_ex(ptr, ival, compare);
Expand Down Expand Up @@ -1284,12 +1295,12 @@ namespace lib_interval_tree
if (Exclusive)
#endif
{
if (ptr->interval().overlaps_exclusive(ival))
if (ptr->interval()->overlaps_exclusive(ival))
return ptr;
}
else
{
if (ptr->interval().overlaps(ival))
if (ptr->interval()->overlaps(ival))
return ptr;
}

Expand All @@ -1311,7 +1322,7 @@ namespace lib_interval_tree
if (Exclusive)
#endif
{
if (ptr->interval().overlaps_exclusive(ival))
if (ptr->interval()->overlaps_exclusive(ival))
{
if (!on_find(IteratorT{ptr, self}))
{
Expand All @@ -1321,7 +1332,7 @@ namespace lib_interval_tree
}
else
{
if (ptr->interval().overlaps(ival))
if (ptr->interval()->overlaps(ival))
{
if (!on_find(IteratorT{ptr, self}))
{
Expand Down
12 changes: 6 additions & 6 deletions tests/test_utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ void testMaxProperty(TreeT const& tree)
{
for (auto i = std::begin(tree); i != std::end(tree); ++i)
{
if (i->left())
if (i.node()->left())
{
EXPECT_LE(i->left()->max(), i->max());
EXPECT_LE(i.node()->left()->max(), i.node()->max());
}
if (i->right())
if (i.node()->right())
{
EXPECT_LE(i->right()->max(), i->max());
EXPECT_LE(i.node()->right()->max(), i.node()->max());
}
EXPECT_GE(i->max(), i->interval().high());
EXPECT_GE(i.node()->max(), i.interval().high());
}
}

Expand All @@ -108,7 +108,7 @@ void testTreeHeightHealth(TreeT const& tree)

auto maxHeight{0};
for (auto i = std::begin(tree); i != std::end(tree); ++i)
maxHeight = std::max(maxHeight, i->height());
maxHeight = std::max(maxHeight, i.node()->height());

EXPECT_LE(maxHeight, 2 * std::log2(treeSize + 1));
}