no
Code:
if(i == 0) { /* handle the first set specially */
if(gen_leftover == 0) /* then gen_overflow was aligned at the start too. */
temp_ctr += (int)STRUCTS_PER_GEN; /* and we need to handle gen 0 */
for(i=GUIconf.numGenerations - gen_leftover + 1; i<=GUIconf.numGenerations; i++)
temp_ctr += (int)(STRUCTS_PER_GEN * sqrt(i));
}
this breaks if gen_leftover = 0 because the loop only works for partial sets.
if you have 1005 gens buffered and are working on gen0 you'd miss out on a whole set -100 points.
the current set doesn't get accounted for at all.
So after some small adjustments I got this.
I think this will work for all situations.
Code:
if(cur_gen-gens_buf+1 >= 0) {
for(i=cur_gen-gens_buf+1; i<cur_gen; i++)
temp_ctr += (int)(STRUCTS_PER_GEN*sqrt(i));
}
else {
int j; /* took some time before I realised you had the same iterator in a nested loop :) */
int gen_overflow = gens_buf - cur_gen - 1;
int set_overflow = (gen_overflow + GUIconf.numGenerations) / (GUIconf.numGenerations + 1);
/* i.e., round up -- 1 to 251 becomes 1, 252 -> 502 becomes 2, etc.
This is the number of full or partial sets finished before the current one. */
int gen_leftover = gen_overflow % (GUIconf.numGenerations + 1);
/* This (^) is the number of gens at the tail end of the first buffered set.
We know that the tail end of gen_overflow is aligned on a set boundary now,
so this should be valid. Right? */
for(i=0; i<set_overflow; i++) {
if(i == 0 && gen_leftover != 0) {
/* handle the first set specially unless there is no overflow (no partial first set)
gen 0 is not needed because it's a partial first set */
for(j=GUIconf.numGenerations - gen_leftover + 1; j<=GUIconf.numGenerations; j++)
temp_ctr += (int)(STRUCTS_PER_GEN * sqrt(j));
}
else {
temp_ctr += (int)STRUCTS_PER_GEN;
for(j=1; j<=GUIconf.numGenerations; j++)
temp_ctr += (int)(STRUCTS_PER_GEN * sqrt(j));
}
}
/* continue with points for the gens in the current set
only add points for gen 0 if it's finished */
if(cur_gen > 0)
temp_ctr += (int)STRUCTS_PER_GEN;
for(i=1; i<cur_gen; i++)
temp_ctr += (int)(STRUCTS_PER_GEN*sqrt(i));
}
Edit: ok, I tested it for the situations I used for my previous code and the buffered first partial set. Every thing seems to work.