ArraySlice indexes no longer zero-based

ArraySlice indices now map directly onto the collection it is slicing
and maintains that mapping even after mutations.

Before:

var a = Array(0..<10)
var s = a[5..<10]
s.indices        // 0..<5
s[0] = 111
s                // [111, 6, 7, 8, 9]
s.removeFirst()
s.indices        // 1..<5

After:

var a = Array(0..<10)
var s = a[5..<10]
s.indices        // 5..<10
s[5] = 99
s                // [99, 6, 7, 8, 9]
s.removeFirst()
s.indices        // 6..<10

- Refactor some of the internals of the buffer types to make it easier
  to read and understand.
- Add Array, ArraySlice, and ContiguousArray to the test suite at the
  RangeReplaceable test entry points, subjecting them to the same tests
  as all of our collections.
- Update existing test expectations for the indexing changes.

rdar://problem/21866825

Swift SVN r30840
This commit is contained in:
David Farler
2015-07-31 03:25:29 +00:00
parent d9088afe4b
commit f924e8e007
9 changed files with 320 additions and 295 deletions

View File

@@ -86,7 +86,7 @@ public func _arrayForceCast<SourceElement, TargetElement>(
p++.initialize(unsafeBitCast(bridged!, TargetElement.self))
}
}
return Array(_ArrayBuffer(buf))
return Array(_ArrayBuffer(buf, shiftedToStartIndex: 0))
case (.Value, .Explicit):
_sanityCheckFailure(
@@ -152,7 +152,7 @@ internal func _arrayConditionalBridgeElements<SourceElement, TargetElement>(
let buf = _ContiguousArrayBuffer<TargetElement>(
count: source.count, minimumCapacity: 0)
var p = buf.baseAddress
var p = buf.firstElementAddress
ElementwiseBridging:
repeat {
@@ -164,12 +164,12 @@ ElementwiseBridging:
}
p++.initialize(value!)
}
return Array(_ArrayBuffer(buf))
return Array(_ArrayBuffer(buf, shiftedToStartIndex: 0))
}
while false
// Don't destroy anything we never created.
buf.count = p - buf.baseAddress
buf.count = p - buf.firstElementAddress
// Report failure
return nil