• 🏆 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!

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