[PATCH] D55760: [ADT] IntervalMap: add contains(a, b) method
Pavel Labath via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Mon Dec 17 04:26:23 PST 2018
labath created this revision.
labath added reviewers: vsk, dblaikie.
Herald added subscribers: kristina, dexonsmith.
This function checks whether the interval map contains any mapping in
the range [a;b]. The motivation is to enable checking for overlap before
inserting a new interval into the map.
Repository:
rL LLVM
https://reviews.llvm.org/D55760
Files:
include/llvm/ADT/IntervalMap.h
unittests/ADT/IntervalMapTest.cpp
Index: unittests/ADT/IntervalMapTest.cpp
===================================================================
--- unittests/ADT/IntervalMapTest.cpp
+++ unittests/ADT/IntervalMapTest.cpp
@@ -611,6 +611,50 @@
}
+TEST(IntervalMapTest, Contains) {
+ UUMap::Allocator allocator;
+ UUMap map(allocator);
+ map.insert(10, 20, 0);
+ map.insert(30, 40, 0);
+ map.insert(50, 60, 0);
+
+ EXPECT_FALSE(map.contains(0, 9));
+ EXPECT_TRUE(map.contains(0, 10));
+ EXPECT_TRUE(map.contains(0, 15));
+ EXPECT_TRUE(map.contains(0, 25));
+ EXPECT_TRUE(map.contains(0, 45));
+ EXPECT_TRUE(map.contains(10, 45));
+ EXPECT_TRUE(map.contains(30, 45));
+ EXPECT_TRUE(map.contains(35, 36));
+ EXPECT_TRUE(map.contains(40, 45));
+ EXPECT_FALSE(map.contains(45, 45));
+ EXPECT_TRUE(map.contains(60, 60));
+ EXPECT_TRUE(map.contains(60, 66));
+ EXPECT_FALSE(map.contains(66, 66));
+}
+
+TEST(IntervalMapTest, ContainsHalfOpen) {
+ UUHalfOpenMap::Allocator allocator;
+ UUHalfOpenMap map(allocator);
+ map.insert(10, 20, 0);
+ map.insert(30, 40, 0);
+ map.insert(50, 60, 0);
+
+ EXPECT_FALSE(map.contains(0, 9));
+ EXPECT_FALSE(map.contains(0, 10));
+ EXPECT_TRUE(map.contains(0, 15));
+ EXPECT_TRUE(map.contains(0, 25));
+ EXPECT_TRUE(map.contains(0, 45));
+ EXPECT_TRUE(map.contains(10, 45));
+ EXPECT_TRUE(map.contains(30, 45));
+ EXPECT_TRUE(map.contains(35, 36));
+ EXPECT_FALSE(map.contains(40, 45));
+ EXPECT_FALSE(map.contains(45, 46));
+ EXPECT_FALSE(map.contains(60, 61));
+ EXPECT_FALSE(map.contains(60, 66));
+ EXPECT_FALSE(map.contains(66, 67));
+}
+
TEST(IntervalMapOverlapsTest, SmallMaps) {
typedef IntervalMapOverlaps<UUMap,UUMap> UUOverlaps;
UUMap::Allocator allocator;
Index: include/llvm/ADT/IntervalMap.h
===================================================================
--- include/llvm/ADT/IntervalMap.h
+++ include/llvm/ADT/IntervalMap.h
@@ -1134,6 +1134,18 @@
I.find(x);
return I;
}
+
+ /// contains(a, b) - Return true if this map contains at least one value from
+ /// the range [a;b]
+ bool contains(KeyT a, KeyT b) {
+ assert(Traits::nonEmpty(a, b));
+ const_iterator I = find(a);
+ if (!I.valid())
+ return false;
+ // [a;b] and [x;y] intersect iff x<=b and a<=y. The find() call guarantees
+ // the second part, so it is sufficient to check the first one.
+ return !Traits::stopLess(b, I.start());
+ }
};
/// treeSafeLookup - Return the mapped value at x or NotFound, assuming a
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D55760.178442.patch
Type: text/x-patch
Size: 2474 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181217/87829204/attachment.bin>
More information about the llvm-commits
mailing list