Vaucanson 1.4
support.hh
Go to the documentation of this file.
00001 // support.hh: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007 The Vaucanson Group.
00006 //
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 //
00012 // The complete GNU General Public Licence Notice can be found as the
00013 // `COPYING' file in the root directory.
00014 //
00015 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
00016 //
00017 #ifndef VCSN_MISC_SUPPORT_HH
00018 # define VCSN_MISC_SUPPORT_HH
00019 
00026 # include <iterator>
00027 # include <map>
00028 # include <set>
00029 # include <string>
00030 # include <vector>
00031 # include <vaucanson/automata/concept/handlers.hh>
00032 
00033 
00034 namespace vcsn
00035 {
00036   namespace misc
00037   {
00038 
00041     template <typename T>
00042     class Support;
00043 
00046     template <class C>
00047     class SupportIterator
00048     {
00049       public:
00050         typedef typename C::key_type            key_type;
00051         typedef typename C::const_iterator      map_iterator;
00052         typedef SupportIterator<C>              self_t;
00053 
00054         typedef typename map_iterator::iterator_category iterator_category;
00055         typedef typename map_iterator::difference_type   difference_type;
00056         typedef key_type                                 value_type;
00057         typedef key_type*                                pointer;
00058         typedef key_type&                                reference;
00059 
00060         /*
00061          * This is a default constructor.
00062          * WARNING: this constructor instantiates an invalid iterator.
00063          *          To use an iterator instantiated by this constructor,
00064          *          you need to initialize it thanks to the '=' operator.
00065          *
00066          * This constructor is useful whenever you want to use an iterator as
00067          * a temporary variable in a loop. For instance:
00068          *
00069          * for (SupportIterator tmp, it = aut.final().begin();
00070          *      it != aut.final().end();)
00071          * {
00072          *      tmp = it++;
00073          *      if (something)
00074          *              del_state(*tmp);
00075          * }
00076          *
00077          * In this example, we delete an object in a set we are already iterating on.
00078          * So we need to save a copy of the next element before deleting the current one.
00079          * Since declaring a temporary variable inside a loop can slow down performances,
00080          * it is declared inside the 'for loop' declaration and, in that case, we are really
00081          * interested in such a constructor.
00082          *
00083          */
00084         SupportIterator () {}
00085         SupportIterator (map_iterator);
00086 
00087         key_type operator* () const;
00088         self_t&  operator++ ();
00089         self_t   operator++ (int);
00090         bool     operator!= (const SupportIterator&) const;
00091         bool     operator== (const SupportIterator&) const;
00092 
00093       private:
00094         map_iterator    i;
00095     };
00096 
00097     template <class U>
00098     class SupportIterator<std::set<U> >
00099     {
00100       public:
00101         typedef std::set<U>                             container_t;
00102         typedef U                                       value_t;
00103         typedef typename container_t::const_iterator    iterator;
00104         typedef SupportIterator<std::set<U> >           self_t;
00105 
00106         typedef typename iterator::iterator_category    iterator_category;
00107         typedef typename iterator::difference_type      difference_type;
00108         typedef value_t                                 value_type;
00109         typedef value_t*                                pointer;
00110         typedef value_t&                                reference;
00111 
00112         /*
00113          * This is a default constructor.
00114          * WARNING: this constructor instantiates an invalid iterator.
00115          *          To use an iterator instantiated by this constructor,
00116          *          you need to initialize it thanks to the '=' operator.
00117          *
00118          * This constructor is useful whenever you want to use an iterator as
00119          * a temporary variable in a loop. For instance:
00120          *
00121          * for (SupportIterator tmp, it = aut.final().begin();
00122          *      it != aut.final().end();)
00123          * {
00124          *      tmp = it++;
00125          *      if (something)
00126          *              del_state(*tmp);
00127          * }
00128          *
00129          * In this example, we delete an object in a set we are already iterating on.
00130          * So we need to save a copy of the next element before deleting the current one.
00131          * Since declaring a temporary variable inside a loop can slow down performances,
00132          * it is declared inside the 'for loop' declaration and, in that case, we are really
00133          * interested in such a constructor.
00134          *
00135          */
00136         SupportIterator () {}
00137         SupportIterator (iterator);
00138 
00139         value_t operator* () const;
00140         self_t&  operator++ ();
00141         self_t   operator++ (int);
00142         bool     operator!= (const SupportIterator&) const;
00143         bool     operator== (const SupportIterator&) const;
00144 
00145       private:
00146         iterator        i;
00147     };
00148 
00150     template <class U, class T>
00151     class Support<std::map<U, T> >
00152     {
00153       public:
00154         typedef SupportIterator<std::map<U, T> > iterator;
00155         typedef SupportIterator<std::map<U, T> > const_iterator;
00157         typedef typename std::map<U, T>::value_type value_type;
00158 
00159         Support (const std::map<U, T>&);
00160         Support (const Support&);
00161 
00162         iterator begin () const;
00163         iterator end () const;
00164         unsigned size () const;
00165 
00166         // Find the element associated to \a k.
00167         iterator find (const U& k) const;
00168 
00170         bool empty () const;
00171 
00172         U back () const;
00173       private:
00174         const std::map<U, T>&   m_;
00175     };
00176 
00178     template <class U>
00179     class Support<std::set<U> >
00180     {
00181       public:
00182         typedef SupportIterator<std::set<U> > iterator;
00183         typedef SupportIterator<std::set<U> > const_iterator;
00185         typedef typename std::set<U>::value_type value_type;
00186 
00187         Support (const std::set<U>&);
00188         Support (const Support&);
00189 
00190         iterator begin () const;
00191         iterator end () const;
00192         unsigned size () const;
00193 
00194         // Find the element associated to \a k.
00195         iterator find (const U& k) const;
00196 
00198         bool empty () const;
00199 
00200         U back () const;
00201       private:
00202         const std::set<U>&      m_;
00203     };
00206   } // misc
00207 } // vcsn
00208 
00209 
00210 # if !defined VCSN_USE_INTERFACE_ONLY || defined VCSN_USE_LIB
00211 #  include <vaucanson/misc/support.hxx>
00212 # endif // VCSN_USE_INTERFACE_ONLY
00213 
00214 
00215 #endif // ! VCSN_MISC_SUPPORT_HH