Blockland Forums > Modification Help

"Efficient" Queue

Pages: (1/2) > >>

DontCare4Free:

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?

Treynolds416:

Could you add a little more information maybe?

DontCare4Free:


--- Quote from: Treynolds416 on March 19, 2012, 04:09:58 PM ---Could you add a little more information maybe?

--- End quote ---
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.

Headcrab Zombie:

Anyways, I've used this for a few things

--- Code: ---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;
}
--- End code ---

Usage:

--- Code: ---%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
--- End code ---


DontCare4Free:


--- Quote from: Headcrab Zombie on March 19, 2012, 04:27:13 PM ---I've used this for a few things


--- Code: ----snip-

--- End code ---

Usage:

--- Code: ---%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
--- End code ---

--- End quote ---
Thanks. What's .onAdd for though?

Pages: (1/2) > >>

Go to full version