- Joined
- Jan 1, 2009
- Messages
- 1,615
e: I didnt find the other List when I was searching
I needed a simple Array without gaps, didnt find one, made one.
Basically a Table that also saves the position of each entry, yes its as awesome as you think.
I needed a simple Array without gaps, didnt find one, made one.
Basically a Table that also saves the position of each entry, yes its as awesome as you think.
JASS:
library ConsistentArray requires Table
//******************************************************************************
//* By Frotty
//*
//* Bigass Credit to Nestharus because hes so annoying
//*
//* This library is basically an array without gaps.
//* Every values position gets saved in a Table, so that you have aconsistent
//* array, making looping throw it easy.
//*
//* ===How to use===
//* Create a new List somewhere:
//* ConsistentArray myArray = ConsistentArray.create()
//*
//* To add a Value simply use the add() method
//* myArray .add(myStruct)
//*
//* To get the value of a certain index use the [] operator
//* myArray [1]
//* This would return the stored integer at position 1
//*
//* To remove a value use the remove() method
//* myArray .remove(myStruct)
//* Because STructIDs are unique and the List saves the position of the struct,
//* it gets removed and all values with a higher index get moved down.
//*
//* In a loop you often want to stop at the end of the list, use the readonly size variable for that
//* This library also includes a simple implementation of a group enumeration
//********************************************************************************
globals
// How many Members a List can have. If you want to save structs this shouldnt be higher than 8910
private constant integer MAX_MEMBERS = 8910
endglobals
struct ConsistentArray
Table objects
readonly integer size = 0
static method create takes nothing returns thistype
local thistype this = thistype.allocate()
set objects = Table.create()
return this
endmethod
method add takes integer value returns integer
if size >= MAX_MEMBERS then
call BJDebugMsg(SCOPE_PREFIX + " Max members reached: " + I2S(MAX_MEMBERS) )
return 8910
endif
set objects[MAX_MEMBERS + value] = size
set objects[size] = value
set size = size + 1
return size - 1
endmethod
method operator [] takes integer index returns integer
if size > 0 then
return objects[index]
else
call BJDebugMsg( SCOPE_PREFIX + " Cant get value because size is 0" )
return 0 // 0 == no Struct
endif
endmethod
method remove takes integer value returns nothing
local integer position = objects[MAX_MEMBERS + value]
if size > 0 then
loop
set objects[position] = objects[position+1]
set objects[MAX_MEMBERS + objects[position]] = position
set position = position + 1
exitwhen position >= size
endloop
set size = size - 1
endif
endmethod
endstruct
endlibrary
Last edited: