Vaucanson  1.4.1
is_deterministic.hxx
1 // is_deterministic.hxx: this file is part of the Vaucanson project.
2 //
3 // Vaucanson, a generic library for finite state machines.
4 //
5 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 The Vaucanson Group.
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
11 //
12 // The complete GNU General Public Licence Notice can be found as the
13 // `COPYING' file in the root directory.
14 //
15 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
16 //
17 #ifndef VCSN_ALGORITHMS_IS_DETERMINISTIC_HXX
18 # define VCSN_ALGORITHMS_IS_DETERMINISTIC_HXX
19 
22 # include <set>
23 # include <vaucanson/misc/usual_macros.hh>
24 # include <vaucanson/automata/concept/automata_base.hh>
25 
26 namespace vcsn {
27 
28  /*-------------------------.
29  | is_state_deterministic. |
30  `-------------------------*/
31 
32  template <typename input_t>
33  static bool
34  is_state_deterministic (input_t& input,
35  typename input_t::state_iterator& current_state,
36  typename input_t::series_set_elt_t::semiring_elt_t&
37  zero_semiring)
38  {
39  AUTOMATON_TYPES(input_t);
40 
41  typedef typename series_set_elt_t::support_t support_t;
42  typedef typename std::list<htransition_t> delta_ret_t;
43  delta_ret_t delta_ret;
44 
45  // FIXME : O(n^2) => O(nlog(n)) There is maybe an algorithm in O(nlog(n))
46  for (delta_iterator j(input.value(), *current_state);
47  ! j.done();
48  j.next())
49  delta_ret.push_back(*j);
50  for_all_const_(delta_ret_t, j, delta_ret)
51  {
52  series_set_elt_t s = input.series_of(*j);
53  typename delta_ret_t::const_iterator k = j;
54  ++k;
55  for (; k != delta_ret.end(); ++k)
56  {
57  series_set_elt_t s_ = input.series_of(*k);
58  for_all_(support_t, supp, s.supp())
59  if (s_.get(*supp) != zero_semiring)
60  return false;
61  }
62  }
63  return true;
64  }
65 
66 
67 
68 
69  /*-------------------.
70  | is_deterministic. |
71  `-------------------*/
72  template <typename A, typename AI>
73  bool
74  do_is_deterministic(const AutomataBase<A>&,
75  const Element<A, AI>& input)
76  {
77  precondition(is_realtime(input));
78 
79  typedef Element<A, AI> automaton_t;
80  AUTOMATON_TYPES(automaton_t);
81  semiring_elt_t zero_semiring
82  = input.structure().series().semiring()
83  .zero(SELECT(typename semiring_elt_t::value_t));
84 
85 
86  if (input.initial().size() > 1)
87  return false;
88 
89  for_all_const_states(i, input)
90  if (not is_state_deterministic (input, i, zero_semiring))
91  return false;
92  return true;
93  }
94 
95 
96  template<typename A, typename AI>
97  bool
98  is_deterministic(const Element<A, AI>& a)
99  {
100  BENCH_TASK_SCOPED("is_deterministic");
101  return do_is_deterministic(a.structure(), a);
102  }
103 
104 } // vcsn
105 
106 #endif // ! VCSN_ALGORITHMS_IS_DETERMINISTIC_HXX