Skip to content

Commit 856a208

Browse files
authored
Merge pull request #22 from 5cript/feat/iterator-arrow-returns-interval
Iterator arrow operator now returns an interval not a node.
2 parents a1b6a49 + 939218b commit 856a208

File tree

3 files changed

+49
-38
lines changed

3 files changed

+49
-38
lines changed

include/interval-tree/draw.hpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace lib_interval_tree
3535
template <typename... List>
3636
std::string iterCaption(typename lib_interval_tree::interval_tree <List...>::const_iterator iter)
3737
{
38-
auto ival = iter->interval();
38+
auto ival = *iter.node()->interval();
3939
std::stringstream sstr;
4040
sstr << '[' << ival.low() << ',' << ival.high() << ']';
4141
return sstr.str();
@@ -172,7 +172,7 @@ namespace lib_interval_tree
172172
circleY,
173173
circleRadius
174174
};
175-
switch (iter->color())
175+
switch (iter.node()->color())
176176
{
177177
case (rb_color::red):
178178
circle.draw(blackPen, Cairo::Colors::Red);
@@ -190,15 +190,15 @@ namespace lib_interval_tree
190190

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

193-
if (iter->color() != rb_color::black)
193+
if (iter.node()->color() != rb_color::black)
194194
caption.draw(iterCaptionPen);
195195
else
196196
caption.draw(Cairo::Colors::White);
197197

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

201-
if (iter->color() != rb_color::red)
201+
if (iter.node()->color() != rb_color::red)
202202
max.draw(ptrPen);
203203
else
204204
max.draw(Cairo::Colors::Yellow);
@@ -255,10 +255,10 @@ namespace lib_interval_tree
255255
{
256256
int y = pY;
257257
int x = pX;
258-
if (!iter->is_root())
258+
if (!iter.node()->is_root())
259259
{
260260
++y;
261-
if (iter->is_right())
261+
if (iter.node()->is_right())
262262
x = x + subtreeSize(iter.left()) + 1;
263263
else // is_left
264264
x = x - subtreeSize(iter.right()) - 1;

include/interval-tree/interval_tree.hpp

+37-26
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,9 @@ namespace lib_interval_tree
218218
{
219219
}
220220

221-
interval_type interval() const
221+
interval_type const* interval() const
222222
{
223-
return interval_;
223+
return &interval_;
224224
}
225225

226226
value_type max() const
@@ -382,7 +382,12 @@ namespace lib_interval_tree
382382

383383
typename tree_type::interval_type interval() const
384384
{
385-
return node_->interval();
385+
return *node_->interval();
386+
}
387+
388+
node_ptr_t node() const
389+
{
390+
return node_;
386391
}
387392

388393
virtual ~basic_interval_tree_iterator() = default;
@@ -464,7 +469,7 @@ namespace lib_interval_tree
464469
typename value_type::interval_type operator*() const
465470
{
466471
if (node_)
467-
return node_->interval();
472+
return *node_->interval();
468473
else
469474
throw std::out_of_range("dereferencing interval_tree_iterator out of bounds");
470475
}
@@ -505,9 +510,12 @@ namespace lib_interval_tree
505510
throw std::out_of_range("interval_tree_iterator out of bounds");
506511
}
507512

508-
value_type const* operator->() const
513+
typename value_type::interval_type* operator->() const
509514
{
510-
return node_;
515+
if (node_)
516+
return node_->interval();
517+
else
518+
throw std::out_of_range("dereferencing interval_tree_iterator out of bounds");
511519
}
512520

513521
private:
@@ -618,14 +626,17 @@ namespace lib_interval_tree
618626
typename value_type::interval_type operator*() const
619627
{
620628
if (node_)
621-
return node_->interval();
629+
return *node_->interval();
622630
else
623631
throw std::out_of_range("interval_tree_iterator out of bounds");
624632
}
625633

626-
value_type* operator->()
634+
typename value_type::interval_type const* operator->() const
627635
{
628-
return node_;
636+
if (node_)
637+
return node_->interval();
638+
else
639+
throw std::out_of_range("dereferencing interval_tree_iterator out of bounds");
629640
}
630641

631642
private:
@@ -776,8 +787,8 @@ namespace lib_interval_tree
776787
return insert(ival);
777788
else
778789
{
779-
auto mergeSet = iter->interval().join(ival);
780-
erase(iter);
790+
auto mergeSet = iter.interval().join(ival);
791+
erase(iter);
781792
return insert_merge_set(mergeSet, mergeSetOverlapping);
782793
}
783794
}
@@ -1061,7 +1072,7 @@ namespace lib_interval_tree
10611072
{
10621073
if (empty())
10631074
return {};
1064-
auto min = std::begin(*this)->interval().low();
1075+
auto min = std::begin(*this)->interval()->low();
10651076
auto max = root_->max_;
10661077
return punch({min, max});
10671078
}
@@ -1078,20 +1089,20 @@ namespace lib_interval_tree
10781089

10791090
interval_tree result;
10801091
auto i = std::begin(*this);
1081-
if (ival.low() < i->interval().low())
1082-
result.insert({ival.low(), i->interval().low()});
1092+
if (ival.low() < i->interval()->low())
1093+
result.insert({ival.low(), i->interval()->low()});
10831094

10841095
for (auto e = end(); i != e; ++i)
10851096
{
10861097
auto next = i; ++next;
10871098
if (next != e)
1088-
result.insert({i->interval().high(), next->interval().low()});
1099+
result.insert({i->interval()->high(), next->interval()->low()});
10891100
else
10901101
break;
10911102
}
10921103

1093-
if (i != end() && i->interval().high() < ival.high())
1094-
result.insert({i->interval().high(), ival.high()});
1104+
if (i != end() && i->interval()->high() < ival.high())
1105+
result.insert({i->interval()->high(), ival.high()});
10951106

10961107
return result;
10971108
}
@@ -1162,9 +1173,9 @@ namespace lib_interval_tree
11621173
};
11631174

