Vaucanson 1.4
listg_sparse_interval.hxx
00001 // listg_sparse_interval.hxx: 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, 2008 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_AUTOMATA_IMPLEMENTATION_LISTG_LISTG_SPARSE_INTERVAL_HXX
00018 # define VCSN_AUTOMATA_IMPLEMENTATION_LISTG_LISTG_SPARSE_INTERVAL_HXX
00019 
00020 #include <vaucanson/misc/limits.hh>
00021 
00022 namespace vcsn
00023 {
00024   namespace misc
00025   {
00026   /*---------------.
00027   | SparseIterator |
00028   `---------------*/
00029 
00030     template <class T, class ExcludedContainer>
00031     SparseIterator<handler<T, unsigned>, ExcludedContainer>::
00032     SparseIterator (integer_t from,
00033                     const excluded_container_t& c)
00034       : excluded_ (&c),
00035         integer_ (from)
00036     {}
00037 
00038     template <class T, class ExcludedContainer>
00039     SparseIterator<handler<T, unsigned>, ExcludedContainer>&
00040     SparseIterator<handler<T, unsigned>, ExcludedContainer>::operator++ ()
00041     {
00042       if (excluded_->size () == 0)
00043         ++integer_;
00044       else
00045         do
00046           ++integer_;
00047         while (excluded_->find (handler_t(integer_)) != excluded_->end ());
00048       return *this;
00049     }
00050 
00051     template <class T, class ExcludedContainer>
00052     SparseIterator<handler<T, unsigned>, ExcludedContainer>&
00053     SparseIterator<handler<T, unsigned>, ExcludedContainer>::operator-- ()
00054     {
00055       if (excluded_->size () == 0)
00056         --integer_;
00057       else
00058         do
00059           --integer_;
00060         while (excluded_->find (handler_t(integer_)) != excluded_->end ());
00061       return *this;
00062     }
00063 
00064     template <class T, class ExcludedContainer>
00065     SparseIterator<handler<T, unsigned>, ExcludedContainer>
00066     SparseIterator<handler<T, unsigned>, ExcludedContainer>::operator++ (int)
00067     {
00068       SparseIterator tmp = *this;
00069       ++*this;
00070       return tmp;
00071     }
00072 
00073     template <class T, class ExcludedContainer>
00074     SparseIterator<handler<T, unsigned>, ExcludedContainer>
00075     SparseIterator<handler<T, unsigned>, ExcludedContainer>::operator-- (int)
00076     {
00077       SparseIterator tmp = *this;
00078       --*this;
00079       return tmp;
00080     }
00081 
00082     template <class T, class ExcludedContainer>
00083     typename SparseIterator<handler<T, unsigned>, ExcludedContainer>::
00084     integer_t
00085     SparseIterator<handler<T, unsigned>, ExcludedContainer>::operator* ()
00086     {
00087       return handler_t(integer_);
00088     }
00089 
00090     template <class T, class ExcludedContainer>
00091     bool
00092     SparseIterator<handler<T, unsigned>, ExcludedContainer>
00093     ::operator!= (const SparseIterator& i) const
00094     {
00095       return i.integer_ != integer_;
00096     }
00097 
00098     template <class T, class ExcludedContainer>
00099     bool
00100     SparseIterator<handler<T, unsigned>, ExcludedContainer>
00101     ::operator== (const SparseIterator& i) const
00102     {
00103       return i.integer_ == integer_;
00104     }
00105 
00106     template <class T, class ExcludedContainer>
00107     SparseIterator<handler<T, unsigned>, ExcludedContainer>&
00108     SparseIterator<handler<T, unsigned>, ExcludedContainer>
00109     ::operator= (const SparseIterator& i)
00110     {
00111       integer_ = i.integer_;
00112       excluded_ = i.excluded_;
00113       return *this;
00114     }
00115 
00116 
00117 
00118 
00119   /*-----------------.
00120   | SparseInterval.  |
00121   `-----------------*/
00122 
00123 
00127     template <class T, class ExcludedContainer>
00128     SparseInterval<handler<T, unsigned>, ExcludedContainer>
00129     ::SparseInterval (integer_t f, integer_t t, const excluded_container_t& c)
00130       : excluded_ (c),
00131         from_ (f),
00132         to_ (t)
00133     {
00134       precondition (from_ <= to_ + 1);
00135       precondition (excluded_.find (handler_t(to_ + 1)) == excluded_.end ());
00136       precondition ((to_ == limits<unsigned int>::max())
00137                     || (to_ + 1 - from_ >= excluded_.size()));
00138     }
00139 
00140     template <class T, class ExcludedContainer>
00141     SparseInterval<handler<T, unsigned>, ExcludedContainer>
00142     ::SparseInterval (const SparseInterval& a)
00143       : excluded_ (a.excluded_),
00144         from_ (a.from_),
00145         to_ (a.to_)
00146     {
00147     }
00148 
00149     template <class T, class ExcludedContainer>
00150     unsigned
00151     SparseInterval<handler<T, unsigned>, ExcludedContainer>::size () const
00152     {
00153       // to_ is equal to limits<unsigned int>::max() if the container
00154       // is empty.  See for instance GClass::states() or GClass::edges().
00155       return to_ == limits<unsigned int>::max() ? 0
00156         : to_ - from_ + 1 - excluded_.size ();
00157     }
00158 
00159     template <class T, class ExcludedContainer>
00160     std::string
00161     SparseInterval<handler<T, unsigned>, ExcludedContainer>::to_string () const
00162     {
00163       std::stringstream s;
00164       s << "from :" << from_ << " to : " << to_ << " ex:";
00165       for (typename ExcludedContainer::iterator i = excluded_.begin ();
00166            i != excluded_.end ();
00167            ++i)
00168         s << *i << " ";
00169       return s.str ();
00170     }
00171 
00172     template <class T, class ExcludedContainer>
00173     typename SparseInterval<handler<T, unsigned>, ExcludedContainer>::iterator
00174     SparseInterval<handler<T, unsigned>, ExcludedContainer>::begin () const
00175     {
00176       unsigned from = from_;
00177 
00178       if (excluded_.size () != 0)
00179         while (excluded_.find (handler_t(from)) != excluded_.end ())
00180           ++from;
00181       return iterator (handler_t(from), excluded_);
00182     }
00183 
00184     template <class T, class ExcludedContainer>
00185     typename SparseInterval<handler<T, unsigned>, ExcludedContainer>::iterator
00186     SparseInterval<handler<T, unsigned>, ExcludedContainer>::end () const
00187     {
00188       return iterator (handler_t(to_ + 1), excluded_);
00189     }
00190 
00191     template <class T, class ExcludedContainer>
00192     typename SparseInterval<handler<T, unsigned>, ExcludedContainer>::handler_t
00193     SparseInterval<handler<T, unsigned>, ExcludedContainer>::back () const
00194     {
00195       unsigned to = to_;
00196 
00197       if (excluded_.size () != 0)
00198         while (excluded_.find (handler_t(to)) != excluded_.end ())
00199           --to;
00200       return *(iterator (handler_t(to), excluded_));
00201     }
00202 
00203 
00204   } // misc
00205 } // vcsn
00206 
00207 #endif // ! VCSN_MISC_SPARSE_INTERVAL_HXX