Question about unit iterator

vuongkkk

New Member
Reaction score
1
My question is very simple:
Normal, everybody always use group to iterate a number of units.
However, i use the iterator is a integer array contain a instance of struct, (of cause i can control my iterator, and it worked). Like group, i do NOT loop all members in array , only units are member of my array.

I dont know which faster? Group or my iterrator

And this is my iterator
JASS:
globals
    private constant integer MAX_INSTANCE = 8190
    private integer array   MyGroup[MAX_INSTANCE]
    private integer         MyGroupCount               = 0
endglobals

struct UnitData
    unit u
    integer index
    
    static method create takes unit u returns UnitData
        set this.u = whichU
        set MyGroup[MyGroupCount] = integer(this)
        set MyGroupCount = MyGroupCount + 1
        set this.index = MyGroupCount
    endmethod
    
    method onDestroy takes nothing returns nothing
        set MyGroupCount = MyGroupCount - 1
        set UnitData(MyGroup[MyGroupCount]).index = this.index
        set MyGroup[this.index-1] = MyGroup[MyGroupCount]
        set MyGroup[MyGroupCount] = 0
    endmethod
endstruct

function MyGroupAddUnit takes unit whichU returns UnitData
    local UnitData d = UnitData.create(whichU)
    return d
endfunction

function MyGroupIterator takes nothing returns nothing
local integer i = 0
local UnitData data
    loop
        exitwhen MyGroup<i> == 0
        set data = UnitData(MyGroup<i>)
        // do something with enum unit here like that
        call SetUnitState(data.u, UNIT_STATE_LIFE, 10000)
        call data.destroy()
        endif
        set i = i +1
    endloop
