Skip to content

Commit b47bd52

Browse files
committed
[ADT] IntervalMap: add overlaps(a, b) method
Summary: This function checks whether the mappings in the interval map overlap with the given range [a;b]. The motivation is to enable checking for overlap before inserting a new interval into the map. Reviewers: vsk, dblaikie Subscribers: dexonsmith, kristina, llvm-commits Differential Revision: https://reviews.llvm.org/D55760 llvm-svn: 349898
1 parent 88fe16c commit b47bd52

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

llvm/include/llvm/ADT/IntervalMap.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,19 @@ class IntervalMap {
11341134
I.find(x);
11351135
return I;
11361136
}
1137+
1138+
/// overlaps(a, b) - Return true if the intervals in this map overlap with the
1139+
/// interval [a;b].
1140+
bool overlaps(KeyT a, KeyT b) {
1141+
assert(Traits::nonEmpty(a, b));
1142+
const_iterator I = find(a);
1143+
if (!I.valid())
1144+
return false;
1145+
// [a;b] and [x;y] overlap iff x<=b and a<=y. The find() call guarantees the
1146+
// second part (y = find(a).stop()), so it is sufficient to check the first
1147+
// one.
1148+
return !Traits::stopLess(b, I.start());
1149+
}
11371150
};
11381151

11391152
/// treeSafeLookup - Return the mapped value at x or NotFound, assuming a

llvm/unittests/ADT/IntervalMapTest.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,50 @@ TEST(IntervalMapTest, RandomCoalescing) {
611611

612612
}
613613

614+
TEST(IntervalMapTest, Overlaps) {
615+
UUMap::Allocator allocator;
616+
UUMap map(allocator);
617+
map.insert(10, 20, 0);
618+
map.insert(30, 40, 0);
619+
map.insert(50, 60, 0);
620+
621+
EXPECT_FALSE(map.overlaps(0, 9));
622+
EXPECT_TRUE(map.overlaps(0, 10));
623+
EXPECT_TRUE(map.overlaps(0, 15));
624+
EXPECT_TRUE(map.overlaps(0, 25));
625+
EXPECT_TRUE(map.overlaps(0, 45));
626+
EXPECT_TRUE(map.overlaps(10, 45));
627+
EXPECT_TRUE(map.overlaps(30, 45));
628+
EXPECT_TRUE(map.overlaps(35, 36));
629+
EXPECT_TRUE(map.overlaps(40, 45));
630+
EXPECT_FALSE(map.overlaps(45, 45));
631+
EXPECT_TRUE(map.overlaps(60, 60));
632+
EXPECT_TRUE(map.overlaps(60, 66));
633+
EXPECT_FALSE(map.overlaps(66, 66));
634+
}
635+
636+
TEST(IntervalMapTest, OverlapsHalfOpen) {
637+
UUHalfOpenMap::Allocator allocator;
638+
UUHalfOpenMap map(allocator);
639+
map.insert(10, 20, 0);
640+
map.insert(30, 40, 0);
641+
map.insert(50, 60, 0);
642+
643+
EXPECT_FALSE(map.overlaps(0, 9));
644+
EXPECT_FALSE(map.overlaps(0, 10));
645+
EXPECT_TRUE(map.overlaps(0, 15));
646+
EXPECT_TRUE(map.overlaps(0, 25));
647+
EXPECT_TRUE(map.overlaps(0, 45));
648+
EXPECT_TRUE(map.overlaps(10, 45));
649+
EXPECT_TRUE(map.overlaps(30, 45));
650+
EXPECT_TRUE(map.overlaps(35, 36));
651+
EXPECT_FALSE(map.overlaps(40, 45));
652+
EXPECT_FALSE(map.overlaps(45, 46));
653+
EXPECT_FALSE(map.overlaps(60, 61));
654+
EXPECT_FALSE(map.overlaps(60, 66));
655+
EXPECT_FALSE(map.overlaps(66, 67));
656+
}
657+
614658
TEST(IntervalMapOverlapsTest, SmallMaps) {
615659
typedef IntervalMapOverlaps<UUMap,UUMap> UUOverlaps;
616660
UUMap::Allocator allocator;

0 commit comments

Comments
 (0)