Aleph-w  1.9
General library for algorithms and data structures
array_it.H
1 
2 /* Aleph-w
3 
4  / \ | | ___ _ __ | |__ __ __
5  / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6  / ___ \| | __/ |_) | | | |_____\ V V / version 1.9b
7  /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8  |_|
9 
10  This file is part of Aleph-w library
11 
12  Copyright (c) 2002-2018 Leandro Rabindranath Leon & Alejandro Mujica
13 
14  This program is free software: you can redistribute it and/or modify
15  it under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  This program is distributed in the hope that it will be useful, but
20  WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  General Public License for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with this program. If not, see <https://www.gnu.org/licenses/>.
26 */
27 # ifndef ARRAY_IT_H
28 # define ARRAY_IT_H
29 
30 # include <cassert>
31 # include <tuple>
32 # include <aleph.H>
33 # include <ahDefs.H>
34 # include <htlist.H>
35 
36 namespace Aleph {
37 
38 template <typename T> class Array_Container;
39 
44  template <class T>
46 {
47  T * ptr = nullptr;
48  long dim = 0;
49  long num_items = 0;
50  long idx = 0;
51  long first = 0;
52  long last = 0;
53  long pos = 0;
54 
55 public:
56 
57  using Item_Type = T;
58 
59  T * get_base() noexcept { return ptr; }
60 
61  Array_Iterator() noexcept {}
62 
63  Array_Iterator(T * p, size_t sz, size_t n)
64  : ptr(p), dim(sz), num_items(n), last(n - 1)
65  {
66  assert(ptr);
67  if (num_items > dim)
68  throw std::domain_error("Array_Iterator(): num_items greater tan dim");
69  }
70 
71  Array_Iterator(NoExceptionCtor, T * p, size_t sz, size_t n)
72  : ptr(p), dim(sz), num_items(n), last(n - 1)
73  {
74  assert(ptr);
75  if (num_items > dim)
76  throw std::domain_error("Array_Iterator(): num_items greater tan dim");
77  }
78 
79  Array_Iterator(T * p, size_t sz, size_t n, long f, long l)
80  : ptr(p), dim(sz), num_items(n), idx(f), first(f), last(l)
81  {
82  assert(ptr);
83  if (num_items > dim)
84  throw std::domain_error("Array_Iterator(): num_items greater tan dim");
85  if (first >= dim)
86  throw std::domain_error("Array_Iterator(): first >= dim");
87  if (last >= dim)
88  throw std::domain_error("Array_Iterator(): last >= dim");
89  }
90 
96  Array_Iterator(NoExceptionCtor, T * p, size_t sz, size_t n, long f, long l)
97  : ptr(p), dim(sz), num_items(n), idx(f), first(f), last(l)
98  {
99  assert(ptr);
100  }
101 
102  Array_Iterator(const Array_Container<T> & c)
103  : Array_Iterator(c.base(), c.capacity(), c.size()) {}
104 
105  bool has_curr() const noexcept { return pos >= 0 and pos < num_items; }
106 
107  bool is_last() const noexcept { return pos == num_items - 1; }
108 
109  long get_pos() const noexcept { return pos; }
110 
111  T & get_curr_ne() const noexcept
112  {
113  return ptr[idx];
114  }
115 
116  T & get_curr() const
117  {
118  if (pos < 0)
119  throw std::underflow_error("MemArray::Iterator::get_curr(): has not current");
120 
121  if (pos >= num_items)
122  throw std::overflow_error("MemArray::Iterator::get_curr(): has not current");
123 
124  return ptr[idx];
125  }
126 
127  void next_ne() noexcept
128  {
129  if (++idx == dim)
130  idx = 0;
131  ++pos;
132  }
133 
134  void next()
135  {
136  if (num_items == 0 or pos >= num_items)
137  throw std::overflow_error("MemArray::Iterator::next(): has not current");
138  next_ne();
139  }
140 
141  void prev_ne() noexcept
142  {
143  if (--idx < 0)
144  idx = dim - 1;
145  --pos;
146  }
147 
148  void prev()
149  {
150  if (num_items == 0 or pos < 0)
151  throw std::underflow_error("MemArray::Iterator::prev(): has not current");
152  prev_ne();
153  }
154 
155  void reset() noexcept
156  {
157  idx = first;
158  pos = 0;
159  }
160 
161  void reset_first() noexcept { reset(); }
162 
163  void reset_last() noexcept
164  {
165  idx = last;
166  pos = num_items - 1;
167  }
168 
169  void end() noexcept
170  {
171  put_itor_at_the_end(*this);
172  }
173 };
174 
175  template <typename T> inline
176 Array_Iterator<T> get_array_it(const T * array, size_t n)
177 {
178  return Array_Iterator<T>(array, n, n);
179 }
180 
181  template <typename T> inline
182 Array_Container<T> get_array_it(const T * array, size_t n)
183 {
184  return Array_Iterator<T>(array, n);
185 }
186 
187 
188 template <typename T>
189 class Array_Container
190  : public GenericTraverse<Array_Container<T>>,
191  public LocateFunctions<Array_Container<T>, T>,
192  public FunctionalMethods<Array_Container<T>, T>,
193  public GenericKeys<Array_Container<T>, T>,
194  public EqualToMethod<Array_Container<T>>,
195  public StlAlephIterator<Array_Container<T>>
196 {
197  T * base = nullptr;
198  const size_t n = 0;
199 
200 public:
201 
202  T * get_base() const noexcept { return base; }
203 
204  Array_Container(T * base_ptr, const size_t d) : base(base_ptr), n(d)
205  {
206  // empty
207  }
208 
209  bool is_empty() const noexcept { return n == 0; }
210 
211  size_t size() const noexcept { return n; }
212 
213  size_t capacity() const noexcept { return n; }
214 
215  T & get_first() const
216  {
217  if (n == 0)
218  throw std::underflow_error("Array_Container::get_first(): n == 0");
219  return base[0];
220  }
221 
222  T & get_last() const
223  {
224  if (n == 0)
225  throw std::underflow_error("Array_Container::get_first(): n == 0");
226  return base[n - 1];
227  }
228 
229  struct Iterator : public Array_Iterator<T>
230  {
231  Iterator(const Array_Container & c) : Array_Iterator<T>(c.base, c.n, c.n) {}
232  };
233 
234  Iterator get_it() const { return Iterator(*this); }
235 };
236 
237 } // end namespace Aleph
238 
239 
240 
241 
242 
243 # endif // ARRAY_IT_H
Definition: htlist.H:450
Definition: htlist.H:1290
Definition: htlist.H:133
Definition: array_it.H:45
Definition: htlist.H:45
Array_Iterator(NoExceptionCtor, T *p, size_t sz, size_t n, long f, long l)
Definition: array_it.H:96
Definition: ah-comb.H:35
Definition: htlist.H:1323

Leandro Rabindranath León