File: UnsafeArray.swift

package info (click to toggle)
swiftlang 6.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,856,264 kB
  • sloc: cpp: 9,995,718; ansic: 2,234,019; asm: 1,092,167; python: 313,940; objc: 82,726; f90: 80,126; lisp: 38,373; pascal: 25,580; sh: 20,378; ml: 5,058; perl: 4,751; makefile: 4,725; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (91 lines) | stat: -rw-r--r-- 3,085 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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 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
//
//===----------------------------------------------------------------------===//

import Foundation

/// A manually allocated and deallocated array with automatic growth on insert.
///
/// - Warning: this type is Unsafe. Writing to an allocated instance must be exclusive to one client.
///   Multiple readers are OK, as long as deallocation is coordinated. Writing must be exclusive because
///   appends can cause realloc, leaving dangling pointers in the copies held by other clients. Appends
///   also would not update the counts in other clients.
internal struct UnsafeArray<Element> {
  private(set) var count = 0
  private(set) var capacity: Int
  private(set) var elements: UnsafeMutablePointer<Element>

  private init(elements: UnsafeMutablePointer<Element>, capacity: Int) {
    self.capacity = capacity
    self.elements = elements
  }

  /// Must be deallocated with `deallocate()`. Will grow beyond `initialCapacity` as elements are added.
  static func allocate(initialCapacity: Int) -> Self {
    Self(elements: UnsafeMutablePointer.allocate(capacity: initialCapacity), capacity: initialCapacity)
  }

  mutating func deallocate() {
    elements.deinitialize(count: count)
    elements.deallocate()
    count = 0
    capacity = 0
  }

  /// Must be deallocated with `deallocate()`.
  func allocateCopy(preservingCapacity: Bool) -> Self {
    var copy = UnsafeArray.allocate(initialCapacity: preservingCapacity ? capacity : count)
    copy.elements.initialize(from: elements, count: count)
    copy.count = count
    return copy
  }

  private mutating func resize(newCapacity: Int) {
    assert(newCapacity >= count)
    elements.resize(fromCount: count, toCount: newCapacity)
    capacity = newCapacity
  }

  mutating func reserve(minimumAdditionalCapacity: Int) {
    let availableAdditionalCapacity = (capacity - count)
    if availableAdditionalCapacity < minimumAdditionalCapacity {
      resize(newCapacity: max(capacity * 2, capacity + minimumAdditionalCapacity))
    }
  }

  mutating func append(_ element: Element) {
    reserve(minimumAdditionalCapacity: 1)
    elements[count] = element
    count += 1
  }

  mutating func append(contentsOf collection: some Collection<Element>) {
    reserve(minimumAdditionalCapacity: collection.count)
    elements.advanced(by: count).initialize(from: collection)
    count += collection.count
  }

  private func assertBounds(_ index: Int) {
    assert(index >= 0)
    assert(index < count)
  }

  subscript(_ index: Int) -> Element {
    get {
      assertBounds(index)
      return elements[index]
    }
    set {
      assertBounds(index)
      elements[index] = newValue
    }
  }
}