Author Topic: CODE RESOURCE: Array Stack  (Read 1667 times)

Several times I've needed a 'Stack' object where I can add objects or other values to it, remove them and get them from any position in the stack. This functions like a SimGroup, except it can hold any values you need it to or store objects.

Quote
Create a stack by this method: %stack = new ScriptObject(){class = ArrayStackSO;count = 0;};

ArrayStackSO::addValue() - Adds a value to the end of a stack.
ArrayStackSO::delValue() - Deletes value [%index] from the stack. 0 is the first value.
ArrayStackSO::delValueID() - Deletes ID [%obj] from the stack, if it exists. Shows a warning if [%obj] is not in the stack.
ArrayStackSO::getValue() - Returns value [%index] from the stack (or -1 if an error occurred). 0 is the first value.
ArrayStackSO::dumpStack() - Debug messages. Echos all values in stack + stack count.

Use it how you want to, in any mod or project. Remember to rename the scriptobject (replace ArrayStackSO with a new name) to prevent conflicts with other add-ons. Credit not required but a mention somewhere visible would be nice.

This is not a functional Add-On, it will not do anything if you just enable it in the Add-Ons menu.

ArrayStackSO.cs

No-one? I've found this quite useful for a replacement to Team DM clientteammanager and for other projects.

You'll find very similar code in both my Portals and Doors Add-Ons.

Tom

BUMP

But I was wondering if it would be possiable to change the name of the stack. More explanation would be appreciated.

For examples of its use, try looking at the code in Team Deathmatch, Wrench Events or Portals.

To use this:
Use Find and Replace to replace "ArrayStackSO" with the name you want. ("TeamStackSO","pgunStackSO")
Create the scriptobject. (Replace Bold with the name you find/replaced and the variable you will need later
Quote
$ReferenceToStack = new ScriptObject(){class = ArrayStackSO;count = 0;};
To add a value to the stack:
Code: [Select]
$ReferenceToStack.addValue("Hello"); //Adds the string as the first value
$ReferenceToStack.addValue(3); //Adds the number three as the second
$ReferenceToStack.addValue(%object); //Adds the variable or object as the third
To delete a value from the stack:
Code: [Select]
$ReferenceToStack.delValue(1); //The second value in the stack, "3", is deleted
$ReferenceToStack.delValueID("Hello"); //The first value which matches "Hello" is the first one in the stack. This is deleted.
To get a value from the stack:
Code: [Select]
echo($ReferenceToStack.getValue(0)); //Returns the first value in the stack, %object (The other two were deleted by the above)
Try using each of the above lines in the console. Use
Code: [Select]
$ReferenceToStack.dumpStack();between each one to see what is currently in the stack so you have some idea of what is 'going on'.

Tom

Ok thanks for the help. Have a  :cookie:

If a value is the contents of an array index, then shouldn't the delValue(%x) method remove the index(es) whose content is equal to %x? Likewise, the delValueID(%x) method should remove the index at position %x.

Also, this might be helpful to add:
Code: [Select]
// ArrayStackSO::getIdByValue()
// %this: Index of stack, %val: Value to search.
// Returns the position of the array index whose content is equal to %val. Returns -1 if %val is not found.
function ArrayStackSO::getIdByValue(%this, %val)
{
   for(%i = 0; %i < %this.count; %i++)
   {
      %currval = %this.value[ %i ];
      if( %currval $= %val )
         return %i;
   }
   return -1;
}
« Last Edit: December 31, 2007, 02:54:58 PM by exidyne »

Tom

How would I turn the output into a variable?
If a value is the contents of an array index, then shouldn't the delValue(%x) method remove the index(es) whose content is equal to %x? Likewise, the delValueID(%x) method should remove the index at position %x.

Also, this might be helpful to add:
Code: [Select]
...
Also can I put whats in the ArrayStackSO.cs into my script?

Looks more like an array or list than a stack, from a Java coders point of view, where a stack involves a "pop" function.

And I don't think you can delete stuff from the middle of a java stack...

Yeah, it's definitely not a stack.
Quote from: Wikipedia
In computer science, a stack is an abstract data type and data structure based on the principle of Last In First Out (LIFO).

It would be really easy to make a stack with methods to push and pop values, and I had to create one for my Befunge interpreter. However, it's unnecessarily complicated use a stack in Blockland for most things when you have the ability to use other methods.