#ifndef _alphabet_hh #define _alphabet_hh #include #include #include #include #include // Alphabet: indexes a set of types template class Alphabet: protected std::map { public: Alphabet() {}; bool empty() const { return std::map::empty(); } int size() const { return std::map::size(); } int operator[](const T &k) const { typename std::map::const_iterator cit = find(k); if (cit != std::map::end()) return cit->second; else return -1; } int lookup(const T &k) const { return (*this)[k]; } int insert(const T &k) { int sz = size(); assert((unsigned) sz == _items.size()); std::pair::iterator, bool> ins = std::map::insert(make_pair(k, sz)); if (ins.second) _items.push_back(k); return ins.first->second; } const T &type(int i) const { assert(i >= 0); assert(i < size()); return _items[i]; } std::ostream &display(std::ostream &out, int i) const { return out << type(i); } private: std::vector _items; }; #endif