Steve Wampler wrote:
> Steve Wampler wrote:
> While it uses co-expressions (lots of them, in fact), I'm pretty sure
> it wouldn't be hard to rewrite it to remove the co-expressions also.
> That would probably reduce the memory footprint significantly.

Yes, replacing the previous implementation of the Triplet class with:

    global multipliers

    class Triplet : Class (base, h)

        method curval()
            return base * multipliers[h]
        end

        method nextVal()
            suspend base*multipliers[h +:= 1]
        end

        initially (baseNum)
            base := baseNum
            h := 1
            /multipliers := [2,3,5]
    end

works nicely, *has* to take less memory, and is probably faster...

(Incidently, Triplet doesn't need to subclass Class(), that's my own
little idiom.)

> ------------------------------------------------------------------------
> 
> # Lazily generate the three hamming numbers that can be derived directly
> #   from a known hamming number h
> class Triplet : Class (h, ce)
> 
>     method curval()
>         return h
>     end
> 
>     method nextVal()
>         if h := @ce then {
>             suspend h
>             }
>     end
> 
>     initially (baseNum)
>         h := 2*baseNum
>         ce := create (3|5)*baseNum
> end
> 
> # Generate hamming numbers, in order.  Default is first 30
> #  But an optional argument can be used to generate more (or less)
> #   e.g. hamming2 5000 generates the first 5000.
> procedure main(args)
>     limit := integer(args[1]) | 30
>     every write("\t", generateHamming(triples) \ limit)
> end
> 
> # Do the work.   Start with known hamming number 1 and maintain
> #   a set of triplet hamming numbers as they get derived from that
> #   one.  Most of the code here is to figure out which hamming
> #   number is next in sequence (while removing duplicates)
> procedure generateHamming()
>     triplers := set()
>     insert(triplers, Triplet(1))
> 
>     suspend 1
>     repeat {
>         # Pick a hamming triplet to *may* have the next smallest number
>         t1 := !triplers         # any will do to start
> 
>         every t2 := !triplers do {
>             if t1 === t2 then next
>             if t1.curval() > t2.curval() then {
>                # oops we were wrong, switch assumption
>                t1 := t2
>                }
>             else if t1.curval() = t2.curval() then {
>                # t2's value is a duplicate, so
>                # advance triplet t2, if none left in t2, remove it
>                if not t2.nextVal() then delete(triplers, t2)
>                }
>            }
> 
>         # Ok, t1 has the next hamming number, grab it
>         nextHamming := t1.curval()
>         # Advance triplet t1, if none left in t1, remove it
>         if not t1.nextVal() then delete(triplers, t1)
>         insert(triplers, Triplet(nextHamming)) 
>         suspend nextHamming
>         }
> end


-- 
Steve Wampler -- [EMAIL PROTECTED]
The gods that smiled on your birth are now laughing out loud.

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Unicon-group mailing list
Unicon-group@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/unicon-group

Reply via email to