00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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 namespace misc {
00032
00035 template <class T>
00036 class Support;
00037
00040 template <class C>
00041 class SupportIterator
00042 {
00043 public:
00044 typedef typename C::key_type key_type;
00045 typedef typename C::const_iterator map_iterator;
00046 typedef SupportIterator<C> self_t;
00047
00048 typedef typename map_iterator::iterator_category iterator_category;
00049 typedef typename map_iterator::difference_type difference_type;
00050 typedef key_type value_type;
00051 typedef key_type* pointer;
00052 typedef key_type& reference;
00053
00054 SupportIterator (map_iterator);
00055
00056 key_type operator* () const;
00057 self_t& operator++ ();
00058 self_t operator++ (int);
00059 bool operator!= (const SupportIterator&) const;
00060 bool operator== (const SupportIterator&) const;
00061
00062 private:
00063 map_iterator i;
00064 };
00065
00067 template <class U, class T>
00068 class Support<std::map<U, T> >
00069 {
00070 public:
00071 typedef SupportIterator<std::map<U, T> > iterator;
00072 typedef SupportIterator<std::map<U, T> > const_iterator;
00074 typedef typename std::map<U, T>::value_type value_type;
00075
00076 Support (const std::map<U, T>&);
00077 Support (const Support&);
00078
00081 value_type operator* () const;
00082
00083 iterator begin () const;
00084 iterator end () const;
00085 unsigned size () const;
00086
00087
00088 iterator find (const U& k) const;
00089
00091 bool empty () const;
00092
00093 U max () const;
00094 private:
00095 const std::map<U, T>& m_;
00096 };
00097
00099 template <class Integer, class ExcludedContainer>
00100 class SparseIterator
00101 {
00102 public:
00103 typedef Integer integer_t;
00104 typedef ExcludedContainer excluded_container_t;
00105
00106 SparseIterator (integer_t, const excluded_container_t&);
00107
00108 SparseIterator& operator++ ();
00109 SparseIterator operator++ (int);
00110 SparseIterator& operator-- ();
00111 SparseIterator operator-- (int);
00112 integer_t operator* ();
00113 bool operator!= (const SparseIterator&);
00114 bool operator== (const SparseIterator&);
00115 SparseIterator& operator= (const SparseIterator&);
00116
00117 private:
00118 const excluded_container_t* excluded_;
00119 integer_t integer_;
00120 };
00121
00124 }
00125 }
00126
00127 namespace std {
00128
00129 template <class Integer, class ExcludedContainer>
00130 struct iterator_traits<vcsn::misc::SparseIterator
00131 <Integer, ExcludedContainer> >
00132 {
00133 typedef input_iterator_tag iterator_category;
00134 typedef Integer value_type;
00135 typedef int difference_type;
00136 typedef int* pointer;
00137 typedef int& reference;
00138 };
00139
00140 }
00141
00142 namespace vcsn {
00143 namespace misc {
00144
00155 template <class Integer, class ExcludedContainer>
00156 class SparseInterval
00157 {
00158 public:
00159 typedef Integer integer_t;
00160 typedef ExcludedContainer excluded_container_t;
00161 typedef SparseIterator<integer_t, excluded_container_t> iterator;
00162
00163 SparseInterval (integer_t, integer_t, const excluded_container_t&);
00164 SparseInterval (const SparseInterval&);
00165
00166 iterator begin () const;
00167 iterator end () const;
00168 unsigned size () const;
00169 integer_t max () const;
00170 std::string to_string () const;
00171
00172 private:
00173 const excluded_container_t& excluded_;
00174 integer_t from_;
00175 integer_t to_;
00176 };
00177
00179 template <template <class> class C, class T>
00180 class SelfIterator
00181 {
00182 public:
00183 SelfIterator (const C<T>& c):
00184 c_ (&c),
00185 pos_ (c.begin ())
00186 {}
00187
00188 SelfIterator ():
00189 c_ (0),
00190 pos_ ()
00191 {}
00192
00193 SelfIterator (const SelfIterator& s):
00194 c_ (s.c_),
00195 pos_ (s.pos_)
00196 {}
00197
00198 const T& operator* () const
00199 {
00200 return *pos_;
00201 }
00202
00203 const SelfIterator& operator++ ()
00204 {
00205 pos_++;
00206 return *this;
00207 }
00208
00209 SelfIterator operator++ (int)
00210 {
00211 SelfIterator tmp (*this);
00212 ++pos_;
00213 return tmp;
00214 }
00215
00216 bool operator!= (const SelfIterator& o) const
00217 {
00218 if (c_ == 0)
00219 if (o.c_ == 0)
00220 return false;
00221 else
00222 return o.pos_ != o.c_->end ();
00223 else if (o.c_ == 0)
00224 return pos_ != c_->end ();
00225 return (o.c_ != c_ ||
00226 o.pos_ != pos_);
00227 }
00228
00229 bool operator== (const SelfIterator& o) const
00230 {
00231 if (c_ == 0)
00232 if (o.c_ == 0)
00233 return true;
00234 else
00235 return o.pos_ == o.c_->end ();
00236 else if (o.c_ == 0)
00237 return pos_ == c_->end ();
00238 return (o.c_ == c_ &&
00239 o.pos_ == pos_);
00240 }
00241
00242 private:
00243 const C<T>* c_;
00244 typename C<T>::const_iterator pos_;
00245 };
00246
00249 }
00250 }
00251
00252
00253 # ifndef VCSN_USE_INTERFACE_ONLY
00254 # include <vaucanson/misc/support.hxx>
00255 # endif // VCSN_USE_INTERFACE_ONLY
00256
00257
00258 #endif // ! VCSN_MISC_SUPPORT_HH