Skip to content

[stdlib] Eliminate (Closed)CountableRange using conditional conformance #13342

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Feb 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1556,7 +1556,7 @@ extension TestSuite {
return
}

func toCollection(_ r: CountableRange<Int>) -> C {
func toCollection(_ r: Range<Int>) -> C {
return makeCollection(r.map { wrapValue(OpaqueValue($0)) })
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ internal enum _CollectionOperation : Equatable {
case .reserveCapacity:
let invalidIndices = newElementsIds.indices
newElementsIds.replaceSubrange(
Range(invalidIndices),
invalidIndices,
with: repeatElement(nextStateId, count: invalidIndices.count))
newEndIndexId = nextStateId

Expand All @@ -180,7 +180,7 @@ internal enum _CollectionOperation : Equatable {

let invalidIndices = subRange.lowerBound..<newElementsIds.endIndex
newElementsIds.replaceSubrange(
Range(invalidIndices),
invalidIndices,
with: repeatElement(nextStateId, count: invalidIndices.count))
newEndIndexId = nextStateId

Expand All @@ -189,7 +189,7 @@ internal enum _CollectionOperation : Equatable {

let invalidIndices = atIndex..<newElementsIds.endIndex
newElementsIds.replaceSubrange(
Range(invalidIndices),
invalidIndices,
with: repeatElement(nextStateId, count: invalidIndices.count))
newEndIndexId = nextStateId

Expand All @@ -200,7 +200,7 @@ internal enum _CollectionOperation : Equatable {

let invalidIndices = atIndex..<newElementsIds.endIndex
newElementsIds.replaceSubrange(
Range(invalidIndices),
invalidIndices,
with: repeatElement(nextStateId, count: invalidIndices.count))
newEndIndexId = nextStateId

Expand All @@ -209,7 +209,7 @@ internal enum _CollectionOperation : Equatable {

let invalidIndices = index..<newElementsIds.endIndex
newElementsIds.replaceSubrange(
Range(invalidIndices),
invalidIndices,
with: repeatElement(nextStateId, count: invalidIndices.count))
newEndIndexId = nextStateId

Expand All @@ -222,7 +222,7 @@ internal enum _CollectionOperation : Equatable {

let invalidIndices = subRange.lowerBound..<newElementsIds.endIndex
newElementsIds.replaceSubrange(
Range(invalidIndices),
invalidIndices,
with: repeatElement(nextStateId, count: invalidIndices.count))
newEndIndexId = nextStateId

Expand Down Expand Up @@ -592,7 +592,7 @@ public struct ${Self}<T> : ${SelfProtocols} {
}

% if StrideableIndex:
public typealias Indices = CountableRange<${Index}>
public typealias Indices = Range<${Index}>
% elif Traversal == 'RandomAccess':
// FIXME: this shouldn't be necessary, should come by default
public typealias Indices = DefaultIndices<${Self}<T>>
Expand Down Expand Up @@ -866,7 +866,7 @@ public struct ${Self}<Element> : ${SelfProtocols} {
public typealias Index = ${Index}

% if StrideableIndex:
public typealias Indices = CountableRange<${Index}>
public typealias Indices = Range<${Index}>
% elif Traversal == 'RandomAccess':
// FIXME: this shouldn't be necessary, should come by default
public typealias Indices = DefaultIndices<${Self}<Element>>
Expand Down
8 changes: 0 additions & 8 deletions stdlib/private/StdlibCollectionUnittest/RangeSelection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ public enum RangeSelection {
}
}

public func countableRange<C : Collection>(in c: C) -> CountableRange<C.Index> {
return CountableRange(range(in: c))
}

public func closedRange<C : Collection>(in c: C) -> ClosedRange<C.Index> {
switch self {
case .emptyRange: fatalError("Closed range cannot be empty")
Expand Down Expand Up @@ -87,8 +83,4 @@ public enum RangeSelection {
return start...end
}
}

public func countableClosedRange<C : Collection>(in c: C) -> CountableClosedRange<C.Index> {
return CountableClosedRange(closedRange(in: c))
}
}
30 changes: 5 additions & 25 deletions stdlib/private/StdlibUnittest/StdlibUnittest.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,8 @@ public func expectGE<T : Comparable>(_ lhs: T, _ rhs: T, ${TRACE}) {
}
}

% for OtherRange in ['Range', 'CountableRange', 'ClosedRange', 'CountableClosedRange']:
extension Range
% if 'Countable' in OtherRange:
where
Bound : Strideable, Bound.Stride : SignedInteger
% end
{
% for OtherRange in ['Range', 'ClosedRange']:
extension Range {
internal func _contains(_ other: ${OtherRange}<Bound>) -> Bool {
if other.lowerBound < lowerBound { return false }
% if 'Closed' in OtherRange:
Expand All @@ -338,7 +333,7 @@ extension Range
}
% end

% for Range in ['Range', 'CountableRange', 'ClosedRange', 'CountableClosedRange']:
% for Range in ['Range', 'ClosedRange']:
public func expectTrapping<Bound>(
_ point: Bound, in range: ${Range}<Bound>, ${TRACE}
) {
Expand Down Expand Up @@ -2433,31 +2428,16 @@ public func expectEqualsUnordered<T : Comparable>(
expectEqualSequence(x, y, ${trace})
}

public func expectEqualsUnordered<
T : Strideable
>(
public func expectEqualsUnordered<T : Strideable>(
_ expected: Range<T>, _ actual: [T], ${TRACE}
) where T.Stride: SignedInteger {
expectEqualsUnordered(
CountableRange(uncheckedBounds:
(lower: expected.lowerBound, upper: expected.upperBound)),
actual,
${trace},
showFrame: false)
}

public func expectEqualsUnordered<T : Strideable>(
_ expected: CountableRange<T>, _ actual: [T], ${TRACE}
) {
if expected.count != actual.count {
expectationFailure("expected elements: \"\(expected)\"\n"
+ "actual: \"\(actual)\" (of type \(String(reflecting: type(of: actual))))",
trace: ${trace})
}
let r = Range(uncheckedBounds:
(lower: expected.lowerBound, upper: expected.upperBound))
for e in actual {
if !r.contains(e) {
if !expected.contains(e) {
expectationFailure("expected elements: \"\(expected)\"\n"
+ "actual: \"\(actual)\" (of type \(String(reflecting: type(of: actual))))",
trace: ${trace})
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/SDK/CoreAudio/CoreAudio.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,6 @@ extension UnsafeMutableAudioBufferListPointer
}
}

public typealias Indices = CountableRange<Int>
public typealias Indices = Range<Int>
}

14 changes: 7 additions & 7 deletions stdlib/public/SDK/Dispatch/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public struct DispatchData : RandomAccessCollection, _ObjectiveCBridgeable {
}
}

private func _copyBytesHelper(to pointer: UnsafeMutableRawPointer, from range: CountableRange<Index>) {
private func _copyBytesHelper(to pointer: UnsafeMutableRawPointer, from range: Range<Index>) {
var copiedCount = 0
if range.isEmpty { return }
let rangeSize = range.count
Expand Down Expand Up @@ -195,8 +195,8 @@ public struct DispatchData : RandomAccessCollection, _ObjectiveCBridgeable {
/// - parameter pointer: A pointer to the buffer you wish to copy the bytes into.
/// - parameter range: The range in the `Data` to copy.
/// - warning: This method does not verify that the contents at pointer have enough space to hold the required number of bytes.
@available(swift, deprecated: 4, message: "Use copyBytes(to: UnsafeMutableRawBufferPointer, from: CountableRange<Index>) instead")
public func copyBytes(to pointer: UnsafeMutablePointer<UInt8>, from range: CountableRange<Index>) {
@available(swift, deprecated: 4, message: "Use copyBytes(to: UnsafeMutableRawBufferPointer, from: Range<Index>) instead")
public func copyBytes(to pointer: UnsafeMutablePointer<UInt8>, from range: Range<Index>) {
_copyBytesHelper(to: pointer, from: range)
}

Expand All @@ -205,7 +205,7 @@ public struct DispatchData : RandomAccessCollection, _ObjectiveCBridgeable {
/// - parameter pointer: A pointer to the buffer you wish to copy the bytes into. The buffer must be large
/// enough to hold `count` bytes.
/// - parameter range: The range in the `Data` to copy.
public func copyBytes(to pointer: UnsafeMutableRawBufferPointer, from range: CountableRange<Index>) {
public func copyBytes(to pointer: UnsafeMutableRawBufferPointer, from range: Range<Index>) {
assert(range.count <= pointer.count, "Buffer too small to copy \(range.count) bytes")
guard pointer.baseAddress != nil else { return }
_copyBytesHelper(to: pointer.baseAddress!, from: range)
Expand All @@ -218,11 +218,11 @@ public struct DispatchData : RandomAccessCollection, _ObjectiveCBridgeable {
/// - parameter buffer: A buffer to copy the data into.
/// - parameter range: A range in the data to copy into the buffer. If the range is empty, this function will return 0 without copying anything. If the range is nil, as much data as will fit into `buffer` is copied.
/// - returns: Number of bytes copied into the destination buffer.
public func copyBytes<DestinationType>(to buffer: UnsafeMutableBufferPointer<DestinationType>, from range: CountableRange<Index>? = nil) -> Int {
public func copyBytes<DestinationType>(to buffer: UnsafeMutableBufferPointer<DestinationType>, from range: Range<Index>? = nil) -> Int {
let cnt = count
guard cnt > 0 else { return 0 }

let copyRange : CountableRange<Index>
let copyRange : Range<Index>
if let r = range {
guard !r.isEmpty else { return 0 }
precondition(r.startIndex >= 0)
Expand Down Expand Up @@ -262,7 +262,7 @@ public struct DispatchData : RandomAccessCollection, _ObjectiveCBridgeable {
/// Return a new copy of the data in a specified range.
///
/// - parameter range: The range to copy.
public func subdata(in range: CountableRange<Index>) -> DispatchData {
public func subdata(in range: Range<Index>) -> DispatchData {
let subrange = __dispatch_data_create_subrange(
__wrapped, range.startIndex, range.endIndex - range.startIndex)
return DispatchData(data: subrange)
Expand Down
10 changes: 2 additions & 8 deletions stdlib/public/SDK/Foundation/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
public typealias Base64DecodingOptions = NSData.Base64DecodingOptions

public typealias Index = Int
public typealias Indices = CountableRange<Int>
public typealias Indices = Range<Int>

@_versioned internal var _backing : _DataStorage
@_versioned internal var _sliceRange: Range<Index>
Expand Down Expand Up @@ -1537,12 +1537,6 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
}
}

@inline(__always)
public mutating func replaceSubrange(_ subrange: CountableRange<Index>, with data: Data) {
let range: Range<Int> = subrange.lowerBound..<subrange.upperBound
replaceSubrange(range, with: data)
}

/// Replace a region of bytes in the data with new bytes from a buffer.
///
/// This will resize the data if required, to fit the entire contents of `buffer`.
Expand Down Expand Up @@ -1743,7 +1737,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
return i + 1
}

public var indices: CountableRange<Int> {
public var indices: Range<Int> {
@inline(__always)
get {
return startIndex..<endIndex
Expand Down
Loading