objinst
Full source code
C++
// -*- mode: c++ -*-
// $Id$
// http://www.bagley.org/~doug/shootout/
#include <stdlib.h>
#include <iostream>
using namespace std;
class Toggle {
public:
Toggle(bool start_state) : state(start_state) { }
virtual ~Toggle() { }
bool value() {
return(state);
}
virtual Toggle& activate() {
state = !state;
return(*this);
}
bool state;
};
class NthToggle : public Toggle {
public:
NthToggle(bool start_state, int max_counter) :
Toggle(start_state), count_max(max_counter), counter(0) {
}
Toggle& activate() {
if (++this->counter >= this->count_max) {
state = !state;
counter = 0;
}
return(*this);
}
private:
int count_max;
int counter;
};
int
main(int argc, char *argv[]) {
#ifdef SMALL_PROBLEM_SIZE
#define LENGTH 1000000
#else
#define LENGTH 70000000
#endif
int n = ((argc == 2) ? atoi(argv[1]) : LENGTH);
Toggle *toggle1 = new Toggle(true);
for (int i=0; i<5; i++) {
cout << ((toggle1->activate().value()) ? "true" : "false") << endl;
}
delete toggle1;
for (int i=0; i<n; i++) {
Toggle *toggle = new Toggle(true);
delete toggle;
}
cout << endl;
NthToggle *ntoggle1 = new NthToggle(true, 3);
for (int i=0; i<8; i++) {
cout << ((ntoggle1->activate().value()) ? "true" : "false") << endl;
}
delete ntoggle1;
for (int i=0; i<n; i++) {
NthToggle *ntoggle = new NthToggle(true, 3);
delete ntoggle;
}
return 0;
}
Rust
// Adapted from https://github.com/llvm/llvm-test-suite and // http://www.bagley.org/~doug/shootout/ use std::env; #[cfg(feature = "small_problem_size")] const LENGTH: i32 = 1000000; #[cfg(not(feature = "small_problem_size"))] const LENGTH: i32 = 70000000; struct Toggle { state: bool } impl Toggle { fn new(start_state: bool) -> Toggle { Toggle { state: start_state } } } trait Togglable { fn activate(&mut self); fn value(&self) -> bool; } impl Togglable for Toggle { fn activate(&mut self) { self.state = !self.state; } fn value(&self) -> bool { return self.state; } } struct NthToggle { state: bool, count_max: i32, counter: i32, } impl NthToggle { fn new(start_state: bool, max_counter: i32) -> NthToggle { NthToggle { state: start_state, count_max: max_counter, counter: 0, } } } impl Togglable for NthToggle { fn activate(&mut self) { self.counter += 1; if self.counter >= self.count_max { self.state = !self.state; self.counter = 0; } } fn value(&self) -> bool { return self.state; } } fn main() { let mut args = env::args(); let n = if args.len() == 2 { args.nth(1).unwrap().parse::<i32>().unwrap() } else { LENGTH }; let mut toggle1 = Toggle::new(true); for _i in 0..5 { toggle1.activate(); println!("{}", if toggle1.value() { "true" } else { "false" }); } for _i in 0..n { let _toggle = Toggle::new(true); } println!(); let mut ntoggle1 = NthToggle::new(true, 3); for _i in 0..8 { ntoggle1.activate(); println!("{}", if ntoggle1.value() { "true" } else { "false" }); } for _i in 0..n { let _ntoggle = NthToggle::new(true, 3); } }
Porting notes
This program is similar to methcall. The Toggle
and the NthToggle
classes or structs are identical and the usage of them are somewhat different in the main
function.