Files
swift-nio/Sources/_NIODataStructures/PriorityQueue.swift
T
Cory Benfield d62c733653 Extract PriorityQueue to its own module. (#1932)
Motivation:

I'd like to move EmbeddedChannel and friends out of the main NIO
repository and into their own module. Unfortunately, EmbeddedChannel
shares the PriorityQueue implementation we wrote with the various POSIX
channels. To avoid duplicating the code, we should pull it out to its
own module.

However, we've never wanted to commit to the API of this data structure,
and the same is true now. To that end, I'm pulling it into an
underscored module that is not a product of the package.

We could have used the `@_spi` annotation here but honestly I'm a bit
nervous about doing that at the low-level of NIO itself, as if the Swift
team does change the spelling of it at any point in the future we'll be
in real trouble. This way works almost as well, and makes our intent a
lot clearer.

Modifications:

- Extracted Heap and PriorityQueue to a new module.
- Made everything @inlinable to do our best to make performance
  acceptable.

Result:

We can re-use PriorityQueue.
2021-08-09 11:00:03 +01:00

99 lines
2.3 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
public struct PriorityQueue<Element: Comparable> {
@usableFromInline
internal var _heap: Heap<Element>
@inlinable
public init() {
self._heap = Heap()
}
@inlinable
public mutating func remove(_ key: Element) {
self._heap.remove(value: key)
}
@inlinable
public mutating func push(_ key: Element) {
self._heap.append(key)
}
@inlinable
public func peek() -> Element? {
return self._heap.storage.first
}
@inlinable
public var isEmpty: Bool {
return self._heap.storage.isEmpty
}
@inlinable
@discardableResult
public mutating func pop() -> Element? {
return self._heap.removeRoot()
}
@inlinable
public mutating func clear() {
self._heap = Heap()
}
}
extension PriorityQueue: Equatable {
@inlinable
public static func ==(lhs: PriorityQueue, rhs: PriorityQueue) -> Bool {
return lhs.count == rhs.count && lhs.elementsEqual(rhs)
}
}
extension PriorityQueue: Sequence {
public struct Iterator: IteratorProtocol {
/* private but */ @usableFromInline
var _queue: PriorityQueue<Element>
/* fileprivate but */ @inlinable
public init(queue: PriorityQueue<Element>) {
self._queue = queue
}
@inlinable
public mutating func next() -> Element? {
return self._queue.pop()
}
}
@inlinable
public func makeIterator() -> Iterator {
return Iterator(queue: self)
}
}
extension PriorityQueue {
@inlinable
public var count: Int {
return self._heap.count
}
}
extension PriorityQueue: CustomStringConvertible {
@inlinable
public var description: String {
return "PriorityQueue(count: \(self.count)): \(Array(self))"
}
}