#import "grid.h"

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

#import <collections/List_linked.h>

@interface MyList : List_linked
+ create: aZone;
@end

@implementation MyList

+ create: aZone
{
  List_linked  *newList;

  newList = [aZone allocIVars: [List_linked self]];
  newList->zone = aZone;
  return newList;
}

@end

int main(void)
{
  id aZone, aList, aTurtle, index, newList;
  int i;

  initModule( grid );

  aZone = [Zone create: globalZone];
  aList = [MyList create: aZone];

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

  for ( i = 0; i < 10; i++ ) {
    aTurtle = [GridTurtle create: aZone];
    [aTurtle turn: -1];
    [aTurtle move: i * 10];
    [aList addLast: aTurtle];
  }

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

  index = [aList begin: aZone];
  while ( (aTurtle = [index next]) ) {
    [aTurtle print];
  }
  [index drop];

  [aList addFirst: [aList removeLast]];
  [aList addFirst: [aList removeLast]];
  [aList addLast: [aList removeFirst]];

  printf( "count: %d\n", [aList count] );
  index = [aList begin: aZone];
  for ( [index setLoc: End]; (aTurtle = [index prev]); ) {
    [aTurtle print];
  }

  printf( "turtle located at offset 3: " );
  [[index setOffset: 3] print];
  printf( "turtle removed at offset 3: " );
  [[index remove] print];

  [index setLoc: Start];
  while ( (aTurtle = [index next]) ); // [aTurtle print];

  [aList forEach: @selector( print )];

  newList = [List createBegin: globalZone];
  [newList setInitialValue: aList];
  newList = [newList createEnd];

  [aList drop];

  printf( "new list count: %d\n", [newList count] );
  [newList forEach: @selector( print )];

  aList = [newList copy: globalZone];
  printf( "copied list count: %d\n", [aList count] );
  [aList forEach: @selector( print )];

  return 0;
}
