1. Updated Resource Submission Rules: All model & skin resource submissions must now include an in-game screenshot. This is to help speed up the moderation process and to show how the model and/or texture looks like from the in-game camera.
    Dismiss Notice
  2. DID YOU KNOW - That you can unlock new rank icons by posting on the forums or winning contests? Click here to customize your rank or read our User Rank Policy to see a list of ranks that you can unlock. Have you won a contest and still havn't received your rank award? Then please contact the administration.
    Dismiss Notice
  3. The Lich King demands your service! We've reached the 19th edition of the Icon Contest. Come along and make some chilling servants for the one true king.
    Dismiss Notice
  4. The 4th SFX Contest has started. Be sure to participate and have a fun factor in it.
    Dismiss Notice
  5. The poll for the 21st Terraining Contest is LIVE. Be sure to check out the entries and vote for one.
    Dismiss Notice
  6. The results are out! Check them out.
    Dismiss Notice
  7. Don’t forget to sign up for the Hive Cup. There’s a 555 EUR prize pool. Sign up now!
    Dismiss Notice
  8. The Hive Workshop Cup contest results have been announced! See the maps that'll be featured in the Hive Workshop Cup tournament!
    Dismiss Notice
  9. Check out the Staff job openings thread.
    Dismiss Notice
Dismiss Notice
60,000 passwords have been reset on July 8, 2019. If you cannot login, read this.

[JASS] HashRecycler

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

  1. Pinzu

    Pinzu

    Joined:
    Nov 30, 2007
    Messages:
    1,177
    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