forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollectionOfOne.swift
89 lines (76 loc) · 2.65 KB
/
CollectionOfOne.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//===--- CollectionOfOne.swift - A CollectionType with one element --------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 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
//
//===----------------------------------------------------------------------===//
/// A generator that produces one or fewer instances of `Element`.
public struct GeneratorOfOne<Element> : GeneratorType, SequenceType {
@available(*, unavailable, renamed="Element")
public typealias T = Element
/// Construct an instance that generates `element!`, or an empty
/// sequence if `element == nil`.
public init(_ element: Element?) {
self.elements = element
}
/// Advance to the next element and return it, or `nil` if no next
/// element exists.
///
/// - Requires: `next()` has not been applied to a copy of `self`
/// since the copy was made, and no preceding call to `self.next()`
/// has returned `nil`.
public mutating func next() -> Element? {
let result = elements
elements = .None
return result
}
var elements: Element?
}
/// A collection containing a single element of type `Element`.
public struct CollectionOfOne<Element> : CollectionType {
@available(*, unavailable, renamed="Element")
public typealias T = Element
/// A type that represents a valid position in the collection.
///
/// Valid indices consist of the position of every element and a
/// "past the end" position that's not valid for use as a subscript.
public typealias Index = Bit
/// Construct an instance containing just `element`.
public init(_ element: Element) {
self.element = element
}
/// The position of the first element.
public var startIndex: Index {
return .Zero
}
/// The "past the end" position; always identical to
/// `startIndex.successor()`.
///
/// - Note: `endIndex` is not a valid argument to `subscript`.
public var endIndex: Index {
return .One
}
/// Return a *generator* over the elements of this *sequence*.
///
/// - Complexity: O(1).
public func generate() -> GeneratorOfOne<Element> {
return GeneratorOfOne(element)
}
/// Access the element at `position`.
///
/// - Requires: `position == .Zero`.
public subscript(position: Index) -> Element {
_precondition(position == .Zero, "Index out of range")
return element
}
/// Return the number of elements (always one).
public var count: Int {
return 1
}
let element: Element
}