File: 0006-rdar20588474.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (157 lines) | stat: -rw-r--r-- 4,260 bytes parent folder | download
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// RUN: not %target-swift-frontend %s -typecheck

public protocol Q_SequenceDefaults {
  typealias Element
  typealias Iterator : IteratorProtocol
  func makeIterator() -> Iterator
}

extension Q_SequenceDefaults {
  public final var underestimatedCount: Int { return 0 }
  public final func preprocessingPass<R>(body: (Self)->R) -> R? {
    return nil
  }

  /// Create a ContiguousArray containing the elements of `self`,
  /// in the same order.
  public final func copyToContiguousArray() -> ContiguousArray<Iterator.Element> {
    let initialCapacity = underestimatedCount

    var result = _ContiguousArrayBuffer<Iterator.Element>(
      count: initialCapacity, minimumCapacity: 0)

    var iter = self.makeIterator()
    while let x = iter.next() {
      result += CollectionOfOne(x)
    }
    return ContiguousArray(result)
  }

  /// Initialize the storage at baseAddress with the contents of this
  /// sequence.
  public final func initializeRawMemory(
    baseAddress: UnsafeMutablePointer<Iterator.Element>
  ) {
    var p = baseAddress
    var iter = self.makeIterator()
    while let element = iter.next() {
      p.initialize(to: element)
      p += 1
    }
  }

  public final static func _constrainElement(Iterator.Element) {}
}

/// A type that can be iterated with a `for`\ ...\ `in` loop.
///
/// `Sequence` makes no requirement on conforming types regarding
/// whether they will be destructively "consumed" by iteration.  To
/// ensure non-destructive iteration, constrain your *sequence* to
/// `Collection`.
public protocol Q_Sequence : Q_SequenceDefaults {
  /// A type that provides the *sequence*\ 's iteration interface and
  /// encapsulates its iteration state.
  typealias Iterator : IteratorProtocol

  func makeIterator() -> Iterator

  /// Return a value less than or equal to the number of elements in
  /// self, **nondestructively**.
  ///
  /// Complexity: O(N)
  var underestimatedCount: Int

  /// If `self` is multi-pass (i.e., a `Collection`), invoke the function
  /// on `self` and return its result.  Otherwise, return `nil`.
  func preprocessingPass<R>(body: (Self)->R) -> R?

  /// Create a ContiguousArray containing the elements of `self`,
  /// in the same order.
  func copyToContiguousArray() -> ContiguousArray<Element>

  /// Initialize the storage at baseAddress with the contents of this
  /// sequence.
  func initializeRawMemory(
    baseAddress: UnsafeMutablePointer<Element>
  )
  
  static func _constrainElement(Element)
}

public extension IteratorProtocol {
  // typealias Iterator = Self
  
  public final func makeIterator() -> Self {
    return self
  }
}

public protocol Q_Indexable {
  typealias Index : ForwardIndex
  typealias Element
  subscript(position: Index) -> Element {get}
  var startIndex: Index {get}
  var endIndex: Index {get}
}

public protocol Q_CollectionDefaults : Q_Sequence, Q_Indexable {
}

extension Q_CollectionDefaults {
  public final func count() -> Index.Distance {
    return distance(startIndex, endIndex)
  }
  
  public final var underestimatedCount: Int {
    let n = count().toIntMax()
    return n > IntMax(Int.max) ? Int.max : Int(n)
  }
  
  public final func preprocessingPass<R>(body: (Self)->R) -> R? {
    return body(self)
  }

  public final func makeIterator() -> Q_IndexingIterator<Boo> {
    return Q_IndexingIterator(pos: self.startIndex, elements: self)
  }
}

public struct Q_IndexingIterator<C: Q_Indexable> : IteratorProtocol {
  public typealias Element = C.Element
  var pos: C.Index
  let elements: C
  
  public mutating func next() -> C.Element? {
    if pos == elements.endIndex {
      return nil
    }
    let ret = elements[pos]
    pos += 1
    return ret
  }
}

public protocol Q_Collection : Q_CollectionDefaults {
  func count() -> Index.Distance
  subscript(position: Index) -> Element {get}
}

extension Array : Q_Collection {
  public func copyToContiguousArray() -> ContiguousArray<Element> {
    return ContiguousArray(self~>_copyToNativeArrayBuffer())
  }
}

struct Boo : Q_Collection {
  let startIndex: Int = 0
  let endIndex: Int = 10
  
  subscript(i: Int) -> String {
    return "Boo"
  }

  func makeIterator() -> Q_IndexingIterator<Boo> {
    return Q_IndexingIterator(pos: self.startIndex, elements: self)
  }
}