Class: DispatchQueue::Heap

Inherits:
Object
  • Object
show all
Defined in:
lib/dispatch_queue_rb/internal/heap.rb

Instance Method Summary collapse

Constructor Details

#initialize(&compare) ⇒ Heap

Returns a new instance of Heap.



13
14
15
16
# File 'lib/dispatch_queue_rb/internal/heap.rb', line 13

def initialize( &compare )
  @heap = []
  @compare = compare
end

Instance Method Details

#countObject Also known as: size, length



35
36
37
# File 'lib/dispatch_queue_rb/internal/heap.rb', line 35

def count
  @heap.count
end

#elementsObject



46
47
48
# File 'lib/dispatch_queue_rb/internal/heap.rb', line 46

def elements
  @heap
end

#empty?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/dispatch_queue_rb/internal/heap.rb', line 42

def empty?
  @heap.empty?
end

#headObject



31
32
33
# File 'lib/dispatch_queue_rb/internal/heap.rb', line 31

def head()
  @heap.first
end

#popObject



23
24
25
26
27
28
29
# File 'lib/dispatch_queue_rb/internal/heap.rb', line 23

def pop()
  head = @heap.first
  tail =  @heap.pop()
  @heap[0] = tail if !@heap.empty?
  _sift_down( 0 )
  head
end

#push(elt) ⇒ Object



18
19
20
21
# File 'lib/dispatch_queue_rb/internal/heap.rb', line 18

def push( elt )
  @heap.push( elt )
  _sift_up( @heap.count - 1 )
end