Index: include/llvm/ADT/IntervalMap.h =================================================================== --- include/llvm/ADT/IntervalMap.h +++ include/llvm/ADT/IntervalMap.h @@ -150,6 +150,12 @@ return a+1 == b; } + /// nonEmpty - Return true if [a;b] is non-empty. + /// This is a <= b for a closed interval, a < b for [a;b) half-open intervals. + static inline bool nonEmpty(const T &a, const T &b) { + return a <= b; + } + }; template @@ -170,6 +176,11 @@ return a == b; } + /// nonEmpty - Return true if [a;b) is non-empty. + static inline bool nonEmpty(const T &a, const T &b) { + return a < b; + } + }; /// IntervalMapImpl - Namespace used for IntervalMap implementation details. @@ -1669,7 +1680,7 @@ template void IntervalMap:: iterator::setStart(KeyT a) { - assert(Traits::stopLess(a, this->stop()) && "Cannot move start beyond stop"); + assert(Traits::nonEmpty(a, this->stop()) && "Cannot move start beyond stop"); KeyT &CurStart = this->unsafeStart(); if (!Traits::startLess(a, CurStart) || !canCoalesceLeft(a, this->value())) { CurStart = a; @@ -1685,7 +1696,7 @@ template void IntervalMap:: iterator::setStop(KeyT b) { - assert(Traits::stopLess(this->start(), b) && "Cannot move stop beyond start"); + assert(Traits::nonEmpty(this->start(), b) && "Cannot move stop beyond start"); if (Traits::startLess(b, this->stop()) || !canCoalesceRight(b, this->value())) { setStopUnchecked(b); Index: unittests/ADT/IntervalMapTest.cpp =================================================================== --- unittests/ADT/IntervalMapTest.cpp +++ unittests/ADT/IntervalMapTest.cpp @@ -15,6 +15,8 @@ namespace { typedef IntervalMap UUMap; +typedef IntervalMap> UUHalfOpenMap; // Empty map tests TEST(IntervalMapTest, EmptyMap) { @@ -125,18 +127,63 @@ EXPECT_EQ(200u, I.stop()); EXPECT_EQ(2u, I.value()); + // Shrink the interval to have a length of 1 + I.setStop(150); + ASSERT_TRUE(I.valid()); + EXPECT_EQ(150u, I.start()); + EXPECT_EQ(150u, I.stop()); + EXPECT_EQ(2u, I.value()); + I.setStop(160); ASSERT_TRUE(I.valid()); EXPECT_EQ(150u, I.start()); EXPECT_EQ(160u, I.stop()); EXPECT_EQ(2u, I.value()); + // Shrink the interval to have a length of 1 + I.setStart(160); + ASSERT_TRUE(I.valid()); + EXPECT_EQ(160u, I.start()); + EXPECT_EQ(160u, I.stop()); + EXPECT_EQ(2u, I.value()); + // Erase last elem. I.erase(); EXPECT_TRUE(map.empty()); EXPECT_EQ(0, std::distance(map.begin(), map.end())); } +// Single entry half-open map tests +TEST(IntervalMapTest, SingleEntryHalfOpenMap) { + UUHalfOpenMap::Allocator allocator; + UUHalfOpenMap map(allocator); + map.insert(100, 150, 1); + EXPECT_FALSE(map.empty()); + + UUHalfOpenMap::iterator I = map.begin(); + ASSERT_TRUE(I.valid()); + + // Shrink the interval to have a length of 1 + I.setStart(149); + ASSERT_TRUE(I.valid()); + EXPECT_EQ(149u, I.start()); + EXPECT_EQ(150u, I.stop()); + EXPECT_EQ(1u, I.value()); + + I.setStop(160); + ASSERT_TRUE(I.valid()); + EXPECT_EQ(149u, I.start()); + EXPECT_EQ(160u, I.stop()); + EXPECT_EQ(1u, I.value()); + + // Shrink the interval to have a length of 1 + I.setStop(150); + ASSERT_TRUE(I.valid()); + EXPECT_EQ(149u, I.start()); + EXPECT_EQ(150u, I.stop()); + EXPECT_EQ(1u, I.value()); +} + // Flat coalescing tests. TEST(IntervalMapTest, RootCoalescing) { UUMap::Allocator allocator;