1. The poll for our 11th Music Contest is up! Help us choose the most awesome cinematic tracks by casting a vote!
    Dismiss Notice
  2. Melee Mapping contest #3 - Poll is up! Vote for the best 4v4 melee maps!
    Dismiss Notice
  3. The 30th edition of the Modeling Contest is finally up! The Portable Buildings need your attention, so come along and have a blast!
    Dismiss Notice
  4. The Aftermath has been revealed for the 19th Terraining Contest! Be sure to check out the Results and see what came out of it.
    Dismiss Notice

[JASS] HashRecycler

Discussion in 'Submissions' started by Pinzu, Feb 12, 2019.

  1. Pinzu

    Pinzu

    Joined:
    Nov 30, 2007
    Messages:
    1,160
    Resources:
    3
    Spells:
    2
    Tutorials:
    1
    Resources:
    3
    This is for those that wish to use a hashtable over Table. It's only allowed to save data into the child key and thus its a fragile system not for the faint of heart.

    So if you prioritize safty and readabilty, and maintainability of code then you should stick with table, in fact i recomend it.^

    Edit: I'm not sure if the above statement is correct. So I'll leave it at that, use Table. ^^

    Code (vJASS):

    library HashRecyler

        struct HashRecyler extends array
    debug    readonly static integer counter    = 0
            readonly static hashtable ht = InitHashtable()
         
            static method operator[] takes integer k returns integer
                return LoadInteger(ht, -1, k)
            endmethod

            static method operator []= takes integer k, integer tb returns nothing
                call SaveInteger(ht, -1, k, tb)
            endmethod
         
            private static method onInit takes nothing returns nothing
                set thistype[0] = 1
            endmethod
         
            static method alloc takes nothing returns integer
                local integer k =  thistype[0]
                if (thistype[k] == 0) then
                    set thistype[0] = k + 1
                else
                    set thistype[0] = thistype[k]
                endif
    debug         set counter = counter + 1
                return k
            endmethod
         
            static method free takes integer k returns nothing
                set thistype[k] = thistype[0]
                set thistype[0] = k
                call FlushChildHashtable(ht, k)
    debug         set counter = counter - 1
            endmethod
        endstruct
    endlibrary
     

    example usage:
    Code (vJASS):

    struct S
    static method create takes nothing returns thistype
        return HashRecyler.alloc()
    endmethod
         
    method destroy takes nothing returns nothing
        call HashRecyler.free(this)
    endmethod

    method operator[] takes integer index returns integer
         return LoadInteger(HashRecyler.ht , this, index)
    endmethod
       
    method operator[]= takes integer index, integer element returns nothing
        call SaveInteger(HashRecyler.ht , this, index, element)
    endmethod
    endstruct
     
     
    Last edited: Feb 12, 2019