HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Fun with operators

03-22-2007, 02:34 AM#1
Vexorian
Collapse JASS:
globals
   gamecache cscache
endglobals

struct IntTable

    method operator[] takes string s returns integer 
        return GetStoredInteger(cscache,I2S( integer(this)),s)
    endmethod

    method operator[]= takes string s, integer k returns nothing
        call StoreInteger(cscache,I2S( integer(this)),s,k)
    endmethod

    method exists takes string s returns boolean
        return HaveStoredInteger(cscache,I2S( integer(this)),s)
    endmethod

    method flush takes string s returns nothing 
        call FlushStoredInteger(cscache,I2S( integer(this)),s)
    endmethod

    method onDestroy takes nothing returns nothing
        call FlushStoredMission(cscache, I2S( integer(this)) )
    endmethod

endstruct


function testIntTables takes nothing returns nothing
 local IntTable o=IntTable.create()

    set o["A"]=12
    set o["Fruit"]=45

    call BJDebugMsg(I2S(o["A"]+o["Fruit"]))

    call o.destroy()
endfunction
03-22-2007, 02:42 AM#2
Anopob
I don't get it :(
03-22-2007, 05:19 AM#3
Daelin
Defining your own operators... I see you're taking the whole OOP programming really seriously. Great job!

~Daelin
03-22-2007, 05:47 AM#4
PipeDream
yeah looks nice.
03-22-2007, 05:51 AM#5
grim001
how about flushing if you set it to zero rather than a seperate flush command?
03-22-2007, 01:04 PM#6
Daelin
Because "0" value for integers and reals are actual values, and not just "null" (as in nothing) like for handles. Therefore, this way is so much better.

~Daelin
03-22-2007, 01:58 PM#7
blu_da_noob
Hey look, now we have dictionaries. Lawl.

Good job Vex.
03-23-2007, 02:59 AM#8
BlinkBoy
can't wait to see operator overloading of mathemathical oprators, it would be nice for vectors and other structures, such as complexes, quaternions, etc.
03-23-2007, 04:15 AM#9
Tiki
How about learn ror then effin jass?
03-23-2007, 12:13 PM#10
grim001
Quote:
Originally Posted by BlinkBoy
can't wait to see operator overloading of mathemathical oprators, it would be nice for vectors and other structures, such as complexes, quaternions, etc.

yes I could see the vector engine needing an update once this is in.
03-23-2007, 04:01 PM#11
Toadcop
it's a cache stuff so it's shit =) i mean to use gamecache for some dynamic things like vectors is crap and need to be a full idiot... if you can use simple arrays and vars !
03-23-2007, 04:27 PM#12
Vexorian
Gamecache is still the best for string hashing, sorry but it is true.

I'd like to see an alternative for gamecache that took strings for keys haha, just the fact that strings leak would make it 10 times worse than gamecache.

Using normal arrays and vars is not the best either.

---
This is not a vector, as blu said it is a dictionary... Not using gamecache for an string dictionary is "crap and need to be a full idiot"
03-23-2007, 04:45 PM#13
Vexorian
more fun

Collapse JASS:




interface comparable
    method operator<
endinterface

interface comparablearray
    method operator[] takes integer i returns comparable 
    method operator[]= takes integer i, comparable v returns nothing

    method size takes nothing returns integer
endinterface


