#import "grid.h"
#import "GridTurtle.h"

#define __USE_FIXED_PROTOTYPES__   // for gcc headers
#include <stdio.h>

int main(void)
{
  id aZone, aOrderedSet, aTurtle, turtles[10], index;
  int i;

  initModule( grid );

  aZone = [Zone create: globalZone];
  aOrderedSet = [OrderedSet createBegin: aZone];
  [aOrderedSet setIndexFromMemberLoc: offsetof( GridTurtle_c, listMembership )];
  aOrderedSet = [aOrderedSet createEnd];

  printf( "count: %d\n", [aOrderedSet count] );

  for ( i = 0; i < 10; i++ ) {
    aTurtle = [GridTurtle create: aZone];
    turtles[i] = aTurtle;
    [aTurtle turn: -1];
    [aTurtle move: i * 10];
    if ( i & 1 ) [aOrderedSet add: aTurtle];
  }

  printf( "count: %d\n", [aOrderedSet count] );

  index = [aOrderedSet begin: aZone];
  while ( (aTurtle = [index next]) ) {
    [aTurtle print];
  }
  for ( i = 0; i < 10; i++ ) {
    if ( ! [aOrderedSet contains: turtles[i]] ) {
      printf( "turtle not found at %d\n", i );
    }
  }

  for ( i = 0; i < 10; i++ ) {
    if ( i & 1 ) {
      [aOrderedSet remove: turtles[i]];
    } else {
      [aOrderedSet add: turtles[i]];
    }
  }
  index = [aOrderedSet begin: aZone];
  while ( (aTurtle = [index next]) ) {
    [aTurtle print];
  }
  for ( i = 0; i < 10; i++ ) {
    if ( ! [aOrderedSet contains: turtles[i]] ) {
      printf( "turtle not found at %d\n", i );
    }
  }
  [index drop];

  return 0;
}
