concurrent-ruby 0.7.0-java
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE.txt +21 -0
- data/README.md +217 -0
- data/lib/concurrent.rb +45 -0
- data/lib/concurrent/actor.rb +104 -0
- data/lib/concurrent/actor/behaviour.rb +70 -0
- data/lib/concurrent/actor/behaviour/abstract.rb +48 -0
- data/lib/concurrent/actor/behaviour/awaits.rb +21 -0
- data/lib/concurrent/actor/behaviour/buffer.rb +54 -0
- data/lib/concurrent/actor/behaviour/errors_on_unknown_message.rb +12 -0
- data/lib/concurrent/actor/behaviour/executes_context.rb +18 -0
- data/lib/concurrent/actor/behaviour/linking.rb +42 -0
- data/lib/concurrent/actor/behaviour/pausing.rb +77 -0
- data/lib/concurrent/actor/behaviour/removes_child.rb +16 -0
- data/lib/concurrent/actor/behaviour/sets_results.rb +36 -0
- data/lib/concurrent/actor/behaviour/supervised.rb +58 -0
- data/lib/concurrent/actor/behaviour/supervising.rb +34 -0
- data/lib/concurrent/actor/behaviour/terminates_children.rb +13 -0
- data/lib/concurrent/actor/behaviour/termination.rb +54 -0
- data/lib/concurrent/actor/context.rb +153 -0
- data/lib/concurrent/actor/core.rb +213 -0
- data/lib/concurrent/actor/default_dead_letter_handler.rb +9 -0
- data/lib/concurrent/actor/envelope.rb +41 -0
- data/lib/concurrent/actor/errors.rb +27 -0
- data/lib/concurrent/actor/internal_delegations.rb +49 -0
- data/lib/concurrent/actor/public_delegations.rb +40 -0
- data/lib/concurrent/actor/reference.rb +81 -0
- data/lib/concurrent/actor/root.rb +37 -0
- data/lib/concurrent/actor/type_check.rb +48 -0
- data/lib/concurrent/actor/utils.rb +10 -0
- data/lib/concurrent/actor/utils/ad_hoc.rb +21 -0
- data/lib/concurrent/actor/utils/balancer.rb +40 -0
- data/lib/concurrent/actor/utils/broadcast.rb +52 -0
- data/lib/concurrent/actor/utils/pool.rb +59 -0
- data/lib/concurrent/actress.rb +3 -0
- data/lib/concurrent/agent.rb +230 -0
- data/lib/concurrent/async.rb +284 -0
- data/lib/concurrent/atomic.rb +91 -0
- data/lib/concurrent/atomic/atomic_boolean.rb +202 -0
- data/lib/concurrent/atomic/atomic_fixnum.rb +203 -0
- data/lib/concurrent/atomic/condition.rb +67 -0
- data/lib/concurrent/atomic/copy_on_notify_observer_set.rb +118 -0
- data/lib/concurrent/atomic/copy_on_write_observer_set.rb +117 -0
- data/lib/concurrent/atomic/count_down_latch.rb +116 -0
- data/lib/concurrent/atomic/cyclic_barrier.rb +106 -0
- data/lib/concurrent/atomic/event.rb +98 -0
- data/lib/concurrent/atomic/synchronization.rb +51 -0
- data/lib/concurrent/atomic/thread_local_var.rb +82 -0
- data/lib/concurrent/atomic_reference/concurrent_update_error.rb +8 -0
- data/lib/concurrent/atomic_reference/direct_update.rb +50 -0
- data/lib/concurrent/atomic_reference/jruby.rb +14 -0
- data/lib/concurrent/atomic_reference/mutex_atomic.rb +77 -0
- data/lib/concurrent/atomic_reference/numeric_cas_wrapper.rb +25 -0
- data/lib/concurrent/atomic_reference/rbx.rb +19 -0
- data/lib/concurrent/atomic_reference/ruby.rb +37 -0
- data/lib/concurrent/atomics.rb +11 -0
- data/lib/concurrent/channel/buffered_channel.rb +85 -0
- data/lib/concurrent/channel/channel.rb +41 -0
- data/lib/concurrent/channel/unbuffered_channel.rb +35 -0
- data/lib/concurrent/channel/waitable_list.rb +40 -0
- data/lib/concurrent/channels.rb +5 -0
- data/lib/concurrent/collection/blocking_ring_buffer.rb +71 -0
- data/lib/concurrent/collection/priority_queue.rb +305 -0
- data/lib/concurrent/collection/ring_buffer.rb +59 -0
- data/lib/concurrent/collections.rb +3 -0
- data/lib/concurrent/configuration.rb +161 -0
- data/lib/concurrent/dataflow.rb +108 -0
- data/lib/concurrent/delay.rb +104 -0
- data/lib/concurrent/dereferenceable.rb +101 -0
- data/lib/concurrent/errors.rb +30 -0
- data/lib/concurrent/exchanger.rb +34 -0
- data/lib/concurrent/executor/cached_thread_pool.rb +44 -0
- data/lib/concurrent/executor/executor.rb +282 -0
- data/lib/concurrent/executor/fixed_thread_pool.rb +33 -0
- data/lib/concurrent/executor/immediate_executor.rb +65 -0
- data/lib/concurrent/executor/java_cached_thread_pool.rb +31 -0
- data/lib/concurrent/executor/java_fixed_thread_pool.rb +41 -0
- data/lib/concurrent/executor/java_single_thread_executor.rb +22 -0
- data/lib/concurrent/executor/java_thread_pool_executor.rb +180 -0
- data/lib/concurrent/executor/per_thread_executor.rb +100 -0
- data/lib/concurrent/executor/ruby_cached_thread_pool.rb +29 -0
- data/lib/concurrent/executor/ruby_fixed_thread_pool.rb +32 -0
- data/lib/concurrent/executor/ruby_single_thread_executor.rb +74 -0
- data/lib/concurrent/executor/ruby_thread_pool_executor.rb +288 -0
- data/lib/concurrent/executor/ruby_thread_pool_worker.rb +72 -0
- data/lib/concurrent/executor/safe_task_executor.rb +35 -0
- data/lib/concurrent/executor/serialized_execution.rb +126 -0
- data/lib/concurrent/executor/single_thread_executor.rb +35 -0
- data/lib/concurrent/executor/thread_pool_executor.rb +68 -0
- data/lib/concurrent/executor/timer_set.rb +143 -0
- data/lib/concurrent/executors.rb +9 -0
- data/lib/concurrent/future.rb +125 -0
- data/lib/concurrent/ivar.rb +111 -0
- data/lib/concurrent/lazy_register.rb +58 -0
- data/lib/concurrent/logging.rb +17 -0
- data/lib/concurrent/mvar.rb +200 -0
- data/lib/concurrent/obligation.rb +171 -0
- data/lib/concurrent/observable.rb +40 -0
- data/lib/concurrent/options_parser.rb +48 -0
- data/lib/concurrent/promise.rb +170 -0
- data/lib/concurrent/scheduled_task.rb +79 -0
- data/lib/concurrent/timer_task.rb +341 -0
- data/lib/concurrent/tvar.rb +248 -0
- data/lib/concurrent/utilities.rb +3 -0
- data/lib/concurrent/utility/processor_count.rb +152 -0
- data/lib/concurrent/utility/timeout.rb +35 -0
- data/lib/concurrent/utility/timer.rb +21 -0
- data/lib/concurrent/version.rb +3 -0
- data/lib/concurrent_ruby.rb +1 -0
- data/lib/concurrent_ruby_ext.jar +0 -0
- data/lib/concurrent_ruby_ext.so +0 -0
- data/lib/extension_helper.rb +28 -0
- metadata +163 -0
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'concurrent/atomic'
|
2
|
+
require 'concurrent/atomic/atomic_boolean'
|
3
|
+
require 'concurrent/atomic/atomic_fixnum'
|
4
|
+
require 'concurrent/atomic/condition'
|
5
|
+
require 'concurrent/atomic/copy_on_notify_observer_set'
|
6
|
+
require 'concurrent/atomic/copy_on_write_observer_set'
|
7
|
+
require 'concurrent/atomic/cyclic_barrier'
|
8
|
+
require 'concurrent/atomic/count_down_latch'
|
9
|
+
require 'concurrent/atomic/event'
|
10
|
+
require 'concurrent/atomic/thread_local_var'
|
11
|
+
require 'concurrent/atomic/synchronization'
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'concurrent/atomic/condition'
|
2
|
+
|
3
|
+
require_relative 'waitable_list'
|
4
|
+
|
5
|
+
module Concurrent
|
6
|
+
class BufferedChannel
|
7
|
+
|
8
|
+
def initialize(size)
|
9
|
+
@mutex = Mutex.new
|
10
|
+
@condition = Condition.new
|
11
|
+
@buffer_condition = Condition.new
|
12
|
+
|
13
|
+
@probe_set = WaitableList.new
|
14
|
+
@buffer = RingBuffer.new(size)
|
15
|
+
end
|
16
|
+
|
17
|
+
def probe_set_size
|
18
|
+
@probe_set.size
|
19
|
+
end
|
20
|
+
|
21
|
+
def buffer_queue_size
|
22
|
+
@mutex.synchronize { @buffer.count }
|
23
|
+
end
|
24
|
+
|
25
|
+
def push(value)
|
26
|
+
until set_probe_or_push_into_buffer(value)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def pop
|
31
|
+
probe = Channel::Probe.new
|
32
|
+
select(probe)
|
33
|
+
probe.value
|
34
|
+
end
|
35
|
+
|
36
|
+
def select(probe)
|
37
|
+
@mutex.synchronize do
|
38
|
+
|
39
|
+
if @buffer.empty?
|
40
|
+
@probe_set.put(probe)
|
41
|
+
true
|
42
|
+
else
|
43
|
+
shift_buffer if probe.set_unless_assigned(peek_buffer, self)
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def remove_probe(probe)
|
50
|
+
@probe_set.delete(probe)
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def push_into_buffer(value)
|
56
|
+
@buffer_condition.wait(@mutex) while @buffer.full?
|
57
|
+
@buffer.offer value
|
58
|
+
@buffer_condition.broadcast
|
59
|
+
end
|
60
|
+
|
61
|
+
def peek_buffer
|
62
|
+
@buffer_condition.wait(@mutex) while @buffer.empty?
|
63
|
+
@buffer.peek
|
64
|
+
end
|
65
|
+
|
66
|
+
def shift_buffer
|
67
|
+
@buffer_condition.wait(@mutex) while @buffer.empty?
|
68
|
+
result = @buffer.poll
|
69
|
+
@buffer_condition.broadcast
|
70
|
+
result
|
71
|
+
end
|
72
|
+
|
73
|
+
def set_probe_or_push_into_buffer(value)
|
74
|
+
@mutex.synchronize do
|
75
|
+
if @probe_set.empty?
|
76
|
+
push_into_buffer(value)
|
77
|
+
true
|
78
|
+
else
|
79
|
+
@probe_set.take.set_unless_assigned(value, self)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'concurrent/ivar'
|
2
|
+
|
3
|
+
module Concurrent
|
4
|
+
module Channel
|
5
|
+
|
6
|
+
class Probe < Concurrent::IVar
|
7
|
+
|
8
|
+
def initialize(value = NO_VALUE, opts = {})
|
9
|
+
super(value, opts)
|
10
|
+
end
|
11
|
+
|
12
|
+
def set_unless_assigned(value, channel)
|
13
|
+
mutex.synchronize do
|
14
|
+
return false if [:fulfilled, :rejected].include? @state
|
15
|
+
|
16
|
+
set_state(true, [value, channel], nil)
|
17
|
+
event.set
|
18
|
+
true
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
alias_method :composite_value, :value
|
23
|
+
|
24
|
+
def value
|
25
|
+
composite_value.nil? ? nil : composite_value[0]
|
26
|
+
end
|
27
|
+
|
28
|
+
def channel
|
29
|
+
composite_value.nil? ? nil : composite_value[1]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.select(*channels)
|
34
|
+
probe = Probe.new
|
35
|
+
channels.each { |channel| channel.select(probe) }
|
36
|
+
result = probe.composite_value
|
37
|
+
channels.each { |channel| channel.remove_probe(probe) }
|
38
|
+
result
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require_relative 'waitable_list'
|
2
|
+
|
3
|
+
module Concurrent
|
4
|
+
class UnbufferedChannel
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@probe_set = WaitableList.new
|
8
|
+
end
|
9
|
+
|
10
|
+
def probe_set_size
|
11
|
+
@probe_set.size
|
12
|
+
end
|
13
|
+
|
14
|
+
def push(value)
|
15
|
+
# TODO set_unless_assigned define on IVar as #set_state? or #try_set_state
|
16
|
+
until @probe_set.take.set_unless_assigned(value, self)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def pop
|
21
|
+
probe = Channel::Probe.new
|
22
|
+
select(probe)
|
23
|
+
probe.value
|
24
|
+
end
|
25
|
+
|
26
|
+
def select(probe)
|
27
|
+
@probe_set.put(probe)
|
28
|
+
end
|
29
|
+
|
30
|
+
def remove_probe(probe)
|
31
|
+
@probe_set.delete(probe)
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'concurrent/atomic/condition'
|
2
|
+
|
3
|
+
module Concurrent
|
4
|
+
class WaitableList
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@mutex = Mutex.new
|
8
|
+
@condition = Condition.new
|
9
|
+
|
10
|
+
@list = []
|
11
|
+
end
|
12
|
+
|
13
|
+
def size
|
14
|
+
@mutex.synchronize { @list.size }
|
15
|
+
end
|
16
|
+
|
17
|
+
def empty?
|
18
|
+
@mutex.synchronize { @list.empty? }
|
19
|
+
end
|
20
|
+
|
21
|
+
def put(value)
|
22
|
+
@mutex.synchronize do
|
23
|
+
@list << value
|
24
|
+
@condition.signal
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def delete(value)
|
29
|
+
@mutex.synchronize { @list.delete(value) }
|
30
|
+
end
|
31
|
+
|
32
|
+
def take
|
33
|
+
@mutex.synchronize do
|
34
|
+
@condition.wait(@mutex) while @list.empty?
|
35
|
+
@list.shift
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'concurrent/atomic/condition'
|
2
|
+
|
3
|
+
module Concurrent
|
4
|
+
class BlockingRingBuffer
|
5
|
+
|
6
|
+
def initialize(capacity)
|
7
|
+
@buffer = RingBuffer.new(capacity)
|
8
|
+
@first = @last = 0
|
9
|
+
@count = 0
|
10
|
+
@mutex = Mutex.new
|
11
|
+
@condition = Condition.new
|
12
|
+
end
|
13
|
+
|
14
|
+
# @return [Integer] the capacity of the buffer
|
15
|
+
def capacity
|
16
|
+
@mutex.synchronize { @buffer.capacity }
|
17
|
+
end
|
18
|
+
|
19
|
+
# @return [Integer] the number of elements currently in the buffer
|
20
|
+
def count
|
21
|
+
@mutex.synchronize { @buffer.count }
|
22
|
+
end
|
23
|
+
|
24
|
+
# @return [Boolean] true if buffer is empty, false otherwise
|
25
|
+
def empty?
|
26
|
+
@mutex.synchronize { @buffer.empty? }
|
27
|
+
end
|
28
|
+
|
29
|
+
# @return [Boolean] true if buffer is full, false otherwise
|
30
|
+
def full?
|
31
|
+
@mutex.synchronize { @buffer.full? }
|
32
|
+
end
|
33
|
+
|
34
|
+
# @param [Object] value the value to be inserted
|
35
|
+
# @return [Boolean] true if value has been inserted, false otherwise
|
36
|
+
def put(value)
|
37
|
+
@mutex.synchronize do
|
38
|
+
wait_while_full
|
39
|
+
@buffer.offer(value)
|
40
|
+
@condition.signal
|
41
|
+
true
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# @return [Object] the first available value and removes it from the buffer. If buffer is empty it blocks until an element is available
|
46
|
+
def take
|
47
|
+
@mutex.synchronize do
|
48
|
+
wait_while_empty
|
49
|
+
result = @buffer.poll
|
50
|
+
@condition.signal
|
51
|
+
result
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# @return [Object] the first available value and without removing it from the buffer. If buffer is empty returns nil
|
56
|
+
def peek
|
57
|
+
@mutex.synchronize { @buffer.peek }
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def wait_while_full
|
63
|
+
@condition.wait(@mutex) while @buffer.full?
|
64
|
+
end
|
65
|
+
|
66
|
+
def wait_while_empty
|
67
|
+
@condition.wait(@mutex) while @buffer.empty?
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,305 @@
|
|
1
|
+
module Concurrent
|
2
|
+
|
3
|
+
# @!macro [attach] priority_queue
|
4
|
+
#
|
5
|
+
# A queue collection in which the elements are sorted based on their
|
6
|
+
# comparison (spaceship) operator `<=>`. Items are added to the queue
|
7
|
+
# at a position relative to their priority. On removal the element
|
8
|
+
# with the "highest" priority is removed. By default the sort order is
|
9
|
+
# from highest to lowest, but a lowest-to-highest sort order can be
|
10
|
+
# set on construction.
|
11
|
+
#
|
12
|
+
# The API is based on the `Queue` class from the Ruby standard library.
|
13
|
+
#
|
14
|
+
# The pure Ruby implementation, `MutexPriorityQueue` uses a heap algorithm
|
15
|
+
# stored in an array. The algorithm is based on the work of Robert Sedgewick
|
16
|
+
# and Kevin Wayne.
|
17
|
+
#
|
18
|
+
# The JRuby native implementation is a thin wrapper around the standard
|
19
|
+
# library `java.util.PriorityQueue`.
|
20
|
+
#
|
21
|
+
# When running under JRuby the class `PriorityQueue` extends `JavaPriorityQueue`.
|
22
|
+
# When running under all other interpreters it extends `MutexPriorityQueue`.
|
23
|
+
#
|
24
|
+
# @note This implementation is *not* thread safe and performs no blocking.
|
25
|
+
#
|
26
|
+
# @see http://en.wikipedia.org/wiki/Priority_queue
|
27
|
+
# @see http://ruby-doc.org/stdlib-2.0.0/libdoc/thread/rdoc/Queue.html
|
28
|
+
#
|
29
|
+
# @see http://algs4.cs.princeton.edu/24pq/index.php#2.6
|
30
|
+
# @see http://algs4.cs.princeton.edu/24pq/MaxPQ.java.html
|
31
|
+
#
|
32
|
+
# @see http://docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html
|
33
|
+
class MutexPriorityQueue
|
34
|
+
|
35
|
+
# @!macro [attach] priority_queue_method_initialize
|
36
|
+
#
|
37
|
+
# Create a new priority queue with no items.
|
38
|
+
#
|
39
|
+
# @param [Hash] opts the options for creating the queue
|
40
|
+
# @option opts [Symbol] :order (:max) dictates the order in which items are
|
41
|
+
# stored: from highest to lowest when `:max` or `:high`; from lowest to
|
42
|
+
# highest when `:min` or `:low`
|
43
|
+
def initialize(opts = {})
|
44
|
+
order = opts.fetch(:order, :max)
|
45
|
+
@comparator = [:min, :low].include?(order) ? -1 : 1
|
46
|
+
clear
|
47
|
+
end
|
48
|
+
|
49
|
+
# @!macro [attach] priority_queue_method_clear
|
50
|
+
#
|
51
|
+
# Removes all of the elements from this priority queue.
|
52
|
+
def clear
|
53
|
+
@queue = [nil]
|
54
|
+
@length = 0
|
55
|
+
true
|
56
|
+
end
|
57
|
+
|
58
|
+
# @!macro [attach] priority_queue_method_delete
|
59
|
+
#
|
60
|
+
# Deletes all items from `self` that are equal to `item`.
|
61
|
+
#
|
62
|
+
# @param [Object] item the item to be removed from the queue
|
63
|
+
# @return [Object] true if the item is found else false
|
64
|
+
def delete(item)
|
65
|
+
original_length = @length
|
66
|
+
k = 1
|
67
|
+
while k <= @length
|
68
|
+
if @queue[k] == item
|
69
|
+
swap(k, @length)
|
70
|
+
@length -= 1
|
71
|
+
sink(k)
|
72
|
+
@queue.pop
|
73
|
+
else
|
74
|
+
k += 1
|
75
|
+
end
|
76
|
+
end
|
77
|
+
@length != original_length
|
78
|
+
end
|
79
|
+
|
80
|
+
# @!macro [attach] priority_queue_method_empty
|
81
|
+
#
|
82
|
+
# Returns `true` if `self` contains no elements.
|
83
|
+
#
|
84
|
+
# @return [Boolean] true if there are no items in the queue else false
|
85
|
+
def empty?
|
86
|
+
size == 0
|
87
|
+
end
|
88
|
+
|
89
|
+
# @!macro [attach] priority_queue_method_include
|
90
|
+
#
|
91
|
+
# Returns `true` if the given item is present in `self` (that is, if any
|
92
|
+
# element == `item`), otherwise returns false.
|
93
|
+
#
|
94
|
+
# @param [Object] item the item to search for
|
95
|
+
#
|
96
|
+
# @return [Boolean] true if the item is found else false
|
97
|
+
def include?(item)
|
98
|
+
@queue.include?(item)
|
99
|
+
end
|
100
|
+
alias_method :has_priority?, :include?
|
101
|
+
|
102
|
+
# @!macro [attach] priority_queue_method_length
|
103
|
+
#
|
104
|
+
# The current length of the queue.
|
105
|
+
#
|
106
|
+
# @return [Fixnum] the number of items in the queue
|
107
|
+
def length
|
108
|
+
@length
|
109
|
+
end
|
110
|
+
alias_method :size, :length
|
111
|
+
|
112
|
+
# @!macro [attach] priority_queue_method_peek
|
113
|
+
#
|
114
|
+
# Retrieves, but does not remove, the head of this queue, or returns `nil`
|
115
|
+
# if this queue is empty.
|
116
|
+
#
|
117
|
+
# @return [Object] the head of the queue or `nil` when empty
|
118
|
+
def peek
|
119
|
+
@queue[1]
|
120
|
+
end
|
121
|
+
|
122
|
+
# @!macro [attach] priority_queue_method_pop
|
123
|
+
#
|
124
|
+
# Retrieves and removes the head of this queue, or returns `nil` if this
|
125
|
+
# queue is empty.
|
126
|
+
#
|
127
|
+
# @return [Object] the head of the queue or `nil` when empty
|
128
|
+
def pop
|
129
|
+
max = @queue[1]
|
130
|
+
swap(1, @length)
|
131
|
+
@length -= 1
|
132
|
+
sink(1)
|
133
|
+
@queue.pop
|
134
|
+
max
|
135
|
+
end
|
136
|
+
alias_method :deq, :pop
|
137
|
+
alias_method :shift, :pop
|
138
|
+
|
139
|
+
# @!macro [attach] priority_queue_method_push
|
140
|
+
#
|
141
|
+
# Inserts the specified element into this priority queue.
|
142
|
+
#
|
143
|
+
# @param [Object] item the item to insert onto the queue
|
144
|
+
def push(item)
|
145
|
+
@length += 1
|
146
|
+
@queue << item
|
147
|
+
swim(@length)
|
148
|
+
true
|
149
|
+
end
|
150
|
+
alias_method :<<, :push
|
151
|
+
alias_method :enq, :push
|
152
|
+
|
153
|
+
# @!macro [attach] priority_queue_method_from_list
|
154
|
+
#
|
155
|
+
# Create a new priority queue from the given list.
|
156
|
+
#
|
157
|
+
# @param [Enumerable] list the list to build the queue from
|
158
|
+
# @param [Hash] opts the options for creating the queue
|
159
|
+
#
|
160
|
+
# @return [PriorityQueue] the newly created and populated queue
|
161
|
+
def self.from_list(list, opts = {})
|
162
|
+
queue = new(opts)
|
163
|
+
list.each{|item| queue << item }
|
164
|
+
queue
|
165
|
+
end
|
166
|
+
|
167
|
+
protected
|
168
|
+
|
169
|
+
# Exchange the values at the given indexes within the internal array.
|
170
|
+
#
|
171
|
+
# @param [Integer] x the first index to swap
|
172
|
+
# @param [Integer] y the second index to swap
|
173
|
+
#
|
174
|
+
# @!visibility private
|
175
|
+
def swap(x, y)
|
176
|
+
temp = @queue[x]
|
177
|
+
@queue[x] = @queue[y]
|
178
|
+
@queue[y] = temp
|
179
|
+
end
|
180
|
+
|
181
|
+
# Are the items at the given indexes ordered based on the priority
|
182
|
+
# order specified at construction?
|
183
|
+
#
|
184
|
+
# @param [Integer] x the first index from which to retrieve a comparable value
|
185
|
+
# @param [Integer] y the second index from which to retrieve a comparable value
|
186
|
+
#
|
187
|
+
# @return [Boolean] true if the two elements are in the correct priority order
|
188
|
+
# else false
|
189
|
+
#
|
190
|
+
# @!visibility private
|
191
|
+
def ordered?(x, y)
|
192
|
+
(@queue[x] <=> @queue[y]) == @comparator
|
193
|
+
end
|
194
|
+
|
195
|
+
# Percolate down to maintain heap invariant.
|
196
|
+
#
|
197
|
+
# @param [Integer] k the index at which to start the percolation
|
198
|
+
#
|
199
|
+
# @!visibility private
|
200
|
+
def sink(k)
|
201
|
+
while (j = (2 * k)) <= @length do
|
202
|
+
j += 1 if j < @length && ! ordered?(j, j+1)
|
203
|
+
break if ordered?(k, j)
|
204
|
+
swap(k, j)
|
205
|
+
k = j
|
206
|
+
end
|
207
|
+
end
|
208
|
+
|
209
|
+
# Percolate up to maintain heap invariant.
|
210
|
+
#
|
211
|
+
# @param [Integer] k the index at which to start the percolation
|
212
|
+
#
|
213
|
+
# @!visibility private
|
214
|
+
def swim(k)
|
215
|
+
while k > 1 && ! ordered?(k/2, k) do
|
216
|
+
swap(k, k/2)
|
217
|
+
k = k/2
|
218
|
+
end
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
if RUBY_PLATFORM == 'java'
|
223
|
+
|
224
|
+
# @!macro priority_queue
|
225
|
+
class JavaPriorityQueue
|
226
|
+
|
227
|
+
# @!macro priority_queue_method_initialize
|
228
|
+
def initialize(opts = {})
|
229
|
+
order = opts.fetch(:order, :max)
|
230
|
+
if [:min, :low].include?(order)
|
231
|
+
@queue = java.util.PriorityQueue.new(11) # 11 is the default initial capacity
|
232
|
+
else
|
233
|
+
@queue = java.util.PriorityQueue.new(11, java.util.Collections.reverseOrder())
|
234
|
+
end
|
235
|
+
end
|
236
|
+
|
237
|
+
# @!macro priority_queue_method_clear
|
238
|
+
def clear
|
239
|
+
@queue.clear
|
240
|
+
true
|
241
|
+
end
|
242
|
+
|
243
|
+
# @!macro priority_queue_method_delete
|
244
|
+
def delete(item)
|
245
|
+
found = false
|
246
|
+
while @queue.remove(item) do
|
247
|
+
found = true
|
248
|
+
end
|
249
|
+
found
|
250
|
+
end
|
251
|
+
|
252
|
+
# @!macro priority_queue_method_empty
|
253
|
+
def empty?
|
254
|
+
@queue.size == 0
|
255
|
+
end
|
256
|
+
|
257
|
+
# @!macro priority_queue_method_include
|
258
|
+
def include?(item)
|
259
|
+
@queue.contains(item)
|
260
|
+
end
|
261
|
+
alias_method :has_priority?, :include?
|
262
|
+
|
263
|
+
# @!macro priority_queue_method_length
|
264
|
+
def length
|
265
|
+
@queue.size
|
266
|
+
end
|
267
|
+
alias_method :size, :length
|
268
|
+
|
269
|
+
# @!macro priority_queue_method_peek
|
270
|
+
def peek
|
271
|
+
@queue.peek
|
272
|
+
end
|
273
|
+
|
274
|
+
# @!macro priority_queue_method_pop
|
275
|
+
def pop
|
276
|
+
@queue.poll
|
277
|
+
end
|
278
|
+
alias_method :deq, :pop
|
279
|
+
alias_method :shift, :pop
|
280
|
+
|
281
|
+
# @!macro priority_queue_method_push
|
282
|
+
def push(item)
|
283
|
+
@queue.add(item)
|
284
|
+
end
|
285
|
+
alias_method :<<, :push
|
286
|
+
alias_method :enq, :push
|
287
|
+
|
288
|
+
# @!macro priority_queue_method_from_list
|
289
|
+
def self.from_list(list, opts = {})
|
290
|
+
queue = new(opts)
|
291
|
+
list.each{|item| queue << item }
|
292
|
+
queue
|
293
|
+
end
|
294
|
+
end
|
295
|
+
|
296
|
+
# @!macro priority_queue
|
297
|
+
class PriorityQueue < JavaPriorityQueue
|
298
|
+
end
|
299
|
+
else
|
300
|
+
|
301
|
+
# @!macro priority_queue
|
302
|
+
class PriorityQueue < MutexPriorityQueue
|
303
|
+
end
|
304
|
+
end
|
305
|
+
end
|