Skip to content

Commit

Permalink
Merge pull request swiftlang#1410 from airspeedswift/uncountable-ranges
Browse files Browse the repository at this point in the history
Remove references to CountableRange
  • Loading branch information
airspeedswift authored Feb 2, 2018
2 parents 4dab235 + d01046c commit bb8f718
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 82 deletions.
10 changes: 2 additions & 8 deletions Foundation/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,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 @@ -1546,12 +1546,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 @@ -1752,7 +1746,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
108 changes: 34 additions & 74 deletions Foundation/IndexSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public struct IndexSet : ReferenceConvertible, Equatable, BidirectionalCollectio
return IndexingIterator(_elements: self)
}

public subscript(index : Index) -> CountableRange<IndexSet.Element> {
public subscript(index : Index) -> Range<IndexSet.Element> {
let indexSetRange = indexSet._range(at: index)
if let intersectingRange = intersectingRange {
return Swift.max(intersectingRange.lowerBound, indexSetRange.lowerBound)..<Swift.min(intersectingRange.upperBound, indexSetRange.upperBound)
Expand Down Expand Up @@ -159,11 +159,9 @@ public struct IndexSet : ReferenceConvertible, Equatable, BidirectionalCollectio
}

/// Initialize an `IndexSet` with a range of integers.
public init(integersIn range: ClosedRange<Element>) { self.init(integersIn: Range(range)) }
/// Initialize an `IndexSet` with a range of integers.
public init(integersIn range: CountableClosedRange<Element>) { self.init(integersIn: Range(range)) }
/// Initialize an `IndexSet` with a range of integers.
public init(integersIn range: CountableRange<Element>) { self.init(integersIn: Range(range)) }
public init<R: RangeExpression>(integersIn range: R) where R.Bound == Element {
self.init(integersIn: range.relative(to: 0..<Int.max))
}

/// Initialize an `IndexSet` with a single integer.
public init(integer: Element) {
Expand Down Expand Up @@ -205,16 +203,9 @@ public struct IndexSet : ReferenceConvertible, Equatable, BidirectionalCollectio
/// Returns a `Range`-based view of `self`.
///
/// - parameter range: A subrange of `self` to view.
public func rangeView(of range : ClosedRange<Element>) -> RangeView { return self.rangeView(of: Range(range)) }
/// Returns a `Range`-based view of `self`.
///
/// - parameter range: A subrange of `self` to view.
public func rangeView(of range : CountableClosedRange<Element>) -> RangeView { return self.rangeView(of: Range(range)) }
/// Returns a `Range`-based view of `self`.
///
/// - parameter range: A subrange of `self` to view.
public func rangeView(of range : CountableRange<Element>) -> RangeView { return self.rangeView(of: Range(range)) }

public func rangeView<R: RangeExpression>(of range : R) -> RangeView where R.Bound == Element {
return self.rangeView(of: range.relative(to: 0..<Int.max))
}

private func _indexOfRange(containing integer : Element) -> RangeView.Index? {
let result = _handle.map {
Expand Down Expand Up @@ -344,32 +335,19 @@ public struct IndexSet : ReferenceConvertible, Equatable, BidirectionalCollectio
/// The resulting range is the range of the intersection of the integers in `range` with the index set.
///
/// - parameter range: The range of integers to include.
public func indexRange(in range: CountableRange<Element>) -> Range<Index> { return self.indexRange(in: Range(range)) }
/// Return a `Range<IndexSet.Index>` which can be used to subscript the index set.
///
/// The resulting range is the range of the intersection of the integers in `range` with the index set.
///
/// - parameter range: The range of integers to include.
public func indexRange(in range: ClosedRange<Element>) -> Range<Index> { return self.indexRange(in: Range(range)) }
/// Return a `Range<IndexSet.Index>` which can be used to subscript the index set.
///
/// The resulting range is the range of the intersection of the integers in `range` with the index set.
///
/// - parameter range: The range of integers to include.
public func indexRange(in range: CountableClosedRange<Element>) -> Range<Index> { return self.indexRange(in: Range(range)) }

public func indexRange<R: RangeExpression>(in range: R) -> Range<Index> where R.Bound == Element {
return self.indexRange(in: range.relative(to: 0..<Int.max))
}

/// Returns the count of integers in `self` that intersect `range`.
public func count(in range: Range<Element>) -> Int {
return _handle.map { $0.countOfIndexes(in: _toNSRange(range)) }
}

/// Returns the count of integers in `self` that intersect `range`.
public func count(in range: CountableRange<Element>) -> Int { return self.count(in: Range(range)) }
/// Returns the count of integers in `self` that intersect `range`.
public func count(in range: ClosedRange<Element>) -> Int { return self.count(in: Range(range)) }
/// Returns the count of integers in `self` that intersect `range`.
public func count(in range: CountableClosedRange<Element>) -> Int { return self.count(in: Range(range)) }
public func count<R: RangeExpression>(in range: R) -> Int where R.Bound == Element {
return self.count(in: range.relative(to: 0..<Int.max))
}

/// Returns `true` if `self` contains `integer`.
public func contains(_ integer: Element) -> Bool {
Expand All @@ -382,12 +360,9 @@ public struct IndexSet : ReferenceConvertible, Equatable, BidirectionalCollectio
}

/// Returns `true` if `self` contains all of the integers in `range`.
public func contains(integersIn range: CountableRange<Element>) -> Bool { return self.contains(integersIn: Range(range)) }
/// Returns `true` if `self` contains all of the integers in `range`.
public func contains(integersIn range: ClosedRange<Element>) -> Bool { return self.contains(integersIn: Range(range)) }
/// Returns `true` if `self` contains all of the integers in `range`.
public func contains(integersIn range: CountableClosedRange<Element>) -> Bool { return self.contains(integersIn: Range(range)) }

public func contains<R: RangeExpression>(integersIn range: R) -> Bool where R.Bound == Element {
return self.contains(integersIn: range.relative(to: 0..<Int.max))
}

/// Returns `true` if `self` contains all of the integers in `indexSet`.
public func contains(integersIn indexSet: IndexSet) -> Bool {
Expand All @@ -400,11 +375,9 @@ public struct IndexSet : ReferenceConvertible, Equatable, BidirectionalCollectio
}

/// Returns `true` if `self` intersects any of the integers in `range`.
public func intersects(integersIn range: CountableRange<Element>) -> Bool { return self.intersects(integersIn: Range(range)) }
/// Returns `true` if `self` intersects any of the integers in `range`.
public func intersects(integersIn range: ClosedRange<Element>) -> Bool { return self.intersects(integersIn: Range(range)) }
/// Returns `true` if `self` intersects any of the integers in `range`.
public func intersects(integersIn range: CountableClosedRange<Element>) -> Bool { return self.intersects(integersIn: Range(range)) }
public func intersects<R: RangeExpression>(integersIn range: R) -> Bool where R.Bound == Element {
return self.intersects(integersIn: range.relative(to: 0..<Int.max))
}

// MARK: -
// Collection
Expand Down Expand Up @@ -490,7 +463,7 @@ public struct IndexSet : ReferenceConvertible, Equatable, BidirectionalCollectio
} else {
let extent = 0..<0
let rangeIndex = 0
return Index(value: value, extent: Range(extent), rangeIndex: rangeIndex, rangeCount: rangeCount)
return Index(value: value, extent: extent, rangeIndex: rangeIndex, rangeCount: rangeCount)
}
}

Expand All @@ -508,11 +481,11 @@ public struct IndexSet : ReferenceConvertible, Equatable, BidirectionalCollectio

var result = IndexSet()
for r in self.rangeView {
result.insert(integersIn: Range(r))
result.insert(integersIn: r)
}

for r in other.rangeView {
result.insert(integersIn: Range(r))
result.insert(integersIn: r)
}
return result
}
Expand Down Expand Up @@ -619,23 +592,19 @@ public struct IndexSet : ReferenceConvertible, Equatable, BidirectionalCollectio
}

/// Insert a range of integers into the `IndexSet`.
public mutating func insert(integersIn range: CountableRange<Element>) { self.insert(integersIn: Range(range)) }
/// Insert a range of integers into the `IndexSet`.
public mutating func insert(integersIn range: ClosedRange<Element>) { self.insert(integersIn: Range(range)) }
/// Insert a range of integers into the `IndexSet`.
public mutating func insert(integersIn range: CountableClosedRange<Element>) { self.insert(integersIn: Range(range)) }
public mutating func insert<R: RangeExpression>(integersIn range: R) where R.Bound == Element {
self.insert(integersIn: range.relative(to: 0..<Int.max))
}

/// Remove a range of integers from the `IndexSet`.
public mutating func remove(integersIn range: Range<Element>) {
_applyMutation { $0.remove(in: _toNSRange(range)) }
}

/// Remove a range of integers from the `IndexSet`.
public mutating func remove(integersIn range: CountableRange<Element>) { self.remove(integersIn: Range(range)) }
/// Remove a range of integers from the `IndexSet`.
public mutating func remove(integersIn range: ClosedRange<Element>) { self.remove(integersIn: Range(range)) }
/// Remove a range of integers from the `IndexSet`.
public mutating func remove(integersIn range: CountableClosedRange<Element>) { self.remove(integersIn: Range(range)) }
public mutating func remove(integersIn range: ClosedRange<Element>) {
self.remove(integersIn: Range(range))
}

/// Returns `true` if self contains no values.
public var isEmpty : Bool {
Expand Down Expand Up @@ -672,17 +641,9 @@ public struct IndexSet : ReferenceConvertible, Equatable, BidirectionalCollectio
///
/// - parameter range: A range of integers. For each integer in the range that intersects the integers in the IndexSet, then the `includeInteger` predicate will be invoked.
/// - parameter includeInteger: The predicate which decides if an integer will be included in the result or not.
public func filteredIndexSet(in range : CountableRange<Element>, includeInteger: (Element) throws -> Bool) rethrows -> IndexSet { return try self.filteredIndexSet(in: Range(range), includeInteger: includeInteger) }
/// Returns an IndexSet filtered according to the result of `includeInteger`.
///
/// - parameter range: A range of integers. For each integer in the range that intersects the integers in the IndexSet, then the `includeInteger` predicate will be invoked.
/// - parameter includeInteger: The predicate which decides if an integer will be included in the result or not.
public func filteredIndexSet(in range : ClosedRange<Element>, includeInteger: (Element) throws -> Bool) rethrows -> IndexSet { return try self.filteredIndexSet(in: Range(range), includeInteger: includeInteger) }
/// Returns an IndexSet filtered according to the result of `includeInteger`.
///
/// - parameter range: A range of integers. For each integer in the range that intersects the integers in the IndexSet, then the `includeInteger` predicate will be invoked.
/// - parameter includeInteger: The predicate which decides if an integer will be included in the result or not.
public func filteredIndexSet(in range : CountableClosedRange<Element>, includeInteger: (Element) throws -> Bool) rethrows -> IndexSet { return try self.filteredIndexSet(in: Range(range), includeInteger: includeInteger) }
public func filteredIndexSet(in range : ClosedRange<Element>, includeInteger: (Element) throws -> Bool) rethrows -> IndexSet {
return try self.filteredIndexSet(in: Range(range), includeInteger: includeInteger)
}

/// Returns an IndexSet filtered according to the result of `includeInteger`.
///
Expand All @@ -692,7 +653,7 @@ public struct IndexSet : ReferenceConvertible, Equatable, BidirectionalCollectio
}

/// For a positive delta, shifts the indexes in [index, INT_MAX] to the right, thereby inserting an "empty space" [index, delta], for a negative delta, shifts the indexes in [index, INT_MAX] to the left, thereby deleting the indexes in the range [index - delta, delta].
public mutating func shift(startingAt integer: Element, by delta: IndexSet.IndexDistance) {
public mutating func shift(startingAt integer: Element, by delta: Int) {
_applyMutation { $0.shiftIndexesStarting(at: integer, by: delta) }
}

Expand Down Expand Up @@ -762,8 +723,8 @@ private struct IndexSetBoundaryIterator : IteratorProtocol {

private var i1: IndexSet.RangeView.Iterator
private var i2: IndexSet.RangeView.Iterator
private var i1Range: CountableRange<Element>?
private var i2Range: CountableRange<Element>?
private var i1Range: Range<Element>?
private var i2Range: Range<Element>?
private var i1UsedLower: Bool
private var i2UsedLower: Bool

Expand Down Expand Up @@ -970,4 +931,3 @@ extension IndexSet : Codable {
}
}
}

0 comments on commit bb8f718

Please sign in to comment.