OneOrManyQueue.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright 2024, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. internal import DequeModule
  17. /// A FIFO-queue which allows for a single element to be stored on the stack and defers to a
  18. /// heap-implementation if further elements are added.
  19. ///
  20. /// This is useful when optimising for unary streams where avoiding the cost of a heap
  21. /// allocation is desirable.
  22. internal struct OneOrManyQueue<Element>: Collection {
  23. private var backing: Backing
  24. private enum Backing: Collection {
  25. case none
  26. case one(Element)
  27. case many(Deque<Element>)
  28. var startIndex: Int {
  29. switch self {
  30. case .none, .one:
  31. return 0
  32. case let .many(elements):
  33. return elements.startIndex
  34. }
  35. }
  36. var endIndex: Int {
  37. switch self {
  38. case .none:
  39. return 0
  40. case .one:
  41. return 1
  42. case let .many(elements):
  43. return elements.endIndex
  44. }
  45. }
  46. subscript(index: Int) -> Element {
  47. switch self {
  48. case .none:
  49. fatalError("Invalid index")
  50. case let .one(element):
  51. assert(index == 0)
  52. return element
  53. case let .many(elements):
  54. return elements[index]
  55. }
  56. }
  57. func index(after index: Int) -> Int {
  58. switch self {
  59. case .none:
  60. return 0
  61. case .one:
  62. return 1
  63. case let .many(elements):
  64. return elements.index(after: index)
  65. }
  66. }
  67. var count: Int {
  68. switch self {
  69. case .none:
  70. return 0
  71. case .one:
  72. return 1
  73. case let .many(elements):
  74. return elements.count
  75. }
  76. }
  77. var isEmpty: Bool {
  78. switch self {
  79. case .none:
  80. return true
  81. case .one:
  82. return false
  83. case let .many(elements):
  84. return elements.isEmpty
  85. }
  86. }
  87. mutating func append(_ element: Element) {
  88. switch self {
  89. case .none:
  90. self = .one(element)
  91. case let .one(one):
  92. var elements = Deque<Element>()
  93. elements.reserveCapacity(16)
  94. elements.append(one)
  95. elements.append(element)
  96. self = .many(elements)
  97. case var .many(elements):
  98. self = .none
  99. elements.append(element)
  100. self = .many(elements)
  101. }
  102. }
  103. mutating func pop() -> Element? {
  104. switch self {
  105. case .none:
  106. return nil
  107. case let .one(element):
  108. self = .none
  109. return element
  110. case var .many(many):
  111. self = .none
  112. let element = many.popFirst()
  113. self = .many(many)
  114. return element
  115. }
  116. }
  117. }
  118. init() {
  119. self.backing = .none
  120. }
  121. var isEmpty: Bool {
  122. return self.backing.isEmpty
  123. }
  124. var count: Int {
  125. return self.backing.count
  126. }
  127. var startIndex: Int {
  128. return self.backing.startIndex
  129. }
  130. var endIndex: Int {
  131. return self.backing.endIndex
  132. }
  133. subscript(index: Int) -> Element {
  134. return self.backing[index]
  135. }
  136. func index(after index: Int) -> Int {
  137. return self.backing.index(after: index)
  138. }
  139. mutating func append(_ element: Element) {
  140. self.backing.append(element)
  141. }
  142. mutating func pop() -> Element? {
  143. return self.backing.pop()
  144. }
  145. }