(*
 * $Id: methcall.ocaml,v 1.6 2001/01/08 03:08:35 doug Exp $
 * http://www.bagley.org/~doug/shootout/
 * from: Benedikt Rosenau
 * with contributions from Markus Mottl
 *)

let print_bool b = print_endline (string_of_bool b)

class toggle start_state = object (self)
  val mutable state = start_state

  method value = state
  method activate = state <- not state; self
end

class nth_toggle start_state max_counter = object (self)
  inherit toggle start_state

  val count_max = max_counter
  val mutable counter = 0

  method activate =
    counter <- counter + 1;
    if counter >= count_max
    then begin
      state <- not state;
      counter <- 0
    end;
    self
end

let _ =
  let n =
    try int_of_string Sys.argv.(1)
    with Invalid_argument _ -> 1 in
  let tog = new toggle true in
  for i = 2 to n do
    ignore tog#activate#value
  done;
  print_bool tog#activate#value;
  let ntog = new nth_toggle true 3 in
  for i = 2 to n do
    ignore ntog#activate#value
  done;
  print_bool ntog#activate#value