Class: Concurrent::Collection::JavaNonConcurrentPriorityQueue
Relationships & Source Files | |
Inherits: | Object |
Defined in: | lib/concurrent-ruby/concurrent/collection/java_non_concurrent_priority_queue.rb |
Overview
This implementation is not thread safe.
**Private Implementation:** This abstraction is a private, internal implementation detail. It should never be used directly.
A queue collection in which the elements are sorted based on their comparison (spaceship) operator <=>
. Items are added to the queue at a position relative to their priority. On removal the element with the “highest” priority is removed. By default the sort order is from highest to lowest, but a lowest-to-highest sort order can be set on construction.
The API is based on the Queue
class from the Ruby standard library.
The pure Ruby implementation, RubyNonConcurrentPriorityQueue
uses a heap algorithm stored in an array. The algorithm is based on the work of Robert Sedgewick and Kevin Wayne.
The JRuby native implementation is a thin wrapper around the standard library java.util.NonConcurrentPriorityQueue
.
When running under JRuby the class NonConcurrentPriorityQueue
extends JavaNonConcurrentPriorityQueue
. When running under all other interpreters it extends RubyNonConcurrentPriorityQueue
.
Class Method Summary
-
.from_list(list, opts = {}) ⇒ NonConcurrentPriorityQueue
Create a new priority queue from the given list.
-
.new(opts = {}) ⇒ JavaNonConcurrentPriorityQueue
constructor
Create a new priority queue with no items.
Instance Attribute Summary
-
#empty? ⇒ Boolean
readonly
Returns
true
ifself
contains no elements.
Instance Method Summary
-
#<<(item)
Alias for #push.
-
#clear
Removes all of the elements from this priority queue.
-
#delete(item) ⇒ Object
Deletes all items from
self
that are equal toitem
. -
#deq
Alias for #pop.
-
#enq(item)
Alias for #push.
-
#has_priority?(item)
Alias for #include?.
-
#include?(item) ⇒ Boolean
(also: #has_priority?)
Returns
true
if the given item is present inself
(that is, if any element ==item
), otherwise returns false. -
#length ⇒ Fixnum
(also: #size)
The current length of the queue.
-
#peek ⇒ Object
Retrieves, but does not remove, the head of this queue, or returns
nil
if this queue is empty. -
#pop ⇒ Object
(also: #deq, #shift)
Retrieves and removes the head of this queue, or returns
nil
if this queue is empty. -
#push(item)
(also: #<<, #enq)
Inserts the specified element into this priority queue.
-
#shift
Alias for #pop.
-
#size
Alias for #length.
Constructor Details
.new(opts = {}) ⇒ JavaNonConcurrentPriorityQueue
Create a new priority queue with no items.
# File 'lib/concurrent-ruby/concurrent/collection/java_non_concurrent_priority_queue.rb', line 14
def initialize(opts = {}) order = opts.fetch(:order, :max) if [:min, :low].include?(order) @queue = java.util.PriorityQueue.new(11) # 11 is the default initial capacity else @queue = java.util.PriorityQueue.new(11, java.util.Collections.reverseOrder()) end end
Class Method Details
.from_list(list, opts = {}) ⇒ NonConcurrentPriorityQueue
Create a new priority queue from the given list.
# File 'lib/concurrent-ruby/concurrent/collection/java_non_concurrent_priority_queue.rb', line 76
def self.from_list(list, opts = {}) queue = new(opts) list.each{|item| queue << item } queue end
Instance Attribute Details
#empty? ⇒ Boolean
(readonly)
Returns true
if self
contains no elements.
# File 'lib/concurrent-ruby/concurrent/collection/java_non_concurrent_priority_queue.rb', line 39
def empty? @queue.size == 0 end
Instance Method Details
#<<(item)
Alias for #push.
# File 'lib/concurrent-ruby/concurrent/collection/java_non_concurrent_priority_queue.rb', line 72
alias_method :<<, :push
#clear
Removes all of the elements from this priority queue.
# File 'lib/concurrent-ruby/concurrent/collection/java_non_concurrent_priority_queue.rb', line 24
def clear @queue.clear true end
#delete(item) ⇒ Object
Deletes all items from self
that are equal to item
.
# File 'lib/concurrent-ruby/concurrent/collection/java_non_concurrent_priority_queue.rb', line 30
def delete(item) found = false while @queue.remove(item) do found = true end found end
#deq
Alias for #pop.
# File 'lib/concurrent-ruby/concurrent/collection/java_non_concurrent_priority_queue.rb', line 64
alias_method :deq, :pop
#enq(item)
Alias for #push.
# File 'lib/concurrent-ruby/concurrent/collection/java_non_concurrent_priority_queue.rb', line 73
alias_method :enq, :push
#has_priority?(item)
Alias for #include?.
# File 'lib/concurrent-ruby/concurrent/collection/java_non_concurrent_priority_queue.rb', line 47
alias_method :has_priority?, :include?
#include?(item) ⇒ Boolean
Also known as: #has_priority?
Returns true
if the given item is present in self
(that is, if any element == item
), otherwise returns false.
# File 'lib/concurrent-ruby/concurrent/collection/java_non_concurrent_priority_queue.rb', line 44
def include?(item) @queue.contains(item) end
#length ⇒ Fixnum
Also known as: #size
The current length of the queue.
# File 'lib/concurrent-ruby/concurrent/collection/java_non_concurrent_priority_queue.rb', line 50
def length @queue.size end
#peek ⇒ Object
Retrieves, but does not remove, the head of this queue, or returns nil
if this queue is empty.
# File 'lib/concurrent-ruby/concurrent/collection/java_non_concurrent_priority_queue.rb', line 56
def peek @queue.peek end
#pop ⇒ Object Also known as: #deq, #shift
Retrieves and removes the head of this queue, or returns nil
if this queue is empty.
# File 'lib/concurrent-ruby/concurrent/collection/java_non_concurrent_priority_queue.rb', line 61
def pop @queue.poll end
#push(item) Also known as: #<<, #enq
Inserts the specified element into this priority queue.
# File 'lib/concurrent-ruby/concurrent/collection/java_non_concurrent_priority_queue.rb', line 68
def push(item) raise ArgumentError.new('cannot enqueue nil') if item.nil? @queue.add(item) end
#shift
Alias for #pop.
# File 'lib/concurrent-ruby/concurrent/collection/java_non_concurrent_priority_queue.rb', line 65
alias_method :shift, :pop
#size
Alias for #length.
# File 'lib/concurrent-ruby/concurrent/collection/java_non_concurrent_priority_queue.rb', line 53
alias_method :size, :length