libUTL++
ArrayIt.h
1 #pragma once
2 
4 
5 #include <libutl/RandIt.h>
6 
8 
9 UTL_NS_BEGIN;
10 
12 
20 
22 class ArrayIt : public RandIt
23 {
25 
26 public:
32  ArrayIt(const Array* array, size_t offset)
33  : _array(const_cast<Array*>(array))
34  , _offset(offset)
35  {
36  IFDEBUG(FwdIt::setOwner(_array));
37  }
38 
43  virtual int compare(const Object& rhs) const;
44 
48  virtual void copy(const Object& rhs);
49 
54  virtual void forward(size_t dist = 1);
55 
56  virtual Object*
57  get() const
58  {
59  if (_offset >= _array->totalSize()) return nullptr;
60  return _array->get(_offset);
61  }
62 
68  virtual Object*
69  get(size_t offset) const
70  {
71  ASSERTD(!_array->hasHole());
72  return _array->get(offset);
73  }
74 
76  Array*
77  getArray() const
78  {
79  return _array;
80  }
81 
82  virtual size_t offset() const;
83 
84  virtual void reverse(size_t dist = 1);
85 
86  virtual void
87  seek(size_t offset)
88  {
89  ASSERTD(!_array->hasHole());
90  _offset = offset;
91  }
92 
93  virtual void set(const Object* object);
94 
95  virtual void
96  set(size_t offset, const Object* object)
97  {
98  ASSERTD(!isConst());
99  _array->set(offset, object);
100  }
101 
102  virtual size_t
103  size() const
104  {
105  return _array->size();
106  }
107 
108  virtual size_t subtract(const RandIt& it) const;
109 
115  ArrayIt operator+(size_t dist) const
116  {
117  ArrayIt res = *this;
118  res.forward(dist);
119  return res;
120  }
121 
127  ArrayIt operator-(size_t dist) const
128  {
129  ArrayIt res = *this;
130  res.reverse(dist);
131  return res;
132  }
133 
135  size_t operator-(const ArrayIt& it) const
136  {
137  return subtract(it);
138  }
139 
140  ArrayIt& operator++()
141  {
142  forward();
143  return *this;
144  }
145 
146  ArrayIt operator++(int)
147  {
148  ArrayIt res = *this;
149  forward();
150  return res;
151  }
152 
153  ArrayIt& operator--()
154  {
155  reverse();
156  return *this;
157  }
158 
159  ArrayIt operator--(int)
160  {
161  ArrayIt res = *this;
162  reverse();
163  return res;
164  }
165 
166 private:
167  void
168  init()
169  {
170  _array = nullptr;
171  _offset = size_t_max;
172  }
173  void
174  deInit()
175  {
176  }
177 
178 private:
179  Array* _array;
180  size_t _offset;
181 };
182 
184 
185 RandIt*
186 Array::beginNew() const
187 {
188  auto it = new base_iterator(this, 0);
189  IFDEBUG(it->setConst(true));
190  return it;
191 }
192 
194 
195 RandIt*
196 Array::beginNew()
197 {
198  return new base_iterator(this, 0);
199 }
200 
202 
203 RandIt*
204 Array::endNew() const
205 {
206  auto it = new base_iterator(this, size_t_max);
207  IFDEBUG(it->setConst(true));
208  return it;
209 }
210 
212 
213 RandIt*
214 Array::endNew()
215 {
216  return new base_iterator(this, size_t_max);
217 }
218 
220 
221 UTL_NS_END;
virtual void seek(size_t offset)
Seek to the given offset.
Definition: ArrayIt.h:87
void deInit()
De-initialize UTL++.
Random-access Array iterator.
Definition: ArrayIt.h:22
Array * getArray() const
Get the associated array.
Definition: ArrayIt.h:77
ArrayIt(const Array *array, size_t offset)
Constructor.
Definition: ArrayIt.h:32
#define UTL_CLASS_DECL(DC, BC)
Declaration of standard UTL++ functionality for a non-template class.
Definition: macros.h:688
Subtraction function.
void reverse(const BidIt &begin, const BidIt &end)
Reverse a sequence.
Random-access iterator abstraction.
Definition: RandIt.h:25
size_t operator-(const ArrayIt &it) const
Return the distance from the given iterator to self.
Definition: ArrayIt.h:135
virtual size_t size() const
Return the current size of the sequence.
Definition: ArrayIt.h:103
const size_t size_t_max
Maximum size_t value.
#define IFDEBUG(x)
Do x in DEBUG mode only.
SortedCollection that stores objects in an array.
Definition: Array.h:116
void copy(T *dest, const T *src, size_t len)
Copy one array of objects to another.
Definition: util_inl.h:690
ArrayIt operator+(size_t dist) const
Return an ArrayIt equivalent to self, but moved forward the given number of objects.
Definition: ArrayIt.h:115
virtual void forward(size_t dist=1)
Move forward the given number of objects.
ArrayIt operator-(size_t dist) const
Return copy of self moved backward the given number of objects.
Definition: ArrayIt.h:127
Root of UTL++ class hierarchy.
Definition: Object.h:52
int compare(bool lhs, bool rhs)
Compare two boolean values.
void init()
Initialize UTL++.
virtual void reverse(size_t dist=1)
Move backward the given number of objects.
#define ASSERTD
Do an assertion in DEBUG mode only.