nfa.cc 2.88 KB
Newer Older
1
// -*- coding: utf-8 -*-
2
3
// Copyright (C) 2008, 2013, 2014 Laboratoire de Recherche et
// Développement de l'Epita (LRDE).
4
5
6
7
8
//
// 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
9
// the Free Software Foundation; either version 3 of the License, or
10
11
12
13
14
15
16
17
// (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
18
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
19

20
#include "config.h"
21
22
#include <cassert>
#include "nfa.hh"
23
#include "formula_tree.hh"
24
25
26
27
28
29

namespace spot
{
  namespace ltl
  {
    nfa::nfa()
30
      : is_(), si_(), arity_(0), name_(), init_(0), finals_()
31
32
33
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
    {
    }

    nfa::~nfa()
    {
      is_map::iterator i;
      for (i = is_.begin(); i != is_.end(); ++i)
      {
	state::iterator i2;
	for (i2 = i->second->begin(); i2 != i->second->end(); ++i2)
	  delete *i2;
	delete i->second;
      }
    }

    nfa::state*
    nfa::add_state(int name)
    {
      is_map::iterator i = is_.find(name);
      if (i == is_.end())
      {
	state* s = new nfa::state;
	is_[name] = s;
	si_[s] = name;

	if (!init_)
	  init_ = s;
	return s;
      }
      return i->second;
    }

    void
64
    nfa::add_transition(int src, int dst, const label lbl)
65
66
67
68
    {
      state* s = add_state(src);
      nfa::transition* t = new transition;
      t->dst = add_state(dst);
69
      t->lbl = lbl;
70
      s->push_back(t);
71
72
73
      size_t arity = formula_tree::arity(formula_tree::node_ptr(lbl));
      if (arity >= arity_)
	arity_ = arity;
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
    }

    void
    nfa::set_init_state(int name)
    {
      init_ = add_state(name);
    }

    void
    nfa::set_final(int name)
    {
      finals_.insert(name);
    }

    bool
    nfa::is_final(const state* s)
    {
      return finals_.find(format_state(s)) != finals_.end();
    }

    bool
    nfa::is_loop()
    {
      return finals_.empty();
    }

100
    unsigned
101
102
    nfa::arity()
    {
103
      return arity_;
104
105
106
107
108
    }

    const nfa::state*
    nfa::get_init_state()
    {
109
      assert(init_);
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
      return init_;
    }

    nfa::iterator
    nfa::begin(const state* s) const
    {
      return nfa::iterator(s->begin());
    }

    nfa::iterator
    nfa::end(const state* s) const
    {
      return nfa::iterator(s->end());
    }

    int
    nfa::format_state(const state* s) const
    {
      si_map::const_iterator i = si_.find(s);
      assert(i != si_.end());
      return i->second;
    }

    const std::string&
    nfa::get_name() const
    {
      return name_;
    }

    void
    nfa::set_name(const std::string& name)
    {
      name_ = name;
    }
  }
}