Students may want to reuse these Timing classes
from SFT2201 in their SFT2021/SFT3021 Assignment.
*************************************************************
deferred class TIMABLE
-- Facilities for setting alarms and awaking sleepers.
inherit
CONSTANTS
feature -- Element change
set_alarm (tm: REAL) is
-- Tell clock to wake me up after "tm" has passed.
require
tm > 0
do
Clock.set_alarm (Current, tm)
end -- set_alarm
wake_up is
-- Action to be performed when clock wakes me up
deferred
end -- wake-up
end -- TIMABLE
*********************************************************
class RANDOM_GENERATOR
creation
make
feature -- Access
item : INTEGER is
-- What is the current number in the sequence?
local r : RANDOM
do
r ?= c;
Result := r.i_th (index)
end
feature -- Element change
next_range: INTEGER is
-- What is the next random number in the interval 1 and 100?
do
generate;
Result := as_range (index)
end;
generate is
-- Generate the next random number.
local
i : integer
do
index := index + 1
i := c.item (index);
ensure
size_increased: index = old index + 1
end
value_between ( lower: INTEGER; upper : INTEGER) : INTEGER is
-- What is the next random number in the range lower - upper?
require
range_ok: lower < upper
do
generate;
Result := ( item \\ (upper - lower)) + 1
end
feature -- Initialization
make is
-- Create a random sequence.
do
!RANDOM!c.make;
end -- make;
feature -- Measurement
count: INTEGER is
-- What size is the random sequence?
do
Result := index
end -- count;
feature -- Conversion
as_real ( i: INTEGER): REAL is
-- What is the i_th random as a real number between 0 and 1?
require
index_in_range: i <= count
local r : RANDOM
do
r ?= c;
Result := r.real_i_th (index)
end;
as_range (i : INTEGER) : INTEGER is
-- What is the i_th random number in the interval 1 and 100?
do
Result := (as_real (i) * 100).rounded
end
feature {NONE}
index: INTEGER
c: COUNTABLE [INTEGER]
end -- class RANDOM_TEST
********************************************************************
class MANUAL_CLOCK
-- A clock which can be manually set.
inherit
CLOCK
creation
make
feature -- Initialization
make (t: REAL) is
-- Create an instance of Manual Clock with a tick value of t.
require
positive_tick: t > 0.0
do
tick := t
time := 1.0
!!sleepers.make
end -- make
feature {SIMULATION}
time: REAL
-- For how many ticks will the clock run?
step is
-- Increment "time" by "tick"
do
time := time + tick
wake_up;
ensure
stepped: time = old time + tick
end -- step
reset is
do
time := 0
end -- reset
feature {NONE}
tick : REAL
-- Duration of each tick.
end -- MANUAL_CLOCK
****************************************************
deferred class CLOCK
feature -- Element change
reset is
-- Reset clock's time to zero.
deferred
ensure
time = 0
end -- reset
set_alarm (sleeper: TIMABLE; tm : REAL) is
-- Register sleeper so that it will be awakened as
-- soon as time >= tm + (current value of time)
require
valid_sleeper: sleeper /= Void
valid_time: tm > 0.0
local
com_pair: COMPARABLE_PAIR [TIMABLE, REAL]
do
!!com_pair.set_x_y (sleeper, time + tm)
sleepers.force (com_pair)
end -- set_alarm
wake_up is
-- Wake up sleepers whose time has come
do
from
until
sleepers.count = 0 or else
time < sleepers.item.y
loop
sleepers.item.x.wake_up
sleepers.remove
end
ensure
-- All sleepers still waiting have a wake up time greater then
-- the current time.
end -- wake_up
feature -- Access
time : REAL is
-- Elapsed time since clock was started or reset
deferred
end -- time
tick : REAL is
-- The granularity of the clock
deferred
end -- tick
feature {NONE}
sleepers : LINKED_PRIORITY_QUEUE [COMPARABLE_PAIR [ TIMABLE, REAL]]
invariant
time_ok: time >= 0.0
ticking: tick > 0.0
end -- CLOCK
***************************************************************************
class COMPARABLE_PAIR [ X, Y -> COMPARABLE]
-- Entities of type X may be compared on the basis of characteristic
-- Y.
inherit
COMPARABLE
creation
set_x_y
feature
x : X
-- The entity to be compared.
y : Y
-- The characteristic to be compared.
set_x_y (an_x: X; a_y: Y) is
-- Create an instance, and set the X and Y values.
do
x := an_x;
y := a_y
end
infix "<" (other : like Current): BOOLEAN is
-- The higher the number, the lower the priority
do
Result := (y > other.y)
ensure then
Result = (y > other.y)
end -- "<"
end -- COMPARABLE_PAIR
**********************************************************************
/