Skip to content
DarkThrone edited this page May 26, 2011 · 1 revision

Docs

Here it is the whole documentation of Q. These methods are pretty self explanatory, but, well, a little bit more info wouldn't hurt anyone.

The queue is implemented to shrink it's size when the collection size is 10% smaller than the array size. Yo can tweek these value in the source code to match your needs.

...
if(l < q.length*0.9){
    //Remove unnecessary and empty elements
    q = q.slice(0,l);
}
...

Constructor

The library only has one constructor. Simple and quick.

Q()

Creates a Q instance with an empty queue and of size 0.

var q = Q();

Fields

The class has only 2 private fields

q[]

An array that contains the whole collection.

l

Integer that holds the offset value of the queue.

Methods

enqueue(elem)

The enqueue method takes a generic element and puts it in the queue.

q.enqueue('example');
q.enqueue(2);
q.enqueue(new Date());
q.enqueue([1,2,3]);

dequeue()

The dequeue method returns the first element enqueued and removes it from the collection

var item  = q.dequeue();
var item2 = q.dequeue();

if(item == item2){
    alert('true');
}else{
    //These part of the block will be triggered
    alert('false');
}

peek()

Returns the first enqueued element of the collection.

var item  = q.peek();
var item2 = q.peek();

if(item == item2){
    //These part of the block will be triggered
    alert('true');
}else{
    alert('false');
}

isEmpty()

If the queue is empety returns true, otherwise returns false.

...
if(q.isEmpty()){
...

size()

Returns the real size of the collection (not the size of the array)

...
if(q.size() < 5)
...
for(var j = 0; j < q.size(); j++)
...

Clone this wiki locally