Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sysread/coro-priorityqueue
A priority queue (heap) for Perl and Coro.
https://github.com/sysread/coro-priorityqueue
Last synced: about 1 month ago
JSON representation
A priority queue (heap) for Perl and Coro.
- Host: GitHub
- URL: https://github.com/sysread/coro-priorityqueue
- Owner: sysread
- License: other
- Created: 2013-12-16T20:43:39.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2013-12-16T20:48:34.000Z (about 11 years ago)
- Last Synced: 2023-03-23T22:19:29.897Z (almost 2 years ago)
- Language: Perl
- Size: 105 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README
- Changelog: Changes
- License: COPYING
Awesome Lists containing this project
README
NAME
Coro::PriorityQueueSYNOPSIS
use Coro::PriorityQueue;
use Coro;my $queue = Coro::PriorityQueue->new(10);
my $producer = async {
foreach my $i (1 .. 10) {
$queue->insert($i);
}$queue->shutdown;
};my $consumer = async {
while (1) {
my $i = $queue->remove or last;
printf("%d * 2 = %d\n", $i, $i * 2);
}
};$producer->join, $consumer->join;
DESCRIPTION
Priority queues automatically order their contents according to the
inserted item's priority. Calling code must ensure that their queue
items are comparable via this strategy (e.g. by overloading the <=>
operator).Underneath, this is implemented as a simple array heap, using
Coro::Semaphore to control access.METHODS
new($max)
Creates a new queue that can store $max items.insert($item)
Inserts an item into the queue. Will block the thread until a slot is
available if necessary. If the queue has been shut down, croaks.It is an error to insert undef into the queue.
remove
Removes and returns an item from the queue. Blocks until an item becomes
available if necessary. If the queue is shutdown, returns undefined
immediately.count
Returns the number of items currently stored.is_empty
Returns true if the queue is empty.is_full
Returns true if the queue is full.shutdown
Shuts down the queue, after which no items may be inserted. Items
already in the queue can be pulled normally until empty, after which
further calls to "remove" will return undefined.AUTHOR
Jeff Ober