Tally: A Counting Dictionary

Here’s a simple object-oriented pattern that is useful to anyone doing statistical work.  I call it a Tally.  It’s a dictionary whose keys are arbitrary objects, whose values are integers, and whose default missing-key value is zero. There’s nothing deep here—the point is just to hide the default zero logic inside the class.  Here’s a Java version.

public class Tally extends HashMap {
   /**
    * @return the count of the item or zero if the item is absent
    */
   @Override
   public Long get(Object item) {
      return containsKey(item) ? super.get(item) : 0;
   }

   /**
    * Increment the count in the map by one.
    *
    * @param item item in the map
    * @return the old count in the map
    */
   public Long add(T item) {
      return add(item, 1L);
   }

   /**
    * Increment the count in the map by the specified amount.
    *
    * @param item      item in the map
    * @param increment count to increment by
    * @return the old count in the map
    */
   public Long add(T item, Long increment) {
      return put(item, get(item) + increment);
   }
}

The composition of an addition operator that sums the counts from two tallies keywise is left as an exercise for the reader.

Different languages make this default zero available in different ways. Python lets you specify it as an argument to a lookup function get, so the Python version of Tally is just a wisp of boilerplate.

class Tally(dict):
    def __getitem__(self, key):
        return self.get(key, 0)

    def add(self, key, increment = 1):
        self[key] += increment

Ruby lets you specify a default value in the Hash constructor.  I find writing a special add function here to be beyond the point of diminishing returns and just go with the out-of-the-box implementation.

t = Hash.new(0)
t[:dog] += 1
puts t[:dog] # Returns 1
t[:dog] += 1
puts t[:dog] # Returns 2
puts t[:cat] # Returns 0

I know everyone rolls their own one of these, but this one’s mine so feel free to copy it.

Advertisements
This entry was posted in Those that have just broken the flower vase. Bookmark the permalink.

One Response to Tally: A Counting Dictionary

  1. guest says:

    In Python you can use defaultdict(int), or better yet, collections.Counter.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s