[Shootout-list] Re: OO (was Re: process creation & message passing)
Isaac Gouy
igouy2@yahoo.com
Thu, 21 Oct 2004 11:25:13 -0700 (PDT)
> I'd like the shootout to be able to help someone show
> his boss "Hey, C++ is way better than our crufty C OO
> library because it's half the LOC to maintain, or it's
> only 0.5% slower on these types of problems, so let's
> use it".
Here's an Oberon-2 implementation of objinst
It doesn't create any objects and it doesn't send any messages.
It's just 1970's Pascal records (gcc structs) and procedure calls.
It's shorter than the OO version, and probably faster.
MODULE ObjInst2;
IMPORT Shootout, Out;
TYPE
ToggleDesc = RECORD
state: BOOLEAN;
END;
Toggle = POINTER TO ToggleDesc;
NthToggleDesc = RECORD
state: ToggleDesc;
countMax, counter: LONGINT;
END;
NthToggle = POINTER TO NthToggleDesc;
VAR
n, i: LONGINT;
toggle: Toggle;
ntoggle: NthToggle;
PROCEDURE NewToggle(state: BOOLEAN): Toggle;
VAR t: Toggle;
BEGIN
NEW(t); t.state := state;
RETURN t;
END NewToggle;
PROCEDURE NewNthToggle(state: BOOLEAN; countMax: LONGINT): NthToggle;
VAR t: NthToggle;
BEGIN
NEW(t);
t.counter := 0;
t.state.state := state;
t.countMax := countMax;
RETURN t;
END NewNthToggle;
PROCEDURE Activate(t: NthToggle);
BEGIN
INC(t.counter);
IF t.counter >= t.countMax THEN
t.state.state := ~t.state.state;
t.counter := 0;
END;
END Activate;
BEGIN
n := Shootout.Argi();
toggle := NewToggle(TRUE);
FOR i := 1 TO 5 DO
toggle.state := ~toggle.state;
IF toggle.state THEN Out.String("true");
ELSE Out.String("false"); END;
Out.Ln;
END;
FOR i := 1 TO n DO toggle := NewToggle(TRUE); END;
Out.Ln;
ntoggle := NewNthToggle(TRUE,3);
FOR i := 1 TO 8 DO
Activate(ntoggle);
IF ntoggle.state.state THEN Out.String("true");
ELSE Out.String("false"); END;
Out.Ln;
END;
FOR i := 1 TO n DO ntoggle := NewNthToggle(TRUE,3); END;
END ObjInst2.
_______________________________
Do you Yahoo!?
Declare Yourself - Register online to vote today!
http://vote.yahoo.com