|
| 1 | +//===--- StackList.swift - defines the StackList data structure -----------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import SILBridging |
| 14 | + |
| 15 | +/// A very efficient implementation of a stack, which can also be iterated over. |
| 16 | +/// |
| 17 | +/// A StackList is the best choice for things like worklists, etc., if no random |
| 18 | +/// access is needed. |
| 19 | +/// Compared to Array, it does not require any memory allocations, because it |
| 20 | +/// uses the bump pointer allocator of the SILModule. |
| 21 | +/// All operations have (almost) zero cost. |
| 22 | +/// |
| 23 | +/// Ideally this would be a move-only type. Until then, only pass StackLists as |
| 24 | +/// inout! |
| 25 | +/// Note: it is required to manually remove all elements - either by pop() or |
| 26 | +/// removeAll(). |
| 27 | +public struct StackList<Element> : Sequence, CustomReflectable { |
| 28 | + |
| 29 | + private let context: BridgedPassContext |
| 30 | + private var firstSlab = BridgedSlab(data: nil) |
| 31 | + private var lastSlab = BridgedSlab(data: nil) |
| 32 | + private var endIndex: Int = slabCapacity |
| 33 | + |
| 34 | + private static var slabCapacity: Int { |
| 35 | + BridgedSlabCapacity / MemoryLayout<Element>.size |
| 36 | + } |
| 37 | + |
| 38 | + private static func bind(_ slab: BridgedSlab) -> UnsafeMutablePointer<Element> { |
| 39 | + return slab.data!.bindMemory(to: Element.self, capacity: StackList.slabCapacity) |
| 40 | + } |
| 41 | + |
| 42 | + public struct Iterator : IteratorProtocol { |
| 43 | + var slab: BridgedSlab |
| 44 | + var index: Int |
| 45 | + let lastSlab: BridgedSlab |
| 46 | + let endIndex: Int |
| 47 | + |
| 48 | + public mutating func next() -> Element? { |
| 49 | + let end = (slab.data == lastSlab.data ? endIndex : slabCapacity) |
| 50 | + if index < end { |
| 51 | + let elem = StackList.bind(slab)[index] |
| 52 | + index += 1 |
| 53 | + |
| 54 | + if index >= end && slab.data != lastSlab.data { |
| 55 | + slab = PassContext_getNextSlab(slab) |
| 56 | + index = 0 |
| 57 | + } |
| 58 | + return elem |
| 59 | + } |
| 60 | + return nil |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public init(context: BridgedPassContext) { self.context = context } |
| 65 | + |
| 66 | + public func makeIterator() -> Iterator { |
| 67 | + return Iterator(slab: firstSlab, index: 0, lastSlab: lastSlab, endIndex: endIndex) |
| 68 | + } |
| 69 | + |
| 70 | + public var first: Element? { |
| 71 | + if isEmpty { |
| 72 | + return nil |
| 73 | + } |
| 74 | + return StackList.bind(firstSlab)[0] |
| 75 | + } |
| 76 | + |
| 77 | + public var last: Element? { |
| 78 | + if isEmpty { |
| 79 | + return nil |
| 80 | + } |
| 81 | + return StackList.bind(lastSlab)[endIndex - 1] |
| 82 | + } |
| 83 | + |
| 84 | + public mutating func push(_ element: Element) { |
| 85 | + if endIndex >= StackList.slabCapacity { |
| 86 | + lastSlab = PassContext_allocSlab(context, lastSlab) |
| 87 | + if firstSlab.data == nil { |
| 88 | + firstSlab = lastSlab |
| 89 | + } |
| 90 | + endIndex = 0 |
| 91 | + } |
| 92 | + (StackList.bind(lastSlab) + endIndex).initialize(to: element) |
| 93 | + endIndex += 1 |
| 94 | + } |
| 95 | + |
| 96 | + public var isEmpty: Bool { return firstSlab.data == nil } |
| 97 | + |
| 98 | + public mutating func pop() -> Element? { |
| 99 | + if isEmpty { |
| 100 | + return nil |
| 101 | + } |
| 102 | + assert(endIndex > 0) |
| 103 | + endIndex -= 1 |
| 104 | + let elem = (StackList.bind(lastSlab) + endIndex).move() |
| 105 | + |
| 106 | + if endIndex == 0 { |
| 107 | + if lastSlab.data == firstSlab.data { |
| 108 | + _ = PassContext_freeSlab(context, lastSlab) |
| 109 | + firstSlab.data = nil |
| 110 | + lastSlab.data = nil |
| 111 | + } else { |
| 112 | + lastSlab = PassContext_freeSlab(context, lastSlab) |
| 113 | + } |
| 114 | + endIndex = StackList.slabCapacity |
| 115 | + } |
| 116 | + |
| 117 | + return elem |
| 118 | + } |
| 119 | + |
| 120 | + public mutating func removeAll() { |
| 121 | + if isEmpty { |
| 122 | + return |
| 123 | + } |
| 124 | + while lastSlab.data != firstSlab.data { |
| 125 | + lastSlab = PassContext_freeSlab(context, lastSlab) |
| 126 | + } |
| 127 | + _ = PassContext_freeSlab(context, lastSlab) |
| 128 | + firstSlab.data = nil |
| 129 | + lastSlab.data = nil |
| 130 | + endIndex = StackList.slabCapacity |
| 131 | + } |
| 132 | + |
| 133 | + public var customMirror: Mirror { |
| 134 | + let c: [Mirror.Child] = map { (label: nil, value: $0) } |
| 135 | + return Mirror(self, children: c) |
| 136 | + } |
| 137 | +} |
0 commit comments