forked from udacity/CppND-Garbage-Collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gc_iterator.h
139 lines (135 loc) · 3.08 KB
/
gc_iterator.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <cstddef>
// Exception thrown when an attempt is made to
// use an Iter that exceeds the range of the
// underlying object.
//
class OutOfRangeExc
{
// Add functionality if needed by your application.
};
// An iterator-like class for cycling through arrays
// that are pointed to by GCPtrs. Iter pointers
// ** do not ** participate in or affect garbage
// collection. Thus, an Iter pointing to
// some object does not prevent that object
// from being recycled.
//
template <class T>
class Iter
{
T *ptr;
// current pointer value
T *end;
// points to element one past end
T *begin; // points to start of allocated array
unsigned length; // length of sequence
public:
Iter()
{
ptr = end = begin = NULL;
length = 0;
}
Iter(T *p, T *first, T *last)
{
ptr = p;
end = last;
begin = first;
length = last - first;
}
// Return length of sequence to which this
// Iter points.
unsigned size() { return length; }
// Return value pointed to by ptr.
// Do not allow out-of-bounds access.
T &operator*()
{
if ((ptr >= end) || (ptr < begin))
throw OutOfRangeExc();
return *ptr;
}
// Return address contained in ptr.
// Do not allow out-of-bounds access.
T *operator->()
{
if ((ptr >= end) || (ptr < begin))
throw OutOfRangeExc();
return ptr;
}
// Prefix ++.
Iter operator++()
{
ptr++;
return *this;
}
// Prefix --.
Iter operator--()
{
ptr--;
return *this;
}
// Postfix ++.
Iter operator++(int notused)
{
T *tmp = ptr;
ptr++;
return Iter<T>(tmp, begin, end);
}
// Postfix --.
Iter operator--(int notused)
{
T *tmp = ptr;
ptr--;
return Iter<T>(tmp, begin, end);
}
// Return a reference to the object at the
// specified index. Do not allow out-of-bounds
// access.
T &operator[](int i)
{
if ((i < 0) || (i >= (end - begin)))
throw OutOfRangeExc();
return ptr[i];
}
// Define the relational operators.
bool operator==(Iter op2)
{
return ptr == op2.ptr;
}
bool operator!=(Iter op2)
{
return ptr != op2.ptr;
}
bool operator<(Iter op2)
{
return ptr < op2.ptr;
}
bool operator<=(Iter op2)
{
return ptr <= op2.ptr;
}
bool operator>(Iter op2)
{
return ptr > op2.ptr;
}
bool operator>=(Iter op2)
{
return ptr >= op2.ptr;
}
// Subtract an integer from an Iter.
Iter operator-(int n)
{
ptr -= n;
return *this;
}
// Add an integer to an Iter.
Iter operator+(int n)
{
ptr += n;
return *this;
}
// Return number of elements between two Iters.
int operator-(Iter<T> &itr2)
{
return ptr - itr2.ptr;
}
};