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 <string> 00029 00030 namespace vcsn 00031 { 00032 namespace misc 00033 { 00034 00037 template <class T> 00038 class Support; 00039 00042 template <class C> 00043 class SupportIterator 00044 { 00045 public: 00046 typedef typename C::key_type key_type; 00047 typedef typename C::const_iterator map_iterator; 00048 typedef SupportIterator<C> self_t; 00049 00050 typedef typename map_iterator::iterator_category iterator_category; 00051 typedef typename map_iterator::difference_type difference_type; 00052 typedef key_type value_type; 00053 typedef key_type* pointer; 00054 typedef key_type& reference; 00055 00056 /* 00057 * This is a default constructor. 00058 * WARNING: this constructor instantiates an invalid iterator. 00059 * To use an iterator instantiated by this constructor, 00060 * you need to initialize it thanks to the '=' operator. 00061 * 00062 * This constructor is useful whenever you want to use an iterator as 00063 * a temporary variable in a loop. For instance: 00064 * 00065 * for (SupportIterator tmp, it = aut.final().begin(); 00066 * it != aut.final().end();) 00067 * { 00068 * tmp = it++; 00069 * if (something) 00070 * del_state(*tmp); 00071 * } 00072 * 00073 * In this example, we delete an object in a set we are already iterating on. 00074 * So we need to save a copy of the next element before deleting the current one. 00075 * Since declaring a temporary variable inside a loop can slow down performances, 00076 * it is declared inside the 'for loop' declaration and, in that case, we are really 00077 * interested in such a constructor. 00078 * 00079 */ 00080 SupportIterator () {} 00081 SupportIterator (map_iterator); 00082 00083 key_type operator* () const; 00084 self_t& operator++ (); 00085 self_t operator++ (int); 00086 bool operator!= (const SupportIterator&) const; 00087 bool operator== (const SupportIterator&) const; 00088 00089 private: 00090 map_iterator i; 00091 }; 00092 00094 template <class U, class T> 00095 class Support<std::map<U, T> > 00096 { 00097 public: 00098 typedef SupportIterator<std::map<U, T> > iterator; 00099 typedef SupportIterator<std::map<U, T> > const_iterator; 00101 typedef typename std::map<U, T>::value_type value_type; 00102 00103 Support (const std::map<U, T>&); 00104 Support (const Support&); 00105 00108 value_type operator* () const; 00109 00110 iterator begin () const; 00111 iterator end () const; 00112 unsigned size () const; 00113 00114 // Find the element associated to \a k. 00115 iterator find (const U& k) const; 00116 00118 bool empty () const; 00119 00120 U max () const; 00121 private: 00122 const std::map<U, T>& m_; 00123 }; 00124 00127 } // misc 00128 } // vcsn 00129 00130 # if !defined VCSN_USE_INTERFACE_ONLY || defined VCSN_USE_LIB 00131 # include <vaucanson/misc/support.hxx> 00132 # endif // VCSN_USE_INTERFACE_ONLY 00133 00134 00135 #endif // ! VCSN_MISC_SUPPORT_HH