struct PriorityQueue
   private comparablearray v
   private integer limit
   private integer n=0

   static method createFrom takes comparablearray source returns PriorityQueue
    local PriorityQueue p=PriorityQueue.create()
       set p.v=source
       set p.limit=source.size()
    return p
   endmethod

   method push takes comparable x returns boolean
    local integer i
    local integer p
       if (this.n==this.limit) then
           return false
       endif

       set i=this.n
       set this.n =i+1

       loop
           exitwhen (i==0) //root
           set p=(i-1)/2
           if (this.v[p] < x) then
               set this.v[i]=this.v[p]
               set i=p
           else
               exitwhen true
           endif
       endloop
       
       set this.v[i]=x
       return true
       
   endmethod

   method top takes nothing returns comparable
       if (.n==0) then
           return 0
       endif
       return this.v[0]
   endmethod


   method pop takes nothing returns boolean
    local integer i=0
    local integer cleft=0 //left child
    local integer cright=0 //right child
    local comparablearray v = this.v
    local comparable x
    local integer n=this.n

       if (n==0) then
           return false
       elseif (n==1) then
           set this.n=n-1
           return true
       endif
       set n=n-1
       set this.n=n
       set x= v[n]

       loop
           exitwhen i==n //should not ever happen
           set cright = (i+1)*2
           set cleft = cright-1

           exitwhen (cleft>=n) // no childs, please leave it here.

           if (cright>=n) then //no right child
               if (v[cleft] > x) then
                   set v[i]=v[cleft]
                   set i=cleft
               else
                   exitwhen true
               endif
           else
               //both childs
               if ((x>v[cleft]) and (x>v[cright])) then
                   //x is the greatest
                   exitwhen true
               elseif ((v[cleft]>x) and (v[cleft]>v[cright]) ) then
                   //v[cleft ] is the greatest

                   set v[i]=v[cleft]
                   set i=cleft
               else //v[cright] is the greatest
                   set v[i]=v[cright]
                   set i=cright
               endif
           endif
       endloop
       set v[i]=x
    return true
   endmethod

   method onDestroy takes nothing returns nothing
    local integer i=0
       loop
           exitwhen i== this.n
           call this.v[i].destroy()
           set i=i+1
       endloop
       call this.v.destroy()
   endmethod 



endstruct


Collapse JASS:
struct fakeinteger extends comparable
   integer v
   static method new takes integer x returns fakeinteger
    local fakeinteger f=fakeinteger.create()
       set f.v=x
    return f
   endmethod

   method operator < takes fakeinteger b returns boolean
       return this.v<b.v
   endmethod
endstruct

type fakeinteger_array extends fakeinteger array [600] //fia
struct fialayer extends comparablearray

   fakeinteger_array v

   static method new takes nothing returns fialayer
    local fialayer f= fialayer.create()
       set f.v= fakeinteger_array.create()
    return f
   endmethod

   method operator[] takes integer i returns comparable
       return this.v[i]
   endmethod

   method operator[]= takes integer i, comparable val returns nothing
       set this.v[i]= fakeinteger( val )
   endmethod

   method size takes nothing returns integer
       return this.v.size
   endmethod

   method onDestroy takes nothing returns nothing
       call this.v.destroy()
   endmethod
endstruct

function Test takes nothing returns nothing
 local PriorityQueue Q= PriorityQueue.createFrom(fialayer.new())
 local integer i=0

 //Very slow because of the debug string:
 local string deb=""
 local integer x
 local fakeinteger fi
    loop
        exitwhen i==20
        set x=GetRandomInt(0,2002)
        set deb=deb+" <- "+I2S(x)
        call Q.push( fakeinteger.new(x))
        set i=i+1
    endloop
    call BJDebugMsg(deb)
    set deb=""

    set i=0
    loop
        set fi = fakeinteger( Q.top())
        exitwhen (fi==0)
        set i=i+1


        call Q.pop()
        if (GetRandomInt(0,100)<5) then
            call TriggerSleepAction(0.)
        endif

        set deb = deb+" -> "+I2S(fi.v)
        call fi.destroy()
    endloop
    call BJDebugMsg(deb)
    call BJDebugMsg("[[[[[[["+I2S(i))
    call Q.destroy()

endfunction


Although I really think textmacros would do better here since this is very slow for a heap... I still don't like what textmacros do to the syntax too much so it actually seems I'll have to make C++ like templates...
03-23-2007, 06:09 PM#14
BlinkBoy
Quote:
Originally Posted by Toadcop
it's a cache stuff so it's shit =) i mean to use gamecache for some dynamic things like vectors is crap and need to be a full idiot... if you can use simple arrays and vars !

who said anything about vectors and gamecaches
03-23-2007, 07:11 PM#15
Toadcop
Quote:
who said anything about vectors and gamecaches
Quote:
yes I could see the vector engine needing an update once this is in.
- ? or i have something wrong understand...