Class: Ibex::Runtime::RepairPriorityQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/json5/generated_parser.rb

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



4636
4637
4638
# File 'lib/json5/generated_parser.rb', line 4636

def initialize
  @entries = []
end

Instance Method Details

#empty?Boolean

RBS:

  • () -> bool

Returns:

  • (Boolean)


4641
# File 'lib/json5/generated_parser.rb', line 4641

def empty? = @entries.empty?

#popObject

RBS:

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



4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
# File 'lib/json5/generated_parser.rb', line 4659

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) ⇒ Object

RBS:

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



4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
# File 'lib/json5/generated_parser.rb', line 4644

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