Vaucanson  1.4.1
build_pattern.hh
1 // build_pattern.hh: 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 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_INTERNAL_BUILD_PATTERN_HH
18 # define VCSN_ALGORITHMS_INTERNAL_BUILD_PATTERN_HH
19 
20 # include <map>
21 # include <vaucanson/automata/concept/automata_base.hh>
23 # include <vaucanson/misc/usual_macros.hh>
24 
25 namespace vcsn {
26  namespace algorithm_patterns
27  {
28 
29  // This is the structure given to the map, which will
30  // call the appropriate function
31  template <typename Self, typename Etiq>
32  struct Comparator
33  {
34  bool operator()(const Etiq& e1, const Etiq& e2) const;
35  };
36 
37  /*-----------------------.
38  | IncAutomataConstructor |
39  `-----------------------*/
40 
41  // This is a pattern for algorithm which build automaton in
42  // an incremental way :
43  // it starts with one state, and with this state, it builds
44  // other states. New states are used to build more and
45  // more states.
46  // It needs the function applied on each state,
47  // and the order used by the map (a default one can be used).
48  template <typename Self, typename T_auto, typename Etiq>
49  class IncAutomataConstructor
50  {
51  public:
52  // Useful types :))
53  typedef T_auto* T_auto_p;
54  AUTOMATON_TYPES(T_auto);
55  AUTOMATON_FREEMONOID_TYPES(T_auto);
56  // Types for the list
57  // It uses a map with an ordered function which can be
58  // redefined in Self
59  typedef
60  std::pair<hstate_t, bool> StateMarked;
61  typedef
62  std::map<Etiq, StateMarked, Comparator<Self, Etiq> > StateMap;
63  typedef typename StateMap::iterator iterator;
64 
65  // To run the algorithm
66  void run();
67  // To get the result
68  T_auto_p get() const;
69  // The function which will compare 2 Etiq
70  // User may redefine it !
71  static bool compare(const Etiq& e1, const Etiq& e2);
72  protected:
73  // The constructors (protected in order to not instance the class)
74  IncAutomataConstructor(const series_set_t& series, const Etiq& etiq);
75  IncAutomataConstructor(const series_set_t& series,
76  const std::list<Etiq>& listexp);
77  // Add a link from current state to indicated state
78  void link_to(const Etiq& etiq, const letter_t& l);
79  void link_to(const Etiq& etiq, const series_set_elt_t& el);
80  // To make the current state final
81  void set_final();
82  void set_final(const series_set_elt_t& el);
83  private:
84  // Function to apply on each state (call on_state function)
85  void on_state_caller(const Etiq& e);
86  // Method to add properly a state
87  hstate_t add_state(const Etiq& etiq);
88  // Attributes
89  int unvisited;
90  T_auto_p auto_p;
91  StateMap states_map;
92  iterator current_state;
93  };
94 
95  /*------------------------.
96  | MathAutomataConstructor |
97  `------------------------*/
98 
99  // This Algorithm builder takes a struture
100  // which contains different functions (like IncAutomataConstructor)
101  // but the functions needed are mathematical definitions
102  // of the automaton, i.e. :
103  // function which return the set of final state,
104  // etc.
105  // FIXME: make it compatible with multiplicity
106  template <typename Self, typename T_auto, typename Etiq>
107  class MathAutomataConstructor
108  {
109  public:
110  // Types used. The map make link between Etiq and hstate_t.
111  AUTOMATON_TYPES(T_auto);
112  AUTOMATON_FREEMONOID_TYPES(T_auto);
113  typedef T_auto* T_auto_p;
114  typedef std::map<Etiq, hstate_t, Comparator<Self, Etiq> > StateMap;
115  typedef typename StateMap::iterator iterator;
116 
117  // To run the algorithm and build the entire automaton
118  void run();
119  // To get the result
120  T_auto_p get() const;
121  // The function which will compare 2 Etiq
122  // User may redefine it !
123  static bool compare(const Etiq& e1, const Etiq& e2);
124  protected:
125  // The constructor
126  template <typename Container>
127  MathAutomataConstructor(const series_set_t& series,
128  const Container container);
129  private:
130  // Function which will call those which are defined by user
131  bool is_initial_caller(const Etiq& e) const;
132  bool is_final_caller(const Etiq& e) const;
133  // Function which will link a state to a set of states
134  template <typename Container>
135  void link_to(const hstate_t& state,
136  const letter_t& letter,
137  const Container container);
138  // Attributes
139  T_auto_p auto_p;
140  StateMap states_map;
141  };
142 
143  }
144 }
145 
146 
147 # if !defined VCSN_USE_INTERFACE_ONLY && !defined VCSN_USE_LIB
148 #include <vaucanson/algorithms/internal/build_pattern.hxx>
149 #endif // VCSN_USE_INTERFACE_ONLY
150 
151 
152 #endif // ! VCSN_ALGORITHMS_INTERNAL_BUILD_PATTERN_HH