claw  1.9.0
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@stuff-o-matic.com
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>
43  class it_index
44  {
45  public:
46  typedef typename std::iterator_traits<T>::value_type value_type;
47  typedef typename std::iterator_traits<T>::difference_type difference_type;
48  typedef typename std::iterator_traits<T>::pointer pointer;
49  typedef typename std::iterator_traits<T>::reference reference;
50 
51  private:
53  T m_it;
54 
56  int m_index;
57 
58  public:
61  : m_it()
62  , m_index()
63  {}
64 
70  it_index(const T& it, int index = 0)
71  : m_it(it)
72  , m_index(index)
73  {}
74 
79  it_index(const it_index<T>& that)
80  : m_it(that.m_it)
81  , m_index(that.m_index)
82  {}
83 
89  void set(const T& it, int index)
90  {
91  m_it = it;
92  m_index = index;
93  }
94 
95  bool operator<(const it_index<T>& that) const
96  {
97  return m_index < that.m_index;
98  }
99 
100  bool operator<(const T& it) const
101  {
102  return m_it < it;
103  }
104  bool operator<(int index) const
105  {
106  return m_index < index;
107  }
108 
109  bool operator<=(const it_index<T>& that) const
110  {
111  return (*this < that) || (*this == that);
112  }
113  bool operator<=(const T& it) const
114  {
115  return m_it <= it;
116  }
117  bool operator<=(int index) const
118  {
119  return m_index <= index;
120  }
121 
122  bool operator>(const it_index<T>& that) const
123  {
124  return m_index > that.m_index;
125  }
126  bool operator>(const T& it) const
127  {
128  return m_it > it;
129  }
130  bool operator>(int index) const
131  {
132  return m_index > index;
133  }
134 
135  bool operator>=(const it_index<T>& that) const
136  {
137  return (*this > that) || (*this == that);
138  }
139  bool operator>=(const T& it) const
140  {
141  return m_it >= it;
142  }
143  bool operator>=(int index) const
144  {
145  return m_index >= index;
146  }
147 
148  bool operator==(const it_index<T>& that) const
149  {
150  return (m_it == that.m_it) && (m_index == that.m_index);
151  }
152  bool operator==(const T& it) const
153  {
154  return m_it == it;
155  }
156  bool operator==(int index) const
157  {
158  return m_index == index;
159  }
160 
161  bool operator!=(const it_index<T>& that) const
162  {
163  return !(*this == *that);
164  }
165  bool operator!=(const T& it) const
166  {
167  return m_it != it;
168  }
169  bool operator!=(int index) const
170  {
171  return m_index != index;
172  }
173 
174  it_index<T> operator+(int index) const
175  {
176  return it_index<T>(m_it + index, m_index + index);
177  }
178  it_index<T> operator-(int index) const
179  {
180  return it_index<T>(m_it - index, m_index - index);
181  }
182  it_index<T> operator*(int index) const
183  {
184  return it_index<T>(m_it + (index - 1) * m_index, m_index * index);
185  }
186  it_index<T> operator/(int index) const
187  {
188  return it_index<T>(m_it - (m_index - m_index / index), m_index / index);
189  }
190 
191  reference operator*() const
192  {
193  return *m_it;
194  }
195  pointer operator->() const
196  {
197  return &*m_it;
198  }
199 
200  // Préincrément
201  it_index<T>& operator++()
202  {
203  ++m_it;
204  ++m_index;
205  return *this;
206  }
207 
208  // Postincrément
209  it_index<T> operator++(int)
210  {
211  it_index<T> r(*this);
212  ++(*this);
213  return r;
214  }
215 
216  // Préincrément
217  it_index<T>& operator--()
218  {
219  --m_it;
220  --m_index;
221  return *this;
222  }
223 
224  // Postincrément
225  it_index<T> operator--(int)
226  {
227  it_index<T> r(*this);
228  --(*this);
229  return r;
230  }
231 
232  it_index<T>& operator+=(int index)
233  {
234  m_it += index;
235  m_index += index;
236  return *this;
237  }
238 
239  it_index<T>& operator-=(int index)
240  {
241  m_it -= index;
242  m_index -= index;
243  return *this;
244  }
245 
246  it_index<T>& operator*=(int index)
247  {
248  m_it += (index - 1) * m_index;
249  m_index *= index;
250  return *this;
251  }
252 
253  it_index<T>& operator/=(int index)
254  {
255  m_it -= m_index - m_index / index;
256  m_index /= index;
257  return *this;
258  }
259 
260  operator int() const
261  {
262  return m_index;
263  }
264  operator T() const
265  {
266  return m_it;
267  }
268 
269  }; // it_index;
270 
271 }
272 
273 #endif // __CLAW_IT_INDEX_HPP__
it_index()
Constructor.
Definition: it_index.hpp:60
it_index(const it_index< T > &that)
Copy constructor.
Definition: it_index.hpp:79
it_index(const T &it, int index=0)
Constructor.
Definition: it_index.hpp:70
A class to manage an index and an iterator easily.
Definition: it_index.hpp:43
This is the main namespace.
Definition: application.hpp:49