// $Id: lists.java,v 1.1 2001/01/14 18:13:15 doug Exp $
// http://www.bagley.org/~doug/shootout/

// ported to tkscript on Wed Apr 7 19:37:10 2004 by Bastian Spiegel <bs@tkscript.de>

// benchmark results: (amd 2400+ "barton", 512MB RAM)
//         gcc: ~0.3125 (source: http://www.bagley.org/~doug/shootout/bench/lists/lists.gcc)
//        perl: ~0.3375s (10.8s for NUM=512)
//      python: ~0.3750s (12s for NUM=512)
//    tkscript: ~0.4375s (14s for NUM=512)
//        java: ~0.5531s (17,7s for NUM=512)
// java(nojit): ~1.2881s (39,3s for NUM=512)
//
// perl0.5.8.0 (cygwin), python2.3 (win32), java 1.4.2_04 (win32), tkscript 0.8.7.2 (win32, MSVC compiled),
//
// benchmark results (g4 800, apple macosX 10.33, 768MB RAM)
//       tkscript: ~2s

#define SIZE 10000

function test_lists()  {
   int i,result = 0;
   // create a list of integers (Li1) from 1 to SIZE
   List Li1<= new List();
   i=1;
   Value v;
   loop(SIZE)
      {
         Li1.addLastInt(i++); // populate List with integers
      }

   // copy the list to Li2 (not by individual items)
   List Li2 <= new List(); Li2=Li1;
   List Li3 <= new List();
   
   // remove each individual item from left side of Li2 and
   // append to right side of Li3 (preserving order)
   while (Li2.head)
      Li3.addLast(Li2.removeFirst());
   
   // Li2 must now be empty
   // remove each individual item from right side of Li3 and
   // append to right side of Li2 (reversing list)
   while (Li3.head)
      Li2.addLast(Li3.removeLast());
   
   // Li3 must now be empty
   // reverse Li1
   Li1.reverse();
   
   // check that first item is now SIZE
   if (Li1.head.intValue != SIZE)
      die "first item of Li1 != SIZE";
   
   if(Li1.size!=Li2.size)
      die "Li1 and Li2 differ.";
   
   // return the length of the list
   return Li1.size;
}


function main() {
   int t=milliSeconds();
   int n = Arguments.numElements?Arguments[0]:512;
   int result = 0;
   loop(n)
      result = test_lists();
   
   print result;
   
   print "time(ms)="+(milliSeconds()-t);
}