spot  2.15.1
ngraph.hh
1 // -*- coding: utf-8 -*-
2 // Copyright (C) by the Spot authors, see the AUTHORS file for details.
3 //
4 // This file is part of Spot, a model checking library.
5 //
6 // Spot is free software; you can redistribute it and/or modify it
7 // under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // Spot is distributed in the hope that it will be useful, but WITHOUT
12 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 // License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 
19 #pragma once
20 
21 #include <unordered_map>
22 #include <vector>
23 #include <spot/graph/graph.hh>
24 
25 namespace spot
26 {
27  template <typename Graph,
28  typename State_Name,
29  typename Name_Hash = std::hash<State_Name>,
30  typename Name_Equal = std::equal_to<State_Name>>
31  class SPOT_API named_graph
32  {
33  protected:
34  Graph& g_;
35  public:
36 
37  typedef typename Graph::state state;
38  typedef typename Graph::edge edge;
39  typedef State_Name name;
40 
41  typedef std::unordered_map<name, state,
42  Name_Hash, Name_Equal> name_to_state_t;
43  name_to_state_t name_to_state;
44  typedef std::vector<name> state_to_name_t;
45  state_to_name_t state_to_name;
46 
47  named_graph(Graph& g)
48  : g_(g)
49  {
50  }
51 
52  Graph& graph()
53  {
54  return g_;
55  }
56 
57  Graph& graph() const
58  {
59  return g_;
60  }
61 
62  template <typename... Args>
63  state new_state(name n, Args&&... args)
64  {
65  auto p = name_to_state.emplace(n, 0U);
66  if (p.second)
67  {
68  unsigned s = g_.new_state(std::forward<Args>(args)...);
69  p.first->second = s;
70  if (state_to_name.size() < s + 1)
71  state_to_name.resize(s + 1);
72  state_to_name[s] = n;
73  return s;
74  }
75  return p.first->second;
76  }
77 
84  bool alias_state(state s, name newname)
85  {
86  auto p = name_to_state.emplace(newname, s);
87  if (!p.second)
88  {
89  // The state already exists. Change its number.
90  auto old = p.first->second;
91  p.first->second = s;
92  // Add the successor of OLD to those of S.
93  auto& trans = g_.edge_vector();
94  auto& states = g_.states();
95  trans[states[s].succ_tail].next_succ = states[old].succ;
96  states[s].succ_tail = states[old].succ_tail;
97  states[old].succ = 0;
98  states[old].succ_tail = 0;
99  // Remove all references to old in edges:
100  unsigned tend = trans.size();
101  for (unsigned t = 1; t < tend; ++t)
102  {
103  if (trans[t].src == old)
104  trans[t].src = s;
105  if (trans[t].dst == old)
106  trans[t].dst = s;
107  }
108  }
109  return !p.second;
110  }
111 
112  state get_state(name n) const
113  {
114  return name_to_state.at(n);
115  }
116 
117  name get_name(state s) const
118  {
119  return state_to_name.at(s);
120  }
121 
122  bool has_state(name n) const
123  {
124  return name_to_state.find(n) != name_to_state.end();
125  }
126 
127  const state_to_name_t& names() const
128  {
129  return state_to_name;
130  }
131 
132  template <typename... Args>
133  edge
134  new_edge(name src, name dst, Args&&... args)
135  {
136  return g_.new_edge(get_state(src), get_state(dst),
137  std::forward<Args>(args)...);
138  }
139 
140  template <typename I, typename... Args>
141  edge
142  new_univ_edge(name src, I dst_begin, I dst_end, Args&&... args)
143  {
144  std::vector<unsigned> d;
145  d.reserve(std::distance(dst_begin, dst_end));
146  while (dst_begin != dst_end)
147  d.emplace_back(get_state(*dst_begin++));
148  return g_.new_univ_edge(get_state(src), d.begin(), d.end(),
149  std::forward<Args>(args)...);
150  }
151 
152  template <typename... Args>
153  edge
154  new_univ_edge(name src,
155  const std::initializer_list<State_Name>& dsts, Args&&... args)
156  {
157  return new_univ_edge(src, dsts.begin(), dsts.end(),
158  std::forward<Args>(args)...);
159  }
160  };
161 }
Definition: ngraph.hh:32
Abstract class for states.
Definition: twa.hh:47
bool alias_state(state s, name newname)
Give an alternate name to a state.
Definition: ngraph.hh:84
@ U
until
Definition: automata.hh:26

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.
Generated on Fri Feb 27 2015 10:00:07 for spot by doxygen 1.9.1