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

Generated on Thu Oct 9 20:22:40 2008 for Vaucanson by  doxygen 1.5.1