include/counter.h
author Dmitriy Morozov <morozov@cs.duke.edu>
Fri, 22 Dec 2006 12:52:35 -0500
changeset 8 7b688bc77e86
parent 0 d95020656286
child 11 a0736dd3c671
permissions -rw-r--r--
Added examples/alphashapes

/*
 * Author: Dmitriy Morozov
 * Department of Computer Science, Duke University, 2005 -- 2006
 */

#ifndef __COUNTER_H__
#define __COUNTER_H__

/* This should be integrated with the logging facilities. Written in the same framework. */

#include <map>
#include <string>
#include <iostream>

typedef 		unsigned long 		CounterType;
typedef			std::string			KeyType;

class CounterFactory
{
	private:
#ifdef COUNTERS
		typedef				std::map<int, CounterType> 			CountersMap;
		typedef				std::map<KeyType, CountersMap>		KeyMap;
		KeyMap				ctrs;
#endif // COUNTERS
		
	public:
		~CounterFactory()
		{
#ifdef COUNTERS
			std::cout << "Counters:" << std::endl;
			for (KeyMap::const_iterator cur = ctrs.begin(); cur != ctrs.end(); ++cur)
			{
				std::cout << cur->first << ": ";
				if (cur->second.size() == 1)
				{
					std::cout << cur->second.begin()->second << std::endl;
					continue;
				}
				std::cout << std::endl;
				for (CountersMap::const_iterator ccur = cur->second.begin();
														  ccur != cur->second.end();
														  ++ccur)
					std::cout << "  " << ccur->first << ": " << ccur->second << std::endl;
			}
#endif // COUNTERS
		}

		void inc(const KeyType& key, int num = 0)
		{
#ifdef COUNTERS
			ctrs[key][num]++;
#endif // COUNTERS
		}

		CounterType lookup(const KeyType& key, int num = 0) const
		{
#ifdef COUNTERS
			return ctrs[key][num];
#endif // COUNTERS
		}
};

static CounterFactory counters;

#endif // __COUNTER_H__