Files
box-o-sand/algs4/src/go/algs4/counter.go
Dan Buch 6ebbf04980 Re-namespacing one more time to get sources back in the same tree.
I dunno...  this whole thing should be its own repo, probably.  Meh.
2012-12-19 22:33:46 -05:00

38 lines
517 B
Go

package algs4
import (
"fmt"
)
type Counter struct {
name string
count int
}
func NewCounter(name string) *Counter {
counter := &Counter{}
counter.name = name
return counter
}
func (c *Counter) Increment() {
c.count += 1
}
func (c *Counter) Tally() int {
return c.count
}
func (c *Counter) String() string {
return fmt.Sprintf("%d %s", c.count, c.name)
}
func (c *Counter) Cmp(other *Counter) int {
if c.count < other.count {
return -1
} else if c.count > other.count {
return 1
}
return 0
}