Skip to content

Commit 00a3d87

Browse files
committed
make steep happy
1 parent 27fed93 commit 00a3d87

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

lib/compsci/heap.rb

+3-4
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ def sift_down(idx = 0)
7979
return self if idx >= @tree.size
8080
cidxs = @tree.class.children_idx(idx, @tree.child_slots)
8181
# promote the heapiest child
82-
cidx = self.heapiest(cidxs)
83-
if !self.heapish?(idx, cidx)
82+
if (cidx = self.heapiest(cidxs)) and !self.heapish?(idx, cidx)
8483
@tree.swap(idx, cidx)
8584
self.sift_down(cidx)
8685
end
@@ -96,7 +95,7 @@ def heapish?(pidx, cidx)
9695
# return the heapiest idx in cidxs
9796
#
9897
def heapiest(cidxs)
99-
idx = cidxs.first
98+
idx = cidxs.first or return nil
10099
cidxs.each { |cidx|
101100
idx = cidx if cidx < @tree.size and heapish?(cidx, idx)
102101
}
@@ -108,7 +107,7 @@ def heapiest(cidxs)
108107
# * calls heapish? on idx's children and then heap? on them recursively
109108
#
110109
def heap?(idx: 0)
111-
check_children = []
110+
check_children = Array.new
112111
@tree.class.children_idx(idx, @tree.child_slots).each { |cidx|
113112
# cidx may not be valid
114113
if cidx < @tree.size

sig/heap.rbs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module CompSci
2+
class Heap
3+
@tree: CompleteTree
4+
@cmp_val: Integer
5+
6+
attr_reader tree: CompleteTree
7+
attr_reader cmp_val: Integer
8+
9+
def initialize: (?cmp_val: Integer,
10+
?minheap: bool,
11+
?child_slots: Integer) -> void
12+
13+
def push: (untyped node) -> self
14+
def pop: () -> untyped
15+
def sift_up: (?Integer idx) -> self
16+
def sift_down: (?Integer idx) -> self
17+
def heapish?: (Integer pidx, Integer cidx) -> bool
18+
def heapiest: (Array[Integer] cidxs) -> Integer?
19+
def heap?: (?idx: ::Integer) -> bool
20+
def size: () -> Integer
21+
alias count size
22+
end
23+
end

0 commit comments

Comments
 (0)