mirror of
https://github.com/apple/swift-nio
synced 2026-11-24 04:37:54 +00:00
PriorityQueue: make it work for partial orders
This commit is contained in:
1 parent
584fdb6507
commit
25f4e2ceff
4 files changed
+67
-6
No files matched your search
@@ -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
|
||||
|
||||
@@ -98,7 +98,7 @@ internal struct Heap<T: Comparable> {
|
||||
private static func heapInsert(storage: inout ContiguousArray<T>, 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<T: Comparable> {
|
||||
// named `HEAP-INCREASE-KEY` in CRLS
|
||||
private static func heapRootify(storage: inout ContiguousArray<T>, 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<T: Comparable> {
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ extension PriorityQueueTest {
|
||||
("testRemoveFromEmpty", testRemoveFromEmpty),
|
||||
("testSomeStringsDesc", testSomeStringsDesc),
|
||||
("testBuildAndRemoveFromRandomPriorityQueues", testBuildAndRemoveFromRandomPriorityQueues),
|
||||
("testPartialOrder", testPartialOrder),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<SomePartiallyOrderedDataType>(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))"
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user