diff --git a/include/interval-tree/draw.hpp b/include/interval-tree/draw.hpp index 7597569..f6768ce 100644 --- a/include/interval-tree/draw.hpp +++ b/include/interval-tree/draw.hpp @@ -35,7 +35,7 @@ namespace lib_interval_tree template std::string iterCaption(typename lib_interval_tree::interval_tree ::const_iterator iter) { - auto ival = iter->interval(); + auto ival = *iter.node()->interval(); std::stringstream sstr; sstr << '[' << ival.low() << ',' << ival.high() << ']'; return sstr.str(); @@ -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); @@ -190,7 +190,7 @@ 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); @@ -198,7 +198,7 @@ namespace lib_interval_tree 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); @@ -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; diff --git a/include/interval-tree/interval_tree.hpp b/include/interval-tree/interval_tree.hpp index 5f44e19..4f4432e 100644 --- a/include/interval-tree/interval_tree.hpp +++ b/include/interval-tree/interval_tree.hpp @@ -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 @@ -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; @@ -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"); } @@ -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: @@ -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: @@ -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); } } @@ -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}); } @@ -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; } @@ -1162,9 +1173,9 @@ namespace lib_interval_tree }; template - 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;) { @@ -1177,7 +1188,7 @@ namespace lib_interval_tree } return end(); } - else + else { for (auto iter = merge_set.begin(), end = merge_set.end(); iter != end;) { @@ -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; @@ -1243,7 +1254,7 @@ namespace lib_interval_tree template 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); @@ -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; } @@ -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})) { @@ -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})) { diff --git a/tests/test_utility.hpp b/tests/test_utility.hpp index 92b715d..6e6ef93 100644 --- a/tests/test_utility.hpp +++ b/tests/test_utility.hpp @@ -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()); } } @@ -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)); }