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 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 utility {
00031 
00034   template <class T>
00035   class Support;
00036 
00039   template <class C>
00040   class SupportIterator
00041   {
00042   public:
00043     typedef typename C::key_type        key_type;
00044     typedef typename C::const_iterator  map_iterator;
00045     typedef SupportIterator<C>          self_t;
00046 
00047     typedef typename map_iterator::iterator_category    iterator_category;
00048     typedef typename map_iterator::difference_type      difference_type;
00049     typedef key_type                                    value_type;
00050     typedef key_type*                                   pointer;
00051     typedef key_type&                                   reference;
00052 
00053     SupportIterator(map_iterator);
00054 
00055     key_type    operator*() const;
00056     self_t&     operator++();
00057     self_t      operator++(int);
00058     bool        operator!=(const SupportIterator&) const;
00059     bool        operator==(const SupportIterator&) const;
00060 
00061   private:
00062     map_iterator        i;
00063   };
00064 
00066   template <class U, class T>
00067   class Support<std::map<U, T> >
00068   {
00069   public:
00070     typedef SupportIterator<std::map<U, T> > iterator;
00071     typedef SupportIterator<std::map<U, T> > const_iterator;
00072 
00073     Support(const std::map<U, T>&);
00074     Support(const Support&);
00075 
00076     iterator begin() const;
00077     iterator end() const;
00078     unsigned size() const;
00079 
00080     U max() const;
00081   private:
00082     const std::map<U, T>&       m_;
00083   };
00084 
00086   template <class Integer, class ExcludedContainer>
00087   class SparseIterator
00088   {
00089   public:
00090     typedef Integer             integer_t;
00091     typedef ExcludedContainer   excluded_container_t;
00092 
00093     SparseIterator(integer_t, const excluded_container_t&);
00094 
00095     SparseIterator& operator++();
00096     SparseIterator  operator++(int);
00097     SparseIterator& operator--();
00098     SparseIterator  operator--(int);
00099     integer_t       operator*();
00100     bool            operator!=(const SparseIterator&);
00101     bool            operator==(const SparseIterator&);
00102     SparseIterator& operator=(const SparseIterator&);
00103 
00104   private:
00105     const excluded_container_t* excluded_;
00106     integer_t                   integer_;
00107   };
00108 
00111 } // utility
00112 
00113 
00114 namespace std {
00115 
00116   template <class Integer, class ExcludedContainer>
00117   struct iterator_traits<utility::SparseIterator
00118   <Integer, ExcludedContainer> >
00119   {
00120     typedef input_iterator_tag  iterator_category;
00121     typedef Integer             value_type;
00122     typedef int                 difference_type;
00123     typedef int*                pointer;
00124     typedef int&                reference;
00125   };
00126 
00127 } // std
00128 
00129 namespace utility {
00130 
00141   template <class Integer, class ExcludedContainer>
00142   class SparseInterval
00143   {
00144   public:
00145     typedef Integer             integer_t;
00146     typedef ExcludedContainer   excluded_container_t;
00147     typedef SparseIterator<integer_t, excluded_container_t> iterator;
00148 
00149     SparseInterval(integer_t, integer_t, const excluded_container_t&);
00150     SparseInterval(const SparseInterval&);
00151 
00152     iterator begin() const;
00153     iterator end() const;
00154     unsigned size() const;
00155     integer_t max() const;
00156     std::string to_string() const;
00157 
00158   private:
00159     const excluded_container_t& excluded_;
00160     integer_t                   from_;
00161     integer_t                   to_;
00162   };
00163 
00165   template <template <class> class C, class T>
00166   class SelfIterator
00167   {
00168   public:
00169     SelfIterator(const C<T>& c):
00170       c_(&c),
00171       pos_(c.begin())
00172     {}
00173 
00174     SelfIterator():
00175       c_(0),
00176       pos_()
00177     {}
00178 
00179     SelfIterator(const SelfIterator& s):
00180       c_(s.c_),
00181       pos_(s.pos_)
00182     {}
00183 
00184     const T& operator*() const
00185     {
00186       return *pos_;
00187     }
00188 
00189     const SelfIterator& operator++()
00190     {
00191       pos_++;
00192       return *this;
00193     }
00194 
00195     SelfIterator operator++(int)
00196     {
00197       SelfIterator tmp(*this);
00198       ++pos_;
00199       return tmp;
00200     }
00201 
00202     bool operator!=(const SelfIterator& o) const
00203     {
00204       if (c_ == 0)
00205         if (o.c_ == 0)
00206           return false;
00207         else
00208           return o.pos_ != o.c_->end();
00209       else if (o.c_ == 0)
00210         return pos_ != c_->end();
00211       return (o.c_ != c_ ||
00212               o.pos_ != pos_);
00213     }
00214 
00215     bool operator==(const SelfIterator& o) const
00216     {
00217       if (c_ == 0)
00218         if (o.c_ == 0)
00219           return true;
00220         else
00221           return o.pos_ == o.c_->end();
00222       else if (o.c_ == 0)
00223         return pos_ == c_->end();
00224       return (o.c_ == c_ &&
00225               o.pos_ == pos_);
00226     }
00227 
00228   private:
00229     const C<T>* c_;
00230     typename C<T>::const_iterator pos_;
00231   };
00232 
00235 } // utility
00236 
00237 
00238 
00239 #ifndef VCSN_USE_INTERFACE_ONLY
00240     # include <vaucanson/misc/support.hxx>
00241 #endif // VCSN_USE_INTERFACE_ONLY
00242 
00243 
00244 #endif // ! VCSN_MISC_SUPPORT_HH

Generated on Fri Jul 28 12:18:53 2006 for Vaucanson by  doxygen 1.4.6