% File "stats.tu" % % Written by Phil Edmonds, March 97. % % Maintains statistics. % Each statistic is named by a string (i.e., has a string as a key), and % has a counter. They are stored in a linked list. A call to Add either % increments the counter for an existing statistic, or creates a new % statistic with counter initialized at 1. % % NOTE: for purposes of the simulation, the World class instantiates % a Statistics object, and accesses its functions. See AddStatistic in % class World. % %+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ unit class Statistics import Set in "set.tu", PriorityQueue in "pq.tu", SimVar in "simvar.tu", ActiveObject in "active.tu", Plain in "plain.tu", Water in "water.tu", Mountain in "mountain.tu", Herbivore in "herbivor.tu" export Init, Add, Prnt %========== INSTANCE VARS. ================================== %============================================================ type statType : record stat : string count : int nextstat : ^statType end record var head : ^statType %========== INITIALIZE ====================================== %============================================================ proc Init head := nil end Init %========== PUBLIC FUNCTIONS ================================ %============================================================ % add to the statistics. Looks for stat in stats list, and updates % the counter. %------------------------------------------------------------ proc Add (stat : string) var curr : ^statType := head loop exit when curr = nil or curr -> stat = stat curr := curr -> nextstat end loop if curr = nil then var temp : ^statType new temp temp -> stat := stat temp -> count := 1 temp -> nextstat := head head := temp else curr -> count += 1 end if end Add % print out all the stats %-------------------------------------------------------------- proc Prnt (stream : int) var d, t : string date (d) time (t) put : stream, "Statistics ("+ d + " " + t +")" put : stream, "----------" put : stream, "" var curr : ^statType := head loop exit when curr = nil put : stream, curr -> stat, " : ", curr -> count curr := curr -> nextstat end loop end Prnt end Statistics