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 utility {
00025
00026 template <class C>
00027 SupportIterator<C>::SupportIterator(map_iterator mp):
00028 i(mp)
00029 {}
00030
00031 template <class C>
00032 typename SupportIterator<C>::key_type
00033 SupportIterator<C>::operator*() const
00034 {
00035 return i->first;
00036 }
00037
00038 template <class C>
00039 SupportIterator<C>& SupportIterator<C>::operator++()
00040 {
00041 ++i;
00042 return *this;
00043 }
00044
00045 template <class C>
00046 SupportIterator<C> SupportIterator<C>::operator++(int)
00047 {
00048 SupportIterator<C> tmp = *this;
00049 ++i;
00050 return tmp;
00051 }
00052
00053 template <class C>
00054 bool
00055 SupportIterator<C>::operator!=(const SupportIterator& o) const
00056 {
00057 return o.i != i;
00058 }
00059
00060 template <class C>
00061 bool
00062 SupportIterator<C>::operator == (const SupportIterator& o) const
00063 {
00064 return !(*this != o);
00065 }
00066
00068 template <class U, class T>
00069 Support<std::map<U, T> >::Support(const Support& s):
00070 m_(s.m_)
00071 {
00072 }
00073
00074 template <class U, class T>
00075 Support<std::map<U, T> >::Support(const std::map<U, T>& m):
00076 m_(m)
00077 {
00078 }
00079
00080 template <class U, class T>
00081 unsigned
00082 Support<std::map<U, T> >::size() const
00083 {
00084 return m_.size();
00085 }
00086
00087 template <class U, class T>
00088 SupportIterator<std::map<U, T> >
00089 Support<std::map<U, T> >::begin() const
00090 {
00091 return iterator(m_.begin());
00092 }
00093
00094 template <class U, class T>
00095 SupportIterator<std::map<U, T> >
00096 Support<std::map<U, T> >::end() const
00097 {
00098 return iterator(m_.end());
00099 }
00100
00101 template <class U, class T>
00102 U
00103 Support< std::map<U, T> >::max() const
00104 {
00105 return *max_element(begin(), end());
00106 }
00107
00109
00110 template <class Integer, class ExcludedContainer>
00111 SparseIterator<Integer, ExcludedContainer>::
00112 SparseIterator(integer_t from,
00113 const excluded_container_t& c):
00114 excluded_(&c),
00115 integer_(from)
00116 {}
00117
00118 template <class Integer, class ExcludedContainer>
00119 SparseIterator<Integer, ExcludedContainer>&
00120 SparseIterator<Integer, ExcludedContainer>::operator++()
00121 {
00122 if (excluded_->size() == 0)
00123 integer_ = integer_ + 1;
00124 else
00125 do
00126 integer_ = integer_ + 1;
00127 while (excluded_->find(integer_) != excluded_->end());
00128 return *this;
00129 }
00130
00131 template <class Integer, class ExcludedContainer>
00132 SparseIterator<Integer, ExcludedContainer>&
00133 SparseIterator<Integer, ExcludedContainer>::operator--()
00134 {
00135 if (excluded_->size() == 0)
00136 integer_ = integer_ - 1;
00137 else
00138 do
00139 integer_ = integer_ - 1;
00140 while (excluded_->find(integer_) != excluded_->end());
00141 return *this;
00142 }
00143
00144 template <class Integer, class ExcludedContainer>
00145 SparseIterator<Integer, ExcludedContainer>
00146 SparseIterator<Integer, ExcludedContainer>::operator++(int)
00147 {
00148 SparseIterator tmp = *this;
00149 ++*this;
00150 return tmp;
00151 }
00152
00153 template <class Integer, class ExcludedContainer>
00154 SparseIterator<Integer, ExcludedContainer>
00155 SparseIterator<Integer, ExcludedContainer>::operator--(int)
00156 {
00157 SparseIterator tmp = *this;
00158 --*this;
00159 return tmp;
00160 }
00161
00162 template <class Integer, class ExcludedContainer>
00163 typename SparseIterator<Integer, ExcludedContainer>::
00164 integer_t
00165 SparseIterator<Integer, ExcludedContainer>::operator*()
00166 {
00167 return integer_;
00168 }
00169
00170 template <class Integer, class ExcludedContainer>
00171 bool
00172 SparseIterator<Integer, ExcludedContainer>
00173 ::operator!=(const SparseIterator& i)
00174 {
00175 return i.integer_ != integer_;
00176 }
00177
00178 template <class Integer, class ExcludedContainer>
00179 bool
00180 SparseIterator<Integer, ExcludedContainer>
00181 ::operator==(const SparseIterator& i)
00182 {
00183 return i.integer_ == integer_;
00184 }
00185
00186 template <class Integer, class ExcludedContainer>
00187 SparseIterator<Integer, ExcludedContainer>&
00188 SparseIterator<Integer, ExcludedContainer>
00189 ::operator=(const SparseIterator& i)
00190 {
00191 integer_ = i.integer_;
00192 excluded_ = i.excluded_;
00193 return *this;
00194 }
00195
00199 template <class Integer, class ExcludedContainer>
00200 SparseInterval<Integer, ExcludedContainer>
00201 ::SparseInterval(integer_t f, integer_t t, const excluded_container_t& c):
00202 excluded_(c),
00203 from_(f),
00204 to_(t)
00205 {
00206 precondition(from_ <= to_ + 1);
00207 precondition(excluded_.find(to_ + 1) == excluded_.end());
00208 }
00209
00210 template <class Integer, class ExcludedContainer>
00211 SparseInterval<Integer, ExcludedContainer>
00212 ::SparseInterval(const SparseInterval& a) :
00213 excluded_(a.excluded_),
00214 from_(a.from_),
00215 to_(a.to_)
00216 {
00217 }
00218
00219 template <class Integer, class ExcludedContainer>
00220 unsigned
00221 SparseInterval<Integer, ExcludedContainer>::size() const
00222 {
00223
00224 return to_ < from_ ? 0 : to_ - from_ + 1 - excluded_.size();
00225 }
00226
00227 template <class Integer, class ExcludedContainer>
00228 typename SparseInterval<Integer, ExcludedContainer>::integer_t
00229 SparseInterval<Integer, ExcludedContainer>::max() const
00230 {
00231 unsigned r = to_;
00232
00233 while (excluded_.find(r) != excluded_.end())
00234 --r;
00235 return r;
00236 }
00237
00238 template <class Integer, class ExcludedContainer>
00239 std::string
00240 SparseInterval<Integer, ExcludedContainer>::to_string() const
00241 {
00242 std::stringstream s;
00243 s << "from :" << from_ << " to : " << to_ << " ex:";
00244 for (typename ExcludedContainer::iterator i = excluded_.begin();
00245 i != excluded_.end();
00246 ++i)
00247 s << *i << " ";
00248 return s.str();
00249 }
00250
00251 template <class Integer, class ExcludedContainer>
00252 typename SparseInterval<Integer, ExcludedContainer>::iterator
00253 SparseInterval<Integer, ExcludedContainer>::begin() const
00254 {
00255 int from = from_;
00256
00257 if (excluded_.size() != 0)
00258 while (excluded_.find(from) != excluded_.end())
00259 from = from + 1;
00260 return iterator(from, excluded_);
00261 }
00262
00263 template <class Integer, class ExcludedContainer>
00264 typename SparseInterval<Integer, ExcludedContainer>::iterator
00265 SparseInterval<Integer, ExcludedContainer>::end() const
00266 {
00267 return iterator(to_ + 1, excluded_);
00268 }
00269
00270 }
00271
00272 # include <vaucanson/misc/support.hxx>
00273
00274 #endif // ! VCSN_MISC_SUPPORT_HXX