Skip to content
Permalink
Browse files

heap: make checkHeapProperty a tests thing (#1276)

Motivation:

In normal operation, we don't need checkHeapProperty anymore.

Modifications:

Move to tests.

Result:

Less code.
  • Loading branch information
weissi authored and Lukasa committed Nov 29, 2019
1 parent cb72048 commit 01b40144f2d85803c4c2d9e315c03af67433a91a
Showing with 33 additions and 37 deletions.
  1. +3 −36 Sources/NIO/Heap.swift
  2. +30 −1 Tests/NIOTests/HeapTests.swift
@@ -29,16 +29,7 @@ internal enum HeapType {
internal struct Heap<T: Comparable> {
internal let type: HeapType
internal private(set) var storage: ContiguousArray<T> = []
private let comparator: (T, T) -> Bool

internal init?(type: HeapType, storage: ContiguousArray<T>) {
self.comparator = type.comparator(type: T.self)
self.storage = storage
self.type = type
if !self.checkHeapProperty() {
return nil
}
}
internal let comparator: (T, T) -> Bool

public init(type: HeapType) {
self.comparator = type.comparator(type: T.self)
@@ -51,12 +42,12 @@ internal struct Heap<T: Comparable> {
}

// named `LEFT` in CLRS
private func leftIndex(_ i: Int) -> Int {
internal func leftIndex(_ i: Int) -> Int {
return 2*i + 1
}

// named `RIGHT` in CLRS
private func rightIndex(_ i: Int) -> Int {
internal func rightIndex(_ i: Int) -> Int {
return 2*i + 2
}

@@ -139,30 +130,6 @@ internal struct Heap<T: Comparable> {
}
return element
}

internal func checkHeapProperty() -> Bool {
func checkHeapProperty(index: Int) -> Bool {
let li = self.leftIndex(index)
let ri = self.rightIndex(index)
if index >= self.storage.count {
return true
} else {
let me = self.storage[index]
var lCond = true
var rCond = true
if li < self.storage.count {
let l = self.storage[li]
lCond = !self.comparator(l, me)
}
if ri < self.storage.count {
let r = self.storage[ri]
rCond = !self.comparator(r, me)
}
return lCond && rCond && checkHeapProperty(index: li) && checkHeapProperty(index: ri)
}
}
return checkHeapProperty(index: 0)
}
}

extension Heap: CustomDebugStringConvertible {
@@ -144,8 +144,37 @@ class HeapTests: XCTestCase {
}

func testRemoveElement() throws {
var h = Heap<Int>(type: .maxHeap, storage: [84, 22, 19, 21, 3, 10, 6, 5, 20])!
var h = Heap<Int>(type: .maxHeap)
for f in [84, 22, 19, 21, 3, 10, 6, 5, 20] {
h.append(f)
}
_ = h.remove(value: 10)
XCTAssertTrue(h.checkHeapProperty(), "\(h.debugDescription)")
}
}

extension Heap {
internal func checkHeapProperty() -> Bool {
func checkHeapProperty(index: Int) -> Bool {
let li = self.leftIndex(index)
let ri = self.rightIndex(index)
if index >= self.storage.count {
return true
} else {
let me = self.storage[index]
var lCond = true
var rCond = true
if li < self.storage.count {
let l = self.storage[li]
lCond = !self.comparator(l, me)
}
if ri < self.storage.count {
let r = self.storage[ri]
rCond = !self.comparator(r, me)
}
return lCond && rCond && checkHeapProperty(index: li) && checkHeapProperty(index: ri)
}
}
return checkHeapProperty(index: 0)
}
}

0 comments on commit 01b4014

Please sign in to comment.
You can’t perform that action at this time.