類別 SyntaxSuggest::PriorityEngulfQueue
追蹤佇列中元素的優先順序,並確保當一個元素吞噬/覆蓋/吃掉另一個元素時,較大的元素會驅逐較小的元素
公開類別方法
new() 按一下以切換原始碼
# File lib/syntax_suggest/priority_engulf_queue.rb, line 9 def initialize @queue = PriorityQueue.new end
公開實例方法
empty?() 按一下以切換原始碼
# File lib/syntax_suggest/priority_engulf_queue.rb, line 17 def empty? @queue.empty? end
length() 按一下以切換原始碼
# File lib/syntax_suggest/priority_engulf_queue.rb, line 21 def length @queue.length end
peek() 按一下以切換原始碼
# File lib/syntax_suggest/priority_engulf_queue.rb, line 25 def peek @queue.peek end
pop() 按一下以切換原始碼
# File lib/syntax_suggest/priority_engulf_queue.rb, line 29 def pop @queue.pop end
push(block) 按一下以切換原始碼
# File lib/syntax_suggest/priority_engulf_queue.rb, line 33 def push(block) prune_engulf(block) @queue << block flush_deleted self end
to_a() 按一下以切換原始碼
# File lib/syntax_suggest/priority_engulf_queue.rb, line 13 def to_a @queue.to_a end
非公開實例方法
flush_deleted() 按一下以切換原始碼
# File lib/syntax_suggest/priority_engulf_queue.rb, line 41 def flush_deleted while @queue&.peek&.deleted? @queue.pop end end
prune_engulf(block) 按一下以切換原始碼
# 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