--- /dev/null
+class Toggle {
+ int state;
+ fun int value() { return state; }
+ fun Toggle activate() {
+//<<<"supr">>>;
+ !state => state;
+ return this;
+ }
+}
+
+class NthToggle extends Toggle {
+ int count, countMax;
+ fun Toggle activate() {
+ if(++count >= countMax) {
+ (this $ Toggle).activate();
+ 0 => count;
+ }
+ return this;
+ }
+}
+
+100000 => int n;
+
+1 => n;
+
+
+Toggle toggle;
+true => int val => toggle.state;
+
+//repeat(n) {
+// toggle.activate().value() => val;
+// toggle.activate().value() => val;
+// toggle.activate().value() => val;
+// toggle.activate().value() => val;
+// toggle.activate().value() => val;
+// toggle.activate().value() => val;
+// toggle.activate().value() => val;
+// toggle.activate().value() => val;
+// toggle.activate().value() => val;
+// toggle.activate().value() => val;
+//}
+
+<<<toggle.value()>>>;
+
+NthToggle ntoggle;
+<<<true => val => ntoggle.state>>>;
+3 => ntoggle.countMax;
+
+repeat(n) {
+ ntoggle.activate().value() => val;
+ ntoggle.activate().value() => val;
+ ntoggle.activate().value() => val;
+ ntoggle.activate().value() => val;
+ ntoggle.activate().value() => val;
+ ntoggle.activate().value() => val;
+ ntoggle.activate().value() => val;
+ ntoggle.activate().value() => val;
+ ntoggle.activate().value() => val;
+ ntoggle.activate().value() => val;
+}
+
+<<<ntoggle.value()>>>;
+
+//
+//Toggle @t;
+//ntoggle @=> t;
+//<<<t.activate().value()>>>;
+//<<<t.activate().value()>>>;
+//toggle @=> t;
+//<<<t.activate().value()>>>;
+//<<<t.activate().value()>>>;
+<<<true>>>;
--- /dev/null
+class Toggle {
+ var _state;
+
+ Toggle(startState) {
+ _state = startState;
+ }
+
+ get value => _state;
+
+ activate() {
+ _state = !_state;
+ return this;
+ }
+}
+
+class NthToggle extends Toggle {
+ var _count;
+ var _countMax;
+
+ NthToggle(startState, maxCounter)
+ : super(startState) {
+ _countMax = maxCounter;
+ _count = 0;
+ }
+
+ activate() {
+ _count = _count + 1;
+ if (_count >= _countMax) {
+ super.activate();
+ _count = 0;
+ }
+
+ return this;
+ }
+}
+
+main() {
+ Stopwatch watch = new Stopwatch();
+ watch.start();
+
+ var n = 100000;
+ var val = true;
+ var toggle = new Toggle(val);
+
+ for (var i = 0; i < n; i++) {
+ val = toggle.activate().value;
+ val = toggle.activate().value;
+ val = toggle.activate().value;
+ val = toggle.activate().value;
+ val = toggle.activate().value;
+ val = toggle.activate().value;
+ val = toggle.activate().value;
+ val = toggle.activate().value;
+ val = toggle.activate().value;
+ val = toggle.activate().value;
+ }
+
+ print(toggle.value);
+
+ val = true;
+ var ntoggle = new NthToggle(val, 3);
+
+ for (var i = 0; i < n; i++) {
+ val = ntoggle.activate().value;
+ val = ntoggle.activate().value;
+ val = ntoggle.activate().value;
+ val = ntoggle.activate().value;
+ val = ntoggle.activate().value;
+ val = ntoggle.activate().value;
+ val = ntoggle.activate().value;
+ val = ntoggle.activate().value;
+ val = ntoggle.activate().value;
+ val = ntoggle.activate().value;
+ }
+
+ print(ntoggle.value);
+ print("elapsed: ${watch.elapsedMilliseconds / 1000}");
+}
--- /dev/null
+class Toggle {
+ bool state;
+ fun bool value() { return state; }
+ fun Toggle activate() {
+ !state => state;
+ return this;
+ }
+}
+
+class NthToggle extends Toggle {
+ int count, countMax;
+ fun Toggle activate() {
+ if(++count >= countMax) {
+ (this $ Toggle).activate();
+ 0 => count;
+ }
+ return this;
+ }
+}
+
+100000 => int n;
+
+Toggle toggle;
+true => bool val => toggle.state;
+
+repeat(n) {
+ toggle.activate().value() => val;
+ toggle.activate().value() => val;
+ toggle.activate().value() => val;
+ toggle.activate().value() => val;
+ toggle.activate().value() => val;
+ toggle.activate().value() => val;
+ toggle.activate().value() => val;
+ toggle.activate().value() => val;
+ toggle.activate().value() => val;
+ toggle.activate().value() => val;
+}
+
+<<<toggle.value()>>>;
+
+NthToggle ntoggle;
+true => val => ntoggle.state;
+3 => ntoggle.countMax;
+
+repeat(n) {
+ ntoggle.activate().value() => val;
+ ntoggle.activate().value() => val;
+ ntoggle.activate().value() => val;
+ ntoggle.activate().value() => val;
+ ntoggle.activate().value() => val;
+ ntoggle.activate().value() => val;
+ ntoggle.activate().value() => val;
+ ntoggle.activate().value() => val;
+ ntoggle.activate().value() => val;
+ ntoggle.activate().value() => val;
+}
+
+<<<ntoggle.value()>>>;
--- /dev/null
+-- $Id: methcall.lua,v 1.2 2004-06-12 16:19:43 bfulgham Exp $
+-- http://shootout.alioth.debian.org
+-- contributed by Roberto Ierusalimschy
+
+--------------------------------------------------------------
+-- Toggle class
+--------------------------------------------------------------
+
+Toggle = {}
+
+function Toggle:value ()
+ return self.state
+end
+
+function Toggle:activate ()
+ self.state = not self.state
+ return self
+end
+
+function Toggle:new (start_state)
+ local o = {state = start_state}
+ self.__index =self
+ setmetatable(o, self)
+ return o
+end
+
+
+--------------------------------------------------------------
+-- NthToggle class
+--------------------------------------------------------------
+
+NthToggle = Toggle:new()
+
+function NthToggle:activate ()
+ self.counter = self.counter + 1
+ if self.counter >= self.count_max then
+ Toggle.activate(self)
+ self.counter = 0
+ end
+ return self
+end
+
+function NthToggle:new (start_state, max_counter)
+ local o = Toggle.new(self, start_state)
+ o.count_max = max_counter
+ o.counter = 0
+ return o
+end
+
+
+-----------------------------------------------------------
+-- main
+-----------------------------------------------------------
+
+function main ()
+ local start = os.clock()
+ local N = 100000
+
+ local val = 1
+ local toggle = Toggle:new(val)
+ for i=1,N do
+ val = toggle:activate():value()
+ val = toggle:activate():value()
+ val = toggle:activate():value()
+ val = toggle:activate():value()
+ val = toggle:activate():value()
+ val = toggle:activate():value()
+ val = toggle:activate():value()
+ val = toggle:activate():value()
+ val = toggle:activate():value()
+ val = toggle:activate():value()
+ end
+ print(val and "true" or "false")
+
+ val = 1
+ local ntoggle = NthToggle:new(val, 3)
+ for i=1,N do
+ val = ntoggle:activate():value()
+ val = ntoggle:activate():value()
+ val = ntoggle:activate():value()
+ val = ntoggle:activate():value()
+ val = ntoggle:activate():value()
+ val = ntoggle:activate():value()
+ val = ntoggle:activate():value()
+ val = ntoggle:activate():value()
+ val = ntoggle:activate():value()
+ val = ntoggle:activate():value()
+ end
+ print(val and "true" or "false")
+ io.write(string.format("elapsed: %.8f\n", os.clock() - start))
+end
+
+main()
+
--- /dev/null
+#!/usr/bin/python
+# http://www.bagley.org/~doug/shootout/
+from __future__ import print_function
+
+import sys
+import time
+
+# Map "range" to an efficient range in both Python 2 and 3.
+try:
+ range = xrange
+except NameError:
+ pass
+
+class Toggle(object):
+ def __init__(self, start_state):
+ self.bool = start_state
+ def value(self):
+ return(self.bool)
+ def activate(self):
+ self.bool = not self.bool
+ return(self)
+
+class NthToggle(Toggle):
+ def __init__(self, start_state, max_counter):
+ Toggle.__init__(self, start_state)
+ self.count_max = max_counter
+ self.counter = 0
+ def activate(self):
+ self.counter += 1
+ if (self.counter >= self.count_max):
+ super(NthToggle, self).activate()
+ self.counter = 0
+ return(self)
+
+
+def main():
+ start = time.clock()
+
+ NUM = 100000
+
+ val = 1
+ toggle = Toggle(val)
+ for i in range(0,NUM):
+ val = toggle.activate().value()
+ val = toggle.activate().value()
+ val = toggle.activate().value()
+ val = toggle.activate().value()
+ val = toggle.activate().value()
+ val = toggle.activate().value()
+ val = toggle.activate().value()
+ val = toggle.activate().value()
+ val = toggle.activate().value()
+ val = toggle.activate().value()
+ if val:
+ print("true")
+ else:
+ print("false")
+
+ val = 1
+ ntoggle = NthToggle(val, 3)
+ for i in range(0,NUM):
+ val = ntoggle.activate().value()
+ val = ntoggle.activate().value()
+ val = ntoggle.activate().value()
+ val = ntoggle.activate().value()
+ val = ntoggle.activate().value()
+ val = ntoggle.activate().value()
+ val = ntoggle.activate().value()
+ val = ntoggle.activate().value()
+ val = ntoggle.activate().value()
+ val = ntoggle.activate().value()
+ if val:
+ print("true")
+ else:
+ print("false")
+
+ print("elapsed: " + str(time.clock() - start))
+
+
+main()
--- /dev/null
+#!/usr/bin/ruby
+# -*- mode: ruby -*-
+# $Id: methcall.ruby,v 1.1 2004-05-19 18:10:41 bfulgham Exp $
+# http://www.bagley.org/~doug/shootout/
+# with help from Aristarkh Zagorodnikov
+
+class Toggle
+ def initialize(start_state)
+ @bool = start_state
+ end
+
+ def value
+ @bool
+ end
+
+ def activate
+ @bool = !@bool
+ self
+ end
+end
+
+class NthToggle < Toggle
+ def initialize(start_state, max_counter)
+ super start_state
+ @count_max = max_counter
+ @counter = 0
+ end
+
+ def activate
+ @counter += 1
+ if @counter >= @count_max
+ super
+ @counter = 0
+ end
+ self
+ end
+end
+
+def main()
+ start = Time.now
+
+ n = 100000
+
+ val = 1
+ toggle = Toggle.new(val)
+ n.times do
+ val = toggle.activate().value()
+ val = toggle.activate().value()
+ val = toggle.activate().value()
+ val = toggle.activate().value()
+ val = toggle.activate().value()
+ val = toggle.activate().value()
+ val = toggle.activate().value()
+ val = toggle.activate().value()
+ val = toggle.activate().value()
+ val = toggle.activate().value()
+ end
+ if val then puts "true" else puts "false" end
+
+ val = 1
+ ntoggle = NthToggle.new(val, 3)
+ n.times do
+ val = ntoggle.activate().value()
+ val = ntoggle.activate().value()
+ val = ntoggle.activate().value()
+ val = ntoggle.activate().value()
+ val = ntoggle.activate().value()
+ val = ntoggle.activate().value()
+ val = ntoggle.activate().value()
+ val = ntoggle.activate().value()
+ val = ntoggle.activate().value()
+ val = ntoggle.activate().value()
+ end
+ if val then puts "true" else puts "false" end
+
+ puts "elapsed: " + (Time.now - start).to_s
+end
+
+main()
--- /dev/null
+class Toggle {
+ construct new(startState) {
+ _state = startState
+ }
+
+ value { _state }
+ activate {
+ _state = !_state
+ return this
+ }
+}
+
+class NthToggle is Toggle {
+ construct new(startState, maxCounter) {
+ super(startState)
+ _countMax = maxCounter
+ _count = 0
+ }
+
+ activate {
+ _count = _count + 1
+ if (_count >= _countMax) {
+ super.activate
+ _count = 0
+ }
+
+ return this
+ }
+}
+
+var start = System.clock
+var n = 100000
+var val = true
+var toggle = Toggle.new(val)
+
+for (i in 0...n) {
+ val = toggle.activate.value
+ val = toggle.activate.value
+ val = toggle.activate.value
+ val = toggle.activate.value
+ val = toggle.activate.value
+ val = toggle.activate.value
+ val = toggle.activate.value
+ val = toggle.activate.value
+ val = toggle.activate.value
+ val = toggle.activate.value
+}
+
+System.print(toggle.value)
+
+val = true
+var ntoggle = NthToggle.new(val, 3)
+
+for (i in 0...n) {
+ val = ntoggle.activate.value
+ val = ntoggle.activate.value
+ val = ntoggle.activate.value
+ val = ntoggle.activate.value
+ val = ntoggle.activate.value
+ val = ntoggle.activate.value
+ val = ntoggle.activate.value
+ val = ntoggle.activate.value
+ val = ntoggle.activate.value
+ val = ntoggle.activate.value
+}
+
+System.print(ntoggle.value)
+System.print("elapsed: %(System.clock - start)")