class SyntaxSuggest::PriorityEngulfQueue
跟踪队列中的元素优先级,并确保当一个元素吞没/覆盖/吃掉另一个元素时,较大的元素会驱逐较小的元素。
Public Class Methods
Source
# File lib/syntax_suggest/priority_engulf_queue.rb, line 9 def initialize @queue = PriorityQueue.new end
Public Instance Methods
Source
# File lib/syntax_suggest/priority_engulf_queue.rb, line 17 def empty? @queue.empty? end
Source
# File lib/syntax_suggest/priority_engulf_queue.rb, line 21 def length @queue.length end
Source
# File lib/syntax_suggest/priority_engulf_queue.rb, line 33 def push(block) prune_engulf(block) @queue << block flush_deleted self end
私有实例方法
Source
# File lib/syntax_suggest/priority_engulf_queue.rb, line 41 def flush_deleted while @queue&.peek&.deleted? @queue.pop end end
Source
# File lib/syntax_suggest/priority_engulf_queue.rb, line 47 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