-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayListIterator.h
96 lines (80 loc) · 1.62 KB
/
ArrayListIterator.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
#ifndef ARRAYLISTITERATOR_H_
#define ARRAYLISTITERATOR_H_
#include "ConcurrentModificationException.h"
template<typename T>
class ArrayListIterator
{
private:
T* array;
int size;
int curIndex;
unsigned long initialMods;
unsigned long* totalMods;
public:
ArrayListIterator(T*, int, unsigned long*, bool = false);
T& operator*();
void operator++();
bool operator!=(const ArrayListIterator<T>& rhs);
private:
bool isComplete();
void checkForModification();
};
template<typename T>
ArrayListIterator<T>::ArrayListIterator(T* array, int size, unsigned long* modifications, bool complete)
{
this->array = array;
this->size = size;
this->initialMods = *modifications;
this->totalMods = modifications;
if (complete)
{
this->curIndex = size;
}
else
{
this->curIndex = 0;
}
}
template<typename T>
T& ArrayListIterator<T>::operator*()
{
checkForModification();
return array[curIndex];
}
template<typename T>
void ArrayListIterator<T>::operator++()
{
if (isComplete())
{
return;
}
checkForModification();
curIndex++;
}
template<typename T>
bool ArrayListIterator<T>::operator!=(const ArrayListIterator<T>& rhs)
{
if (rhs.array != array)
{
return true;
}
if (rhs.size != size)
{
return true;
}
return rhs.curIndex != curIndex;
}
template<typename T>
bool ArrayListIterator<T>::isComplete()
{
return curIndex >= size;
}
template<typename T>
void ArrayListIterator<T>::checkForModification()
{
if (initialMods != *totalMods)
{
throw ConcurrentModificationException();
}
}
#endif /* ARRAYLISTITERATOR_H_ */