Skip to content

Commit 6c31278

Browse files
committed
use Forwardable to delegate to Array
- remove CompleteTree#last_idx
1 parent e2f7e78 commit 6c31278

File tree

4 files changed

+15
-27
lines changed

4 files changed

+15
-27
lines changed

lib/compsci/complete_tree.rb

+8-15
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
require 'compsci'
1+
require 'forwardable'
22

33
module CompSci
44
# A CompleteTree can very efficiently use an array for storage using
55
# simple arithmetic to determine parent child relationships.
66
# Any type of value may be stored in the array / tree at any location.
77
#
88
class CompleteTree
9+
extend Forwardable
10+
911
# given a child index and child count (e.g. 2 for a binary tree)
1012
# integer math maps several children to one parent index
1113
def self.parent_idx(idx, n)
@@ -31,26 +33,17 @@ def self.generation(idx, n)
3133
end
3234

3335
attr_reader :array
36+
def_delegators :@array,
37+
:push, :pop, :shift, :unshift,
38+
:size, :count, :first, :last, :[]
3439

3540
def initialize(array: [], child_slots: 2)
3641
@array = array
3742
@child_slots = child_slots
3843
end
3944

40-
def push val
41-
@array.push val
42-
end
43-
44-
def pop
45-
@array.pop
46-
end
47-
48-
def size
49-
@array.size
50-
end
51-
52-
def last_idx
53-
@array.size - 1 unless @array.empty?
45+
def swap(idx1, idx2)
46+
@array[idx1], @array[idx2] = @array[idx2], @array[idx1]
5447
end
5548

5649
def display(width: 80)

lib/compsci/heap.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ def peek
6363
# * idx represents the node suspected to violate the heap
6464
# * intended to be O(log n) on heap size (log base child_slots)
6565
#
66-
def sift_up(idx)
66+
def sift_up(idx = nil)
67+
idx ||= @array.size - 1
6768
return self if idx <= 0
6869
pidx = self.class.parent_idx(idx, @child_slots)
6970
if !self.heapish?(pidx, idx)
@@ -78,7 +79,8 @@ def sift_up(idx)
7879
# * intended to be O(log n) on heap size (log base child_slots)
7980
# * slower than sift_up because one parent vs multiple children
8081
#
81-
def sift_down(idx)
82+
def sift_down(idx = nil)
83+
idx ||= 0
8284
return self if idx >= @array.size
8385
cidxs = self.class.children_idx(idx, @child_slots)
8486
# promote the heapiest child

test/complete_tree.rb

-7
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,5 @@
124124
expect(@empty.size).must_equal 0
125125
expect(@nonempty.size).must_equal @array.size
126126
end
127-
128-
it "must have a last_idx, nil when empty" do
129-
expect(@empty.last_idx.nil?).must_equal true
130-
expect(@nonempty.last_idx).must_equal 99
131-
end
132-
133-
# TODO: push, pop, display
134127
end
135128
end

test/heap.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
@maxheap.array.push 10
2828
expect(@maxheap.heap?).must_equal false
29-
@maxheap.sift_up @maxheap.last_idx
29+
@maxheap.sift_up
3030
expect(@maxheap.heap?).must_equal true
3131
end
3232

@@ -68,7 +68,7 @@
6868

6969
@minheap.array.push 0
7070
expect(@minheap.heap?).must_equal false
71-
@minheap.sift_up @minheap.last_idx
71+
@minheap.sift_up
7272
expect(@minheap.heap?).must_equal true
7373
end
7474

@@ -111,7 +111,7 @@
111111

112112
@heap3.array.push 10
113113
expect(@heap3.heap?).must_equal false
114-
@heap3.sift_up @heap3.last_idx
114+
@heap3.sift_up
115115
expect(@heap3.heap?).must_equal true
116116
end
117117

0 commit comments

Comments
 (0)