Class: Ferret::Utils::PriorityQueue
Summary
A PriorityQueue is a very useful data structure and one that needs a fast implementation. Hence this priority queue is implemented in C. It is pretty easy to use; basically you just insert elements into the queue and pop them off.
The elements are sorted with the lowest valued elements on the top of the heap, ie the first to be popped off. Elements are ordered using the less_than ’<’ method. To change the order of the queue you can either reimplement the ’<’ method pass a block when you initialize the queue.
You can also set the capacity of the PriorityQueue. Once you hit the capacity, the lowest values elements are automatically popped of the top of the queue as more elements are added.
Example
Here is a toy example that sorts strings by their length and has a capacity of 5;
q = PriorityQueue.new(5) {|a, b| a.size < b.size}
q << "x"
q << "xxxxx"
q << "xxx"
q << "xxxx"
q << "xxxxxx"
q << "xx" # hit capacity so "x" will be popped off the top
puts q.size #=> 5
word = q.pop #=> "xx"
q.top << "yyyy" # "xxxyyyy" will still be at the top of the queue
q.adjust # move "xxxyyyy" to its correct location in queue
word = q.pop #=> "xxxx"
word = q.pop #=> "xxxxx"
word = q.pop #=> "xxxxxx"
word = q.pop #=> "xxxyyyy"
word = q.pop #=> nil
Public Class Methods
PriorityQueue.new(capacity = 32) → new_pq
PriorityQueue.new({:capacity => 32,
:less_than_proc => lambda{|a, b| a < b}) → new_pq
PriorityQueue.new({:capacity => 32}) {|a, b| a < b} → new_pq
Returns a new empty priority queue object with an optional capacity. Once the capacity is filled, the lowest valued elements will be automatically popped off the top of the queue as more elements are inserted into the queue.
/*
* call-seq:
* PriorityQueue.new(capacity = 32) -> new_pq
* PriorityQueue.new({:capacity => 32,
* :less_than_proc => lambda{|a, b| a < b}) -> new_pq
* PriorityQueue.new({:capacity => 32}) {|a, b| a < b} -> new_pq
*
* Returns a new empty priority queue object with an optional capacity.
* Once the capacity is filled, the lowest valued elements will be
* automatically popped off the top of the queue as more elements are
* inserted into the queue.
*/
static VALUE
frb_pq_init(int argc, VALUE *argv, VALUE self)
{
if (argc >= 1) {
PriQ *pq;
VALUE options = argv[0];
VALUE param;
int capa = PQ_START_CAPA;
GET_PQ(pq, self);
switch (TYPE(options)) {
case T_FIXNUM:
capa = FIX2INT(options);
break;
case T_HASH:
if (!NIL_P(param = rb_hash_aref(options,
ID2SYM(id_capacity)))) {
capa = FIX2INT(param);
}
if (!NIL_P(param = rb_hash_aref(options,
ID2SYM(id_less_than)))) {
pq->proc = param;
}
break;
default:
rb_raise(rb_eArgError,
"PriorityQueue#initialize only takes a Hash or "
"an integer");
break;
}
if (capa < 0) {
rb_raise(rb_eIndexError,
"PriorityQueue must have a capacity > 0. %d < 0",
capa);
}
pq->capa = capa;
if (rb_block_given_p()) {
pq->proc = rb_block_proc();
}
if (argc > 1) {
rb_raise(rb_eArgError,
"PriorityQueue#initialize only takes one parameter");
}
}
return self;
}Public Instance Methods
pq.insert(elem) → self
pq << elem → self
Insert an element into a queue. It will be inserted into the correct position in the queue according to its priority.
/*
* call-seq:
* pq.insert(elem) -> self
* pq << elem -> self
*
* Insert an element into a queue. It will be inserted into the correct
* position in the queue according to its priority.
*/
static VALUE
frb_pq_insert(VALUE self, VALUE elem)
{
PriQ *pq;
GET_PQ(pq, self);
if (pq->size < pq->capa) {
pq_push(pq, elem);
}
else if (pq->size > 0 && frb_pq_lt(pq->proc, pq->heap[1], elem)) {
pq->heap[1] = elem;
pq_down(pq);
}
/* else ignore the element */
return self;
}
pq.adjust → self
Sometimes you modify the top element in the priority queue so that its priority changes. When you do this you need to reorder the queue and you do this by calling the adjust method.
/*
* call-seq:
* pq.adjust -> self
*
* Sometimes you modify the top element in the priority queue so that its
* priority changes. When you do this you need to reorder the queue and you
* do this by calling the adjust method.
*/
static VALUE
frb_pq_adjust(VALUE self)
{
PriQ *pq;
GET_PQ(pq, self);
pq_down(pq);
return self;
}
pq.capacity → integer
Returns the capacity of the queue, ie. the number of elements that can be stored in a Priority queue before they start to drop off the end. The size of a PriorityQueue can never be greater than its capacity
/*
* call-seq:
* pq.capacity -> integer
*
* Returns the capacity of the queue, ie. the number of elements that can be
* stored in a Priority queue before they start to drop off the end. The
* _size_ of a PriorityQueue can never be greater than its
* _capacity_
*/
static VALUE
frb_pq_capa(VALUE self)
{
PriQ *pq;
GET_PQ(pq, self);
return INT2FIX(pq->capa);
}
pq.clear → self
Clears all elements from the priority queue. The size will be reset to 0.
/*
* call-seq:
* pq.clear -> self
*
* Clears all elements from the priority queue. The size will be reset to 0.
*/
static VALUE
frb_pq_clear(VALUE self)
{
PriQ *pq;
GET_PQ(pq, self);
pq->size = 0;
return self;
}
pq.clone → pq_clone
Returns a shallow clone of the priority queue. That is only the priority queue is cloned, its contents are not cloned.
/*
* call-seq:
* pq.clone -> pq_clone
*
* Returns a shallow clone of the priority queue. That is only the priority
* queue is cloned, its contents are not cloned.
*/
static VALUE
frb_pq_clone(VALUE self)
{
PriQ *pq, *new_pq = ALLOC(PriQ);
GET_PQ(pq, self);
memcpy(new_pq, pq, sizeof(PriQ));
new_pq->heap = ALLOC_N(VALUE, new_pq->mem_capa);
memcpy(new_pq->heap, pq->heap, sizeof(VALUE) * (new_pq->size + 1));
return Data_Wrap_Struct(cPriorityQueue, &frb_pq_mark, &frb_pq_free, new_pq);
}
pq.insert(elem) → self
pq << elem → self
Insert an element into a queue. It will be inserted into the correct position in the queue according to its priority.
/*
* call-seq:
* pq.insert(elem) -> self
* pq << elem -> self
*
* Insert an element into a queue. It will be inserted into the correct
* position in the queue according to its priority.
*/
static VALUE
frb_pq_insert(VALUE self, VALUE elem)
{
PriQ *pq;
GET_PQ(pq, self);
if (pq->size < pq->capa) {
pq_push(pq, elem);
}
else if (pq->size > 0 && frb_pq_lt(pq->proc, pq->heap[1], elem)) {
pq->heap[1] = elem;
pq_down(pq);
}
/* else ignore the element */
return self;
}
pq.pop → elem
Returns the top element in the queue removing it from the queue.
/*
* call-seq:
* pq.pop -> elem
*
* Returns the top element in the queue removing it from the queue.
*/
static VALUE
frb_pq_pop(VALUE self)
{
PriQ *pq;
GET_PQ(pq, self);
if (pq->size > 0) {
VALUE result = pq->heap[1]; /* save first value */
pq->heap[1] = pq->heap[pq->size]; /* move last to first */
pq->heap[pq->size] = Qnil;
pq->size--;
pq_down(pq); /* adjust heap */
return result;
}
else {
return Qnil;
}
}
pq.size → integer
Returns the size of the queue, ie. the number of elements currently stored in the queue. The size of a PriorityQueue can never be greater than its capacity
/*
* call-seq:
* pq.size -> integer
*
* Returns the size of the queue, ie. the number of elements currently stored
* in the queue. The _size_ of a PriorityQueue can never be greater than
* its _capacity_
*/
static VALUE
frb_pq_size(VALUE self)
{
PriQ *pq;
GET_PQ(pq, self);
return INT2FIX(pq->size);
}
pq.top → elem
Returns the top element in the queue but does not remove it from the queue.
/*
* call-seq:
* pq.top -> elem
*
* Returns the top element in the queue but does not remove it from the
* queue.
*/
static VALUE
frb_pq_top(VALUE self)
{
PriQ *pq;
GET_PQ(pq, self);
return (pq->size > 0) ? pq->heap[1] : Qnil;
}