Class: FastPriorityQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/fast_priority_queue.rb,
lib/fast_priority_queue/version.rb

Constant Summary collapse

VERSION =
"0.1.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFastPriorityQueue

Returns a new instance of FastPriorityQueue.



8
9
10
11
12
13
14
15
# File 'lib/fast_priority_queue.rb', line 8

def initialize
  @array = [nil]
  if block_given?
    @cmp = ->(a,b) { yield a,b }
  else
    @cmp = ->(a,b) { a <=> b }
  end
end

Instance Attribute Details

#cmpObject (readonly)

Returns the value of attribute cmp.



6
7
8
# File 'lib/fast_priority_queue.rb', line 6

def cmp
  @cmp
end

Instance Method Details

#_pop_last_elementObject



37
38
39
40
# File 'lib/fast_priority_queue.rb', line 37

def _pop_last_element
  #TODO: find a way to do this in Rust
  @array.pop
end

#add(x) ⇒ Object



21
22
23
# File 'lib/fast_priority_queue.rb', line 21

def add(x)
  _add(@array,@cmp,x)
end

#empty?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/fast_priority_queue.rb', line 33

def empty?
  length == 0
end

#lengthObject



17
18
19
# File 'lib/fast_priority_queue.rb', line 17

def length
  @array.length - 1
end

#popObject



29
30
31
# File 'lib/fast_priority_queue.rb', line 29

def pop
  _pop(@array,@cmp)
end

#topObject



25
26
27
# File 'lib/fast_priority_queue.rb', line 25

def top
  @array[1]
end