• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

How to pick all struct instance?

Status
Not open for further replies.
Level 23
Joined
Feb 6, 2014
Messages
2,466
As of now, what I did was to keep track of the max Instance and used
JASS:
set tempStruct = this         //temporarily store current instance
loop
    exitwhen i > maxInstance
    set this = i
    //Do Struct actions here
    //.....
    //end struct actions
    set i = i + 1
endloop
set this = tempStruct        //restore current instance
But it feels inefficient and I can't figure out how to keep track of the max Instance.

Now, if there is a better way to pick all struct instance, please let me know, if not then how can I keep track of the maxInstace? On allocation, I used:
JASS:
set i = this
if i > maxInstance then
    set maxInstance = i
endif
but in deallocation, how will the maxInstance change? The method I currently have in mind is keep decrementing maxInstance and checking if a certain member is null or not. If it is not null, then that is the maxInstance but again, I feel there is a better way to do that.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,285
A linked list member would work.

Basically the struct is given a member variable of a reference to another struct.instance of its type. A static struct reference member keeps track of the head of the list. When allocation occurs you set the member to point at the head of the list and the head of the list to point at the newly allocated instance. When destruction occurs you search down from the head until you find the instance being destroyed and then set the reference member of the instance above it to point at what that member of the being destroyed instance points to. Fast allocation, slower destruction.

An improvement is to have two struct reference members in the struct for each direction to form a doubly linked list. This makes insertion and deletion slightly more complicated but solves the inefficiency with deletion (no searching for the instance being removed).

Ideally this linked list functionality should be implemented as an extendable or implementable struct component or even a macro. That would mean one implementation could be easily used for any number of different struct types to have them joined in a linked list.
 
Status
Not open for further replies.
Top