Stack

I decided to implement – creating from scratch – a Stack collection in BlitzMax.

A stack is a ‘data structure’ – a way to collect data – and its features is to be a LIFO (Last In First Out) collection.


This means that the last thing added (push), is the first to gets pulled (pop)

BlitzMax has only Arrays, Lists and Maps as base collection structures.

My own implementation has the following methods


  • Push(object) – insert an object at the ‘top’ of the stack
  • Pop() – it returns the Object stored
  • ToList() – it returns a list containing all the object in the stack
  • ToArray() – it returns the array of objects
  • Clear() – it clears the stack
  • Size() – it returns the number of items in it
  • Contains(object) – returns TRUE if the stack contains the object
  • FindIndex(object) – returns the INDEX position in the stack of the object
  • Get(ID) – returns the Object stored at the ID position in the stack
  • Set(ID,Object) – set/replace the object at the ID position in the stack
  • InsertAt(index,object) – inserts at index position another object
  • Remove(index) – removes at index position the object
  • RemoveObject(object) – removes the object from the stack
  • Sort() – to sort the items in the stack
  • Copy() – to create a copy of the stack
  • Extract(from,to) – to extract – in a new stack – specific items
  • Split(from) – to ‘split’ the original stack in 2, losing the items
  • Join(stack) – to joins to 2 stack into one


Moreover it implements an Enumerator so it’s possibile to use it with the constructor FOR.. EACHIN to iterate with the collected objects.

Here the source code – I will include it in the LIB section as soon as possible.



I’m thinking to add some other methods like JOIN to add another stack, and SORT.


EXAMPLE


Edit:

I’ve added new methods like JOIN, SPLIT, EXTRACT, SORT, COPY and added in source description/documentation.

On my own BlitzMax installation I’ve made a BRL.STACK module and now it’s part of the ‘standard’ command set. Here I’ve added the ‘module source code’ (just remember to create in BlitzMax/mod/BRL another folder (Stack.mod) and save the source code in it as stack.bmx.

Then compile and rebuild documentation.


local_offerevent_note May 4, 2019

account_box degac


local_offer

Leave a Reply

Your email address will not be published.