Class: Ibex::Runtime::RepairPriorityQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/ibex/runtime/repair_priority_queue.rb,
sig/ibex/runtime/repair_priority_queue.rbs

Overview

Minimal binary heap ordered by an immutable Array priority.

Instance Method Summary collapse

Constructor Details

#initializeRepairPriorityQueue

Returns a new instance of RepairPriorityQueue.

RBS:

  • () -> void



13
14
15
# File 'lib/ibex/runtime/repair_priority_queue.rb', line 13

def initialize
  @entries = []
end

Instance Method Details

#compare(left, right) ⇒ Integer

RBS:

  • ([priority, Object?] left, [priority, Object?] right) -> Integer

Parameters:

  • left ([ priority, Object? ])
  • right ([ priority, Object? ])

Returns:

  • (Integer)


57
58
59
60
61
# File 'lib/ibex/runtime/repair_priority_queue.rb', line 57

def compare(left, right)
  left_priority = left.fetch(0) #: priority
  right_priority = right.fetch(0) #: priority
  (left_priority <=> right_priority) || 0
end

#empty?Boolean

RBS:

  • () -> bool

Returns:

  • (Boolean)


18
# File 'lib/ibex/runtime/repair_priority_queue.rb', line 18

def empty? = @entries.empty?

#pop[ priority, Object? ]?

RBS:

  • () -> [priority, Object?]?

Returns:

  • ([ priority, Object? ], nil)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ibex/runtime/repair_priority_queue.rb', line 36

def pop
  first = @entries.first
  tail = @entries.pop
  return first if @entries.empty? || !first || !tail

  index = 0
  while (child = (index * 2) + 1) < @entries.length
    right = child + 1
    child = right if right < @entries.length && compare(@entries[right], @entries[child]).negative?
    break if compare(tail, @entries[child]) <= 0

    @entries[index] = @entries[child]
    index = child
  end
  @entries[index] = tail
  first
end

#push(priority, value) ⇒ void

This method returns an undefined value.

RBS:

  • (priority priority, Object? value) -> void

Parameters:

  • priority (priority)
  • value (Object, nil)


21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ibex/runtime/repair_priority_queue.rb', line 21

def push(priority, value)
  entry = [priority, value] #: [priority, Object?]
  @entries << entry
  index = @entries.length - 1
  while index.positive?
    parent = (index - 1) / 2
    break if compare(@entries[parent], entry) <= 0

    @entries[index] = @entries[parent]
    index = parent
  end
  @entries[index] = entry
end