• 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.

AllocList

Status
Not open for further replies.

AGD

AGD

Level 16
Joined
Mar 29, 2016
Messages
688
An allocation method that automatically adds nodes to a doubly-linked list using the fewest amount of variables possible. Can especially be useful for some spells.

Here goes
JASS:
library AllocList


    module AllocList

        private thistype array _prev
        private thistype array _next

        method operator prev takes nothing returns thistype
            return _prev[this]
        endmethod
        method operator next takes nothing returns thistype
            return _next[this]
        endmethod

        static method allocate takes nothing returns thistype
            local thistype node = _next[1]
            set _next[1] = _next[node]
            set _next[node] = 0
            set _prev[node] = _prev[0]
            set _next[_prev[0]] = node
            set _prev[0] = node
            return node
        endmethod

        method deallocate takes nothing returns nothing
            set _next[_prev[this]] = _next[this]
            set _prev[_next[this]] = _prev[this]
            set _next[this] = _next[1]
            set _next[1] = this
        endmethod

        private static method onInit takes nothing returns nothing
            local thistype node = JASS_MAX_ARRAY_SIZE - 2
            set _next[node] = 0
            set node = node - 1
            loop
                exitwhen node == 0
                set _next[node] = node + 1
                set node = node - 1
            endloop
        endmethod

    endmodule


endlibrary


Note: Allocated nodes start at 2, not 1
 
Status
Not open for further replies.
Top