11641175
template <typename MergeSet>
1165-
iterator insert_merge_set(MergeSet const& merge_set, bool mergeSetOverlapping)
1176+
iterator insert_merge_set(MergeSet const& merge_set, bool mergeSetOverlapping)
11661177
{
1167-
if (mergeSetOverlapping)
1178+
if (mergeSetOverlapping)
11681179
{
11691180
for (auto iter = merge_set.begin(), end = merge_set.end(); iter != end;)
11701181
{
@@ -1177,7 +1188,7 @@ namespace lib_interval_tree
11771188
}
11781189
return end();
11791190
}
1180-
else
1191+
else
11811192
{
11821193
for (auto iter = merge_set.begin(), end = merge_set.end(); iter != end;)
11831194
{
@@ -1215,7 +1226,7 @@ namespace lib_interval_tree
12151226
ComparatorFunctionT const& compare
12161227
)
12171228
{
1218-
if (compare(ptr->interval(), ival))
1229+
if (compare(*ptr->interval(), ival))
12191230
{
12201231
if (!on_find(IteratorT{ptr, self}))
12211232
return false;
@@ -1243,7 +1254,7 @@ namespace lib_interval_tree
12431254
template <typename ComparatorFunctionT>
12441255
node_type* find_i(node_type* ptr, interval_type const& ival, ComparatorFunctionT const& compare) const
12451256
{
1246-
if (compare(ptr->interval(), ival))
1257+
if (compare(*ptr->interval(), ival))
12471258
return ptr;
12481259
else
12491260
return find_i_ex(ptr, ival, compare);
@@ -1284,12 +1295,12 @@ namespace lib_interval_tree
12841295
if (Exclusive)
12851296
#endif
12861297
{
1287-
if (ptr->interval().overlaps_exclusive(ival))
1298+
if (ptr->interval()->overlaps_exclusive(ival))
12881299
return ptr;
12891300
}
12901301
else
12911302
{
1292-
if (ptr->interval().overlaps(ival))
1303+
if (ptr->interval()->overlaps(ival))
12931304
return ptr;
12941305
}
12951306

@@ -1311,7 +1322,7 @@ namespace lib_interval_tree
13111322
if (Exclusive)
13121323
#endif
13131324
{
1314-
if (ptr->interval().overlaps_exclusive(ival))
1325+
if (ptr->interval()->overlaps_exclusive(ival))
13151326
{
13161327
if (!on_find(IteratorT{ptr, self}))
13171328
{
@@ -1321,7 +1332,7 @@ namespace lib_interval_tree
13211332
}
13221333
else
13231334
{
1324-
if (ptr->interval().overlaps(ival))
1335+
if (ptr->interval()->overlaps(ival))
13251336
{
13261337
if (!on_find(IteratorT{ptr, self}))
13271338
{

tests/test_utility.hpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,15 @@ void testMaxProperty(TreeT const& tree)
8989
{
9090
for (auto i = std::begin(tree); i != std::end(tree); ++i)
9191
{
92-
if (i->left())
92+
if (i.node()->left())
9393
{
94-
EXPECT_LE(i->left()->max(), i->max());
94+
EXPECT_LE(i.node()->left()->max(), i.node()->max());
9595
}
96-
if (i->right())
96+
if (i.node()->right())
9797
{
98-
EXPECT_LE(i->right()->max(), i->max());
98+
EXPECT_LE(i.node()->right()->max(), i.node()->max());
9999
}
100-
EXPECT_GE(i->max(), i->interval().high());
100+
EXPECT_GE(i.node()->max(), i.interval().high());
101101
}
102102
}
103103

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

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

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

0 commit comments

Comments
 (0)