Vcsn  2.3
Be Rational
state-bimap.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <boost/bimap.hpp>
4 #include <boost/bimap/set_of.hpp>
5 #include <boost/bimap/unordered_set_of.hpp>
6 
8 #include <vcsn/misc/bimap.hh> // vcsn::has
9 #include <vcsn/misc/static-if.hh> // vcsn::has
10 #include <vcsn/misc/unordered_map.hh> // vcsn::has
11 
12 namespace vcsn
13 {
14  namespace detail
15  {
21  template <typename StateNameset, typename Stateset,
22  bool Bidirectional>
23  class state_bimap;
24 
31  template <typename StateNameset, typename Stateset>
32  class state_bimap<StateNameset, Stateset, true>
33  {
34  public:
35  using state_nameset_t = StateNameset;
36  using state_name_t = typename state_nameset_t::value_t;
37 
38  using stateset_t = Stateset;
39  using state_t = typename stateset_t::value_t;
40 
42  using left_t
43  = boost::bimaps::unordered_set_of<state_name_t,
47  using right_t = boost::bimaps::set_of<state_t>;
49  using bimap_t = boost::bimap<left_t, right_t>;
50 
51  using const_iterator = typename bimap_t::const_iterator;
52 
56  template <typename... Args>
57  auto emplace(Args&&... args)
58  {
59  return map_.insert({ std::forward<Args>(args)... });
60  }
61 
62  auto find_key(const state_name_t& s) const
63  {
64  return map_.left.find(s);
65  }
66 
67  auto end_key() const
68  {
69  return map_.left.end();
70  }
71 
73  static const state_name_t& state_name(const const_iterator& i)
74  {
75  return i->left;
76  }
77 
79  static state_t state(const const_iterator& i)
80  {
81  return i->right;
82  }
83 
85  using origins_t = typename bimap_t::right_map;
86  const origins_t& origins() const
87  {
88  return map_.right;
89  }
90 
91  private:
93  };
94 
95 
102  template <typename StateNameset, typename Stateset>
103  class state_bimap<StateNameset, Stateset, false>
104  {
105  public:
106  using state_nameset_t = StateNameset;
107  using state_name_t = typename state_nameset_t::value_t;
108 
109  using stateset_t = Stateset;
110  using state_t = typename stateset_t::value_t;
111 
112  using map_t = std::unordered_map<state_name_t, state_t,
113  vcsn::hash<state_nameset_t>,
115 
116  using const_iterator = typename map_t::const_iterator;
117 
121  template <typename... Args>
122  auto emplace(Args&&... args)
123  {
124  return map_.emplace(std::forward<Args>(args)...);
125  }
126 
127  auto find_key(const state_name_t& sn) const
128  {
129  return map_.find(sn);
130  }
131 
132  auto end_key() const
133  {
134  return map_.end();
135  }
136 
138  static const state_name_t& state_name(const const_iterator& i)
139  {
140  return i->first;
141  }
142 
144  static state_t state(const const_iterator& i)
145  {
146  return i->second;
147  }
148 
150  using origins_t = std::map<state_t, state_name_t>;
152  const origins_t& origins() const
153  {
154  if (origins_.empty())
155  for (const auto& p: map_)
156  origins_.emplace(p.second, p.first);
157  return origins_;
158  }
159 
160  private:
162  };
163  }
164 } // namespace vcsn
static state_t state(const const_iterator &i)
Get the state from a const_iterator.
Definition: state-bimap.hh:144
Request the unordered_map implementation.
auto emplace(Args &&...args)
Insert a new state.
Definition: state-bimap.hh:57
std::unordered_map< state_name_t, state_t, vcsn::hash< state_nameset_t >, vcsn::equal_to< state_nameset_t >> map_t
Definition: state-bimap.hh:114
Definition: a-star.hh:8
This is useful to make hashes with labels or weights as keys without using non-default constructors; ...
Definition: functional.hh:57
boost::bimaps::unordered_set_of< state_name_t, vcsn::hash< state_nameset_t >, vcsn::equal_to< state_nameset_t >> left_t
Storage for state names.
Definition: state-bimap.hh:45
A bidirectional map from state names to state numbers.
Definition: state-bimap.hh:23
auto emplace(Args &&...args)
Insert a new state.
Definition: state-bimap.hh:122
std::map< state_t, state_name_t > origins_t
A map from state indexes to state names.
Definition: state-bimap.hh:150
static const state_name_t & state_name(const const_iterator &i)
Get the state name from a const_iterator.
Definition: state-bimap.hh:138
boost::bimap< left_t, right_t > bimap_t
Bidirectional map state_name_t -> state_t;.
Definition: state-bimap.hh:49
boost::bimaps::set_of< state_t > right_t
Storage for state index.
Definition: state-bimap.hh:47
typename bimap_t::right_map origins_t
A map from state indexes to state names.
Definition: state-bimap.hh:85
static const state_name_t & state_name(const const_iterator &i)
Get the state name from a const_iterator.
Definition: state-bimap.hh:73
static state_t state(const const_iterator &i)
Get the state from a const_iterator.
Definition: state-bimap.hh:79
This is useful to make hashes with labels or weights as keys without using non-default constructors; ...
Definition: functional.hh:12