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

[JASS] HashRecycler

Level 15
Joined
Nov 30, 2007
Messages
1,202
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. ^^

JASS:
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:
JASS:
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:
Top