Author Topic: "Efficient" Queue  (Read 659 times)

The main way I can think of right now is a single-linked list, but that would require N objects. I guess they could be recycled, but it still feels inefficient. Any alternative ideas?
« Last Edit: March 19, 2012, 02:14:20 PM by DontCare4Free »

Could you add a little more information maybe?

Could you add a little more information maybe?
Code in various places add stuff to the queue. Meanwhile, another thing is processing the queue in order, and then calls a callback for each processed queue item.

Anyways, I've used this for a few things
Code: [Select]
function Queue::onAdd(%this)
{
%this.nextOut = 0;
%this.count = 0;
}

function Queue::push(%this, %data)
{
if(%data $= "")
{
echo("Empty value pushed, ignoring");
backtrace();
return;
}
%this.data[%this.count] = %data;
%this.count++;
}

function Queue::pop(%this)
{
if(%this.nextOut >= %this.count)
return "";
else
{
%r = %this.data[%this.nextOut];
%this.data[%this.nextOut] = "";
%this.nextOut++;
return %r;
}
}

function Queue::peek(%this)
{
return %this.data[%this.nextOut];
}

function Queue::getSize(%this)
{
return %this.count - %this.nextOut;
}

function Queue::clear(%this)
{
for(%i=0;%i<%this.count;%i++)
%this.data[%i] = "";
%this.count = 0;
%this.nextOut = 0;
}

Usage:
Code: [Select]
%queue = new ScriptObject(){ class=Queue; }; //Create a new queue
%queue.push(%data); //Add %data to the queue
%queue.pop(); //Removes and returns next item from the queue
%queue.peek(); //Returns the next item in the queue, without removing it
%queue.getSize(); /Returns the number of items in the queue
%queue.clear(); //remove everything from the queue
« Last Edit: March 19, 2012, 04:30:40 PM by Headcrab Zombie »

I've used this for a few things

Code: [Select]
-snip-

Usage:
Code: [Select]
%queue = new ScriptObject(){ class=Queue; }; //Create a new queue
%queue.push(%data); //Add %data to the queue
%queue.pop(); //Removes and returns next item from the queue
%queue.peek(); //Returns the next item in the queue, without removing it
%queue.getSize(); /Returns the number of items in the queue
%queue.clear(); //remove everything from the queue
Thanks. What's .onAdd for though?
« Last Edit: March 19, 2012, 04:32:42 PM by DontCare4Free »

So it sets some values to 0 when the object is created.

So it sets some values to 0 when the object is created.
This

I didn't think it was necessary, but I adapted it off of code for a stack provided by others who know what they're doing, so I left it there.

Just automatically set count and nextOut to 0 when you create the object. I didn't think it was necessary, but I adapted it off of code for a stack provided by others who know what they're doing, so I left it there.
Oh, okay. Didn't even know that there was such a feature.

It actually looks important because of these lines:
   %this.data[%this.count] = %data;
   %this.count++;
Because without it, it could end up like this:
%this.data = %data;
Rather than:
%this.data0 = %data;

for the first thing pushed.

You want that 0 there.

It actually looks important because of these lines:
   %this.data[%this.count] = %data;
   %this.count++;
Because without it, it could end up like this:
%this.data = %data;
Rather than:
%this.data0 = %data;

for the first thing pushed.

You want that 0 there.
Yeah, I know about that catch, I just didn't see when .onAdd was called. I've always worked around that by adding 0 first.