Files
swift-nio/Sources/_NIODataStructures/PriorityQueue.swift
T
Franz Busch c9756e1083 Adopt swift-format (#2794)
* Apply formatting

* Apply no block comments rule

* Apply OmitExplicitReturns

* Apple OnlyOneTrailingClosureArgument

* Apply NoAssignmentInExpressions

* Fix up DontRepeatTypeInStaticProperties lint errors

* Apply `OrderedImports`

* Apply `ReplaceForEachWithForLoop`

* format file

* Enable the formatting pipeline

* Adopt `AmbiguousTrailingClosureOverload`

* Fix license header

* Fix format check

* Fix `EndOfLineComment`

* Fix CI

* Adapt CI script to check if changes when running formatting

* Separate lint and format into to steps

* Fix format

* Adopt `UseEarlyExits`

* Revert "Adopt `UseEarlyExits`"

This reverts commit d1ac5bbe12.
2024-07-19 11:48:17 +02:00

107 lines
2.5 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 removeFirst(where shouldBeRemoved: (Element) throws -> Bool) rethrows {
try self._heap.removeFirst(where: shouldBeRemoved)
}
@inlinable
public mutating func push(_ key: Element) {
self._heap.append(key)
}
@inlinable
public func peek() -> Element? {
self._heap.storage.first
}
@inlinable
public var isEmpty: Bool {
self._heap.storage.isEmpty
}
@inlinable
@discardableResult
public mutating func pop() -> Element? {
self._heap.removeRoot()
}
@inlinable
public mutating func clear() {
self._heap = Heap()
}
}
extension PriorityQueue: Equatable {
@inlinable
public static func == (lhs: PriorityQueue, rhs: PriorityQueue) -> Bool {
lhs.count == rhs.count && lhs.elementsEqual(rhs)
}
}
extension PriorityQueue: Sequence {
public struct Iterator: IteratorProtocol {
@usableFromInline
var _queue: PriorityQueue<Element>
@inlinable
public init(queue: PriorityQueue<Element>) {
self._queue = queue
}
@inlinable
public mutating func next() -> Element? {
self._queue.pop()
}
}
@inlinable
public func makeIterator() -> Iterator {
Iterator(queue: self)
}
}
extension PriorityQueue {
@inlinable
public var count: Int {
self._heap.count
}
}
extension PriorityQueue: CustomStringConvertible {
@inlinable
public var description: String {
"PriorityQueue(count: \(self.count)): \(Array(self))"
}
}
extension PriorityQueue: Sendable where Element: Sendable {}
extension PriorityQueue.Iterator: Sendable where Element: Sendable {}