endfunction
</i></i>
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
Group is faster.
This is faster than your code as well in iteration.
JASS:
library AGroup requires optional AIDS

    globals
        private constant boolean AUTO_REMOVE=true
    endglobals
    
    globals
        private hashtable hasht=InitHashtable()
        private trigger array Trig
        private integer N_Trig=0
    endglobals
    
    private function NewTrig takes nothing returns trigger
        if N_Trig==0 then
            return CreateTrigger()
        endif
        set N_Trig=N_Trig-1
        return Trig[N_Trig]
    endfunction
    
    private function ReleaseTrig takes trigger trig returns nothing
        set Trig[N_Trig]=trig
        set N_Trig=N_Trig+1
    endfunction
    
    static if AUTO_REMOVE then
        private keyword AGroupList
        private keyword UnitStruct
    endif
    
    struct AGroup
        private thistype prev
        private thistype next
        private unit u
        private trigger trig
        
        static method create takes nothing returns thistype
            local thistype this=thistype.allocate()
            set this.prev=this
            set this.next=this
            set this.u=null
            return this
        endmethod
        
        method addUnit takes unit u returns nothing
            local thistype new
            if u==null then
                return
            endif
            static if AUTO_REMOVE then
                if HaveSavedInteger(hasht,this,GetUnitId(u)) then
                    return
                endif
            else
                if HaveSavedInteger(hasht,this,GetHandleId(u)) then
                    return
                endif
            endif
            set new=thistype.allocate()
            set new.u=u
            set this.prev.next=new
            set new.prev=this.prev
            set this.prev=new
            set new.next=this
            static if AUTO_REMOVE then
                call SaveInteger(hasht,this,GetUnitId(u),new)
                call UnitStruct<u>.addToList(new)
            else
                call SaveInteger(hasht,this,GetHandleId(u),new)
            endif
        endmethod
        
        method removeUnit takes unit u returns nothing
            static if AUTO_REMOVE then
                local thistype d=LoadInteger(hasht,this,GetUnitId(u))
            else
                local thistype d=LoadInteger(hasht,this,GetHandleId(u))
            endif
            if d!=0 then
                set d.prev.next=d.next
                set d.next.prev=d.prev
                call d.destroy()
                static if AUTO_REMOVE then
                    call RemoveSavedInteger(hasht,this,GetUnitId(d.u))
                    call UnitStruct<u>.removeFromList(d)
                else
                    call RemoveSavedInteger(hasht,this,GetHandleId(d.u))
                endif
            endif
        endmethod
        
        private static unit array enumUnit
        private static integer stackLevel=0
        
        static method getEnumUnit takes nothing returns unit
            return enumUnit[stackLevel]
        endmethod
        
        method forUnits takes code c returns nothing
            local trigger trig=NewTrig()
            local thistype d=this
            local triggeraction t=TriggerAddAction(trig,c)
            set this=this.next
            loop
            exitwhen this==d
                set stackLevel=stackLevel+1
                set enumUnit[stackLevel]=this.u
                call TriggerExecute(trig)
                set stackLevel=stackLevel-1
                set this=this.next
            endloop
            call TriggerRemoveAction(trig,t)
            call ReleaseTrig(trig)
            set trig=null
            set t=null
        endmethod
        
        method hasUnit takes unit u returns boolean
            static if AUTO_REMOVE then
                return HaveSavedInteger(hasht,this,GetUnitId(u))
            else
                return HaveSavedInteger(hasht,this,GetHandleId(u))
            endif
        endmethod
        
        method release takes nothing returns nothing
            local thistype d=this
            set this=this.next
            loop
            exitwhen this==d
                set this.prev.next=this.next
                set this.next.prev=this.prev
                set this.u=null
                call this.destroy()
                set this=this.next
            endloop
            call this.destroy()
            call FlushChildHashtable(hasht,this)
        endmethod
        
    endstruct
    
    static if AUTO_REMOVE then
        private struct AGroupList
            private thistype prev
            private thistype next
            private AGroup g
            
            static method createList takes nothing returns thistype
                local thistype this=thistype.allocate()
                set this.prev=this
                set this.next=this
                set this.g=0
                return this
            endmethod
            
            method addGroup takes AGroup g returns nothing
                local thistype new=thistype.allocate()
                set new.g=g
                set this.prev.next=new
                set new.prev=this.prev
                set this.prev=new
                set new.next=this
            endmethod
            
            method removeGroup takes AGroup g returns nothing
                local thistype d=this
                set this=this.next
                loop
                exitwhen this==d
                    if this.g==g then
                        set this.prev.next=this.next
                        set this.next.prev=this.prev
                        set this.g=0
                        call this.destroy()
                        return
                    endif
                    set this=this.next
                endloop
            endmethod
            
            method chainDestroy takes unit u returns nothing
                local thistype d=this
                set this=this.next
                loop
                exitwhen this==d
                    set this.prev.next=this.next
                    set this.next.prev=this.prev
                    call this.g.removeUnit(u)
                    set this.g=0
                    call this.destroy()
                    set this=this.next
                endloop
            endmethod
        endstruct
        
        private struct UnitStruct extends array
            private AGroupList gl
            
            private method AIDS_onCreate takes nothing returns nothing
                set this.gl=AGroupList.createList()
            endmethod
            
            private method AIDS_onDestroy takes nothing returns nothing
                call this.gl.chainDestroy(this.unit)
            endmethod
            
            method addToList takes AGroup g returns nothing
                call this.gl.addGroup(g)
            endmethod
            
            method removeFromList takes AGroup g returns nothing
                call this.gl.removeGroup(g)
            endmethod
            //! runtextmacro AIDS()
        endstruct
    endif
    
endlibrary
</u></u>
 

vuongkkk

New Member
Reaction score
1
Are you sure your code is faster than mine? Why?

My code only uses struct and array while yours uses both linked list and hashtable, even AIDS. I think what is simple also fast or i m wrong :confused:

I really need the fastest way for my map no matter what !
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
Hashtable and AIDS are not involved in iteration, why bother them for iteration speed.
The only thing involved in iteration is linked list.
It is faster than array in iteration.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top