Thread: Table 3.1
View Single Post
Old 07-03-2008, 02:38 AM   #2
Here-b-Trollz
Corkscrew Chainsaw!!!
 
Join Date: Jun 2006
Posts: 711

Here-b-Trollz has a spectacular aura about (149)

Hero Contest #2 - 2nd Place

Default

Hmm, minimal but also possible... what about this?

Collapse JASS:
library Table initializer init
//***************************************************************
//* Table object
//* ------------
//*
//*   set t=Table.create() - instanceates a new table object
//*   call t.destroy()     - destroys it
//*   t[1234567]           - Get value for index 1234567
//*                          (zero if not assigned previously)
//*   set t[12341]=32      - Assigning it.
//*   call t.flush(12341)  - Flushes the stored value, so it
//*                          doesn't use any more memory
//*   call t.reset()       - Flushes the whole contents of the
//*                          Table.
//*   call t.destroy()     - Does reset() and also recycles the id.
//*
//*
//***************************************************************

//=============================================================
    globals
        private constant integer MAX_INSTANCES=400000

    //=========================================================
        private gamecache gc
    endglobals

    struct Table[MAX_INSTANCES]
        private string index=I2S(this)
        method operator [] takes integer key returns integer
            return GetStoredInteger(gc,.index,I2S(key))
        endmethod

        method operator []= takes integer key, integer value returns nothing
            call StoreInteger(gc,.index,I2S(key), value)
        endmethod

        method flush takes integer key returns nothing
            call FlushStoredInteger(gc,.index,I2S(key))
        endmethod

        method reset takes nothing returns nothing
            call FlushStoredMission(gc,.index)
        endmethod

        private method onDestroy takes nothing returns nothing
            call FlushStoredMission(gc,.index)
        endmethod
    endstruct


    private function init takes nothing returns nothing
        call FlushGameCache(InitGameCache("libtable.gc"))
        set gc=InitGameCache("libtable.gc")
    endfunction
endlibrary

Seems to work for me, and it saves a function call. Although... i suppose it would depend on whether or not your table indexes go up past one array's worth.
__________________
By reading this signature, you agree that I cannot be held accountable for anything that I might say or do.
Here-b-Trollz is offline   Reply With Quote