• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[JASS] The First Steps of a pseudo-array system

Status
Not open for further replies.
Level 16
Joined
Feb 22, 2006
Messages
960
Thats what i got so far, i added textmacros for more variable types which will be suported in the future. it only supports integers actually

the problem is, that u only can have actually 1 pseudo array (will improve system for more)

here the code:
JASS:
//****************************************************************
//* Pseudo-Array System [v0.1]
//*       By xD.Schurke
//*       06.01.2009
//****************************************************************
//! textmacro Arrays takes NAME, TYPE
library $NAME$arrays

globals
    private constant integer max = 8190
    private $NAME$Array $NAME$Object
endglobals

struct $NAME$Array
    $TYPE$ array $NAME$ [max] 
    integer $NAME$counter = 0
    //initializes the new pseudo-array
    static method new takes nothing returns $NAME$Array
        local $NAME$Array dat = $NAME$Array.allocate()
        set $NAME$Object = dat
        return dat
    endmethod
    
    //adds a value to the pseudo-array
    static method add takes $TYPE$ var returns nothing
        set $NAME$Object.$NAME$[$NAME$Object.$NAME$counter] = var
        set $NAME$Object.$NAME$counter = $NAME$Object.$NAME$counter + 1      
    endmethod
    //gets the value at the index u want
    static method get takes integer index returns $TYPE$
        return $NAME$Object.$NAME$[index]
    endmethod
    
    //sorts the values in the pseudo-array
    static method sort takes integer count returns nothing
    local integer i = 0
    local integer j = 0
    local integer k = 0
    if count > max then
        return
    endif
    loop
        set j = 0

        loop
        if $NAME$Object.$NAME$[j] > $NAME$Object.$NAME$[j+1] then
            set k = $NAME$Object.$NAME$[j]
            set $NAME$Object.$NAME$[j] = $NAME$Object.$NAME$[j+1]
            set $NAME$Object.$NAME$[j+1] = k
        endif
        set j = j + 1
        exitwhen j >= count - i
        endloop

        set i = i + 1
        exitwhen i >= count
    endloop
    endmethod
    //searchs for a value in the pseudo-array and returns the position of the value
    static method seach takes $TYPE$ var returns integer
        local integer left = 0
        local integer right = $NAME$Object.$NAME$counter - 1
        local integer mid = 0
        loop
            set mid = R2I((I2R(left+right)/2)+0.5)
            if $NAME$Object.$NAME$[mid] == var then
                return mid
            elseif $NAME$Object.$NAME$[mid] > var then
                set right = mid - 1
            elseif $NAME$Object.$NAME$[mid] < var then
                set left = mid + 1
            endif
            exitwhen left >= right
        endloop
        return 0
    endmethod
endstruct
endlibrary
//! endtextmacro
//! runtextmacro Arrays("Integer","integer")
 
Level 16
Joined
Feb 22, 2006
Messages
960
true, i will contact an admin,
this sys could be usefull (if i improve it xD)... u could manage to handle big amounts of datas and to search single parts if u need them, for example for a rpg if u store 100 items its much faster to find a specific item then with the linear way (you only need between 4-5 steps for example if its 99) with the linear way you would need 99 steps
 
Status
Not open for further replies.
Top