- Joined
- Jun 20, 2011
- Messages
- 249
Fully reusable FirstOfGroup loops with highest possible speed.
JASS:
//! zinc
library DoubleGroup{
// Allows FirstOfGroup loops to run multiple times on the same
// group.
/**********************************************************************
API
struct DoubleGroup
// Manages groups that have a backup group to store removed units through
// FirstOfGroup loops.
--->
static method create takes nothing returns DoubleGroup
// allocates new DoubleGroup
method destroy takes nothing returns nothing
// deallocates the DoubleGroup
readonly group handle
// returns the actual unit group
readonly unit FirstOfGroup
// Don't read more than 1 per loop, returns a different unit every
// time. Will return null when the loop is over, after that it's
// fully reusable in the next loop.
**********************************************************************/
integer n=0;
integer m=0;
integer r[];
group g[];
struct DoubleGroup[]{
private{
group g1;
group g2;
boolean gc;}
method operator handle()->group{
if(this.gc) return this.g1;
else return this.g2;}
private method operator second()->group{
if(this.gc) return this.g2;
else return this.g1;}
method operator FirstOfGroup()->unit{
unit u;
u=FirstOfGroup(this.handle);
if(null==u) this.gc=!(this.gc);
else{
GroupAddUnit(this.second,u);
GroupRemoveUnit(this.handle,u);}
return u;}
static method create()->thistype{
thistype this;
if(0==r[0]){
m+=1;
this=m;}
else{
this=r[0];
r[0]=r[r[0]];}
if(n>0){
n-=2;
this.g1=g[n];
this.g2=g[n+1];}
else{
this.g1=CreateGroup();
this.g2=CreateGroup();}
return this;}
method destroy(){
r[this]=r[0];
r[0]=this;
GroupClear(this.g1);
GroupClear(this.g2);
g[n]=this.g1;
g[n+1]=this.g2;
n+=2;}}}
//! endzinc