AllocList

AGD

AGD

Level 13
Joined
Mar 29, 2016
Messages
677
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
 
Top