//===--- Stride.swift.gyb - Components for stride(...) iteration ----------===// // // This source file is part of the Swift.org open source project // // Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See https://swift.org/LICENSE.txt for license information // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors // //===----------------------------------------------------------------------===// // FIXME(ABI)#69 (Overload Resolution): Remove `_Strideable`. // WORKAROUND rdar://25214598 - should be: // protocol Strideable : Comparable {...} % for Self in ['_Strideable', 'Strideable']: %{ Conformance = ( 'Equatable' if Self == '_Strideable' else '_Strideable, Comparable' ) }% /// Conforming types are notionally continuous, one-dimensional /// values that can be offset and measured. public protocol ${Self} : ${Conformance} { /// A type that can represent the distance between two values of `Self`. associatedtype Stride : SignedNumber /// Returns a stride `x` such that `self.advanced(by: x)` approximates /// `other`. /// /// If `Stride` conforms to `Integer`, then `self.advanced(by: x) == other`. /// /// - Complexity: O(1). func distance(to other: Self) -> Stride /// Returns a `Self` `x` such that `self.distance(to: x)` approximates `n`. /// /// If `Stride` conforms to `Integer`, then `self.distance(to: x) == n`. /// /// - Complexity: O(1). func advanced(by n: Stride) -> Self /// `_step` is an implementation detail of Strideable; do not use it directly. static func _step( after current: (index: Int?, value: Self), from start: Self, by distance: Self.Stride ) -> (index: Int?, value: Self) associatedtype _DisabledRangeIndex = _DisabledRangeIndex_ } % end /// Compare two `Strideable`s. public func < (x: T, y: T) -> Bool { return x.distance(to: y) > 0 } public func == (x: T, y: T) -> Bool { return x.distance(to: y) == 0 } public func + (lhs: T, rhs: T.Stride) -> T { return lhs.advanced(by: rhs) } public func + (lhs: T.Stride, rhs: T) -> T { return rhs.advanced(by: lhs) } public func - (lhs: T, rhs: T.Stride) -> T { return lhs.advanced(by: -rhs) } public func - (lhs: T, rhs: T) -> T.Stride { return rhs.distance(to: lhs) } public func += (lhs: inout T, rhs: T.Stride) { lhs = lhs.advanced(by: rhs) } public func -= (lhs: inout T, rhs: T.Stride) { lhs = lhs.advanced(by: -rhs) } //===--- Deliberately-ambiguous operators for UnsignedIntegerTypes --------===// // The UnsignedIntegerTypes all have a signed Stride type. Without these // // overloads, expressions such as UInt(2) + Int(3) would compile. // //===----------------------------------------------------------------------===// public func + ( lhs: T, rhs: T._DisallowMixedSignArithmetic ) -> T { _sanityCheckFailure("Should not be callable.") } public func + ( lhs: T._DisallowMixedSignArithmetic, rhs: T ) -> T { _sanityCheckFailure("Should not be callable.") } public func - ( lhs: T, rhs: T._DisallowMixedSignArithmetic ) -> T { _sanityCheckFailure("Should not be callable.") } public func - ( lhs: T, rhs: T ) -> T._DisallowMixedSignArithmetic { _sanityCheckFailure("Should not be callable.") } public func += ( lhs: inout T, rhs: T._DisallowMixedSignArithmetic ) { _sanityCheckFailure("Should not be callable.") } public func -= ( lhs: inout T, rhs: T._DisallowMixedSignArithmetic ) { _sanityCheckFailure("Should not be callable.") } //===----------------------------------------------------------------------===// extension Strideable { public static func _step( after current: (index: Int?, value: Self), from start: Self, by distance: Self.Stride ) -> (index: Int?, value: Self) { return (nil, current.value + distance) } } extension Strideable where Stride : FloatingPoint { public static func _step( after current: (index: Int?, value: Self), from start: Self, by distance: Self.Stride ) -> (index: Int?, value: Self) { if let i = current.index { return (i + 1, start + Stride(i + 1) * distance) } // If current.index == nil, either we're just starting out (in which case // the next index is 1), or we should proceed without an index just as // though this floating point specialization doesn't exist. return (current.value == start ? 1 : nil, current.value + distance) } } /// An iterator for `StrideTo`. public struct StrideToIterator : IteratorProtocol { internal let _start: Element internal let _end: Element internal let _stride: Element.Stride internal var _current: (index: Int?, value: Element) internal init(_start: Element, end: Element, stride: Element.Stride) { self._start = _start _end = end _stride = stride _current = (nil, _start) } /// Advances to the next element and returns it, or `nil` if no next element /// exists. /// /// Once `nil` has been returned, all subsequent calls return `nil`. public mutating func next() -> Element? { let result = _current.value if _stride > 0 ? result >= _end : result <= _end { return nil } _current = Element._step(after: _current, from: _start, by: _stride) return result } } /// A `Sequence` of values formed by striding over a half-open interval. public struct StrideTo : Sequence, CustomReflectable { // FIXME: should really be a Collection, as it is multipass /// Returns an iterator over the elements of this sequence. /// /// - Complexity: O(1). public func makeIterator() -> StrideToIterator { return StrideToIterator(_start: _start, end: _end, stride: _stride) } internal init(_start: Element, end: Element, stride: Element.Stride) { _precondition(stride != 0, "stride size must not be zero") // At start, striding away from end is allowed; it just makes for an // already-empty Sequence. self._start = _start self._end = end self._stride = stride } internal let _start: Element internal let _end: Element internal let _stride: Element.Stride public var customMirror: Mirror { return Mirror(self, children: ["from": _start, "to": _end, "by": _stride]) } } /// Returns the sequence of values (`self`, `self + stride`, `self + /// 2 * stride`, ... *last*) where *last* is the last value in the /// progression that is less than `end`. public func stride( from start: T, to end: T, by stride: T.Stride ) -> StrideTo { return StrideTo(_start: start, end: end, stride: stride) } /// An iterator for `StrideThrough`. public struct StrideThroughIterator : IteratorProtocol { internal let _start: Element internal let _end: Element internal let _stride: Element.Stride internal var _current: (index: Int?, value: Element) internal var _didReturnEnd: Bool = false internal init(_start: Element, end: Element, stride: Element.Stride) { self._start = _start _end = end _stride = stride _current = (nil, _start) } /// Advances to the next element and returns it, or `nil` if no next element /// exists. /// /// Once `nil` has been returned, all subsequent calls return `nil`. public mutating func next() -> Element? { let result = _current.value if _stride > 0 ? result >= _end : result <= _end { // This check is needed because if we just changed the above operators // to > and <, respectively, we might advance current past the end // and throw it out of bounds (e.g. above Int.max) unnecessarily. if result == _end && !_didReturnEnd { _didReturnEnd = true return result } return nil } _current = Element._step(after: _current, from: _start, by: _stride) return result } } /// A `Sequence` of values formed by striding over a closed interval. public struct StrideThrough< Element : Strideable > : Sequence, CustomReflectable { // FIXME: should really be a CollectionType, as it is multipass /// Returns an iterator over the elements of this sequence. /// /// - Complexity: O(1). public func makeIterator() -> StrideThroughIterator { return StrideThroughIterator(_start: _start, end: _end, stride: _stride) } internal init(_start: Element, end: Element, stride: Element.Stride) { _precondition(stride != 0, "stride size must not be zero") self._start = _start self._end = end self._stride = stride } internal let _start: Element internal let _end: Element internal let _stride: Element.Stride public var customMirror: Mirror { return Mirror(self, children: ["from": _start, "through": _end, "by": _stride]) } } /// Returns the sequence of values (`self`, `self + stride`, `self + /// 2 * stride`, ... *last*) where *last* is the last value in the /// progression less than or equal to `end`. /// /// - Note: There is no guarantee that `end` is an element of the sequence. public func stride( from start: T, through end: T, by stride: T.Stride ) -> StrideThrough { return StrideThrough(_start: start, end: end, stride: stride) } @available(*, unavailable, renamed: "StrideToIterator") public struct StrideToGenerator {} @available(*, unavailable, renamed: "StrideThroughIterator") public struct StrideThroughGenerator {} extension Strideable { @available(*, unavailable, message: "Use stride(from:to:by:) free function instead") public func stride(to end: Self, by stride: Stride) -> StrideTo { Builtin.unreachable() } @warn_unqualified_access @available(*, unavailable, message: "Use stride(from:through:by:) free function instead") public func stride( through end: Self, by stride: Stride ) -> StrideThrough { Builtin.unreachable() } }