ngraph.hh 3.31 KB
Newer Older
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
// -*- coding: utf-8 -*-
// Copyright (C) 2014 Laboratoire de Recherche et Développement de
// l'Epita.
//
// This file is part of Spot, a model checking library.
//
// Spot is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// Spot is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
// License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#ifndef SPOT_GRAPH_NGRAPH_HH
# define SPOT_GRAPH_NGRAPH_HH

#include <unordered_map>
#include <vector>
#include "graph.hh"

namespace spot
{
  template <typename Graph,
	    typename State_Name,
	    typename Name_Hash = std::hash<State_Name>,
	    typename Name_Equal = std::equal_to<State_Name>>
33
  class SPOT_API named_graph
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
  {
  protected:
    Graph& g_;
  public:

    typedef typename Graph::state state;
    typedef typename Graph::transition transition;
    typedef State_Name name;

    typedef std::unordered_map<name, state,
			       Name_Hash, Name_Equal> name_to_state_t;
    name_to_state_t name_to_state;
    typedef std::vector<name> state_to_name_t;
    state_to_name_t state_to_name;

    named_graph(Graph& g)
      : g_(g)
    {
    }

    Graph& graph()
    {
      return g_;
    }

    Graph& graph() const
    {
      return g_;
    }

    template <typename... Args>
    state new_state(name n, Args&&... args)
    {
      auto p = name_to_state.insert(std::make_pair(n, 0U));
      if (p.second)
	{
	  unsigned s = g_.new_state(std::forward<Args>(args)...);
	  p.first->second = s;
	  if (state_to_name.size() < s + 1)
	    state_to_name.resize(s + 1);
	  state_to_name[s] = n;
	  return s;
	}
      return p.first->second;
    }

80
81
82
83
84
85
86
    /// \brief Give an alternate name to a state.
    /// \return true iff the newname was already used.
    bool alias_state(state s, name newname)
    {
      return !name_to_state.insert(std::make_pair(newname, s)).second;
    }

87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
    state get_state(name n) const
    {
      return name_to_state.at(n);
    }

    name get_name(state s) const
    {
      return state_to_name.at(s);
    }

    template <typename... Args>
    transition
    new_transition(name src, name dst, Args&&... args)
    {
      return g_.new_transition(get_state(src), get_state(dst),
			       std::forward<Args>(args)...);
    }

    template <typename... Args>
    transition
    new_transition(name src,
		   const std::vector<State_Name>& dst, Args&&... args)
    {
      std::vector<State_Name> d;
      d.reserve(dst.size());
      for (auto n: dst)
	d.push_back(get_state(n));
      return g_.new_transition(get_state(src), d, std::forward<Args>(args)...);
    }

    template <typename... Args>
    transition
    new_transition(name src,
		   const std::initializer_list<State_Name>& dst, Args&&... args)
    {
      std::vector<state> d;
      d.reserve(dst.size());
      for (auto n: dst)
	d.push_back(get_state(n));
      return g_.new_transition(get_state(src), d, std::forward<Args>(args)...);
    }
  };

}

#endif // SPOT_GRAPH_NGRAPH_HH