HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Script Idea: TimerCache

01-25-2010, 09:44 AM#1
Anachron
Uses my other idea AdvancedTable.

I wanted to make a new library that gives me timers and stores them so you don't have to create thousands of new timers ever and ever again. It just uses old timers which are stored.

It gives you a new timerhandle each time you use GetTimer().
ReleaseTimer() will destroy or store the timer.
Collapse JASS:
library AdvancedTable

    //: CnP from Vexorians Table. 
    //: Sorry dude, there is no other way.
    //=============================================================
    globals
        private constant integer MAX_INSTANCES=8100 //400000
        //Feel free to change max instances if necessary, it will only affect allocation
        //speed which shouldn't matter that much.
        
        private hashtable ht    
        // Actually this is the data holder
    endglobals
    //=========================================================

    private struct GTable[MAX_INSTANCES]

        method reset takes nothing returns nothing
            call FlushChildHashtable(ht, integer(this) )
        endmethod

        private method onDestroy takes nothing returns nothing
            call this.reset()
        endmethod

        //=============================================================
        // initialize it all.
        //
        private static method onInit takes nothing returns nothing
            set ht = InitHashtable()
        endmethod

    endstruct

    //: Setup textmacros for table creation.
    //! textmacro AdvancedTable_make takes name, valType, func, funcPref, hashType, paraType, keyType, key, value
    struct $name$Table extends GTable
    
        method operator [] takes $keyType$ key returns $valType$
            return Load$func$$funcPref$(ht, integer(this), $key$)
        endmethod

        method operator []= takes $keyType$ key, $paraType$ value returns nothing
            call Save$func$$funcPref$(ht,  integer(this)  ,$key$, $value$)
        endmethod

        method flush takes $keyType$ key returns nothing
            call RemoveSaved$hashType$(ht, integer(this), $key$)
        endmethod

        static method flush2D takes string firstkey returns nothing
            call $name$Table(- StringHash(firstkey)).reset()
        endmethod

        static method operator [] takes string firstkey returns $name$Table
            return $name$Table(- StringHash(firstkey) )
        endmethod

    endstruct
    //! endtextmacro
    
    //: Finally create our crazy tables.
    //                                  TableName   DataType    Func        Prefix      hashID      paraType    keyType     key     value
    //! runtextmacro AdvancedTable_make("Integer", "integer",  "Integer",  "",         "Integer",  "integer",   "integer",  "key",   "value")
    //! runtextmacro AdvancedTable_make("Boolean", "boolean",  "Boolean",  "",         "Boolean",  "boolean",   "integer",  "key",   "value")
    //! runtextmacro AdvancedTable_make("Unit",    "unit",     "Unit",     "Handle",   "Handle",   "unit",      "integer",  "key",   "value")
    //! runtextmacro AdvancedTable_make("Item",    "item",     "Item",     "Handle",   "Handle",   "item",      "integer",  "key",   "value")
    //! runtextmacro AdvancedTable_make("Timer",   "timer",    "Timer",    "Handle",   "Handle",   "timer",     "integer",  "key",   "value")
endlibrary

Collapse JASS:
library TimerCache initializer init requires AdvancedTable

    globals
        private TimerTable      Timers          = 0
        private IntegerTable    TimerToSlot     = 0
        private integer         index           = 0
        
        private TimerTable      FreeTimers      = 0
        private integer         freeIndex       = 0
        
        private constant        integer MAX_FREE_TIMERS = 8191
    endglobals
    
    private function NewTimer takes nothing returns timer
        set Timers[index]   = CreateTimer()
        set TimerToSlot[GetHandleId(Timers[index])] = index
        set index           = index +1
        
        return Timers[index -1]
    endfunction
    
    private function RenewTimer takes integer i returns timer
        set Timers[index]   = FreeTimers[i]
        set TimerToSlot[GetHandleId(Timers[index])] = index
        set index           = index +1
        
        set FreeTimers[i] = null
        set freeIndex = freeIndex -1
        
        return Timers[index -1]
    endfunction
    
    function GetTimer takes nothing returns timer
        if freeIndex > 0 then
            return RenewTimer(freeIndex)
        else
            return NewTimer()
        endif
    endfunction
    
    function ReleaseTimer takes timer t returns nothing
        local integer slot = TimerToSlot[GetHandleId(t)]
        
        set TimerToSlot[GetHandleId(t)] = TimerToSlot[index]
        set Timers[slot] = Timers[index]
        
        set TimerToSlot.flush(GetHandleId(Timers[index]))
        set Timers[index] = null
        
        set index = index -1
        
        if freeIndex < MAX_FREE_TIMERS then
            set FreeTimers[freeIndex] = t
            set freeIndex = freeIndex +1
        else
            call DestroyTimer(t)
        endif
    endfunction
    
    private function init takes nothing returns nothing
        set Timers = TimerTable.create()
        set FreeTimers = TimerTable.create()
    endfunction

endlibrary

Collapse JASS:
library TimerTest initializer init requires TimerCache
    globals
        private integer handleIDOne = 0
        private integer handleIDTwo = 0
    endglobals
    
    private function checkResult takes nothing returns nothing
        set handleIDTwo = GetHandleId(GetExpiredTimer())
        call BJDebugMsg("Test: " + I2S(handleIDOne) + " - " + I2S(handleIDTwo))
    endfunction
    
    private function testAgain takes nothing returns nothing
        set handleIDOne = GetHandleId(GetExpiredTimer())
        call ReleaseTimer(GetExpiredTimer())
        
        call TimerStart(GetTimer(), false, 0.01, function checkResult)
    endfunction
    
    private function TestTimers takes nothing returns nothing
        local timer t = GetTimer()
        
        call TimerStart(t, false, 0.01, function testAgain)
    endfunction
endlibrary

Please post suggestions / feedback comments! :) c
01-25-2010, 10:55 AM#2
Themerion
How does this differ from TimerUtils?
01-25-2010, 11:15 AM#3
Anachron
Because you can do it with everything.
You can also easily modify it to use this as lightning, cache, fogcache or whatever.

It basically stored every type you need into a table and gives it to you when required. Timer was sort of test.
01-25-2010, 02:49 PM#4
Tot
set TimerToSlot[GetHandleId(Timers[index])] = index
shure that handleids are always below 8190?
cause assigning non-existant array-fields crashes the game
01-25-2010, 02:55 PM#5
Anachron
Its a hashtable dude xD
01-25-2010, 03:05 PM#6
Tot
ohhhhhhh.............
01-25-2010, 03:11 PM#7
Anachron
Btw, get your ass into the CustomInventory thread. And give constructive critique! =)
01-25-2010, 03:12 PM#8
Tot
is on hive....

what happend to your missile-sys?
01-26-2010, 08:29 AM#9
Anachron
Wehrm, my pc crashed and I lost the last version of it. Wanna have it? Its working, but not perfect.
01-26-2010, 12:03 PM#10
Tot
yes...give it to me...*mein schatz*
01-26-2010, 12:05 PM#11
Anachron
Okay, will upload it tomorrow. :) Glad finally someone wanna see it xD
01-26-2010, 04:19 PM#12
Tot
am i allowed to improve/change/whatever it?
01-26-2010, 04:39 PM#13
Anachron
Sure, would be nice if you atleast put my name into the system somewhere.
01-26-2010, 06:30 PM#14
Anitarf
You know, we have this nice little feature on this site called private messages. Let's try to keep any further replies in this thread on topic.