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

[Snippet] Base Indexer

Level 7
Joined
Apr 30, 2011
Messages
359
JASS:
//========================================================================================
//      
//      Integer Indexer
//      -*- overcold_ice -*-
//      
//     -[*] Requirements:
//          - JNGP
//          - latest version of JassHelper
//      
//     -[*] Optional Requirements:
//          - /*New*/ Table             [url]http://www.hiveworkshop.com/forums/jass-resources-412/snippet-new-table-188084/[/url]
//      
//          For your almost unlimitted integer-indexing needs
//      Supports large and small indexing.
//      
//     -[*] API:
//      
//      function GetIntLId   takes integer i returns integer
//      function GetIntByLId takes integer i returns integer
//      
//      function GetIntSId   takes integer i returns integer
//      function GetIntBySId takes integer i returns integer
//      
//========================================================================================
library IntegerIndexer requires optional /*New*/Table
    
    //====================================================================================
    //  G L O B A L S
    //====================================================================================
    
    //===---
    // Large Index Globals
    //===---
    globals
        private key       id
        private key       int
        
        private integer   c
    endglobals
    
    static if LIBRARY_Table then
        private module TableInit
            private static method onInit takes nothing returns nothing
                set .t = TableArray [2]
            endmethod
        endmodule
    endif
    
    private struct T
        static if LIBRARY_Table then
            static TableArray t
            implement TableInit
        else
            static hashtable t
        endif
    endstruct
    
    //===---
    //  Small Index Globals
    //===---
    
    globals
        private integer array value
        private integer array index
        private integer       count = 0
    endglobals
    
    //====================================================================================
    //  A P I
    //====================================================================================
    
    //===---
    //  Large Index API
    //===---
    function GetIntLId takes integer i returns integer
        static if LIBRARY_Table then
            if T.t [id].has(i) then
                return T.t [id] [i]
            else
                set T.t  [id] [i] = c
                set T.t [int] [c] = i
            endif
        else
            if HaveSavedInteger(T.t, id, i) then
                return LoadInteger(T.t, id, i)
            else
                call SaveInteger(T.t,  id, i, c)
                call SaveInteger(T.t, int, c, i)
            endif
        endif
        
        set c = c + 1
        
        return c - 1
    endfunction
    
    function GetIntByLId takes integer i returns integer
        static if LIBRARY_Table then
            return T.t [int] [i]
        else
            return LoadInteger(T.t, int, i)
        endif
    endfunction
    
    //===---
    //  Small Index API
    //===---
    function GetIntSId takes integer i returns integer
        if index [i] != 0 then
            return index [i]
        else
            set count         = count + 1
            set index     [i] = count
            set value [count] = i
        endif
        
        return count
    endfunction
    
    function GetIntBySId takes integer i returns integer
        return value [i]
    endfunction
endlibrary
 
Last edited:
Level 7
Joined
Oct 11, 2008
Messages
304
Sorry but your Docs lacks.

JASS:
//      function BIndex_$TYPE$  takes $VAR$   v returns integer
//      function Get$TYPE$BId   takes $VAR$   v returns integer
//      function Get$TYPE$ByBId takes integer i returns $VAR$
//
//      $TYPE$ and $VAR$:
//          - Int  (integer)
//          - Real (real)
//          - Str  (string)

/* VS */

//      function BIndex_Int  takes integer   v returns integer
//      function GetIntBId   takes integer   v returns integer
//      function GetIntByBId takes integer   i returns integer
//
//      function BIndex_Real  takes real    v returns integer
//      function GetRealBId   takes real    v returns integer
//      function GetRealByBId takes integer i returns real
//
//      function BIndex_Str  takes string   v returns integer
//      function GetStrBId   takes string   v returns integer
//      function GetStrByBId takes integer  i returns string
 
Level 7
Joined
Apr 30, 2011
Messages
359
this have some pros . . . .
mine can handle integers/strings with same value . . .
they have different indexes, and this will make indexing more safe . .
 
Level 7
Joined
Apr 30, 2011
Messages
359
removed that RealIndexer . . .

but shouldn't indexers supposed to be more dynamic?
returning different indexes for same values will be better than returning same indexes in some cases . .
 
Level 7
Joined
Apr 30, 2011
Messages
359
here's what i meant:
JASS:
function DoubleIndex takes integer i1, integer i2 returns integer
    local integer id1 = IndexInteger(i1)
    local integer id2 = IndexInteger(i2)
    
    if id1 == id2 then
        call BJDebugMsg("same index")
        return -1
    else
        call BJDebugMsg("not same index")
    endif
    
    return id1 * id2
endfunction

function Test takes nothing returns nothing
    call DoubleIndex(1, 1)
    call DoubleIndex(2, 4)
endfunction

// Test will shows:
//   same index
//   not same index
it uses the same index for each integers of values X

get it ?_?
 
Level 7
Joined
Apr 30, 2011
Messages
359
no . . .
from the name itself (RawCode), we know that it'll index one value once, not twice or more . .
and each time it indexes, it checks whether the value had an index/not, if so return the index, otherwise return a new one . .
the same goes to StringIndexer . .
 
Level 7
Joined
Apr 30, 2011
Messages
359
JASS:
set id1 = MyIntegerIndexer(1)
set id2 = MyIntegerIndexer(1)

// id1 and id2 won't have the same value
//   using BasicIndexer they will have these values
//   id1 => 1
//   id2 => 9001 (+9000 from original index)
//   id3 => 18001
//   . . . . . . . . .
 
Level 7
Joined
Apr 30, 2011
Messages
359
that makes sense for safety reasons . .
especially when it indexes unknown values that mostly can have a same value . .

and i'll fix that 9000 modifier thing tomorrow
 
Level 7
Joined
Apr 30, 2011
Messages
359
oh god . . . . .
i don't believe this #.#
finally you awake me out of my dreams . . .
that multi-indices won't make any sense indeed . .
sorry xD

i'll add other features next day . . .
 
You forgot "InitHashtable".

Honestly I don't see the purpose of having a large ID. If the ID doesn't fit into array bounds then it's not a useful ID because the user should then just be using the direct RawCode value in the first place.

For smaller ID's you have RawCodeIndexer.

I recommend to focus on more productive systems that don't already exist, and let me graveyard this finally ;)
 
Top