Vaucanson  1.4.1
support.hh
Go to the documentation of this file.
1 // support.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, 2007 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_MISC_SUPPORT_HH
18 # define VCSN_MISC_SUPPORT_HH
19 
26 # include <iterator>
27 # include <map>
28 # include <set>
29 # include <string>
30 # include <vector>
31 # include <vaucanson/automata/concept/handlers.hh>
32 
33 
34 namespace vcsn
35 {
36  namespace misc
37  {
38 
41  template <typename T>
42  class Support;
43 
46  template <class C>
48  {
49  public:
50  typedef typename C::key_type key_type;
51  typedef typename C::const_iterator map_iterator;
52  typedef SupportIterator<C> self_t;
53 
54  typedef typename map_iterator::iterator_category iterator_category;
55  typedef typename map_iterator::difference_type difference_type;
56  typedef key_type value_type;
57  typedef key_type* pointer;
58  typedef key_type& reference;
59 
60  /*
61  * This is a default constructor.
62  * WARNING: this constructor instantiates an invalid iterator.
63  * To use an iterator instantiated by this constructor,
64  * you need to initialize it thanks to the '=' operator.
65  *
66  * This constructor is useful whenever you want to use an iterator as
67  * a temporary variable in a loop. For instance:
68  *
69  * for (SupportIterator tmp, it = aut.final().begin();
70  * it != aut.final().end();)
71  * {
72  * tmp = it++;
73  * if (something)
74  * del_state(*tmp);
75  * }
76  *
77  * In this example, we delete an object in a set we are already iterating on.
78  * So we need to save a copy of the next element before deleting the current one.
79  * Since declaring a temporary variable inside a loop can slow down performances,
80  * it is declared inside the 'for loop' declaration and, in that case, we are really
81  * interested in such a constructor.
82  *
83  */
84  SupportIterator () {}
85  SupportIterator (map_iterator);
86 
87  key_type operator* () const;
88  self_t& operator++ ();
89  self_t operator++ (int);
90  bool operator!= (const SupportIterator&) const;
91  bool operator== (const SupportIterator&) const;
92 
93  private:
94  map_iterator i;
95  };
96 
97  template <class U>
98  class SupportIterator<std::set<U> >
99  {
100  public:
101  typedef std::set<U> container_t;
102  typedef U value_t;
103  typedef typename container_t::const_iterator iterator;
104  typedef SupportIterator<std::set<U> > self_t;
105 
106  typedef typename iterator::iterator_category iterator_category;
107  typedef typename iterator::difference_type difference_type;
108  typedef value_t value_type;
109  typedef value_t* pointer;
110  typedef value_t& reference;
111 
112  /*
113  * This is a default constructor.
114  * WARNING: this constructor instantiates an invalid iterator.
115  * To use an iterator instantiated by this constructor,
116  * you need to initialize it thanks to the '=' operator.
117  *
118  * This constructor is useful whenever you want to use an iterator as
119  * a temporary variable in a loop. For instance:
120  *
121  * for (SupportIterator tmp, it = aut.final().begin();
122  * it != aut.final().end();)
123  * {
124  * tmp = it++;
125  * if (something)
126  * del_state(*tmp);
127  * }
128  *
129  * In this example, we delete an object in a set we are already iterating on.
130  * So we need to save a copy of the next element before deleting the current one.
131  * Since declaring a temporary variable inside a loop can slow down performances,
132  * it is declared inside the 'for loop' declaration and, in that case, we are really
133  * interested in such a constructor.
134  *
135  */
136  SupportIterator () {}
137  SupportIterator (iterator);
138 
139  value_t operator* () const;
140  self_t& operator++ ();
141  self_t operator++ (int);
142  bool operator!= (const SupportIterator&) const;
143  bool operator== (const SupportIterator&) const;
144 
145  private:
146  iterator i;
147  };
148 
150  template <class U, class T>
151  class Support<std::map<U, T> >
152  {
153  public:
157  typedef typename std::map<U, T>::value_type value_type;
158 
159  Support (const std::map<U, T>&);
160  Support (const Support&);
161 
162  iterator begin () const;
163  iterator end () const;
164  unsigned size () const;
165 
166  // Find the element associated to \a k.
167  iterator find (const U& k) const;
168 
170  bool empty () const;
171 
172  U back () const;
173  private:
174  const std::map<U, T>& m_;
175  };
176 
178  template <class U>
179  class Support<std::set<U> >
180  {
181  public:
182  typedef SupportIterator<std::set<U> > iterator;
183  typedef SupportIterator<std::set<U> > const_iterator;
185  typedef typename std::set<U>::value_type value_type;
186 
187  Support (const std::set<U>&);
188  Support (const Support&);
189 
190  iterator begin () const;
191  iterator end () const;
192  unsigned size () const;
193 
194  // Find the element associated to \a k.
195  iterator find (const U& k) const;
196 
198  bool empty () const;
199 
200  U back () const;
201  private:
202  const std::set<U>& m_;
203  };
206  } // misc
207 } // vcsn
208 
209 
210 # if !defined VCSN_USE_INTERFACE_ONLY || defined VCSN_USE_LIB
211 # include <vaucanson/misc/support.hxx>
212 # endif // VCSN_USE_INTERFACE_ONLY
213 
214 
215 #endif // ! VCSN_MISC_SUPPORT_HH