File: ExperimentalFixedArray.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 (241 lines) | stat: -rw-r--r-- 6,293 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Contains experimental implementations of FixedArray and BufferView

@frozen
public struct FixedArray<T> : ~Copyable {
  @usableFromInline
  let buffer: UnsafeMutablePointer<T>

  public let capacity: Int

  @usableFromInline
  internal var _count: Int

  // Workaround until FixedArray itself can conform to ExpressibleByArrayLiteral
  @frozen
  public struct _Literal: ExpressibleByArrayLiteral {
    @usableFromInline
    let array: [T]

    @_transparent
    @_alwaysEmitIntoClient
    public init(arrayLiteral elements: T...) {
      self.array = elements
    }
  }

  @_transparent
  @_alwaysEmitIntoClient
  public init(capacity: Int) {
    self.capacity = capacity
    self.buffer = _allocateVector(elementType: T.self, capacity: capacity)
    self._count = 0
  }

  @_transparent
  @_alwaysEmitIntoClient
  public init(elements: [T]) {
    let count = elements.count
    self._count = count
    self.capacity = count
    self.buffer = elements.createVector()
  }

  @_transparent
  @_alwaysEmitIntoClient
  public init(repeating repeatedValue: T, count: Int) {
    self.capacity = count
    self.buffer = _allocateVector(elementType: T.self, capacity: capacity)
    self._count = count
    for i in 0..<count {
      (buffer + i).initialize(to: repeatedValue)
    }
  }

  @_alwaysEmitIntoClient
  public var count: Int { _count }

  // TODO: make this a computed property, once rdar://119305513 is fixed
  @_alwaysEmitIntoClient
  public func view() -> BufferView<T> {
    BufferView(baseAddress: buffer, count: count)
  }

  public subscript(position: Int) -> T {
    @_alwaysEmitIntoClient
    get {
      precondition(position >= 0 && position < count)
      return buffer[position]
    }

    @_alwaysEmitIntoClient
    set {
      precondition(position >= 0 && position < count)
      buffer[position] = newValue
    }
  }


  // Will be used once it's possible to let FixedArray conform to Collection.
  public var startIndex: Int { 0 }
  public var endIndex: Int { count }
  public func index(after i: Int) -> Int { return i + 1 }

  @_alwaysEmitIntoClient
  public mutating func append(_ element: T) {
    precondition(count < capacity, "capacity overflow")
    (buffer + count).initialize(to: element)
    _count &+= 1
  }

  @_alwaysEmitIntoClient
  deinit {
    buffer.deinitialize(count: count)
  }
}

// Syntatic sugar for creating a FixedArray literal
prefix operator ^

@_transparent
@_alwaysEmitIntoClient
public prefix func ^<T>(_ literal: FixedArray<T>._Literal) -> FixedArray<T> {
  return FixedArray<T>(elements: literal.array)
}

// Syntatic sugar for getting a BufferView to a FixedArray
prefix operator *

@_transparent
@_alwaysEmitIntoClient
public prefix func *<T>(array: borrowing FixedArray<T>) -> BufferView<T> {
  return array.view()
}

extension Array {
  @_transparent
  @_alwaysEmitIntoClient
  public func createVector() -> UnsafeMutablePointer<Element> {
    let count = self.count
    let vector = _allocateVector(elementType: Element.self, capacity: count)
    copyInto(vectorBuffer: vector)
    return vector
  }

  @_alwaysEmitIntoClient
  @_semantics("array.copy_into_vector")
  internal func copyInto(vectorBuffer: UnsafeMutablePointer<Element>) {
    withUnsafeBufferPointer {
      vectorBuffer.initialize(from: $0.baseAddress!, count: $0.count)
    }
  }
}

@frozen
public struct BufferViewIndex<Element> : Equatable {
  public typealias Pointer = UnsafePointer<Element>

  @usableFromInline
  let _pointer: Pointer

  @_alwaysEmitIntoClient
  public init(pointer: Pointer) {
    _pointer = pointer
  }
}

extension BufferViewIndex : Strideable {
  public typealias Stride = Int

  @_alwaysEmitIntoClient
  public func distance(to other: BufferViewIndex) -> Int {
    _pointer.distance(to: other._pointer)
  }

  @_alwaysEmitIntoClient
  public func advanced(by n: Int) -> BufferViewIndex {
    .init(pointer: _pointer.advanced(by: n))
  }
}

extension BufferViewIndex : Comparable {
  @_alwaysEmitIntoClient
  public static func < (lhs: BufferViewIndex, rhs: BufferViewIndex) -> Bool {
    lhs._pointer < rhs._pointer
  }
}

@frozen
public struct BufferView<Element>: Collection {
  // BufferView is self-slicing. The implementation is omitted for our purpose.
  public typealias SubSequence = BufferView<Element>

  public typealias Index = BufferViewIndex<Element>
  public typealias Pointer = Index.Pointer

  @usableFromInline
  let start: Index

  public let count: Int

  // Initialization is internal because a user-specified count is unsafe.
  @_alwaysEmitIntoClient
  init(start index: Index, count: Int) {
    precondition(count >= 0, "Count must not be negative")
    self.start = index
    self.count = count
  }

  @_alwaysEmitIntoClient
  init(baseAddress: Pointer, count: Int) {
    self.init(start: .init(pointer: baseAddress), count: count)
  }

  // An unsafe public API serves as the low-level entry point for
  // BufferView creation.
  @_alwaysEmitIntoClient
  public static func withTemporaryView<ResultType>(
    unsafeBaseAddress: Pointer, unsafeCount: Int,
    _ body: (borrowing BufferView<Element>) throws -> ResultType
  ) rethrows -> ResultType {
    try body(BufferView<Element>(baseAddress: unsafeBaseAddress,
                                 count: unsafeCount))
  }

  @_alwaysEmitIntoClient
  public var startIndex: Index { start }

  @_alwaysEmitIntoClient
  public var endIndex: Index { start.advanced(by: count) }

  @_alwaysEmitIntoClient
  func _checkBounds(_ position: Index) {
    precondition(startIndex <= position && position < endIndex, "Index out of bounds")
  }

  public subscript(position: Index) -> Element {
    @_alwaysEmitIntoClient
    get {
      _checkBounds(position)
      return self[unchecked: position]
    }
  }

  public subscript(unchecked position: Index) -> Element {
    @_alwaysEmitIntoClient
    get {
      return position._pointer.pointee
    }
  }

  @_alwaysEmitIntoClient
  public subscript(bounds: Range<Index>) -> BufferView<Element> {
    precondition(startIndex <= bounds.lowerBound && bounds.upperBound <= endIndex, "range out of bounds")
    return BufferView(start: bounds.lowerBound, count: bounds.lowerBound.distance(to: bounds.upperBound))
  }

  @_alwaysEmitIntoClient
  public func index(after i: Index) -> Index {
    return i.advanced(by: 1)
  }
}