KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Token_map.h
Go to the documentation of this file.
1 #ifndef TOKEN_MAP_H_
2 #define TOKEN_MAP_H_
3 
11 #include <boost/bimap.hpp>
12 #include <boost/bind.hpp>
13 #include <fstream>
14 #include <algorithm>
15 #include <string>
16 
17 namespace semantics
18 {
19 
20 class Token_map
21 {
22 public:
23  typedef std::string Key_type;
24  typedef size_t Val_type;
25  typedef boost::bimap<Key_type, Val_type> Map;
26  typedef Map::value_type Key_val_pair;
27  typedef Map::right_value_type Val_key_pair;
28  typedef Map::const_iterator const_iterator;
29 
31 
38  virtual ~Token_map() {}
39 
42  virtual Val_type get_code(const Token_map::Key_type& key);
43 
46  virtual Val_type encode(const Key_type& key, bool learn = false);
49  virtual const Key_type& decode(const Val_type& val);
52  virtual Val_type next_val() {return next_val_;}
53  friend std::ostream& operator<<(std::ostream& os, Token_map& map);
54 
55  const_iterator begin() {return map_.begin();}
56  const_iterator end(){return map_.end();}
57 
58 protected:
59  virtual const Key_type& unknown_key() = 0;
62 };
63 
64  template<class T>
65  void multi_encode(Token_map& map, const T& keys)
66  {
67  std::for_each(
68  keys.begin(), keys.end(),
69  boost::bind(&Token_map::encode, &map, _1, true)
70  );
71  }
72 
73  template<class T, class U, class M>
74  T multi_map_encode(M maps, U keys)
75  {
76  T vals;
77  std::transform(
78  keys.begin(), keys.end(), maps.begin(), vals.begin(),
79  boost::bind(&Token_map::encode, _1, _2, false)
80  );
81  return vals;
82  }
83 
84  template<class U, class T, class M>
85  U multi_map_decode(M maps, T vals)
86  {
87  U keys;
88  std::transform(
89  vals.begin(), vals.end(), maps.begin(), keys.begin(),
90  boost::bind(&Token_map::decode, _2, _1)
91  );
92  return keys;
93  }
94 
95 };
96 
97 #endif
T multi_map_encode(M maps, U keys)
Definition: Token_map.h:74
virtual Val_type get_code(const Token_map::Key_type &key)
look up code associated with key, or return UNKNOWN_TOKEN_VAL
Definition: Token_map.cpp:13
boost::bimap< Key_type, Val_type > Map
Definition: Token_map.h:25
Map::right_value_type Val_key_pair
Definition: Token_map.h:27
Map map_
Definition: Token_map.h:60
void multi_encode(Token_map &map, const T &keys)
Definition: Token_map.h:65
std::string Key_type
Definition: Token_map.h:23
Map::value_type Key_val_pair
Definition: Token_map.h:26
const_iterator end()
Definition: Token_map.h:56
static const Val_type UNKNOWN_TOKEN_VAL
Definition: Token_map.h:30
Definition: Token_map.h:20
virtual Val_type encode(const Key_type &key, bool learn=false)
look up code associated with key, adding it if not found
Definition: Token_map.cpp:20
size_t Val_type
Definition: Token_map.h:24
virtual Val_type next_val()
gets the next unused code available (e.g., for UNKNOWN)
Definition: Token_map.h:52
virtual const Key_type & unknown_key()=0
Token_map()
construct an empty token map
Definition: Token_map.h:34
Val_type next_val_
Definition: Token_map.h:61
virtual ~Token_map()
translate key into code
Definition: Token_map.h:38
const_iterator begin()
Definition: Token_map.h:55
U multi_map_decode(M maps, T vals)
Definition: Token_map.h:85
Map::const_iterator const_iterator
Definition: Token_map.h:28
virtual const Key_type & decode(const Val_type &val)
look up key associated with code
Definition: Token_map.cpp:33
friend std::ostream & operator<<(std::ostream &os, Token_map &map)
Definition: Token_map.cpp:40