Skip to content

Commit

Permalink
Replace Index with Int where possible (apple#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
Frizlab authored May 10, 2021
1 parent b91621e commit f6d8f82
Show file tree
Hide file tree
Showing 14 changed files with 109 additions and 109 deletions.
2 changes: 1 addition & 1 deletion Sources/DequeModule/Deque+MutableCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extension Deque: MutableCollection {
/// - Complexity: O(1) when this instance has a unique reference to its
/// underlying storage; O(`count`) otherwise.
@inlinable
public mutating func swapAt(_ i: Index, _ j: Index) {
public mutating func swapAt(_ i: Int, _ j: Int) {
precondition(i >= 0 && i < count, "Index out of bounds")
precondition(j >= 0 && j < count, "Index out of bounds")
_storage.ensureUnique()
Expand Down
20 changes: 10 additions & 10 deletions Sources/DequeModule/Deque+RandomAccessCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extension Deque: RandomAccessCollection {
/// - Complexity: O(1)
@inlinable
@inline(__always)
public var startIndex: Index { 0 }
public var startIndex: Int { 0 }

/// The deque’s “past the end” position—that is, the position one greater than
/// the last valid subscript argument.
Expand All @@ -39,7 +39,7 @@ extension Deque: RandomAccessCollection {
/// - Complexity: O(1)
@inlinable
@inline(__always)
public var endIndex: Index { count }
public var endIndex: Int { count }

/// Returns the position immediately after the given index.
///
Expand All @@ -51,7 +51,7 @@ extension Deque: RandomAccessCollection {
/// - Complexity: O(1)
@inlinable
@inline(__always)
public func index(after i: Index) -> Index {
public func index(after i: Int) -> Int {
// Note: Like `Array`, index manipulation methods on deques don't trap on
// invalid indices. (Indices are still validated on element access.)
return i + 1
Expand All @@ -65,7 +65,7 @@ extension Deque: RandomAccessCollection {
/// - Complexity: O(1)
@inlinable
@inline(__always)
public func formIndex(after i: inout Index) {
public func formIndex(after i: inout Int) {
// Note: Like `Array`, index manipulation methods on deques
// don't trap on invalid indices.
// (Indices are still validated on element access.)
Expand All @@ -82,7 +82,7 @@ extension Deque: RandomAccessCollection {
/// - Complexity: O(1)
@inlinable
@inline(__always)
public func index(before i: Index) -> Index {
public func index(before i: Int) -> Int {
// Note: Like `Array`, index manipulation methods on deques don't trap on
// invalid indices. (Indices are still validated on element access.)
return i - 1
Expand All @@ -95,7 +95,7 @@ extension Deque: RandomAccessCollection {
/// - Complexity: O(1)
@inlinable
@inline(__always)
public func formIndex(before i: inout Index) {
public func formIndex(before i: inout Int) {
// Note: Like `Array`, index manipulation methods on deques don't trap on
// invalid indices. (Indices are still validated on element access.)
i -= 1
Expand All @@ -118,7 +118,7 @@ extension Deque: RandomAccessCollection {
/// - Complexity: O(1)
@inlinable
@inline(__always)
public func index(_ i: Index, offsetBy distance: Int) -> Index {
public func index(_ i: Int, offsetBy distance: Int) -> Int {
// Note: Like `Array`, index manipulation methods on deques don't trap on
// invalid indices. (Indices are still validated on element access.)
return i + distance
Expand Down Expand Up @@ -170,7 +170,7 @@ extension Deque: RandomAccessCollection {
/// - Complexity: O(1)
@inlinable
@inline(__always)
public func distance(from start: Index, to end: Index) -> Int {
public func distance(from start: Int, to end: Int) -> Int {
// Note: Like `Array`, index manipulation method on deques
// don't trap on invalid indices.
// (Indices are still validated on element access.)
Expand All @@ -187,7 +187,7 @@ extension Deque: RandomAccessCollection {
/// unless the deque’s storage is shared with another deque, in which case
/// writing is O(`count`).
@inlinable
public subscript(index: Index) -> Element {
public subscript(index: Int) -> Element {
get {
precondition(index >= 0 && index < count, "Index out of bounds")
return _storage.read { $0.ptr(at: $0.slot(forOffset: index)).pointee }
Expand Down Expand Up @@ -228,7 +228,7 @@ extension Deque: RandomAccessCollection {
/// The accessed slice uses the same indices for the same elements as the
/// original collection.
@inlinable
public subscript(bounds: Range<Index>) -> Slice<Self> {
public subscript(bounds: Range<Int>) -> Slice<Self> {
get {
precondition(bounds.lowerBound >= 0 && bounds.upperBound <= count,
"Invalid bounds")
Expand Down
10 changes: 5 additions & 5 deletions Sources/DequeModule/Deque+RangeReplaceableCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ extension Deque: RangeReplaceableCollection {
/// `subrange`.
@inlinable
public mutating func replaceSubrange<C: Collection>(
_ subrange: Range<Index>,
_ subrange: Range<Int>,
with newElements: __owned C
) where C.Element == Element {
precondition(subrange.lowerBound >= 0 && subrange.upperBound <= count, "Index range out of bounds")
Expand Down Expand Up @@ -272,7 +272,7 @@ extension Deque: RangeReplaceableCollection {
/// elements that need to be moved. When inserting at the start or the end,
/// this reduces the complexity to amortized O(1).
@inlinable
public mutating func insert(_ newElement: Element, at index: Index) {
public mutating func insert(_ newElement: Element, at index: Int) {
precondition(index >= 0 && index <= count,
"Can't insert element at invalid index")
_storage.ensureUnique(minimumCapacity: count + 1)
Expand Down Expand Up @@ -310,7 +310,7 @@ extension Deque: RangeReplaceableCollection {
/// amortized O(1).
@inlinable
public mutating func insert<C: Collection>(
contentsOf newElements: __owned C, at index: Index
contentsOf newElements: __owned C, at index: Int
) where C.Element == Element {
precondition(index >= 0 && index <= count,
"Can't insert elements at an invalid index")
Expand Down Expand Up @@ -338,7 +338,7 @@ extension Deque: RangeReplaceableCollection {
/// deque costs O(1) if the deque's storage isn't shared.
@inlinable
@discardableResult
public mutating func remove(at index: Index) -> Element {
public mutating func remove(at index: Int) -> Element {
precondition(index >= 0 && index < self.count, "Index out of bounds")
// FIXME: Implement storage shrinking
_storage.ensureUnique()
Expand All @@ -364,7 +364,7 @@ extension Deque: RangeReplaceableCollection {
/// - Complexity: O(`count`). Removing elements from the start or end of the
/// deque costs O(`bounds.count`) if the deque's storage isn't shared.
@inlinable
public mutating func removeSubrange(_ bounds: Range<Index>) {
public mutating func removeSubrange(_ bounds: Range<Int>) {
precondition(bounds.lowerBound >= 0 && bounds.upperBound <= self.count,
"Index range out of bounds")
_storage.ensureUnique()
Expand Down
2 changes: 1 addition & 1 deletion Sources/DequeModule/Deque+Sequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ extension Deque: Sequence {
}

@inlinable
internal init(_base: Deque, from index: Index) {
internal init(_base: Deque, from index: Int) {
assert(index <= _base.count)
self = _base._storage.read { handle in
let start = handle.slot(forOffset: index)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ extension OrderedDictionary.Elements: RandomAccessCollection {
/// - Complexity: O(1)
@inlinable
@inline(__always)
public func index(after i: Index) -> Index { i + 1 }
public func index(after i: Int) -> Int { i + 1 }

/// Returns the position immediately before the given index.
///
Expand All @@ -168,7 +168,7 @@ extension OrderedDictionary.Elements: RandomAccessCollection {
/// - Complexity: O(1)
@inlinable
@inline(__always)
public func index(before i: Index) -> Index { i - 1 }
public func index(before i: Int) -> Int { i - 1 }

/// Replaces the given index with its successor.
///
Expand All @@ -180,7 +180,7 @@ extension OrderedDictionary.Elements: RandomAccessCollection {
/// - Complexity: O(1)
@inlinable
@inline(__always)
public func formIndex(after i: inout Index) { i += 1 }
public func formIndex(after i: inout Int) { i += 1 }

/// Replaces the given index with its predecessor.
///
Expand All @@ -192,7 +192,7 @@ extension OrderedDictionary.Elements: RandomAccessCollection {
/// - Complexity: O(1)
@inlinable
@inline(__always)
public func formIndex(before i: inout Index) { i -= 1 }
public func formIndex(before i: inout Int) { i -= 1 }

/// Returns an index that is the specified distance from the given index.
///
Expand All @@ -211,7 +211,7 @@ extension OrderedDictionary.Elements: RandomAccessCollection {
/// - Complexity: O(1)
@inlinable
@inline(__always)
public func index(_ i: Index, offsetBy distance: Int) -> Index {
public func index(_ i: Int, offsetBy distance: Int) -> Int {
i + distance
}

Expand All @@ -238,10 +238,10 @@ extension OrderedDictionary.Elements: RandomAccessCollection {
@inlinable
@inline(__always)
public func index(
_ i: Index,
_ i: Int,
offsetBy distance: Int,
limitedBy limit: Index
) -> Index? {
limitedBy limit: Int
) -> Int? {
_base._values.index(i, offsetBy: distance, limitedBy: limit)
}

Expand All @@ -257,7 +257,7 @@ extension OrderedDictionary.Elements: RandomAccessCollection {
/// - Complexity: O(1)
@inlinable
@inline(__always)
public func distance(from start: Index, to end: Index) -> Int {
public func distance(from start: Int, to end: Int) -> Int {
end - start
}

Expand All @@ -270,7 +270,7 @@ extension OrderedDictionary.Elements: RandomAccessCollection {
/// - Complexity: O(1)
@inlinable
@inline(__always)
public subscript(position: Index) -> Element {
public subscript(position: Int) -> Element {
(_base._keys[position], _base._values[position])
}

Expand Down Expand Up @@ -313,13 +313,13 @@ extension OrderedDictionary.Elements: RandomAccessCollection {

@inlinable
@inline(__always)
public func _failEarlyRangeCheck(_ index: Index, bounds: ClosedRange<Index>) {
public func _failEarlyRangeCheck(_ index: Int, bounds: ClosedRange<Int>) {
_base._values._failEarlyRangeCheck(index, bounds: bounds)
}

@inlinable
@inline(__always)
public func _failEarlyRangeCheck(_ range: Range<Index>, bounds: Range<Index>) {
public func _failEarlyRangeCheck(_ range: Range<Int>, bounds: Range<Int>) {
_base._values._failEarlyRangeCheck(range, bounds: bounds)
}
}
Expand Down Expand Up @@ -373,7 +373,7 @@ extension OrderedDictionary.Elements {
/// value; O(`count`) otherwise.
@inlinable
@inline(__always)
public mutating func swapAt(_ i: Index, _ j: Index) {
public mutating func swapAt(_ i: Int, _ j: Int) {
_base.swapAt(i, j)
}

Expand All @@ -398,7 +398,7 @@ extension OrderedDictionary.Elements {
@inline(__always)
public mutating func partition(
by belongsInSecondPartition: (Element) throws -> Bool
) rethrows -> Index {
) rethrows -> Int {
try _base.partition(by: belongsInSecondPartition)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extension OrderedDictionary {
/// - Complexity: O(1) when the dictionary's storage isn't shared with another
/// value; O(`count`) otherwise.
@inlinable
public mutating func swapAt(_ i: Index, _ j: Index) {
public mutating func swapAt(_ i: Int, _ j: Int) {
_keys.swapAt(i, j)
_values.swapAt(i, j)
}
Expand All @@ -47,7 +47,7 @@ extension OrderedDictionary {
@inlinable
public mutating func partition(
by belongsInSecondPartition: (Element) throws -> Bool
) rethrows -> Index {
) rethrows -> Int {
let pivot = try _values.withUnsafeMutableBufferPointer { values in
try _keys._partition(values: values, by: belongsInSecondPartition)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ extension OrderedDictionary.Values: RandomAccessCollection {
/// - Complexity: O(1)
@inlinable
@inline(__always)
public func index(after i: Index) -> Index { i + 1 }
public func index(after i: Int) -> Int { i + 1 }

/// Returns the position immediately before the given index.
///
Expand All @@ -157,7 +157,7 @@ extension OrderedDictionary.Values: RandomAccessCollection {
/// - Complexity: O(1)
@inlinable
@inline(__always)
public func index(before i: Index) -> Index { i - 1 }
public func index(before i: Int) -> Int { i - 1 }

/// Replaces the given index with its successor.
///
Expand All @@ -169,7 +169,7 @@ extension OrderedDictionary.Values: RandomAccessCollection {
/// - Complexity: O(1)
@inlinable
@inline(__always)
public func formIndex(after i: inout Index) { i += 1 }
public func formIndex(after i: inout Int) { i += 1 }

/// Replaces the given index with its predecessor.
///
Expand All @@ -181,7 +181,7 @@ extension OrderedDictionary.Values: RandomAccessCollection {
/// - Complexity: O(1)
@inlinable
@inline(__always)
public func formIndex(before i: inout Index) { i -= 1 }
public func formIndex(before i: inout Int) { i -= 1 }

/// Returns an index that is the specified distance from the given index.
///
Expand All @@ -200,7 +200,7 @@ extension OrderedDictionary.Values: RandomAccessCollection {
/// - Complexity: O(1)
@inlinable
@inline(__always)
public func index(_ i: Index, offsetBy distance: Int) -> Index {
public func index(_ i: Int, offsetBy distance: Int) -> Int {
i + distance
}

Expand All @@ -227,10 +227,10 @@ extension OrderedDictionary.Values: RandomAccessCollection {
@inlinable
@inline(__always)
public func index(
_ i: Index,
_ i: Int,
offsetBy distance: Int,
limitedBy limit: Index
) -> Index? {
limitedBy limit: Int
) -> Int? {
_base._values.index(i, offsetBy: distance, limitedBy: limit)
}

Expand All @@ -246,7 +246,7 @@ extension OrderedDictionary.Values: RandomAccessCollection {
/// - Complexity: O(1)
@inlinable
@inline(__always)
public func distance(from start: Index, to end: Index) -> Int {
public func distance(from start: Int, to end: Int) -> Int {
end - start
}

Expand Down Expand Up @@ -279,7 +279,7 @@ extension OrderedDictionary.Values: MutableCollection {
/// - Complexity: O(1)
@inlinable
@inline(__always)
public subscript(position: Index) -> Value {
public subscript(position: Int) -> Value {
get {
_base._values[position]
}
Expand Down
Loading

0 comments on commit f6d8f82

Please sign in to comment.