blob: 1db928da3669d0913aff1d2312b3554e410606c6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
#ifndef _alphabet_hh
#define _alphabet_hh
#include <cassert>
#include <iosfwd>
#include <map>
#include <string>
#include <vector>
// Alphabet: indexes a set of types
template <typename T>
class Alphabet: protected std::map<T, int>
{
public:
Alphabet() {};
bool empty() const { return std::map<T,int>::empty(); }
int size() const { return std::map<T,int>::size(); }
int operator[](const T &k) const
{
typename std::map<T,int>::const_iterator cit = find(k);
if (cit != std::map<T,int>::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<typename std::map<T,int>::iterator, bool>
ins = std::map<T,int>::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<T> _items;
};
#endif
|