DeSiGNAR  0.5a
Data Structures General Library
iterator.H
Go to the documentation of this file.
1 /*
2  This file is part of Designar.
3  Copyright (C) 2017 by Alejandro J. Mujica
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  Any user request of this software, write to
19 
20  Alejandro Mujica
21 
22  aledrums@gmail.com
23 */
24 
25 # ifndef DSGITERATOR_H
26 # define DSGITERATOR_H
27 
28 # include <types.H>
29 
30 namespace Designar
31 {
32  template <bool B, typename T, typename F>
33  using RetType = typename std::conditional<B, T, F>::type;
34 
35  template <class Iterator, typename T, bool RET_CPY = false>
37  {
38  protected:
39  Iterator & me()
40  {
41  return *static_cast<Iterator *>(this);
42  }
43 
44  const Iterator & const_me() const
45  {
46  return *static_cast<const Iterator *>(this);
47  }
48 
49  public:
50  bool has_curr() const
51  {
52  return const_me().has_current();
53  }
54 
56  {
57  return me().get_current();
58  }
59 
61  {
62  return const_me().get_current();
63  }
64 
66  {
67  return me().get_current();
68  }
69 
71  {
72  return const_me().get_current();
73  }
74 
76  {
77  return &me().get_current();
78  }
79 
80  bool operator == (const Iterator & it) const
81  {
82  return const_me().get_location() == it.get_location();
83  }
84 
85  bool operator != (const Iterator & it) const
86  {
87  return const_me().get_location() != it.get_location();
88  }
89  };
90 
91  template <class Iterator, typename T, bool RET_CPY = false>
92  class ForwardIterator : public BasicIterator<Iterator, T, RET_CPY>
93  {
95 
96  public:
97  Iterator & operator ++ ()
98  {
99  Base::me().next();
100  return Base::me();
101  }
102 
103  Iterator operator ++ (int)
104  {
105  Iterator ret_val = Base::me();
106  Base::me().next();
107  return ret_val;
108  }
109  };
110 
111  template <class Iterator, typename T, bool RET_CPY = false>
112  class BidirectionalIterator : public ForwardIterator<Iterator, T, RET_CPY>
113  {
115 
116  public:
117  Iterator & operator -- ()
118  {
119  Base::me().prev();
120  return Base::me();
121  }
122 
123  Iterator operator -- (int)
124  {
125  Iterator ret_val = Base::me();
126  Base::me().prev();
127  return ret_val;
128  }
129  };
130 
131  template <class Iterator, typename T, bool RET_CPY = false>
132  class RandomAccessIterator : public BidirectionalIterator<Iterator, T, RET_CPY>
133  {
135 
136  public:
138  {
139  Base::me().move_to(i);
140  return Base::mes().get_current();
141  }
142 
143  Iterator & operator += (nat_t p)
144  {
145  Base::me().next_n(p);
146  return Base::me();
147  }
148 
149  Iterator operator + (nat_t p)
150  {
151  Iterator it = Base::me();
152  it += p;
153  return it;
154  }
155 
156  Iterator & operator -= (nat_t p)
157  {
158  Base::me().prev_n(p);
159  return Base::me();
160  }
161 
162  Iterator operator - (nat_t p)
163  {
164  Iterator it = Base::me();
165  it -= p;
166  return it;
167  }
168 
169  bool operator < (const Iterator & it) const
170  {
171  return Base::const_me().get_location() < it.get_location();
172  }
173 
174  bool operator <= (const Iterator & it) const
175  {
176  return Base::const_me().get_location() <= it.get_location();
177  }
178 
179  bool operator > (const Iterator & it) const
180  {
181  return Base::const_me().get_location() > it.get_location();
182  }
183 
184  bool operator >= (const Iterator & it) const
185  {
186  return Base::const_me().get_location() >= it.get_location();
187  }
188  };
189 
190 } // end namespace Designar
191 
192 # endif // DSGITERATOR_H
RetType< RET_CPY, T, T & > get_curr()
Definition: iterator.H:55
RetType< RET_CPY, T, const T & > get_curr() const
Definition: iterator.H:60
Definition: iterator.H:92
Definition: iterator.H:132
Definition: iterator.H:36
typename std::conditional< B, T, F >::type RetType
Definition: iterator.H:33
bool operator!=(const Iterator &it) const
Definition: iterator.H:85
bool operator==(const Iterator &it) const
Definition: iterator.H:80
Iterator & me()
Definition: iterator.H:39
T * operator->()
Definition: iterator.H:75
RetType< RET_CPY, T, T & > operator*()
Definition: iterator.H:65
Definition: iterator.H:112
Definition: array.H:32
unsigned long int nat_t
Definition: types.H:50
const Iterator & const_me() const
Definition: iterator.H:44
bool has_curr() const
Definition: iterator.H:50