| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- /*
- * Copyright 2024, gRPC Authors All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- internal import DequeModule
- /// A FIFO-queue which allows for a single element to be stored on the stack and defers to a
- /// heap-implementation if further elements are added.
- ///
- /// This is useful when optimising for unary streams where avoiding the cost of a heap
- /// allocation is desirable.
- internal struct OneOrManyQueue<Element>: Collection {
- private var backing: Backing
- private enum Backing: Collection {
- case none
- case one(Element)
- case many(Deque<Element>)
- var startIndex: Int {
- switch self {
- case .none, .one:
- return 0
- case let .many(elements):
- return elements.startIndex
- }
- }
- var endIndex: Int {
- switch self {
- case .none:
- return 0
- case .one:
- return 1
- case let .many(elements):
- return elements.endIndex
- }
- }
- subscript(index: Int) -> Element {
- switch self {
- case .none:
- fatalError("Invalid index")
- case let .one(element):
- assert(index == 0)
- return element
- case let .many(elements):
- return elements[index]
- }
- }
- func index(after index: Int) -> Int {
- switch self {
- case .none:
- return 0
- case .one:
- return 1
- case let .many(elements):
- return elements.index(after: index)
- }
- }
- var count: Int {
- switch self {
- case .none:
- return 0
- case .one:
- return 1
- case let .many(elements):
- return elements.count
- }
- }
- var isEmpty: Bool {
- switch self {
- case .none:
- return true
- case .one:
- return false
- case let .many(elements):
- return elements.isEmpty
- }
- }
- mutating func append(_ element: Element) {
- switch self {
- case .none:
- self = .one(element)
- case let .one(one):
- var elements = Deque<Element>()
- elements.reserveCapacity(16)
- elements.append(one)
- elements.append(element)
- self = .many(elements)
- case var .many(elements):
- self = .none
- elements.append(element)
- self = .many(elements)
- }
- }
- mutating func pop() -> Element? {
- switch self {
- case .none:
- return nil
- case let .one(element):
- self = .none
- return element
- case var .many(many):
- self = .none
- let element = many.popFirst()
- self = .many(many)
- return element
- }
- }
- }
- init() {
- self.backing = .none
- }
- var isEmpty: Bool {
- return self.backing.isEmpty
- }
- var count: Int {
- return self.backing.count
- }
- var startIndex: Int {
- return self.backing.startIndex
- }
- var endIndex: Int {
- return self.backing.endIndex
- }
- subscript(index: Int) -> Element {
- return self.backing[index]
- }
- func index(after index: Int) -> Int {
- return self.backing.index(after: index)
- }
- mutating func append(_ element: Element) {
- self.backing.append(element)
- }
- mutating func pop() -> Element? {
- return self.backing.pop()
- }
- }
|