diff --git a/Sources/BasicContainers/RigidArray/RigidArray+ContainerOperations.swift b/Sources/BasicContainers/RigidArray/RigidArray+ContainerOperations.swift index 9f90ed3e..c912e2f4 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+ContainerOperations.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+ContainerOperations.swift @@ -410,7 +410,9 @@ extension RigidArray where Element: ~Copyable { after index: inout Int ) -> MutableSpan { _checkValidIndex(index) - return _mutableSpan(in: unsafe Range(uncheckedBounds: (index, count))) + let start = index + index = count + return _mutableSpan(in: unsafe Range(uncheckedBounds: (start, count))) } @inlinable @@ -423,7 +425,9 @@ extension RigidArray where Element: ~Copyable { precondition(maxCount > 0, "maxCount must be positive") let end = index._clampedUp( towards: count, maxDistance: maxCount, limitedBy: limit) - return _mutableSpan(in: unsafe Range(uncheckedBounds: (index, end))) + let start = index + index = end + return _mutableSpan(in: unsafe Range(uncheckedBounds: (start, end))) } @_alwaysEmitIntoClient diff --git a/Sources/ContainersPreview/Protocols/Container/BidirectionalContainer.swift b/Sources/ContainersPreview/Protocols/Container/BidirectionalContainer.swift index 8ef1bffe..ff3db536 100644 --- a/Sources/ContainersPreview/Protocols/Container/BidirectionalContainer.swift +++ b/Sources/ContainersPreview/Protocols/Container/BidirectionalContainer.swift @@ -76,7 +76,7 @@ where Self: ~Copyable & ~Escapable, Element: ~Copyable { self.spanBoundary( before: index, maxDistance: Int.max, - limitedBy: self.endIndex) + limitedBy: self.startIndex) } @_alwaysEmitIntoClient @@ -88,7 +88,7 @@ where Self: ~Copyable & ~Escapable, Element: ~Copyable { self.spanBoundary( before: index, maxDistance: maxDistance, - limitedBy: self.endIndex) + limitedBy: self.startIndex) } @_alwaysEmitIntoClient @@ -101,7 +101,7 @@ where Self: ~Copyable & ~Escapable, Element: ~Copyable { self.previousSpan( before: &index, maxCount: maxCount, - limitedBy: self.endIndex) + limitedBy: self.startIndex) } @_alwaysEmitIntoClient @@ -147,9 +147,13 @@ where Self: ~Copyable & ~Escapable, Element: ~Copyable { } return index } - n = -n + n = 0 &- n + let start = self.startIndex while n > 0 { - let r = self.spanBoundary(before: index, maxDistance: n, limitedBy: end) + let r = self.spanBoundary(before: index, maxDistance: n, limitedBy: start) + precondition( + r.distance > 0, + "Cannot move index before the start of the container") index = r.index n &-= r.distance } @@ -160,7 +164,6 @@ where Self: ~Copyable & ~Escapable, Element: ~Copyable { public func formIndex( _ index: inout Index, offsetBy n: inout Int, limitedBy limit: Index ) { - var n = n if n >= 0 { while n > 0 { let c = self.nextSpan(after: &index, maxCount: n, limitedBy: limit).count @@ -170,9 +173,9 @@ where Self: ~Copyable & ~Escapable, Element: ~Copyable { } return } - n = -n - while n > 0 { - let r = self.spanBoundary(before: index, maxDistance: n, limitedBy: limit) + while n < 0 { + let r = self.spanBoundary( + before: index, maxDistance: 0 &- n, limitedBy: limit) if r.distance == 0 { // We hit the limit (or the start) return diff --git a/Sources/ContainersPreview/Protocols/Container/MutableContainer.swift b/Sources/ContainersPreview/Protocols/Container/MutableContainer.swift index 0d684534..f16656ce 100644 --- a/Sources/ContainersPreview/Protocols/Container/MutableContainer.swift +++ b/Sources/ContainersPreview/Protocols/Container/MutableContainer.swift @@ -159,6 +159,7 @@ where var i = start let span = self.nextMutableSpan(after: &i, limitedBy: index) precondition(i == index, "Invalid BidirectionalContainer") + index = start return span } diff --git a/Sources/DequeModule/_UnsafeDequeHandle.swift b/Sources/DequeModule/_UnsafeDequeHandle.swift index 7257e7ea..da2c7fc1 100644 --- a/Sources/DequeModule/_UnsafeDequeHandle.swift +++ b/Sources/DequeModule/_UnsafeDequeHandle.swift @@ -418,7 +418,7 @@ extension _UnsafeDequeHandle where Element: ~Copyable { towards: startOffset <= wrapOffset ? 0 : wrapOffset, maxDistance: maxDistance, limitedBy: limitOffset) - return (p, p &- startOffset) + return (p, startOffset &- p) } @_alwaysEmitIntoClient diff --git a/Sources/InternalCollectionsUtilities/Strideable+LimitHelpers.swift b/Sources/InternalCollectionsUtilities/Strideable+LimitHelpers.swift index 92868be8..6b9d9c24 100644 --- a/Sources/InternalCollectionsUtilities/Strideable+LimitHelpers.swift +++ b/Sources/InternalCollectionsUtilities/Strideable+LimitHelpers.swift @@ -19,7 +19,7 @@ extension Strideable { assert(self <= boundary) assert(maxDistance >= 0) let limit = (limit >= self ? Swift.min(limit, boundary) : boundary) - if limit.distance(to: self) <= maxDistance { + if self.distance(to: limit) <= maxDistance { return limit } return self.advanced(by: maxDistance) @@ -32,7 +32,7 @@ extension Strideable { assert(self >= boundary) assert(maxDistance >= 0) let limit = (limit <= self ? Swift.max(limit, boundary) : boundary) - if self.distance(to: limit) <= maxDistance { + if limit.distance(to: self) <= maxDistance { return limit } return self.advanced(by: -maxDistance) diff --git a/Tests/BasicContainersTests/RigidArrayTests.swift b/Tests/BasicContainersTests/RigidArrayTests.swift index 10e7d2ef..e7ed9d64 100644 --- a/Tests/BasicContainersTests/RigidArrayTests.swift +++ b/Tests/BasicContainersTests/RigidArrayTests.swift @@ -255,50 +255,160 @@ class RigidArrayTests: CollectionTestCase { } } - func test_nextSpan() { - // RigidArray is expected to have exactly one span. + func test_nextSpan_after() { withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in withLifetimeTracking { tracker in let a = tracker.rigidArray(layout: layout) let whole = a.span - var i = 0 - let first = a.span(after: &i) - expectEqual(i, layout.count) - expectTrue(first.isIdentical(to: whole)) - let second = a.span(after: &i) - expectEqual(i, layout.count) - expectTrue(second.isEmpty) + withEvery("i", in: 0 ... layout.count) { i in + var j = i + let span = a.nextSpan(after: &j) + expectEqual(j, layout.count) + expectTrue(span.isIdentical(to: whole.extracting(i...))) + + let second = a.nextSpan(after: &j) + expectEqual(j, layout.count) + expectTrue(second.isEmpty) + } } } } - func test_previousSpan() { - // RigidArray is expected to have exactly one span. +#if compiler(>=6.4) && UnstableContainersPreview + @available(SwiftStdlib 6.4, *) + func test_nextSpan_after_maxCount() { withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in withLifetimeTracking { tracker in let a = tracker.rigidArray(layout: layout) let whole = a.span - var i = layout.count - let first = a.span(before: &i) - expectEqual(i, 0) - expectTrue(first.isIdentical(to: whole)) - let second = a.span(before: &i) - expectEqual(i, 0) - expectTrue(second.isEmpty) + withEvery("i", in: 0 ... layout.count) { i in + withEvery( + "maxCount", + in: ([1, 2, layout.count / 2, layout.count, Int.max] as Set).filter { $0 > 0 } + ) { maxCount in + let expected = i + Swift.min(maxCount, layout.count - i) + + var j = i + let span = a.nextSpan(after: &j, maxCount: maxCount) + expectEqual(j, expected) + expectTrue(span.isIdentical(to: whole.extracting(i ..< expected))) + } + } } } } +#endif + + func test_nextSpan_after_maxCount_limitedBy() { + withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in + withLifetimeTracking { tracker in + let a = tracker.rigidArray(layout: layout) + let whole = a.span + withEvery("i", in: 0 ... layout.count) { i in + withEvery( + "maxCount", + in: ([1, 2, layout.count / 2, layout.count, Int.max] as Set).filter { $0 > 0 } + ) { maxCount in + withEvery("limit", in: 0 ... layout.count) { limit in + var expected = i + Swift.min(maxCount, layout.count - i) + if limit >= i { + expected = Swift.min(limit, expected) + } + + var j = i + let span = a.nextSpan(after: &j, maxCount: maxCount, limitedBy: limit) + expectEqual(j, expected) + expectTrue(span.isIdentical(to: whole.extracting(i ..< expected))) + } + } + } + } + } + } + + func test_previousSpan_before() { + withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in + withLifetimeTracking { tracker in + let a = tracker.rigidArray(layout: layout) + let whole = a.span + withEvery("i", in: 0 ... layout.count) { i in + var j = i + let first = a.previousSpan(before: &j) + expectEqual(j, 0) + expectTrue(first.isIdentical(to: whole.extracting(0 ..< i))) + let second = a.previousSpan(before: &j) + expectEqual(j, 0) + expectTrue(second.isEmpty) + } + } + } + } + + func test_previousSpan_before_maxCount() { + withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in + withLifetimeTracking { tracker in + let a = tracker.rigidArray(layout: layout) + let whole = a.span + withEvery("i", in: 0 ... layout.count) { i in + withEvery( + "maxCount", + in: ([1, 2, layout.count / 2, layout.count, Int.max] as Set).filter { $0 > 0 } + ) { maxCount in + let expected = Swift.max(0, i - maxCount) + + var j = i + let span = a.previousSpan(before: &j, maxCount: maxCount) + expectEqual(j, expected) + expectTrue(span.isIdentical(to: whole.extracting(expected ..< i))) + } + } + } + } + } + +#if compiler(>=6.4) && UnstableContainersPreview + @available(SwiftStdlib 6.4, *) + func test_previousSpan_before_maxCount_limitedBy() { + withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in + withLifetimeTracking { tracker in + let a = tracker.rigidArray(layout: layout) + let whole = a.span + withEvery("i", in: 0 ... layout.count) { i in + withEvery( + "maxCount", + in: ([1, 2, layout.count / 2, layout.count, Int.max] as Set).filter { $0 > 0 } + ) { maxCount in + withEvery("limit", in: 0 ... layout.count) { limit in + var expected = Swift.max(0, i - maxCount) + if limit <= i { + expected = Swift.max(limit, expected) + } + + var j = i + let span = a.previousSpan(before: &j, maxCount: maxCount, limitedBy: limit) + expectEqual(j, expected) + if expected == i { + expectTrue(span.isEmpty) + } else { + expectTrue(span.isIdentical(to: whole.extracting(expected ..< i))) + } + } + } + } + } + } + } +#endif func test_nextMutableSpan() { - // RigidArray is expected to have exactly one span. withSomeArrayLayouts("layout", ofCapacities: [0, 10, 100]) { layout in withLifetimeTracking { tracker in var a = tracker.rigidArray(layout: layout) var i = 0 - var span = a.mutableSpan(after: &i) + var span = a.nextMutableSpan(after: &i) expectEqual(i, layout.count) expectEqual(span.count, layout.count) - span = a.mutableSpan(after: &i) + span = a.nextMutableSpan(after: &i) expectEqual(i, layout.count) expectTrue(span.isEmpty) } diff --git a/Tests/BasicContainersTests/UniqueArrayTests.swift b/Tests/BasicContainersTests/UniqueArrayTests.swift index eeaa49a9..5db9a2ae 100644 --- a/Tests/BasicContainersTests/UniqueArrayTests.swift +++ b/Tests/BasicContainersTests/UniqueArrayTests.swift @@ -295,10 +295,10 @@ class UniqueArrayTests: CollectionTestCase { let a = tracker.uniqueArray(layout: layout) let whole = a.span var i = 0 - let first = a.span(after: &i) + let first = a.nextSpan(after: &i) expectEqual(i, layout.count) expectTrue(first.isIdentical(to: whole)) - let second = a.span(after: &i) + let second = a.nextSpan(after: &i) expectEqual(i, layout.count) expectTrue(second.isEmpty) } @@ -312,10 +312,10 @@ class UniqueArrayTests: CollectionTestCase { let a = tracker.uniqueArray(layout: layout) let whole = a.span var i = layout.count - let first = a.span(before: &i) + let first = a.previousSpan(before: &i) expectEqual(i, 0) expectTrue(first.isIdentical(to: whole)) - let second = a.span(before: &i) + let second = a.previousSpan(before: &i) expectEqual(i, 0) expectTrue(second.isEmpty) } @@ -328,10 +328,10 @@ class UniqueArrayTests: CollectionTestCase { withLifetimeTracking { tracker in var a = tracker.uniqueArray(layout: layout) var i = 0 - var span = a.mutableSpan(after: &i) + var span = a.nextMutableSpan(after: &i) expectEqual(i, layout.count) expectEqual(span.count, layout.count) - span = a.mutableSpan(after: &i) + span = a.nextMutableSpan(after: &i) expectEqual(i, layout.count) expectTrue(span.isEmpty) } diff --git a/Tests/DequeTests/DequeInternals.swift b/Tests/DequeTests/DequeInternals.swift index 90f28273..948e512b 100644 --- a/Tests/DequeTests/DequeInternals.swift +++ b/Tests/DequeTests/DequeInternals.swift @@ -80,6 +80,10 @@ internal struct DequeLayout: Hashable, CustomStringConvertible { var isWrapped: Bool { startSlot + count > capacity } + + var wrapIndex: Int? { + isWrapped ? capacity - startSlot : nil + } } extension Deque { diff --git a/Tests/DequeTests/RigidDequeTests.swift b/Tests/DequeTests/RigidDequeTests.swift index 2a4018bc..45176298 100644 --- a/Tests/DequeTests/RigidDequeTests.swift +++ b/Tests/DequeTests/RigidDequeTests.swift @@ -538,6 +538,163 @@ final class RigidDequeTests: CollectionTestCase { } } + @available(SwiftStdlib 6.4, *) + func test_nextSpan_after() { + withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in + let d = RigidDeque(layout: layout, contents: layout.valueRange) + withEvery("i", in: 0 ... layout.count) { i in + var expected = layout.count + if let w = layout.wrapIndex, i < w { + expected = w + } + + var actual = i + let span = d.nextSpan(after: &actual) + expectGreaterThanOrEqual(actual, i) + expectEqual(span.count, actual - i) + expectEqual(span.isEmpty, i == layout.count) + expectEqual(actual, expected) + } + } + } + +#if compiler(>=6.4) && UnstableContainersPreview + @available(SwiftStdlib 6.4, *) + func test_nextSpan_after_maxCount() { + withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in + let d = RigidDeque(layout: layout, contents: layout.valueRange) + withEvery("i", in: 0 ... layout.count) { i in + withEvery( + "maxCount", + in: ([1, 2, layout.count / 2, layout.count, Int.max] as Set).filter { $0 > 0 } + ) { maxCount in + var expected = i + Swift.min(maxCount, layout.count - i) + if let w = layout.wrapIndex, i < w, expected >= w { + expected = w + } + + var actual = i + let span = d.nextSpan(after: &actual, maxCount: maxCount) + expectGreaterThanOrEqual(actual, i) + expectLessThanOrEqual(span.count, maxCount) + expectEqual(span.count, actual - i) + expectEqual(span.isEmpty, i == layout.count) + expectEqual(actual, expected) + } + } + } + } +#endif + + @available(SwiftStdlib 6.4, *) + func test_nextSpan_after_maxCount_limitedBy() { + withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in + let d = RigidDeque(layout: layout, contents: layout.valueRange) + withEvery("i", in: 0 ... layout.count) { i in + withEvery( + "maxCount", + in: ([1, 2, layout.count / 2, layout.count, Int.max] as Set).filter { $0 > 0 } + ) { maxCount in + withEvery("limit", in: 0 ... layout.count) { limit in + var expected = i + Swift.min(maxCount, layout.count - i) + if limit >= i { + expected = Swift.min(limit, expected) + } + if let w = layout.wrapIndex, i < w, expected >= w { + expected = w + } + + var actual = i + let span = d.nextSpan(after: &actual, maxCount: maxCount, limitedBy: limit) + expectGreaterThanOrEqual(actual, i) + expectLessThanOrEqual(span.count, maxCount) + expectEqual(span.count, actual - i) + expectEqual(span.isEmpty, i == layout.count || i == limit) + expectEqual(actual, expected) + } + } + } + } + } + + @available(SwiftStdlib 6.4, *) + func test_spanBoundary_before() { + withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in + let d = RigidDeque(layout: layout, contents: layout.valueRange) + withEvery("i", in: 0 ... layout.count) { i in + let r = d.spanBoundary(before: i) + expectGreaterThanOrEqual(r.distance, 0) + expectEqual(r.distance, i - r.index) + expectEqual(r.distance == 0, i == 0) + if let w = layout.wrapIndex, i > w { + expectEqual(r.index, w) + } else { + expectEqual(r.index, 0) + } + } + } + } + +#if compiler(>=6.4) && UnstableContainersPreview + @available(SwiftStdlib 6.4, *) + func test_spanBoundary_before_maxDistance() { + withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in + let d = RigidDeque(layout: layout, contents: layout.valueRange) + withEvery("i", in: 0 ... layout.count) { i in + withEvery( + "maxDistance", + in: ([1, 2, layout.count / 2, layout.count, Int.max] as Set).filter { $0 > 0 } + ) { maxDistance in + let r = d.spanBoundary(before: i, maxDistance: maxDistance) + expectGreaterThanOrEqual(r.distance, 0) + expectLessThanOrEqual(r.distance, maxDistance) + expectEqual(r.distance, i - r.index) + expectEqual(r.distance == 0, i == 0) + let j = Swift.max(i - maxDistance, 0) + if let w = layout.wrapIndex, i > w, j <= w { + expectEqual(r.index, w) + } else { + expectEqual(r.index, j) + } + } + } + } + } +#endif + + @available(SwiftStdlib 6.4, *) + func test_spanBoundary_before_maxDistance_limitedBy() { + withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in + let d = RigidDeque(layout: layout, contents: layout.valueRange) + withEvery("i", in: 0 ... layout.count) { i in + withEvery( + "maxDistance", + in: ([1, 2, layout.count / 2, layout.count, Int.max] as Set).filter { $0 > 0 } + ) { maxDistance in + withEvery("limit", in: 0 ... layout.count) { limit in + let r = d.spanBoundary( + before: i, + maxDistance: maxDistance, + limitedBy: limit) + expectGreaterThanOrEqual(r.distance, 0) + expectLessThanOrEqual(r.distance, maxDistance) + expectEqual(r.distance, i - r.index) + expectEqual(r.distance == 0, i == 0 || i == limit) + var j = Swift.max(i - maxDistance, 0) + if limit <= i { + j = Swift.max(limit, j) + } + if let w = layout.wrapIndex, i > w, j <= w { + expectEqual(r.index, w) + } else { + expectEqual(r.index, j) + } + } + } + } + } + } + func test_popFirst() { withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in withLifetimeTracking { tracker in @@ -1575,7 +1732,7 @@ final class RigidDequeTests: CollectionTestCase { } } -#if UnstableContainersPreview +#if compiler(>=6.4) && UnstableContainersPreview func test_consume_subrange() { withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in withEvery("from", in: 0 ... layout.count) { from in @@ -1592,7 +1749,7 @@ final class RigidDequeTests: CollectionTestCase { } } - func test_nextMutableSpan_maxCount() { + func test_nextMutableSpan_maxCount_limitedBy() { withEveryDeque("layout", ofCapacities: [0, 1, 2, 3, 5, 10]) { layout in withEvery("maxCount", in: [1, 2, 3, 5, 100]) { maxCount in var deque = RigidDeque(layout: layout, contents: layout.valueRange)