All Source For se |
Ackermann's Function |
-- -*- mode: eiffel -*-
-- $Id: ackermann.se,v 1.1 2000/10/12 07:11:42 doug Exp $
-- http://www.bagley.org/~doug/shootout/
class ACKERMANN
creation make
feature
make is
local
num: INTEGER;
do
if argument_count = 1 then
num := argument(1).to_integer
else
num := 1
end
std_output.put_string("Ack(3,")
std_output.put_integer(num)
std_output.put_string("): ")
std_output.put_integer(ack(3, num))
std_output.put_new_line
end;
feature {NONE}
ack(m, n:INTEGER):INTEGER is
do
if m = 0 then
Result := n + 1
elseif n = 0 then
Result := ack(m - 1, 1)
else
Result := ack(m - 1, ack(m, (n - 1)))
end
end;
end
|
Array Access |
-- -*- mode: eiffel -*-
-- $Id: ary3.se,v 1.1 2001/05/31 02:27:48 doug Exp $
-- http://www.bagley.org/~doug/shootout/
-- Friedrich Dominicus points out that it is about twice as fast
-- when we substitute NATIVE_ARRAY for ARRAY. I've commented out
-- my original code below and now use NATIVE_ARRAY.
class ARY3
creation make
feature
make is
local
x: NATIVE_ARRAY[INTEGER];
y: NATIVE_ARRAY[INTEGER];
-- x: ARRAY[INTEGER];
-- y: ARRAY[INTEGER];
i,k,n: INTEGER;
do
if argument_count = 1 then
n := argument(1).to_integer
else
n := 1
end
x := x.calloc(n);
y := y.calloc(n);
-- !!x.make(0,n)
-- !!y.make(0,n)
from
i := 0
until
i = n
loop
x.put(i + 1, i)
i := i + 1
end
from
k := 0
until
k = 1000
loop
from
i := n-1
until
i < 0
loop
y.put(x.item(i) + y.item(i),i)
i := i - 1
end
k := k + 1
end
std_output.put_integer(y.item(0))
std_output.put_character(' ')
std_output.put_integer(y.item(n-1))
std_output.put_character('%N')
end
end
|
Count Lines/Words/Chars |
-- -*- mode: eiffel -*-
-- $Id: wc.se,v 1.1 2001/05/14 17:39:13 doug Exp $
-- http://www.bagley.org/~doug/shootout/
class WC
creation make
feature
make is
local
nl, nw, nc, state: INTEGER;
c: CHARACTER;
do
nl := 0
nw := 0
nc := 0
state := 0
from
io.read_character
until
io.end_of_input
loop
c := io.last_character
nc := nc + 1
if c = '%N' then
nl := nl + 1
end
if c = ' ' or c = '%N' or c = '%T' then
state := 0
else
if state = 0 then
state := 1
nw := nw + 1
end
end
io.read_character
end
std_output.put_integer(nl)
std_output.put_character(' ')
std_output.put_integer(nw)
std_output.put_character(' ')
std_output.put_integer(nc)
std_output.put_character('%N')
end
end
|
Exception Mechanisms |
-- -*- mode: eiffel -*-
-- $Id: except.se,v 1.2 2001/05/23 18:26:02 doug Exp $
-- http://www.bagley.org/~doug/shootout/
-- from: Friedrich Dominicus
-- <LOC-OFF>
indexing
description: "Eiffel implementation of the Exceptions "
"in the shootout examples";
note: "Tested with SmallEiffel and ISE-Eiffel"
-- <LOC-ON>
class
EXCEPT
inherit
ARGUMENTS
creation
make
feature -- Initialization
high: INTEGER;
low: INTEGER;
high_exception: STRING is "high_exception";
low_exception: STRING is "low_exception";
run_n, outer_i: INTEGER;
exc: EXCEPTIONS;
make is
do
create exc;
if argument_count /= 1 then
io.put_string("exception_test <integer>%N");
exc.die(1);
end;
if argument(1).is_integer then
run_n := argument(1).to_integer;
else
io.put_string("Argument wasn't an integer, giving up%N");
exc.die(1);
end;
outer_i := 1;
some_fun(run_n);
end;
some_fun(n: INTEGER) is
do
from
invariant
i_in_bounds: outer_i >= 1 and then outer_i <= n + 1;
variant
really_decreasing: n - outer_i + 1
until outer_i > run_n
loop
high_fun(outer_i);
-- an exception should be raised somwehere below
-- `high_fun' in the call chain
exc.raise("should not come here%N")
-- outer_i := outer_i + 1
end;
io.put_string("Exceptions: HI=");
io.put_integer(high);
io.put_string(" / LO=");
io.put_integer(low);
io.put_character('%N');
rescue
outer_i := outer_i + 1;
retry;
end;
high_fun (i: INTEGER) is
do
low_fun(i);
rescue
if exc.developer_exception_name.is_equal(high_exception)
then
high := high + 1;
end
end;
low_fun (i: INTEGER) is
do
-- exc.catch(exc.Developer_exception);
blow_up(i);
rescue
if exc.developer_exception_name.is_equal(low_exception) then
low := low + 1;
end
end;
blow_up (i : INTEGER) is
do
if ((i \\ 2) = 0) then
exc.raise(low_exception);
else
exc.raise(high_exception);
end;
end;
end -- class EXCEPT
|
Fibonacci Numbers |
-- -*- mode: eiffel -*-
-- $Id: fibo.se,v 1.2 2000/12/24 19:10:50 doug Exp $
-- http://www.bagley.org/~doug/shootout/
class FIBO
creation make
feature
make is
local
n: INTEGER;
do
if argument_count = 1 then
n := argument(1).to_integer
else
n := 1
end
std_output.put_integer(fib(n))
std_output.put_new_line
end;
feature {NONE}
fib(n:INTEGER):INTEGER is
do
if n < 2 then
Result := 1
else
Result := fib(n-2) + fib(n-1)
end
end;
end
|
Hash (Associative Array) Access |
-- -*- mode: eiffel -*-
-- $Id: hash.se,v 1.3 2001/01/18 20:57:05 doug Exp $
-- http://www.bagley.org/~doug/shootout/
class HASH
creation make
feature -- Initialization
make is
local
n : INTEGER
i, j, c : INTEGER
s : STRING
ht : DICTIONARY[INTEGER, STRING]
arg: ARGUMENTS
do
n := argument(1).to_integer
!!ht.with_capacity(n);
from i := 1
until i > n
loop
s := i.to_hexadecimal
from j := 1
until s @ j /= '0'
loop j := j + 1
end
ht.put (i, s.substring(j,s.count))
i := i+1
end
from i := n
until i = 0
loop
s := i.to_string
if ht.has (s) then
c := c + 1
end
i := i - 1
end
print (c.out + "%N")
end
end -- class HASH
|
Hashes, Part II |
-- -*- mode: eiffel -*-
-- $Id: hash2.se,v 1.2 2001/04/26 07:23:16 doug Exp $
-- http://www.bagley.org/~doug/shootout/
class HASH2
creation make
feature -- Initialization
make is
local
n, i, prev : INTEGER
hash1 : DICTIONARY[INTEGER, STRING]
hash2 : DICTIONARY[INTEGER, STRING]
it: ITERATOR[STRING]
do
n := argument(1).to_integer
!!hash1.with_capacity(10000);
!!hash2.with_capacity(10000);
-- std_output.put_string("building hash1")
-- std_output.put_character('%N')
from i := 0
until i = 10000
loop
-- std_output.put_integer(i)
-- std_output.put_character('%N')
hash1.put(i, "foo_" + i.to_string)
i := i + 1
end
from i := 0
until i = n
loop
-- std_output.put_string("building hash2")
-- std_output.put_character('%N')
it := hash1.get_new_iterator_on_keys
from it.start
until it.is_off
loop
if hash2.has(it.item) then
prev := hash2.at(it.item)
else
prev := 0
end
hash2.put(prev + hash1.at(it.item), it.item)
it.next;
end
i := i + 1
end
std_output.put_integer(hash1.at("foo_1"))
std_output.put_string(" ")
std_output.put_integer(hash1.at("foo_9999"))
std_output.put_string(" ")
std_output.put_integer(hash2.at("foo_1"))
std_output.put_string(" ")
std_output.put_integer(hash2.at("foo_9999"))
std_output.put_character('%N')
end
end -- class HASH2
|
Heapsort |
-- -*- mode: eiffel -*-
-- $Id: heapsort.se,v 1.1 2001/05/21 18:18:41 doug Exp $
-- http://www.bagley.org/~doug/shootout/
class HEAPSORT
creation make
feature
make is
local
array: ARRAY[DOUBLE]
n: INTEGER
do
n := argument(1).to_integer
!!array.make(1, n)
fill_array(array)
sort_array(n, array)
io.put_string(array.item(n).to_string_format(10))
io.put_new_line
end
sort_array(n: INTEGER; ra: ARRAY[DOUBLE]) is
local
i, j, ir, l: INTEGER
rra: DOUBLE
done: BOOLEAN
do
j := 0
i := 0
rra := 0.0
ir := n
l := n // 2
done := false
from until done loop
if l > 1 then
l := l - 1
rra := ra.item(l)
else
rra := ra.item(ir)
ra.put(ra.item(1), ir)
ir := ir - 1
if ir = 1 then
ra.put(rra, 1)
-- should throw exception out of here instead of
-- using boolean
done := true
end
end
if not done then
i := l
j := l * 2
from until j > ir loop
if (j < ir) and (ra.item(j) < ra.item(j+1)) then
j := j + 1
end
if rra < ra.item(j) then
ra.put(ra.item(j), i)
i := j
j := j + i
else
j := ir + 1
end
end
ra.put(rra, i)
end
end
end
fill_array(an_array: ARRAY[DOUBLE]) is
local
rand: RANDOMNUMBER
index: INTEGER
do
from
!!rand.make
index := an_array.lower
until
index > an_array.upper
loop
an_array.put(rand.next(1), index)
index := index + 1
end
end
end
|
Hello World |
-- -*- mode: eiffel -*-
-- $Id: hello.se,v 1.2 2001/06/18 01:13:58 doug Exp $
-- http://www.bagley.org/~doug/shootout/
class HELLO
creation make
feature
make is
local
do
std_output.put_string("hello world")
std_output.put_new_line
end
end
|
Matrix Multiplication |
-- -*- mode: eiffel -*-
-- $Id: matrix.se,v 1.3 2001/05/23 18:28:58 doug Exp $
-- http://www.bagley.org/~doug/shootout/
-- from Steve Thompson
-- <LOC-OFF>
indexing
description: "This class performs the matrix multiplication test"
author : Steve Thompson
email : "Steve_Thompson@prodigy.net"
date : February 18, 2001
compile: "compile -clean -boost -no_split -O3 main.e -o main"
run : "main 300"
-- <LOC-ON>
class MATRIX
creation make
feature -- Creation
make is
local
index, count: INTEGER
m1, m2: like matrix
do
from
if argument_count < 1 then
count := 1
else
count := argument(1).to_integer
end
index := 0
m1 := new_matrix(30, 30)
m2 := new_matrix(30, 30)
!!matrix.make(0, 29, 0, 29)
until
index = count
loop
mmult(30, 30, m1, m2)
index := index + 1
end -- from
print(matrix.item(0, 0).to_string + " " + matrix.item(2, 3).to_string + " " +
matrix.item(3, 2).to_string + " " + matrix.item(4, 4).to_string + "%N")
end -- make
feature -- Queries
matrix: ARRAY2[INTEGER]
new_matrix(rows, columns: INTEGER): like matrix is
-- Create and populate a new matrix.
local
i, j, count: INTEGER
do
!!Result.make(0, rows - 1, 0, columns - 1)
from
count := 1
i := 0
until i = rows loop
from j := 0 until j = columns loop
Result.put(count, i, j)
count := count + 1
j := j + 1
end
i := i + 1
end
end -- new_matrix
feature -- Commands
zero_matrix(rows, columns: INTEGER; a_matrix: like matrix) is
-- Clear a matrix
do
matrix.make(0, rows - 1, 0, columns - 1)
end -- zero_matrix
mmult(rows, columns: INTEGER; first, second: like matrix) is
-- Multiply two matrices.
local
i, j, k, val: INTEGER
do
zero_matrix(rows, columns, matrix)
from i := 0 until i = rows loop
from j := 0 until j = columns loop
val := 0
from k := 0 until k = columns loop
val := val + first.item(i, k) * second.item(k, j)
k := k + 1
end
matrix.put(val, i, j)
j := j + 1
end
i := i + 1
end -- from
end -- mmult
end
|
Method Calls |
-- -*- mode: eiffel -*-
-- $Id: methcall.se,v 1.2 2001/05/23 18:29:16 doug Exp $
-- http://www.bagley.org/~doug/shootout/
-- from Steve Thompson
-- <LOC-OFF>
indexing
description: "This class is the entry point for the method call test"
author : Steve Thompson
email : "Steve_Thompson@prodigy.net"
date : February 18, 2001
compile: "compile -clean -boost -no_split -O3 main.e -o main"
run : "main 400000"
-- <LOC-ON>
class METHCALL
creation make
feature -- Creation
make is
local
index: INTEGER
n: INTEGER
toggle: TOGGLE
value: BOOLEAN
nth_toggle: NTH_TOGGLE
do
if argument_count = 0 then
n := 1
else
n := argument(1).to_integer
end
value := True
!!toggle.make(value)
from index := 0 until index = n loop
value := toggle.activate.value
index := index + 1
end
if value then print("true%N") else print("false%N") end
value := True
!!nth_toggle.make(value, 3)
from index := 0 until index = n loop
value := nth_toggle.activate.value
index := index + 1
end
if value then print("true%N") else print("false%N") end
end -- make
end
|
Nested Loops |
-- -*- mode: eiffel -*-
-- $Id: nestedloop.se,v 1.1 2000/12/30 21:42:57 doug Exp $
-- http://www.bagley.org/~doug/shootout/
class NESTEDLOOP
creation make
feature
make is
local
n,a,b,c,d,e,f,x: INTEGER;
do
if argument_count = 1 then
n := argument(1).to_integer
else
n := 1
end
x := 0
from
a := 0
until
a = n
loop
from
b := 0
until
b = n
loop
from
c := 0
until
c = n
loop
from
d := 0
until
d = n
loop
from
e := 0
until
e = n
loop
from
f := 0
until
f = n
loop
x := x + 1
f := f + 1
end
e := e + 1
end
d := d + 1
end
c := c + 1
end
b := b + 1
end
a := a + 1
end
std_output.put_integer(x)
std_output.put_character('%N')
end;
end
|
Object Instantiation |
-- -*- mode: eiffel -*-
-- $Id: objinst.se,v 1.2 2001/05/23 18:29:50 doug Exp $
-- http://www.bagley.org/~doug/shootout/
-- from Steve Thompson
-- <LOC-OFF>
indexing
description: "This class is the entry point for the instantiation performance test"
author : Steve Thompson
email : "Steve_Thompson@prodigy.net"
date : February 18, 2001
compile: "compile -clean -boost -no_split -O3 main.e -o main"
run : "main 400000"
-- <LOC-ON>
class OBJINST
creation make
feature -- Creation
make is
local
index: INTEGER
n: INTEGER
toggle: TOGGLE
nth_toggle: NTH_TOGGLE
do
if argument_count = 0 then
n := 1
else
n := argument(1).to_integer
end
!!toggle.make(True)
from index := 0 until index = 5 loop
print(toggle.activate.value)
print("%N")
index := index + 1
end
from index := 0 until index = n loop
!!toggle.make(True)
index := index + 1
end
print("%N")
!!nth_toggle.make(True, 3)
from index := 0 until index = 8 loop
print(nth_toggle.activate.value)
print("%N")
index := index + 1
end
from index := 0 until index = n loop
!!nth_toggle.make(True, 3)
index := index + 1
end
end -- make
end
|
Random Number Generator |
-- -*- mode: eiffel -*-
-- $Id: random.se,v 1.3 2001/05/23 18:30:39 doug Exp $
-- http://www.bagley.org/~doug/shootout/
-- <LOC-OFF>
indexing
description: "This class is the entry point for the random number generation test"
author : Steve Thompson
email : "Steve_Thompson@prodigy.net"
date : February 18, 2001
compile: "compile -clean -case_insensitive -boost -no_split -O3 main.e -o main"
run : "main 900000"
-- <LOC-ON>
class RANDOM
creation make
feature -- Creation
make is
local
random: RANDOMNUMBER
n: INTEGER
answer: DOUBLE
index: INTEGER
do
if argument_count = 0 then
n := 1
else
n := argument(1).to_integer
end
from
index := n
!!random.make
until index = 0 loop
answer := random.next(100.0)
index := index - 1
end
io.put_string(answer.to_string_format(9))
io.put_new_line
end
end
|
Reverse a File |
-- -*- mode: eiffel -*-
-- $Id: reversefile.se,v 1.3 2001/07/07 00:46:25 doug Exp $
-- http://www.bagley.org/~doug/shootout/
-- from Daniel F Moisset
class REVERSEFILE
creation make
feature
make is
local
i: ITERATOR[STRING]
ll_string: LINKED_LIST[STRING];
do
!!ll_string.make;
from
io.read_line
until io.end_of_input loop
ll_string.add_first(clone(io.last_string));
io.read_line;
end;
i := ll_string.get_new_iterator
from
i.start
until i.is_off loop
io.put_string (i.item);
io.put_character('%N');
i.next;
end;
end;
end -- class REVERSEFILE
|
Sieve of Erathostenes |
-- -*- mode: eiffel -*-
-- $Id: sieve.se,v 1.4 2001/06/10 04:23:34 doug Exp $
-- http://www.bagley.org/~doug/shootout/
class SIEVE
creation make
feature
make is
local
count: INTEGER;
flags: ARRAY[CHARACTER];
i: INTEGER;
num: INTEGER;
j: INTEGER;
k: INTEGER;
do
if argument_count = 1 then
num := argument(1).to_integer
else
num := 1
end
!!flags.make(0, 8193)
from
j := 0
until
j = num
loop
count := 0
from
i := 2
until
i > 8192
loop
flags.put('t', i)
i := i + 1
end
from
i := 2
until
i > 8192
loop
if flags.item(i) = 't' then
from
k := i + i
until
k > 8192
loop
flags.put('f', k)
k := k + i
end
count := count + 1
end
i := i + 1
end
j := j + 1
end
std_output.put_string("Count: ")
std_output.put_integer(count)
std_output.put_character('%N')
end;
end
|
Spell Checker |
-- -*- mode: eiffel -*-
-- $Id: spellcheck.se,v 1.3 2001/05/23 18:24:46 doug Exp $
-- http://www.bagley.org/~doug/shootout/
-- from Steve Thompson
-- <LOC-OFF>
indexing
description: "This class performs the spell check test"
author : Steve Thompson
email : "Steve_Thompson@prodigy.net"
date : February 18, 2001
compile: "compile -clean -boost -no_split -O3 main.e -o main"
run : "main < Input"
-- <LOC-ON>
class SPELLCHECK
creation make
feature -- Creation
make is
do
read_dictionary
from
std_input.read_line
until std_input.end_of_input loop
if dictionary.has(std_input.last_string) = False then
print(std_input.last_string + "%N")
end
std_input.read_line
end
end -- make
feature -- Queries
dictionary: DICTIONARY[INTEGER, STRING]
feature -- Commands
read_dictionary is
local
file: STD_FILE_READ
value: INTEGER
do
value := 1
from
!!dictionary.with_capacity(60000)
!!file.connect_to("Usr.Dict.Words")
until
file.end_of_input
loop
file.read_line
dictionary.add(value, file.last_string)
end
file.disconnect
end -- read_dictionary
end
|
Statistical Moments |
-- -*- mode: eiffel -*-
-- $Id: moments.se,v 1.2 2001/05/23 18:29:33 doug Exp $
-- http://www.bagley.org/~doug/shootout/
-- from Steve Thompson
-- <LOC-OFF>
indexing
description: "This class performs the statistical moments test"
author : Steve Thompson
email : "Steve_Thompson@prodigy.net"
date : February 18, 2001
compile: "compile -clean -boost -no_split -O3 main.e -o main"
run : "main < Input"
-- <LOC-ON>
class MOMENTS
creation make
feature -- Creation
make is
local
index: INTEGER
number: INTEGER
mid: INTEGER
deviation: DOUBLE
sorter: COLLECTION_SORTER[INTEGER]
do
!!values.make(0,499)
read_values
mean := sum / values.count
from index := values.lower until index > values.upper loop
number := values @ index
deviation := number - mean
average_deviation := average_deviation + deviation.abs
variance := variance + deviation ^ 2
skew := skew + deviation ^ 3
kurtosis := kurtosis + deviation ^ 4
index := index + 1
end
average_deviation := average_deviation / values.count
variance := variance / (values.count - 1)
standard_deviation := variance.sqrt --math.sqrt(variance)
if variance > 0.0 then
skew := skew / (values.count * variance * standard_deviation)
kurtosis := kurtosis / (values.count * variance * variance) - 3.0
end
sorter.sort(values)
mid := values.count // 2
if (values.count \\ 2) = 0 then
median := (values.item(mid) + values.item(mid - 1)) / 2
else
median := values @ mid
end
print ("n: " + values.count.to_string + "%N")
print ("median: " + median.to_string + "%N")
print ("mean: " + mean.to_string + "%N")
print ("average_deviation: " + average_deviation.to_string + "%N")
print ("standard_deviation: " + standard_deviation.to_string + "%N")
print ("variance: " + variance.to_string + "%N")
print ("skew: " + skew.to_string + "%N")
print ("kurtosis: " + kurtosis.to_string + "%N")
end -- make
feature -- Queries
sum: INTEGER
mean,
median,
average_deviation,
standard_deviation,
variance,
skew,
kurtosis: DOUBLE
values: ARRAY[INTEGER]
-- Values read from stdin
read_values is
local
value: INTEGER
index: INTEGER
do
from
index := values.lower
std_input.read_line
until std_input.end_of_input loop
value := std_input.last_string.to_integer
values.force(value, index)
std_input.read_line
sum := sum + value
index := index + 1
end
end -- get_stdin
end
|
String Concatenation |
-- -*- mode: eiffel -*-
-- $Id: strcat.se,v 1.3 2001/04/29 06:13:05 doug Exp $
-- http://www.bagley.org/~doug/shootout/
-- from: Friedrich Dominicus
class STRCAT
creation
make
feature
concat_string : STRING is "hello%N";
make is
local
i,n : INTEGER;
str: STRING;
do
if argument_count = 1 then
n := argument(1).to_integer
else
n := 1
end
from i := 1
!!str.make(100);
until i > n
loop
str.append(concat_string);
i := i + 1;
end;
io.put_integer(str.count);
io.put_character ('%N');
end;
end
|
Sum a Column of Integers |
-- -*- mode: eiffel -*-
-- $Id: sumcol.se,v 1.1 2000/10/12 07:11:43 doug Exp $
-- http://www.bagley.org/~doug/shootout/
class SUMCOL
creation make
feature
make is
local
sum: INTEGER;
do
sum := 0
from
io.read_line
until
io.end_of_input
loop
sum := sum + io.last_string.to_integer
io.read_line
end
std_output.put_integer(sum)
std_output.put_character('%N')
end
end
|