// -*- mode: c++ -*-
// $Id: methcall.g++,v 1.8 2001/10/09 23:53:03 doug Exp $
// http://www.bagley.org/~doug/shootout/

// with some help from Bill Lear

// see http://www.bagley.org/~doug/shootout/bench/methcall/

// ported to tkscript on Wed Apr 7 14:52:15 2004 by Bastian Spiegel <bs@tkscript.de>
//
// note: I tried to do a direct port of the C++ benchmark code.
//       benchmark results: (amd 2400+ "barton", n=1000000)
//
//          g++: ~0.013s
//  visualc++-6: ~0.015s
//         java: ~0.021s
//     tkscript: ~1.2s
//       python: ~3.9s
//         perl: ~6s
//
// 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, n=1000000)
//    tkscript: ~5s

class Toggle {
    int state;

    /*void*/   init    (int start_state) { state=start_state; }
    /*int*/    value   ()                { return state; }
    /*Toggle*/ activate()                { state = !state; return this; }
}

class NthToggle : Toggle {
  int count_max;
  int counter;
  
  init(int start_state, int max_counter) {
    Toggle::init(start_state);
    count_max=max_counter;
    counter=0;
  }
  
  activate() {
    if (++counter >= count_max) {
      state = !state;
      counter = 0;
    }
    return this;
  }
}


function main() {
    int n = ((Arguments.numElements == 1) ? Arguments[0] : 1000000);

    int tp=milliSeconds();

    int val = true;
    Toggle toggle<=new Toggle,t; toggle.init(val);
    loop(n)
	val = toggle.activate().value();

    print (val ? "true" : "false");
    toggle<=null;

    val = true;
    NthToggle ntoggle<=new NthToggle; ntoggle.init(val,3);
    loop(n)
  	val = ntoggle.activate().value();
    ntoggle<=null;

    print (val ? "true" : "false");

    print "time(ms)="+(milliSeconds()-tp);
}