monotone

monotone Mtn Source Tree

Root/interner.hh

1#ifndef __INTERNER_HH__
2#define __INTERNER_HH__
3
4// copyright (C) 2002, 2003 graydon hoare <graydon@pobox.com>
5// all rights reserved.
6// licensed to the public under the terms of the GNU GPL (>= 2)
7// see the file COPYING for details
8
9#include <map>
10#include <string>
11
12#include "sanity.hh"
13
14template <typename T>
15struct interner
16{
17 std::map<std::string, T> fwd;
18 std::map<T, std::string> rev;
19 T max;
20 interner() : max(0) {}
21 std::string lookup (T in) const
22 {
23 typename std::map<T, std::string>::const_iterator i = rev.find(in);
24 I(i != rev.end());
25 return i->second;
26 }
27 T intern(std::string const & s)
28 {
29 typename std::map<std::string, T>::const_iterator i = fwd.find(s);
30 if (i == fwd.end())
31 {
32++max;
33I(rev.find(max) == rev.end());
34fwd.insert(make_pair(s, max));
35rev.insert(make_pair(max, s));
36return max;
37 }
38 else
39 return i->second;
40 }
41};
42
43#endif // __INTERNER_HH__

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status