Class: Rx::Util::Heap

Inherits:
Object
  • Object
show all
Defined in:
lib/rx/util/heap.rb

Instance Method Summary collapse

Constructor Details

#initialize(items = [], &comparator) ⇒ Heap

Returns a new instance of Heap.



37
38
39
40
41
# File 'lib/rx/util/heap.rb', line 37

def initialize(items = [], &comparator)
  @heap = items.dup
  @comparator = block_given? ? comparator : -> (a, b) { a < b }
  sort!
end

Instance Method Details

#<<(item) ⇒ Object



43
44
45
# File 'lib/rx/util/heap.rb', line 43

def <<(item)
  push(item)
end

#peekObject



47
48
49
# File 'lib/rx/util/heap.rb', line 47

def peek
  heap.first
end

#popObject



51
52
53
54
55
# File 'lib/rx/util/heap.rb', line 51

def pop
  item = heap.shift
  sort!
  item
end

#push(item) ⇒ Object



57
58
59
60
61
# File 'lib/rx/util/heap.rb', line 57

def push(item)
  heap << item
  sort!
  self
end