Filling the gaps in an array

Magentix

if (OP.statement == false) postCount++;
Reaction score
107
I got this function, but I'm pretty sure that there's a way more efficient and OP limit-friendly version of it...

JASS:

private function SortUnitArray takes nothing returns nothing
    local integer i=1
    local integer gap=0
    local integer goodUnits=0
        
    loop
    exitwhen gap >= 10
        
        if UNITS<i>==null then
            set gap=gap+1
        else
            if gap&gt;0 then
                set UNITS[i-gap] = UNITS<i>
                set UNITS<i> = null
            endif
               
            set good_units=goodUnits+1
            set gap=0
        endif
            
        set i=i+1
    endloop
        
    set totalUnitsInArray = goodUnits
endfunction</i></i></i>


Checks for gaps up to a maximum of 10.
Since there will almost never be a gap greater than 10 at any given time, this should suffice, although very inefficient...

Anyone got a better idea?


For those of you who may doubt:
- Reason this is inneficient is because if somewhere there's a gap of, let's say, 8: it will loop those 8 times through the gaps every time until everything has shuffled to the left.

- Secondly, a series like this: UnnUnnnnUnnnnnnnUnnnnU (U = Unit, n = null),
would quickly result in UUUnnnnnnnnnnnnnUnnnnU, halting the loop when it shouldn't.
 

AceHart

Your Friendly Neighborhood Admin
Reaction score
1,494
A better idea is maintaining a perfect state at all times.

By keeping track of the current highest index,
- that's the place to add the next one
- upon removal of a units, put the last one in the now free spot
 

T.s.e

Wish I was old and a little sentimental
Reaction score
133
If it is for storing units, you could use Cohadar's PUI.
 

Magentix

if (OP.statement == false) postCount++;
Reaction score
107
I used units as an example, but in fact it is for Groups/Timers/etc...
Whereas, for example, checking if a group died completely (=a loop) every time will swiftly hit the OP limit.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top