Class: RubySprites::Packer::HorizontalSplit::Heap
- Inherits:
-
Object
- Object
- RubySprites::Packer::HorizontalSplit::Heap
- Defined in:
- lib/lash-sprites/packer/horizontal_split.rb
Instance Method Summary collapse
- #empty? ⇒ Boolean
-
#initialize(&comparer) ⇒ Heap
constructor
A new instance of Heap.
- #insert(el) ⇒ Object
- #inspect ⇒ Object
- #peek ⇒ Object
- #remove ⇒ Object
Constructor Details
#initialize(&comparer) ⇒ Heap
Returns a new instance of Heap.
99 100 101 102 |
# File 'lib/lash-sprites/packer/horizontal_split.rb', line 99 def initialize(&comparer) @comparer = comparer @heap = [0] end |
Instance Method Details
#empty? ⇒ Boolean
146 147 148 |
# File 'lib/lash-sprites/packer/horizontal_split.rb', line 146 def empty? return @heap.length == 1 end |
#insert(el) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/lash-sprites/packer/horizontal_split.rb', line 104 def insert(el) pos = @heap.length @heap.push(el) new_pos = pos while new_pos != 1 && @comparer.call(el, @heap[new_pos / 2]) < 0 new_pos /= 2 end while(pos > new_pos) @heap[pos] = @heap[pos / 2] pos /= 2 end @heap[pos] = el end |
#inspect ⇒ Object
150 151 152 |
# File 'lib/lash-sprites/packer/horizontal_split.rb', line 150 def inspect return @heap.to_s end |
#peek ⇒ Object
141 142 143 144 |
# File 'lib/lash-sprites/packer/horizontal_split.rb', line 141 def peek return nil if @heap.length == 1 return @heap[1] end |
#remove ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/lash-sprites/packer/horizontal_split.rb', line 119 def remove return nil if @heap.length == 1 el = @heap[1] shifter = @heap.pop if @heap.length != 1 pos = 1 while(!@heap[pos * 2].nil?) lesser_pos = pos * 2 lesser_pos += 1 if !@heap[pos * 2 + 1].nil? && @comparer.call(@heap[pos * 2 + 1], @heap[pos * 2]) < 0 if(@comparer.call(@heap[lesser_pos], shifter) < 0) @heap[pos] = @heap[lesser_pos] pos = lesser_pos else break end end @heap[pos] = shifter end return el end |