diff --git a/Sources/NIO/EventLoop.swift b/Sources/NIO/EventLoop.swift index cdab49968..1ed5cc82c 100644 --- a/Sources/NIO/EventLoop.swift +++ b/Sources/NIO/EventLoop.swift @@ -732,6 +732,12 @@ private final class ScheduledTask { } } +extension ScheduledTask: CustomStringConvertible { + var description: String { + return "ScheduledTask(readyTime: \(self.readyTime))" + } +} + extension ScheduledTask : Comparable { public static func < (lhs: ScheduledTask, rhs: ScheduledTask) -> Bool { return lhs.readyTime < rhs.readyTime diff --git a/Sources/NIOPriorityQueue/Heap.swift b/Sources/NIOPriorityQueue/Heap.swift index 4dabcf868..32046e92c 100644 --- a/Sources/NIOPriorityQueue/Heap.swift +++ b/Sources/NIOPriorityQueue/Heap.swift @@ -98,7 +98,7 @@ internal struct Heap { private static func heapInsert(storage: inout ContiguousArray, compare: (T, T) -> Bool, key: T) { var i = storage.count storage.append(key) - while i > 0 && !(storage[parentIndex(i)] == storage[i]) && !compare(storage[parentIndex(i)], storage[i]) { + while i > 0 && compare(storage[i], storage[parentIndex(i)]) { storage.swapAt(i, parentIndex(i)) i = parentIndex(i) } @@ -107,12 +107,12 @@ internal struct Heap { // named `HEAP-INCREASE-KEY` in CRLS private static func heapRootify(storage: inout ContiguousArray, compare: (T, T) -> Bool, index: Int, key: T) { var index = index - if key != storage[index] && !compare(key, storage[index]) { - fatalError() + if compare(storage[index], key) { + fatalError("New key must be closer to the root than current key") } storage[index] = key - while index > 0 && (storage[parentIndex(index)] != storage[index]) && !compare(storage[parentIndex(index)], storage[index]) { + while index > 0 && compare(storage[index], storage[parentIndex(index)]) { storage.swapAt(index, parentIndex(index)) index = parentIndex(index) } @@ -166,11 +166,11 @@ internal struct Heap { var rCond = true if li < self.storage.count { let l = self.storage[li] - lCond = me == l || self.comparator(me, l) + lCond = !self.comparator(l, me) } if ri < self.storage.count { let r = self.storage[ri] - rCond = me == r || self.comparator(me, r) + rCond = !self.comparator(r, me) } return lCond && rCond && checkHeapProperty(index: li) && checkHeapProperty(index: ri) } diff --git a/Tests/NIOPriorityQueueTests/PriorityQueueTests+XCTest.swift b/Tests/NIOPriorityQueueTests/PriorityQueueTests+XCTest.swift index 55ec9995a..e26a85173 100644 --- a/Tests/NIOPriorityQueueTests/PriorityQueueTests+XCTest.swift +++ b/Tests/NIOPriorityQueueTests/PriorityQueueTests+XCTest.swift @@ -31,6 +31,7 @@ extension PriorityQueueTest { ("testRemoveFromEmpty", testRemoveFromEmpty), ("testSomeStringsDesc", testSomeStringsDesc), ("testBuildAndRemoveFromRandomPriorityQueues", testBuildAndRemoveFromRandomPriorityQueues), + ("testPartialOrder", testPartialOrder), ] } } diff --git a/Tests/NIOPriorityQueueTests/PriorityQueueTests.swift b/Tests/NIOPriorityQueueTests/PriorityQueueTests.swift index 9e8b00a75..e6106eb2c 100644 --- a/Tests/NIOPriorityQueueTests/PriorityQueueTests.swift +++ b/Tests/NIOPriorityQueueTests/PriorityQueueTests.swift @@ -108,4 +108,58 @@ class PriorityQueueTest: XCTestCase { } } } + + func testPartialOrder() { + let clearlyTheSmallest = SomePartiallyOrderedDataType(width: 0, height: 0) + let clearlyTheLargest = SomePartiallyOrderedDataType(width: 100, height: 100) + let inTheMiddles = zip(1...99, (1...99).reversed()).map { SomePartiallyOrderedDataType(width: $0, height: $1) } + + /* + the four values are only partially orderd (from small (top) to large (bottom)): + + clearlyTheSmallest + / | \ + inTheMiddle[0] | inTheMiddle[1...] + \ | / + clearlyTheLargest + */ + + var pq = PriorityQueue(ascending: true) + pq.push(clearlyTheLargest) + pq.push(inTheMiddles[0]) + pq.push(clearlyTheSmallest) + inTheMiddles[1...].forEach { + pq.push($0) + } + let pop1 = pq.pop() + XCTAssertEqual(clearlyTheSmallest, pop1) + for _ in inTheMiddles { + let popN = pq.pop()! + XCTAssert(inTheMiddles.contains(popN)) + } + XCTAssertEqual(clearlyTheLargest, pq.pop()!) + XCTAssert(pq.isEmpty) + } +} + +/// This data type is only partially ordered. Ie. from `a < b` and `a != b` we can't imply `a > b`. +struct SomePartiallyOrderedDataType: Comparable, CustomStringConvertible { + public static func <(lhs: SomePartiallyOrderedDataType, rhs: SomePartiallyOrderedDataType) -> Bool { + return lhs.width < rhs.width && lhs.height < rhs.height + } + + public static func ==(lhs: SomePartiallyOrderedDataType, rhs: SomePartiallyOrderedDataType) -> Bool { + return lhs.width == rhs.width && lhs.height == rhs.height + } + + private let width: Int + private let height: Int + public init(width: Int, height: Int) { + self.width = width + self.height = height + } + + public var description: String { + return "(w: \(self.width), h: \(self.height))" + } }