Useful General Purpose Classes in Mash

I have created some general purpose, OTcl only, classes that maybe useful to other OpenMash developers. Some of these classes re-implement existing functionality in OpenMash (such as Timer).

Here is a summary:

Data Structure Classes

  • MashQueue
  • MashStack
  • MashGraph
  • MashHeap

    (These should be self-explanatory).

    Synopsis

        set q [new MashQueue]
        $q enq "hello"
        $q enq "world"
        while {![$q is_empty]} {
    	puts [$q deq]
        }
    
        set s [new MashStack]
        $s push "hello"
        $s push "world"
        while {![$s is_empty]} {
    	puts [$s pop]
        }
    
        set h [new MashHeap]
        $h insert "James" 40
        $h insert "Wei" 31
        $h insert "Jason" 3
        $h decrease_key "Jason" 1
        while {![$h is_empty]} {
    	puts [$h delete_min]
        }
    
        set g [new MashGraph]
        $g add_node Berkeley
        $g add_node Oakland
        $g add_edge Berkeley Oakland
        puts "[$g get_in_edges Oakland]"
    
    

    Soft-State Classes

  • MashRNG
  • MashTimer
  • MashSoftState
  • MashSoftTable

    MashRNG is a random number generator. MashTimer is a replacement for Timer class. To use the Timer class, you have to either inherit from it (which, in most cases, results in bad OO design), and override its timeout{} method. MashTimer is easier and cleaner to use -- you pass in the command to eval whenever the timer expired, when you create a new timer.

    MashSoftState is an abstraction for a soft-state. You associate a value to the state, a timeout, and a command to eval when the state expired. You can refresh the state.

    MashSoftTable is an abstraction for a table of soft-states.

    Synopsis

        MashRNG seed 100
        MashRNG uniform 0 1   
        MashRNG integer 100
        MashRNG normal 3 5
    
        set t [new MashTimer/Once 3000 "puts Hi!"]
        $t cancel
        set t [new MashTimer/Periodic 3000 "puts Hi!"]
        $t cancel
    
        set alive [new MashSoftState "1" 1000 {puts "He's dead, Jim!"}]
        $alive set_value 1
        puts [$alive get_value]
        $alive refresh
    
        set table [new MashSoftTable 1000]
        $table set Jim 1
        $table set Bones 4
        puts [$table get Jim]
        if [$table has Bones] {
    	$table refresh Bones
        }
        $table delete Jim
    

    Misc.

  • MashLog

    A general purpose logging facility that allow you to switch logging output to files or stdout/stderr easily. Support timestamping and pid printing. (This is a factorization of logging code from hm).

    Synopsis

        MashLog file mymsg /var/mymsg.log
        MashLog timestamp on
        MashLog pid on
        MashLog off
        MashLog on
        MashLog log mymsg "This is a message!"
        MashLog mymsg "This is NOT a typo!"
    

    Wei Tsang Ooi Tue Aug 6 14:48:59 PDT 2002