Vaucanson 1.4
window.hxx
Go to the documentation of this file.
00001 // window.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 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_MISC_WINDOW_HXX
00018 # define VCSN_MISC_WINDOW_HXX
00019 
00028 # include <vaucanson/misc/window.hh>
00029 # include <vaucanson/misc/contract.hh>
00030 
00031 namespace vcsn {
00032   namespace misc {
00033 
00036     template <class InputIterator, class Letter>
00037     Window<InputIterator, Letter>::Window (const iterator_t& stream,
00038                                            const iterator_t& eof,
00039                                            letter_t eol,
00040                                            length_t length) :
00041       begin_     (stream),
00042       stream_    (stream),
00043       end_       (eof),
00044       eol_       (eol),
00045       length_    (length)
00046     {
00047       precondition (length > 0);
00048 
00049       compute_size ();
00050     }
00051 
00052     template <class InputIterator, class Letter>
00053     bool
00054     Window<InputIterator, Letter>::eof () const
00055     {
00056       return stream_ == end_;
00057     }
00058 
00059     template <class InputIterator, class Letter>
00060     bool
00061     Window<InputIterator, Letter>::eol () const
00062     {
00063       return eof () || (*stream_ == eol_);
00064     }
00065 
00066     template <class InputIterator, class Letter>
00067     void
00068     Window<InputIterator, Letter>::shift (unsigned int n)
00069     {
00070       precondition (n <= size_);
00071 
00072       if ((stream_ != end_) && (*stream_ == eol_))
00073         ++stream_;
00074       else
00075         stream_ += n;
00076       compute_size ();
00077     }
00078 
00079     template <class InputIterator, class Letter>
00080     void
00081     Window<InputIterator, Letter>::shift ()
00082     {
00083       shift (size ());
00084     }
00085 
00086     template <class InputIterator, class Letter>
00087     void
00088     Window<InputIterator, Letter>::moveto (length_t offset)
00089     {
00090       stream_ = begin_ + offset;
00091       compute_size ();
00092     }
00093 
00094     template <class InputIterator, class Letter>
00095     void
00096     Window<InputIterator, Letter>::moveto (iterator_t position)
00097     {
00098       stream_ = position;
00099       compute_size ();
00100     }
00101 
00102     template <class InputIterator, class Letter>
00103     typename Window<InputIterator, Letter>::length_t
00104     Window<InputIterator, Letter>::size () const
00105     {
00106       return size_;
00107     }
00108 
00109     template <class InputIterator, class Letter>
00110     typename Window<InputIterator, Letter>::letter_t
00111     Window<InputIterator, Letter>::operator[] (length_t i) const
00112     {
00113       precondition (i < size_);
00114 
00115       return stream_[i];
00116     }
00117 
00118     template <class InputIterator, class Letter>
00119     std::ostream&
00120     Window<InputIterator, Letter>::print (std::ostream& ostr) const
00121     {
00122       return ostr << window ();
00123     }
00124 
00125     template <class InputIterator, class Letter>
00126     typename Window<InputIterator, Letter>::letter_t
00127     Window<InputIterator, Letter>::eol_value () const
00128     {
00129       return eol_;
00130     }
00131 
00132     template <class InputIterator, class Letter>
00133     typename Window<InputIterator, Letter>::iterator_t
00134     Window<InputIterator, Letter>::begin () const
00135     {
00136       return begin_;
00137     }
00138 
00139     template <class InputIterator, class Letter>
00140     typename Window<InputIterator, Letter>::iterator_t
00141     Window<InputIterator, Letter>::stream () const
00142     {
00143       return stream_;
00144     }
00145 
00146     template <class InputIterator, class Letter>
00147     typename Window<InputIterator, Letter>::iterator_t
00148     Window<InputIterator, Letter>::end () const
00149     {
00150       return end_;
00151     }
00152 
00153     template <class InputIterator, class Letter>
00154     size_t
00155     Window<InputIterator, Letter>::offset () const
00156     {
00157       return stream_ - begin_;
00158     }
00159 
00160     template <class InputIterator, class Letter>
00161     typename Window<InputIterator, Letter>::length_t
00162     Window<InputIterator, Letter>::length () const
00163     {
00164       return length_;
00165     }
00166 
00167     template <class InputIterator, class Letter>
00168     typename Window<InputIterator, Letter>::string_t
00169     Window<InputIterator, Letter>::window () const
00170     {
00171       return string_t (stream_, stream_ + size_);
00172     }
00173 
00174     template <class InputIterator, class Letter>
00175     void
00176     Window<InputIterator, Letter>::compute_size ()
00177     {
00178       size_ = 0;
00179       while ((size_ < length_) &&
00180              (stream_ + size_ != end_) &&
00181              (stream_[size_] != eol_))
00182         ++size_;
00183     }
00184 
00185     template <class InputIterator, class Letter>
00186     std::ostream&
00187     operator<< (std::ostream& ostr, const Window<InputIterator, Letter>& w)
00188     {
00189       return w.print (ostr);
00190     }
00191 
00194   } // misc
00195 } // vcsn
00196 
00197 #endif // ! VCSN_MISC_WINDOW_HXX