123456789_123456789_123456789_123456789_123456789_

Class: SyntaxSuggest::PriorityEngulfQueue

Relationships & Source Files
Inherits: Object
Defined in: lib/syntax_suggest/priority_engulf_queue.rb

Overview

Keeps track of what elements are in the queue in priority and also ensures that when one element engulfs/covers/eats another that the larger element evicts the smaller element

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.newPriorityEngulfQueue

[ GitHub ]

  
# File 'lib/syntax_suggest/priority_engulf_queue.rb', line 9

def initialize
  @queue = PriorityQueue.new
end

Instance Attribute Details

#empty?Boolean (readonly)

[ GitHub ]

  
# File 'lib/syntax_suggest/priority_engulf_queue.rb', line 17

def empty?
  @queue.empty?
end

Instance Method Details

#flush_deleted (private)

[ GitHub ]

  
# File 'lib/syntax_suggest/priority_engulf_queue.rb', line 41

private def flush_deleted
  while @queue&.peek&.deleted?
    @queue.pop
  end
end

#length

[ GitHub ]

  
# File 'lib/syntax_suggest/priority_engulf_queue.rb', line 21

def length
  @queue.length
end

#peek

[ GitHub ]

  
# File 'lib/syntax_suggest/priority_engulf_queue.rb', line 25

def peek
  @queue.peek
end

#pop

[ GitHub ]

  
# File 'lib/syntax_suggest/priority_engulf_queue.rb', line 29

def pop
  @queue.pop
end

#prune_engulf(block) (private)

[ GitHub ]

  
# File 'lib/syntax_suggest/priority_engulf_queue.rb', line 47

private def prune_engulf(block)
  # If we're about to pop off the same block, we can skip deleting
  # things from the frontier this iteration since we'll get it
  # on the next iteration
  return if @queue.peek && (block <=> @queue.peek) == 1

  if block.starts_at != block.ends_at # A block of size 1 cannot engulf another
    @queue.to_a.each { |b|
      if b.starts_at >= block.starts_at && b.ends_at <= block.ends_at
        b.delete
        true
      end
    }
  end
end

#push(block)

[ GitHub ]

  
# File 'lib/syntax_suggest/priority_engulf_queue.rb', line 33

def push(block)
  prune_engulf(block)
  @queue << block
  flush_deleted

  self
end

#to_a

[ GitHub ]

  
# File 'lib/syntax_suggest/priority_engulf_queue.rb', line 13

def to_a
  @queue.to_a
end