llvm.org GIT mirror llvm / 6897d9b
[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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@349898 91177308-0d34-0410-b5e6-96231b3b80d8 Pavel Labath 1 year, 10 months ago
2 changed file(s) with 57 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
11331133 I.find(x);
11341134 return I;
11351135 }
1136
1137 /// overlaps(a, b) - Return true if the intervals in this map overlap with the
1138 /// interval [a;b].
1139 bool overlaps(KeyT a, KeyT b) {
1140 assert(Traits::nonEmpty(a, b));
1141 const_iterator I = find(a);
1142 if (!I.valid())
1143 return false;
1144 // [a;b] and [x;y] overlap iff x<=b and a<=y. The find() call guarantees the
1145 // second part (y = find(a).stop()), so it is sufficient to check the first
1146 // one.
1147 return !Traits::stopLess(b, I.start());
1148 }
11361149 };
11371150
11381151 /// treeSafeLookup - Return the mapped value at x or NotFound, assuming a
608608 EXPECT_EQ(40959u, map.stop());
609609 EXPECT_EQ(1, std::distance(map.begin(), map.end()));
610610
611 }
612
613 TEST(IntervalMapTest, Overlaps) {
614 UUMap::Allocator allocator;
615 UUMap map(allocator);
616 map.insert(10, 20, 0);
617 map.insert(30, 40, 0);
618 map.insert(50, 60, 0);
619
620 EXPECT_FALSE(map.overlaps(0, 9));
621 EXPECT_TRUE(map.overlaps(0, 10));
622 EXPECT_TRUE(map.overlaps(0, 15));
623 EXPECT_TRUE(map.overlaps(0, 25));
624 EXPECT_TRUE(map.overlaps(0, 45));
625 EXPECT_TRUE(map.overlaps(10, 45));
626 EXPECT_TRUE(map.overlaps(30, 45));
627 EXPECT_TRUE(map.overlaps(35, 36));
628 EXPECT_TRUE(map.overlaps(40, 45));
629 EXPECT_FALSE(map.overlaps(45, 45));
630 EXPECT_TRUE(map.overlaps(60, 60));
631 EXPECT_TRUE(map.overlaps(60, 66));
632 EXPECT_FALSE(map.overlaps(66, 66));
633 }
634
635 TEST(IntervalMapTest, OverlapsHalfOpen) {
636 UUHalfOpenMap::Allocator allocator;
637 UUHalfOpenMap map(allocator);
638 map.insert(10, 20, 0);
639 map.insert(30, 40, 0);
640 map.insert(50, 60, 0);
641
642 EXPECT_FALSE(map.overlaps(0, 9));
643 EXPECT_FALSE(map.overlaps(0, 10));
644 EXPECT_TRUE(map.overlaps(0, 15));
645 EXPECT_TRUE(map.overlaps(0, 25));
646 EXPECT_TRUE(map.overlaps(0, 45));
647 EXPECT_TRUE(map.overlaps(10, 45));
648 EXPECT_TRUE(map.overlaps(30, 45));
649 EXPECT_TRUE(map.overlaps(35, 36));
650 EXPECT_FALSE(map.overlaps(40, 45));
651 EXPECT_FALSE(map.overlaps(45, 46));
652 EXPECT_FALSE(map.overlaps(60, 61));
653 EXPECT_FALSE(map.overlaps(60, 66));
654 EXPECT_FALSE(map.overlaps(66, 67));
611655 }
612656
613657 TEST(IntervalMapOverlapsTest, SmallMaps) {