Files
swift-mirror/stdlib/public/core/Indices.swift.gyb
Dave Abrahams 11259d1d14 [stdlib] indexing model: rename in-place indexing
This time, choose something that's at least compliant with the API
guidelines.
2016-03-23 17:16:20 -07:00

126 lines
3.5 KiB
Plaintext

//===----------------------------------------------------------------------===//
//
// 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 http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
%{
from gyb_stdlib_support import TRAVERSALS, collectionForTraversal
def defaultIndicesForTraversal(traversal):
if traversal == 'Forward':
return 'DefaultIndices'
if traversal == 'Bidirectional':
return 'DefaultBidirectionalIndices'
if traversal == 'RandomAccess':
return 'DefaultRandomAccessIndices'
assert False, 'unknown traversal'
}%
% for Traversal in TRAVERSALS:
% Self = defaultIndicesForTraversal(Traversal)
// FIXME(ABI)(compiler limitation): There should be just one default
// indices type that has conditional conformances to
// `BidirectionalCollection` and `RandomAccessCollection`.
public struct ${Self}<
Elements : ${collectionForTraversal(Traversal).replace('Collection', 'Indexable')}
// FIXME(ABI)(compiler limitation):
// Elements : Collection
> : ${collectionForTraversal(Traversal)} {
// FIXME(compiler limitation): this typealias should be inferred.
public typealias Index = Elements.Index
internal init(
_elements: Elements,
startIndex: Elements.Index,
endIndex: Elements.Index
) {
self._elements = _elements
self._startIndex = startIndex
self._endIndex = endIndex
}
public var startIndex: Elements.Index {
return _startIndex
}
public var endIndex: Elements.Index {
return _endIndex
}
public subscript(i: Index) -> Elements.Index {
// FIXME: swift-3-indexing-model: range check.
return i
}
// FIXME(compiler limitation): this typealias should be inferred.
public typealias SubSequence = ${Self}<Elements>
public subscript(bounds: Range<Index>) -> ${Self}<Elements> {
// FIXME: swift-3-indexing-model: range check.
return ${Self}(
_elements: _elements,
startIndex: bounds.lowerBound,
endIndex: bounds.upperBound)
}
@warn_unused_result
public func successor(of i: Index) -> Index {
// FIXME: swift-3-indexing-model: range check.
return _elements.successor(of: i)
}
public func formSuccessor(i: inout Index) {
// FIXME: swift-3-indexing-model: range check.
_elements.formSuccessor(&i)
}
% if Traversal in ['Bidirectional', 'RandomAccess']:
@warn_unused_result
public func predecessor(of i: Index) -> Index {
// FIXME: swift-3-indexing-model: range check.
return _elements.predecessor(of: i)
}
public func formPredecessor(i: inout Index) {
// FIXME: swift-3-indexing-model: range check.
_elements.formPredecessor(&i)
}
% end
// FIXME(compiler limitation): this typealias should be inferred.
public typealias Indices = ${Self}<Elements>
public var indices: Indices {
return self
}
internal var _elements: Elements
internal var _startIndex: Elements.Index
internal var _endIndex: Elements.Index
}
extension ${collectionForTraversal(Traversal)} where Indices == ${Self}<Self> {
public var indices: ${Self}<Self> {
return ${Self}(
_elements: self,
startIndex: self.startIndex,
endIndex: self.endIndex)
}
}
% end
// ${'Local Variables'}:
// eval: (read-only-mode 1)
// End: