rcolyer.net
RC Lib  Version 202403231100
Iter.h
Go to the documentation of this file.
1 /////////////////////////////////////////////////////////////////////
2 //
3 // RC Library, (c) 2011-2014, Ryan A. Colyer
4 // Distributed under the Boost Software License, v1.0. (LICENSE.txt)
5 //
6 /// \file Iter.h
7 /// Provides a bounds-checked iterator that knows when its target
8 /// was deleted.
9 /////////////////////////////////////////////////////////////////////
10 
11 #ifndef RC_ITER_H
12 #define RC_ITER_H
13 
14 #include "Types.h"
15 #include "Macros.h"
16 #include "RevPtr.h"
17 #include <iterator>
18 
19 
20 namespace RC {
21  /// A bounds-checked iterator for random-access containers that knows
22  /// when its target was deleted.
23  /** See Data1D.h for a usage example.
24  */
25  template <class Cont, class T>
26  class RAIter
27  : public std::iterator<std::random_access_iterator_tag,T,size_t> {
28  public:
29 
30  /// Default constructor to nothing, by default invalid for use.
31  inline RAIter() {
32  pos = 0;
33  c = NULL;
34  }
35 
36  /// Creates an iterator to a container starting at index pos.
37  /** When the iterator is dereferenced, it corresponds to
38  * (*container)[pos]. Note that the RevPtr for container should be set
39  * to AutoRevoke when the container is deleted.
40  * @param container A RevPtr to the container.
41  * @param pos The starting index for this iterator.
42  */
43  inline RAIter(RevPtr<Cont> container, size_t pos)
44  : pos (pos),
45  c (container) {
46  }
47 
48 
49  /// True if the iterator still points to an existing container and
50  /// remains in bounds.
51  inline bool IsValid() const {
52  if ( c.IsSet() && (pos < c->size()) ) {
53  return true;
54  }
55  else {
56  return false;
57  }
58  }
59 
60 
61  /// Return the index of the container which this iterator corresponds to.
62  inline size_t GetIndex() const { return pos; }
63  /// Directly access the RevPtr to the container.
64  inline RevPtr<Cont> Raw() { return c; }
65 
66 
67  /// Dereference the iterator, returning a reference to the corresponding
68  /// element.
69  inline T& operator* () { return (*c)[pos]; }
70  /// Const version of operator*
71  inline const T& operator* () const { return (*c)[pos]; }
72  /// Provides access to members of the element this iterator points to.
73  inline const T* operator-> () const { return &(*c)[pos]; }
74 
75  /// Provides access to the element at offset x from the current index.
76  inline T& operator[] (size_t x) { return (*c)[pos+x]; }
77  /// Const version of operator[]
78  inline const T& operator[] (size_t x) const { return (*c)[pos+x]; }
79 
80  /// Point to the next higher numbered indexed element.
81  inline RAIter<Cont,T>& operator++ () { pos++; return (*this); }
82  /// Point to the next lower numbered indexed element.
83  inline RAIter<Cont,T>& operator-- () { pos--; return (*this); }
84  /// Increase the index by x.
85  inline RAIter<Cont,T>& operator+= (size_t x) { pos += x; return (*this); }
86  /// Decrease the index by x.
87  inline RAIter<Cont,T>& operator-= (size_t x) { pos -= x; return (*this); }
88 
89  /// Support for iter++, but for efficiency, use ++iter.
91  RAIter<Cont,T> tmp = *this;
92  ++*this;
93  return tmp;
94  }
95 
96  /// Support for iter--, but for efficiency, use --iter.
98  RAIter<Cont,T> tmp = *this;
99  --*this;
100  return tmp;
101  }
102 
103  /// Returns a new iterator with an offset incremented by x.
104  inline RAIter<Cont,T> operator+ (size_t x) const {
105  RAIter<Cont,T> newiter(*this);
106  newiter += x;
107  return newiter;
108  }
109 
110  /// Returns a new iterator with an offset decremented by x.
111  inline RAIter<Cont,T> operator- (size_t x) const {
112  RAIter<Cont,T> newiter(*this);
113  newiter -= x;
114  return newiter;
115  }
116 
117  /// Returns the distance between two iterators.
118  inline size_t operator- (const RAIter<Cont,T>& first) const {
119  return (GetIndex() - first.GetIndex());
120  }
121 
122 
123  /// True if the two iterators have the same index.
124  inline bool operator== (const RAIter<Cont,T>& other) const {
125  return (GetIndex() == other.GetIndex());
126  }
127 
128  /// True if this iterator is less.
129  inline bool operator< (const RAIter<Cont,T>& other) const {
130  return (GetIndex() < other.GetIndex());
131  }
132 
134 
135  private:
136 
137  size_t pos;
138  RevPtr<Cont> c;
139  };
140 
141 
142  /// Returns a new iterator with an offset incremented by x.
143  template <class Cont, class T>
144  inline RAIter<Cont,T> operator+ (size_t x, const RAIter<Cont,T>& iter) {
145  RAIter<Cont,T> newiter = iter;
146  newiter += x;
147  return newiter;
148  }
149 
150 }
151 
152 #endif // RC_ITER_H
153 
Provides a set of convenience macros for metaprogramming and debugging.
#define RC_DEFAULT_COMPARISON()
Given == and <, generates !=, >, <=, and >=.
Definition: Macros.h:246
A reference counting pointer that revokes (NULLs) all copies when one set to AutoRevoke(true) leaves ...
Provides typedefs and routines for working with primitives.
A bounds-checked iterator for random-access containers that knows when its target was deleted.
Definition: Iter.h:27
RAIter< Cont, T > operator-(size_t x) const
Returns a new iterator with an offset decremented by x.
Definition: Iter.h:111
RAIter()
Default constructor to nothing, by default invalid for use.
Definition: Iter.h:31
RAIter< Cont, T > operator+(size_t x) const
Returns a new iterator with an offset incremented by x.
Definition: Iter.h:104
RAIter< Cont, T > & operator-=(size_t x)
Decrease the index by x.
Definition: Iter.h:87
RevPtr< Cont > Raw()
Directly access the RevPtr to the container.
Definition: Iter.h:64
bool operator==(const RAIter< Cont, T > &other) const
True if the two iterators have the same index.
Definition: Iter.h:124
const T * operator->() const
Provides access to members of the element this iterator points to.
Definition: Iter.h:73
RAIter< Cont, T > & operator+=(size_t x)
Increase the index by x.
Definition: Iter.h:85
RAIter< Cont, T > & operator--()
Point to the next lower numbered indexed element.
Definition: Iter.h:83
bool IsValid() const
True if the iterator still points to an existing container and remains in bounds.
Definition: Iter.h:51
bool operator<(const RAIter< Cont, T > &other) const
True if this iterator is less.
Definition: Iter.h:129
RAIter(RevPtr< Cont > container, size_t pos)
Creates an iterator to a container starting at index pos.
Definition: Iter.h:43
RAIter< Cont, T > & operator++()
Point to the next higher numbered indexed element.
Definition: Iter.h:81
T & operator*()
Dereference the iterator, returning a reference to the corresponding element.
Definition: Iter.h:69
T & operator[](size_t x)
Provides access to the element at offset x from the current index.
Definition: Iter.h:76
size_t GetIndex() const
Return the index of the container which this iterator corresponds to.
Definition: Iter.h:62
Definition: APtr.h:25
RAIter< Cont, T > operator+(size_t x, const RAIter< Cont, T > &iter)
Returns a new iterator with an offset incremented by x.
Definition: Iter.h:144
email address
— (c) 2015