Skip to content

Commit 7c9df00

Browse files
authored
Merge pull request #328 from lorentey/heap-minmax-properties
[Heap] Convert min() and max() to properties
2 parents 2c4060f + 53831b3 commit 7c9df00

File tree

3 files changed

+55
-55
lines changed

3 files changed

+55
-55
lines changed

Documentation/Heap.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ This works by adding the new element into the end of the backing array and then
5353
You can also insert a sequence of elements into a `Heap`:
5454

5555
```swift
56-
var heap = Heap((0..<10))
57-
heap.insert(contentsOf: (20...100).shuffled())
56+
var heap = Heap(0 ..< 10)
57+
heap.insert(contentsOf: (20 ... 100).shuffled())
5858
heap.insert(contentsOf: [-5, -6, -8, -12, -3])
5959
```
6060

@@ -63,9 +63,9 @@ heap.insert(contentsOf: [-5, -6, -8, -12, -3])
6363
As mentioned earlier, the smallest and largest elements can be queried in constant time:
6464

6565
```swift
66-
var heap = Heap((1...20))
67-
let min = heap.min() // min = 1
68-
let max = heap.max() // max = 20
66+
var heap = Heap(1 ... 20)
67+
let min = heap.min // 1
68+
let max = heap.max // 20
6969
```
7070

7171
In a min-max heap, the smallest element is stored at index 0 in the backing array; the largest element is stored at either index 1 or index 2, the first max level in the heap (so to look up the largest, we compare the two and return the larger one).

