• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Generic Linked List

Status
Not open for further replies.
Level 24
Joined
Aug 1, 2013
Messages
4,657
Hi all.

I dont really know if such a thing exists like this but I just created it... dont know why.
If there does exist one, please link it.
Otherwise, just this:
JASS:
/! textmacro CREATE_LINKED_LIST takes NAME
library LL$NAME$
    globals
        
        integer             LL_$NAME$_NewIndex = 0
        boolean array       LL_$NAME$_IsOccupied
        
        integer array       LL_$NAME$_FirstIndex
        integer array       LL_$NAME$_LastIndex
        
        integer array       LL_$NAME$_PreviousIndex
        integer array       LL_$NAME$_NextIndex
        
    endglobals
    
    //System function
    function $NAME$_NewIndex takes nothing returns integer
        loop
            set LL_$NAME$_NewIndex = LL_$NAME$_NewIndex +1
            if LL_$NAME$_NewIndex >= 8191 then
                set LL_$NAME$_NewIndex = 1
            endif
            exitwhen not LL_$NAME$_IsOccupied[LL_$NAME$_NewIndex]
        endloop
        set LL_$NAME$_IsOccupied[LL_$NAME$_NewIndex] = true
        return LL_$NAME$_NewIndex
    endfunction
    
    //User function
    function $NAME$_CreateIndex takes integer id returns integer
        local integer newIndex = $NAME$_NewIndex()
        set LL_$NAME$_NextIndex[LL_$NAME$_LastIndex[id]] = newIndex
        set LL_$NAME$_PreviousIndex[newIndex] = LL_$NAME$_LastIndex[id]
        set LL_$NAME$_LastIndex[id] = newIndex
        return newIndex
    endfunction
    
    //User function
    function $NAME$_GetIndex takes integer id, integer index returns integer
        local integer i = 0
        local integer result = LL_$NAME$_FirstIndex[id]
        loop
            exitwhen i >= index
            set result = LL_$NAME$_NextIndex[result]
            if result == 0 then
                return -1
            endif
            set i = i +1
        endloop
        return result
    endfunction
    
    //User function
    function $NAME$_RemoveIndex takes integer id, integer index returns nothing
        local integer i = 0
        local integer remove = LL_$NAME$_FirstIndex[id]
        
        loop
            exitwhen i >= index
            set remove = LL_$NAME$_NextIndex[remove]
            set i = i +1
        endloop
        
        set LL_$NAME$_PreviousIndex[LL_$NAME$_NextIndex[remove]] = LL_$NAME$_PreviousIndex[remove]
        set LL_$NAME$_NextIndex[LL_$NAME$_PreviousIndex[remove]] = LL_$NAME$_NextIndex[remove]
        set LL_$NAME$_IsOccupied[remove] = false
        
        if remove == LL_$NAME$_LastIndex[id] then
            set LL_$NAME$_LastIndex[id] = LL_$NAME$_PreviousIndex[remove]
        endif
        if remove == LL_$NAME$_FirstIndex[id] then
            set LL_$NAME$_FirstIndex[id] = LL_$NAME$_NextIndex[remove]
        endif
    endfunction
endlibrary
//! endtextmacro

Sorry for no documentation but it should be very simple.
You create a new object for each linked list that you need.
Every Linked List Object (created using textmacros) has 8190 indexes.
Every Linked List Object creates a linked list for every id.
The actual data of each index can be created by the system or spell that uses the linked list.

You as map maker should be able to make sure that there will never be more than 8190 indexes.
 
Level 31
Joined
Jul 10, 2007
Messages
6,306
Here's is an example of my newer work concerning linked lists. The API provided in the following are rather standard for linked list libraries to date. You can use that API for a reference. The libraries are organized into 3 main parts. The core is the raw data and memory allocation, which are the arrays. Above those are the operations, like push and pop. Above those is error checking.

https://github.com/nestharus/JASS/blob/master/jass/Data Structures/List/1a1a/src/main.j
https://github.com/nestharus/JASS/blob/master/jass/Data Structures/List/1t1t/main.j
 
Status
Not open for further replies.
Top