Claw  1.7.3
it_index.hpp
Go to the documentation of this file.
1 /*
2  CLAW - a C++ Library Absolutely Wonderful
3 
4  CLAW is a free library without any particular aim but being useful to
5  anyone.
6 
7  Copyright (C) 2005-2011 Julien Jorge
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 
23  contact: julien.jorge@gamned.org
24 */
30 #ifndef __CLAW_IT_INDEX_HPP__
31 #define __CLAW_IT_INDEX_HPP__
32 
33 #include <iostream>
34 
35 namespace claw
36 {
42  template<class T> class it_index
43  {
44  public:
45  typedef typename std::iterator_traits<T>::value_type value_type;
46  typedef typename std::iterator_traits<T>::difference_type difference_type;
47  typedef typename std::iterator_traits<T>::pointer pointer;
48  typedef typename std::iterator_traits<T>::reference reference;
49 
50  private:
52  T m_it;
53 
55  int m_index;
56 
57  public:
60  : m_it(), m_index()
61  { }
62 
68  it_index(const T& it, int index=0)
69  : m_it(it), m_index(index)
70  { }
71 
76  it_index( const it_index<T>& that )
77  : m_it( that.m_it ), m_index( that.m_index )
78  { }
79 
85  void set( const T& it, int index )
86  {
87  m_it = it;
88  m_index = index;
89  }
90 
91  bool operator<( const it_index<T>& that ) const
92  { return m_index < that.m_index; }
93 
94  bool operator<( const T& it ) const { return m_it < it; }
95  bool operator<( int index ) const { return m_index < index; }
96 
97  bool operator<=( const it_index<T>& that ) const
98  { return (*this < that) || (*this == that); }
99  bool operator<=( const T& it ) const { return m_it <= it; }
100  bool operator<=( int index ) const { return m_index <= index; }
101 
102  bool operator>( const it_index<T>& that ) const
103  { return m_index > that.m_index; }
104  bool operator>( const T& it ) const { return m_it > it; }
105  bool operator>( int index ) const { return m_index > index; }
106 
107  bool operator>=( const it_index<T>& that ) const
108  { return (*this > that) || (*this == that); }
109  bool operator>=( const T& it ) const { return m_it >= it; }
110  bool operator>=( int index ) const { return m_index >= index; }
111 
112  bool operator==( const it_index<T>& that ) const
113  { return (m_it == that.m_it) && (m_index == that.m_index); }
114  bool operator==( const T& it ) const { return m_it == it; }
115  bool operator==( int index ) const { return m_index==index; }
116 
117  bool operator!=( const it_index<T>& that ) const
118  { return !(*this == *that); }
119  bool operator!=( const T& it ) const { return m_it != it; }
120  bool operator!=( int index ) const { return m_index!=index; }
121 
122  it_index<T> operator+( int index ) const
123  { return it_index<T>(m_it + index, m_index + index); }
124  it_index<T> operator-( int index ) const
125  { return it_index<T>(m_it - index, m_index - index); }
126  it_index<T> operator*( int index ) const
127  { return it_index<T>(m_it + (index-1) * m_index, m_index * index); }
128  it_index<T> operator/( int index ) const
129  { return it_index<T>(m_it - (m_index - m_index/index), m_index / index); }
130 
131  reference operator*() const { return *m_it; }
132  pointer operator->() const { return &*m_it; }
133 
134  // Préincrément
135  it_index<T>& operator++()
136  {
137  ++m_it;
138  ++m_index;
139  return *this;
140  }
141 
142  // Postincrément
143  it_index<T> operator++(int)
144  {
145  it_index<T> r(*this);
146  ++(this);
147  return r;
148  }
149 
150  // Préincrément
151  it_index<T>& operator--()
152  {
153  --m_it;
154  --m_index;
155  return *this;
156  }
157 
158  // Postincrément
159  it_index<T> operator--(int)
160  {
161  it_index<T> r(*this);
162  --(this);
163  return r;
164  }
165 
166  it_index<T>& operator+=( int index )
167  {
168  m_it += index;
169  m_index += index;
170  return *this;
171  }
172 
173  it_index<T>& operator-=( int index )
174  {
175  m_it -= index;
176  m_index -= index;
177  return *this;
178  }
179 
180  it_index<T>& operator*=( int index )
181  {
182  m_it += (index-1) * m_index;
183  m_index *= index;
184  return *this;
185  }
186 
187  it_index<T>& operator/=( int index )
188  {
189  m_it -= m_index - m_index/index;
190  m_index /= index;
191  return *this;
192  }
193 
194  operator int() const { return m_index; }
195  operator T() const { return m_it; }
196 
197  }; // it_index;
198 
199 } // namespace claw
200 
201 #endif // __CLAW_IT_INDEX_HPP__