Sources/HeapModule/Heap.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
///
1818
/// var queue: Heap<Int> = [3, 4, 1, 2]
1919
/// queue.insert(0)
20-
/// print(queue.min()) // 0
20+
/// print(queue.min) // 0
2121
/// print(queue.popMax()) // 4
22-
/// print(queue.max()) // 3
22+
/// print(queue.max) // 3
2323
///
2424
/// `Heap` implements the min-max heap data structure, based on
2525
/// [Atkinson et al. 1986].
@@ -117,15 +117,15 @@ extension Heap {
117117
///
118118
/// - Complexity: O(1)
119119
@inlinable
120-
public func min() -> Element? {
120+
public var min: Element? {
121121
_storage.first
122122
}
123123

124124
/// Returns the element with the highest priority, if available.
125125
///
126126
/// - Complexity: O(1)
127127
@inlinable
128-
public func max() -> Element? {
128+
public var max: Element? {
129129
_storage.withUnsafeBufferPointer { buffer in
130130
guard buffer.count > 2 else {
131131
// If count is 0, `last` will return `nil`

Tests/HeapTests/HeapTests.swift

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,12 @@ final class HeapTests: CollectionTestCase {
124124

125125
heap.insert(contentsOf: (21...50).shuffled())
126126
expectEqual(heap.count, 38)
127-
expectEqual(heap.max(), 50)
128-
expectEqual(heap.min(), 2)
127+
expectEqual(heap.max, 50)
128+
expectEqual(heap.min, 2)
129129

130130
heap.insert(contentsOf: [-10, -9, -8, -7, -6, -5].shuffled())
131131
expectEqual(heap.count, 44)
132-
expectEqual(heap.min(), -10)
132+
expectEqual(heap.min, -10)
133133
}
134134

135135
func test_insert_contentsOf_withSequenceFunction() {
@@ -165,36 +165,36 @@ final class HeapTests: CollectionTestCase {
165165

166166
func test_min() {
167167
var heap = Heap<Int>()
168-
expectNil(heap.min())
168+
expectNil(heap.min)
169169

170170
heap.insert(5)
171-
expectEqual(5, heap.min())
171+
expectEqual(5, heap.min)
172172

173173
heap.insert(12)
174-
expectEqual(5, heap.min())
174+
expectEqual(5, heap.min)
175175

176176
heap.insert(2)
177-
expectEqual(2, heap.min())
177+
expectEqual(2, heap.min)
178178

179179
heap.insert(1)
180-
expectEqual(1, heap.min())
180+
expectEqual(1, heap.min)
181181
}
182182

183183
func test_max() {
184184
var heap = Heap<Int>()
185-
expectNil(heap.max())
185+
expectNil(heap.max)
186186

187187
heap.insert(42)
188-
expectEqual(42, heap.max())
188+
expectEqual(42, heap.max)
189189

190190
heap.insert(20)
191-
expectEqual(42, heap.max())
191+
expectEqual(42, heap.max)
192192

193193
heap.insert(63)
194-
expectEqual(63, heap.max())
194+
expectEqual(63, heap.max)
195195

196196
heap.insert(90)
197-
expectEqual(90, heap.max())
197+
expectEqual(90, heap.max)
198198
}
199199

200200
func test_popMin() {
@@ -345,133 +345,133 @@ final class HeapTests: CollectionTestCase {
345345
var heap = Heap(stride(from: 0, through: 27, by: 3).shuffled())
346346
expectEqual(
347347
heap.itemsInAscendingOrder(), [0, 3, 6, 9, 12, 15, 18, 21, 24, 27])
348-
expectEqual(heap.min(), 0)
348+
expectEqual(heap.min, 0)
349349

350350
// No change
351351
heap.replaceMin(with: 0)
352352
expectEqual(
353353
heap.itemsInAscendingOrder(), [0, 3, 6, 9, 12, 15, 18, 21, 24, 27])
354-
expectEqual(heap.min(), 0)
354+
expectEqual(heap.min, 0)
355355

356356
// Even smaller
357357
heap.replaceMin(with: -1)
358358
expectEqual(
359359
heap.itemsInAscendingOrder(), [-1, 3, 6, 9, 12, 15, 18, 21, 24, 27])
360-
expectEqual(heap.min(), -1)
360+
expectEqual(heap.min, -1)
361361

362362
// Larger, but not enough to usurp
363363
heap.replaceMin(with: 2)
364364
expectEqual(
365365
heap.itemsInAscendingOrder(), [2, 3, 6, 9, 12, 15, 18, 21, 24, 27])
366-
expectEqual(heap.min(), 2)
366+
expectEqual(heap.min, 2)
367367

368368
// Larger, moving another element to be the smallest
369369
heap.replaceMin(with: 5)
370370
expectEqual(
371371
heap.itemsInAscendingOrder(), [3, 5, 6, 9, 12, 15, 18, 21, 24, 27])
372-
expectEqual(heap.min(), 3)
372+
expectEqual(heap.min, 3)
373373
}
374374

375375
func test_maximumReplacement() {
376376
var heap = Heap(stride(from: 0, through: 27, by: 3).shuffled())
377377
expectEqual(
378378
heap.itemsInAscendingOrder(), [0, 3, 6, 9, 12, 15, 18, 21, 24, 27])
379-
expectEqual(heap.max(), 27)
379+
expectEqual(heap.max, 27)
380380

381381
// No change
382382
heap.replaceMax(with: 27)
383383
expectEqual(
384384
heap.itemsInAscendingOrder(), [0, 3, 6, 9, 12, 15, 18, 21, 24, 27])
385-
expectEqual(heap.max(), 27)
385+
expectEqual(heap.max, 27)
386386

387387
// Even larger
388388
heap.replaceMax(with: 28)
389389
expectEqual(
390390
heap.itemsInAscendingOrder(), [0, 3, 6, 9, 12, 15, 18, 21, 24, 28])
391-
expectEqual(heap.max(), 28)
391+
expectEqual(heap.max, 28)
392392

393393
// Smaller, but not enough to usurp
394394
heap.replaceMax(with: 26)
395395
expectEqual(
396396
heap.itemsInAscendingOrder(), [0, 3, 6, 9, 12, 15, 18, 21, 24, 26])
397-
expectEqual(heap.max(), 26)
397+
expectEqual(heap.max, 26)
398398

399399
// Smaller, moving another element to be the largest
400400
heap.replaceMax(with: 23)
401401
expectEqual(
402402
heap.itemsInAscendingOrder(), [0, 3, 6, 9, 12, 15, 18, 21, 23, 24])
403-
expectEqual(heap.max(), 24)
403+
expectEqual(heap.max, 24)
404404

405405
// Check the finer details. As these peek into the stored structure, they
406406
// may need to be updated whenever the internal format changes.
407407
var heap2 = Heap(raw: [1])
408-
expectEqual(heap2.max(), 1)
408+
expectEqual(heap2.max, 1)
409409
expectEqual(Array(heap2.unordered), [1])
410410
expectEqual(heap2.replaceMax(with: 2), 1)
411-
expectEqual(heap2.max(), 2)
411+
expectEqual(heap2.max, 2)
412412
expectEqual(Array(heap2.unordered), [2])
413413

414414
heap2 = Heap(raw: [1, 2])
415-
expectEqual(heap2.max(), 2)
415+
expectEqual(heap2.max, 2)
416416
expectEqual(Array(heap2.unordered), [1, 2])
417417
expectEqual(heap2.replaceMax(with: 3), 2)
418-
expectEqual(heap2.max(), 3)
418+
expectEqual(heap2.max, 3)
419419
expectEqual(Array(heap2.unordered), [1, 3])
420420
expectEqual(heap2.replaceMax(with: 0), 3)
421-
expectEqual(heap2.max(), 1)
421+
expectEqual(heap2.max, 1)
422422
expectEqual(Array(heap2.unordered), [0, 1])
423423

424424
heap2 = Heap(raw: [5, 20, 31, 16, 8, 7, 18])
425-
expectEqual(heap2.max(), 31)
425+
expectEqual(heap2.max, 31)
426426
expectEqual(Array(heap2.unordered), [5, 20, 31, 16, 8, 7, 18])
427427
expectEqual(heap2.replaceMax(with: 29), 31)
428428
expectEqual(Array(heap2.unordered), [5, 20, 29, 16, 8, 7, 18])
429-
expectEqual(heap2.max(), 29)
429+
expectEqual(heap2.max, 29)
430430
expectEqual(heap2.replaceMax(with: 19), 29)
431431
expectEqual(Array(heap2.unordered), [5, 20, 19, 16, 8, 7, 18])
432-
expectEqual(heap2.max(), 20)
432+
expectEqual(heap2.max, 20)
433433
expectEqual(heap2.replaceMax(with: 15), 20)
434434
expectEqual(Array(heap2.unordered), [5, 16, 19, 15, 8, 7, 18])
435-
expectEqual(heap2.max(), 19)
435+
expectEqual(heap2.max, 19)
436436
expectEqual(heap2.replaceMax(with: 4), 19)
437437
expectEqual(Array(heap2.unordered), [4, 16, 18, 15, 8, 7, 5])
438-
expectEqual(heap2.max(), 18)
438+
expectEqual(heap2.max, 18)
439439
}
440440

441441
// MARK: -
442442

443443
func test_min_struct() {
444444
var heap = Heap<Task>()
445-
expectNil(heap.min())
445+
expectNil(heap.min)
446446

447447
let firstTask = Task(name: "Do something", priority: 10)
448448
heap.insert(firstTask)
449-
expectEqual(heap.min(), firstTask)
449+
expectEqual(heap.min, firstTask)
450450

451451
let higherPriorityTask = Task(name: "Urgent", priority: 100)
452452
heap.insert(higherPriorityTask)
453-
expectEqual(heap.min(), firstTask)
453+
expectEqual(heap.min, firstTask)
454454

455455
let lowerPriorityTask = Task(name: "Get this done today", priority: 1)
456456
heap.insert(lowerPriorityTask)
457-
expectEqual(heap.min(), lowerPriorityTask)
457+
expectEqual(heap.min, lowerPriorityTask)
458458
}
459459

460460
func test_max_struct() {
461461
var heap = Heap<Task>()
462-
expectNil(heap.max())
462+
expectNil(heap.max)
463463

464464
let firstTask = Task(name: "Do something", priority: 10)
465465
heap.insert(firstTask)
466-
expectEqual(heap.max(), firstTask)
466+
expectEqual(heap.max, firstTask)
467467

468468
let lowerPriorityTask = Task(name: "Get this done today", priority: 1)
469469
heap.insert(lowerPriorityTask)
470-
expectEqual(heap.max(), firstTask)
470+
expectEqual(heap.max, firstTask)
471471

472472
let higherPriorityTask = Task(name: "Urgent", priority: 100)
473473
heap.insert(higherPriorityTask)
474-
expectEqual(heap.max(), higherPriorityTask)
474+
expectEqual(heap.max, higherPriorityTask)
475475
}
476476

477477
func test_popMin_struct() {
@@ -516,7 +516,7 @@ final class HeapTests: CollectionTestCase {
516516

517517
func test_initializer_fromCollection() {
518518
var heap = Heap((1...20).shuffled())
519-
expectEqual(heap.max(), 20)
519+
expectEqual(heap.max, 20)
520520

521521
expectEqual(heap.popMin(), 1)
522522
expectEqual(heap.popMax(), 20)
@@ -565,12 +565,12 @@ final class HeapTests: CollectionTestCase {
565565
let input = (0 ..< c).shuffled(using: &rng)
566566
let heap = Heap(input)
567567
if c > 0 {
568-
expectEqual(heap.min(), 0)
569-
expectEqual(heap.max(), c - 1)
568+
expectEqual(heap.min, 0)
569+
expectEqual(heap.max, c - 1)
570570
expectEqualElements(heap.itemsInAscendingOrder(), 0 ..< c)
571571
} else {
572-
expectNil(heap.min())
573-
expectNil(heap.max())
572+
expectNil(heap.min)
573+
expectNil(heap.max)
574574
expectEqualElements(heap.itemsInAscendingOrder(), [])
575575
}
576576
}

0 commit comments

Comments
 (0)