Claw 1.7.0
it_index.hpp
Go to the documentation of this file.
00001 /*
00002   CLAW - a C++ Library Absolutely Wonderful
00003 
00004   CLAW is a free library without any particular aim but being useful to 
00005   anyone.
00006 
00007   Copyright (C) 2005-2011 Julien Jorge
00008 
00009   This library is free software; you can redistribute it and/or
00010   modify it under the terms of the GNU Lesser General Public
00011   License as published by the Free Software Foundation; either
00012   version 2.1 of the License, or (at your option) any later version.
00013 
00014   This library is distributed in the hope that it will be useful,
00015   but WITHOUT ANY WARRANTY; without even the implied warranty of
00016   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017   Lesser General Public License for more details.
00018 
00019   You should have received a copy of the GNU Lesser General Public
00020   License along with this library; if not, write to the Free Software
00021   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00022 
00023   contact: julien.jorge@gamned.org
00024 */
00030 #ifndef __CLAW_IT_INDEX_HPP__
00031 #define __CLAW_IT_INDEX_HPP__
00032 
00033 #include <iostream>
00034 
00035 namespace claw
00036 {
00042   template<class T> class it_index
00043   {
00044   public:
00045     typedef typename std::iterator_traits<T>::value_type value_type;
00046     typedef typename std::iterator_traits<T>::difference_type difference_type;
00047     typedef typename std::iterator_traits<T>::pointer pointer;
00048     typedef typename std::iterator_traits<T>::reference reference;
00049 
00050   private:
00052     T   m_it;
00053 
00055     int m_index;
00056 
00057   public:
00059     it_index() 
00060       : m_it(), m_index()
00061     { }
00062 
00068     it_index(const T& it, int index=0) 
00069       : m_it(it), m_index(index)
00070     { }
00071 
00076     it_index( const it_index<T>& that ) 
00077       : m_it( that.m_it ), m_index( that.m_index )
00078     { }
00079 
00085     void set( const T& it, int index )
00086     {
00087       m_it = it;
00088       m_index = index;
00089     }
00090 
00091     bool operator<( const it_index<T>& that ) const
00092     { return m_index < that.m_index; }
00093 
00094     bool operator<( const T& it ) const { return m_it < it; }
00095     bool operator<( int index ) const { return m_index < index; }
00096   
00097     bool operator<=( const it_index<T>& that ) const
00098     { return (*this < that) || (*this == that); }
00099     bool operator<=( const T& it ) const { return m_it <= it; }
00100     bool operator<=( int index ) const { return m_index <= index; }
00101   
00102     bool operator>( const it_index<T>& that ) const
00103     { return m_index > that.m_index; }
00104     bool operator>( const T& it ) const { return m_it > it; }
00105     bool operator>( int index ) const { return m_index > index; }
00106         
00107     bool operator>=( const it_index<T>& that ) const
00108     { return (*this > that) || (*this == that); }
00109     bool operator>=( const T& it ) const { return m_it >= it; }
00110     bool operator>=( int index ) const { return m_index >= index; }
00111 
00112     bool operator==( const it_index<T>& that ) const
00113     { return (m_it == that.m_it) && (m_index == that.m_index); }
00114     bool operator==( const T& it ) const { return m_it == it; }
00115     bool operator==( int index ) const { return m_index==index; }
00116 
00117     bool operator!=( const it_index<T>& that ) const
00118     { return !(*this == *that); }
00119     bool operator!=( const T& it ) const { return m_it != it; }
00120     bool operator!=( int index ) const { return m_index!=index; }
00121 
00122     it_index<T> operator+( int index ) const
00123     { return it_index<T>(m_it + index, m_index + index); }
00124     it_index<T> operator-( int index ) const
00125     { return it_index<T>(m_it - index, m_index - index); }
00126     it_index<T> operator*( int index ) const
00127     { return it_index<T>(m_it + (index-1) * m_index, m_index * index); }
00128     it_index<T> operator/( int index ) const
00129     { return it_index<T>(m_it - (m_index - m_index/index), m_index / index); }
00130   
00131     reference operator*() const { return *m_it; }
00132     pointer operator->() const { return &*m_it; }
00133 
00134     // Préincrément
00135     it_index<T>& operator++() 
00136     {
00137       ++m_it;
00138       ++m_index;
00139       return *this;
00140     }
00141 
00142     // Postincrément
00143     it_index<T> operator++(int) 
00144     {
00145       it_index<T> r(*this);
00146       ++(this);
00147       return r;
00148     }
00149 
00150     // Préincrément
00151     it_index<T>& operator--() 
00152     {
00153       --m_it;
00154       --m_index;
00155       return *this;
00156     }
00157 
00158     // Postincrément
00159     it_index<T> operator--(int) 
00160     {
00161       it_index<T> r(*this);
00162       --(this);
00163       return r;
00164     }
00165 
00166     it_index<T>& operator+=( int index ) 
00167     { 
00168       m_it += index;
00169       m_index += index;
00170       return *this;
00171     }
00172   
00173     it_index<T>& operator-=( int index ) 
00174     { 
00175       m_it -= index;
00176       m_index -= index;
00177       return *this;
00178     }
00179   
00180     it_index<T>& operator*=( int index ) 
00181     { 
00182       m_it += (index-1) * m_index;
00183       m_index *= index;
00184       return *this;
00185     }
00186   
00187     it_index<T>& operator/=( int index ) 
00188     { 
00189       m_it -= m_index - m_index/index;
00190       m_index /= index;
00191       return *this;
00192     }
00193   
00194     operator int() const { return m_index; }
00195     operator T() const { return m_it; }
00196   
00197   }; // it_index;
00198 
00199 } // namespace claw
00200 
00201 #endif // __CLAW_IT_INDEX_HPP__