Skip to content

Commit

Permalink
make steep happy
Browse files Browse the repository at this point in the history
  • Loading branch information
rickhull committed Jun 20, 2024
1 parent 27fed93 commit 00a3d87
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
7 changes: 3 additions & 4 deletions lib/compsci/heap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ def sift_down(idx = 0)
return self if idx >= @tree.size
cidxs = @tree.class.children_idx(idx, @tree.child_slots)
# promote the heapiest child
cidx = self.heapiest(cidxs)
if !self.heapish?(idx, cidx)
if (cidx = self.heapiest(cidxs)) and !self.heapish?(idx, cidx)
@tree.swap(idx, cidx)
self.sift_down(cidx)
end
Expand All @@ -96,7 +95,7 @@ def heapish?(pidx, cidx)
# return the heapiest idx in cidxs
#
def heapiest(cidxs)
idx = cidxs.first
idx = cidxs.first or return nil
cidxs.each { |cidx|
idx = cidx if cidx < @tree.size and heapish?(cidx, idx)
}
Expand All @@ -108,7 +107,7 @@ def heapiest(cidxs)
# * calls heapish? on idx's children and then heap? on them recursively
#
def heap?(idx: 0)
check_children = []
check_children = Array.new
@tree.class.children_idx(idx, @tree.child_slots).each { |cidx|
# cidx may not be valid
if cidx < @tree.size
Expand Down
23 changes: 23 additions & 0 deletions sig/heap.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module CompSci
class Heap
@tree: CompleteTree
@cmp_val: Integer

attr_reader tree: CompleteTree
attr_reader cmp_val: Integer

def initialize: (?cmp_val: Integer,
?minheap: bool,
?child_slots: Integer) -> void

def push: (untyped node) -> self
def pop: () -> untyped
def sift_up: (?Integer idx) -> self
def sift_down: (?Integer idx) -> self
def heapish?: (Integer pidx, Integer cidx) -> bool
def heapiest: (Array[Integer] cidxs) -> Integer?
def heap?: (?idx: ::Integer) -> bool
def size: () -> Integer
alias count size
end
end

0 comments on commit 00a3d87

Please sign in to comment.