00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef VCSN_MISC_SUPPORT_HXX
00018 # define VCSN_MISC_SUPPORT_HXX
00019
00020 # include <vaucanson/misc/support.hh>
00021 # include <vaucanson/misc/contract.hh>
00022 # include <sstream>
00023
00024 namespace vcsn {
00025 namespace misc {
00026
00027
00028
00029
00030
00031 template <class C>
00032 SupportIterator<C>::SupportIterator (map_iterator mp):
00033 i (mp)
00034 {}
00035
00036 template <class C>
00037 typename SupportIterator<C>::key_type
00038 SupportIterator<C>::operator* () const
00039 {
00040 return i->first;
00041 }
00042
00043 template <class C>
00044 SupportIterator<C>& SupportIterator<C>::operator++ ()
00045 {
00046 ++i;
00047 return *this;
00048 }
00049
00050 template <class C>
00051 SupportIterator<C> SupportIterator<C>::operator++ (int)
00052 {
00053 SupportIterator<C> tmp = *this;
00054 ++i;
00055 return tmp;
00056 }
00057
00058 template <class C>
00059 bool
00060 SupportIterator<C>::operator!= (const SupportIterator& o) const
00061 {
00062 return o.i != i;
00063 }
00064
00065 template <class C>
00066 bool
00067 SupportIterator<C>::operator== (const SupportIterator& o) const
00068 {
00069 return ! (*this != o);
00070 }
00071
00072
00073
00074
00075
00076
00077
00078
00080 template <class U, class T>
00081 Support<std::map<U, T> >::Support (const Support& s):
00082 m_ (s.m_)
00083 {
00084 }
00085
00086 template <class U, class T>
00087 Support<std::map<U, T> >::Support (const std::map<U, T>& m):
00088 m_ (m)
00089 {
00090 }
00091
00092 template <class U, class T>
00093 unsigned
00094 Support<std::map<U, T> >::size () const
00095 {
00096 return m_.size ();
00097 }
00098
00099 template <class U, class T>
00100 typename Support<std::map<U, T> >::iterator
00101 Support<std::map<U, T> >::find (const U& k) const
00102 {
00103 return m_.find (k);
00104 }
00105
00106 template <class U, class T>
00107 bool
00108 Support<std::map<U, T> >::empty () const
00109 {
00110 return m_.empty ();
00111 }
00112
00113 template <class U, class T>
00114 typename Support<std::map<U, T> >::value_type
00115 Support<std::map<U, T> >::operator* () const
00116 {
00117 precondition (m_.size () == 1);
00118 return *m_.begin ();
00119 }
00120
00121 template <class U, class T>
00122 typename Support<std::map<U, T> >::iterator
00123 Support<std::map<U, T> >::begin () const
00124 {
00125 return iterator (m_.begin ());
00126 }
00127
00128 template <class U, class T>
00129 typename Support<std::map<U, T> >::iterator
00130 Support<std::map<U, T> >::end () const
00131 {
00132 return iterator (m_.end ());
00133 }
00134
00135 template <class U, class T>
00136 U
00137 Support< std::map<U, T> >::max () const
00138 {
00139 return *max_element (begin (), end ());
00140 }
00141
00142
00143
00144
00145
00146
00147
00148 template <class Integer, class ExcludedContainer>
00149 SparseIterator<Integer, ExcludedContainer>::
00150 SparseIterator (integer_t from,
00151 const excluded_container_t& c):
00152 excluded_ (&c),
00153 integer_ (from)
00154 {}
00155
00156 template <class Integer, class ExcludedContainer>
00157 SparseIterator<Integer, ExcludedContainer>&
00158 SparseIterator<Integer, ExcludedContainer>::operator++ ()
00159 {
00160 if (excluded_->size () == 0)
00161 integer_ = integer_ + 1;
00162 else
00163 do
00164 integer_ = integer_ + 1;
00165 while (excluded_->find (integer_) != excluded_->end ());
00166 return *this;
00167 }
00168
00169 template <class Integer, class ExcludedContainer>
00170 SparseIterator<Integer, ExcludedContainer>&
00171 SparseIterator<Integer, ExcludedContainer>::operator-- ()
00172 {
00173 if (excluded_->size () == 0)
00174 integer_ = integer_ - 1;
00175 else
00176 do
00177 integer_ = integer_ - 1;
00178 while (excluded_->find (integer_) != excluded_->end ());
00179 return *this;
00180 }
00181
00182 template <class Integer, class ExcludedContainer>
00183 SparseIterator<Integer, ExcludedContainer>
00184 SparseIterator<Integer, ExcludedContainer>::operator++ (int)
00185 {
00186 SparseIterator tmp = *this;
00187 ++*this;
00188 return tmp;
00189 }
00190
00191 template <class Integer, class ExcludedContainer>
00192 SparseIterator<Integer, ExcludedContainer>
00193 SparseIterator<Integer, ExcludedContainer>::operator-- (int)
00194 {
00195 SparseIterator tmp = *this;
00196 --*this;
00197 return tmp;
00198 }
00199
00200 template <class Integer, class ExcludedContainer>
00201 typename SparseIterator<Integer, ExcludedContainer>::
00202 integer_t
00203 SparseIterator<Integer, ExcludedContainer>::operator* ()
00204 {
00205 return integer_;
00206 }
00207
00208 template <class Integer, class ExcludedContainer>
00209 bool
00210 SparseIterator<Integer, ExcludedContainer>
00211 ::operator!= (const SparseIterator& i)
00212 {
00213 return i.integer_ != integer_;
00214 }
00215
00216 template <class Integer, class ExcludedContainer>
00217 bool
00218 SparseIterator<Integer, ExcludedContainer>
00219 ::operator== (const SparseIterator& i)
00220 {
00221 return i.integer_ == integer_;
00222 }
00223
00224 template <class Integer, class ExcludedContainer>
00225 SparseIterator<Integer, ExcludedContainer>&
00226 SparseIterator<Integer, ExcludedContainer>
00227 ::operator= (const SparseIterator& i)
00228 {
00229 integer_ = i.integer_;
00230 excluded_ = i.excluded_;
00231 return *this;
00232 }
00233
00234
00235
00236
00237
00238
00239
00240
00241
00245 template <class Integer, class ExcludedContainer>
00246 SparseInterval<Integer, ExcludedContainer>
00247 ::SparseInterval (integer_t f, integer_t t, const excluded_container_t& c):
00248 excluded_ (c),
00249 from_ (f),
00250 to_ (t)
00251 {
00252 precondition (from_ <= to_ + 1);
00253 precondition (excluded_.find (to_ + 1) == excluded_.end ());
00254 }
00255
00256 template <class Integer, class ExcludedContainer>
00257 SparseInterval<Integer, ExcludedContainer>
00258 ::SparseInterval (const SparseInterval& a) :
00259 excluded_ (a.excluded_),
00260 from_ (a.from_),
00261 to_ (a.to_)
00262 {
00263 }
00264
00265 template <class Integer, class ExcludedContainer>
00266 unsigned
00267 SparseInterval<Integer, ExcludedContainer>::size () const
00268 {
00269 return to_ < from_ ? 0 : to_ - from_ + 1 - excluded_.size ();
00270 }
00271
00272 template <class Integer, class ExcludedContainer>
00273 typename SparseInterval<Integer, ExcludedContainer>::integer_t
00274 SparseInterval<Integer, ExcludedContainer>::max () const
00275 {
00276 unsigned r = to_;
00277
00278 while (excluded_.find (r) != excluded_.end ())
00279 --r;
00280 return r;
00281 }
00282
00283 template <class Integer, class ExcludedContainer>
00284 std::string
00285 SparseInterval<Integer, ExcludedContainer>::to_string () const
00286 {
00287 std::stringstream s;
00288 s << "from :" << from_ << " to : " << to_ << " ex:";
00289 for (typename ExcludedContainer::iterator i = excluded_.begin ();
00290 i != excluded_.end ();
00291 ++i)
00292 s << *i << " ";
00293 return s.str ();
00294 }
00295
00296 template <class Integer, class ExcludedContainer>
00297 typename SparseInterval<Integer, ExcludedContainer>::iterator
00298 SparseInterval<Integer, ExcludedContainer>::begin () const
00299 {
00300 int from = from_;
00301
00302 if (excluded_.size () != 0)
00303 while (excluded_.find (from) != excluded_.end ())
00304 from = from + 1;
00305 return iterator (from, excluded_);
00306 }
00307
00308 template <class Integer, class ExcludedContainer>
00309 typename SparseInterval<Integer, ExcludedContainer>::iterator
00310 SparseInterval<Integer, ExcludedContainer>::end () const
00311 {
00312 return iterator (to_ + 1, excluded_);
00313 }
00314
00315 }
00316 }
00317
00318 #endif // ! VCSN_MISC_SUPPORT_HXX