-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterator.h
executable file
·124 lines (111 loc) · 3.87 KB
/
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
//===-- stevemac::iterator.h --------------------------------------*- C -*-===//
//
// This file is distributed under the GNU General Public License, version 2
// (GPLv2). See https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html
// Author: Stephen E. MacKenzie
//===----------------------------------------------------------------------===//
#pragma once
#include <iterator>
namespace stevemac {
///===----------------------------------------------------------------------===//
///
/// \class stevemac::iterator
/// \brief Implementation started in fall of 2014. It is is derived from:
/// std::iterator<std::random_access_iterator_tag>. It has only been tested
/// with stevemac::vector.
///
//===----------------------------------------------------------------------===//
/// Class Template stevemac::iterator
///
//===----------------------------------------------------------------------===//
template <typename Container>
class vector_iterator : public std::iterator<std::random_access_iterator_tag,
typename Container::value_type> {
public:
using value_type = typename Container::value_type;
using pointer = typename Container::pointer;
using reference = typename Container::reference;
using difference_type = typename Container::size_type;
protected:
/// pointee is owned by the Container
///
pointer pointee;
//===----------------------------------------------------------------------===//
/// construct/destroy: The constructor is used for conversion
/// The pointee member is owned and managed by the Container which is why
/// iterator does not have any destruction duties
//===----------------------------------------------------------------------===//
public:
vector_iterator() {}
/// explicit constructor used for converting to the underlying pointer type
/// provided by the Container: Container::pointer.
explicit vector_iterator(const pointer ptr) : pointee(ptr) {}
/// the Container owns the only resource: pointee; hence no duties here.
~vector_iterator() {}
//===----------------------------------------------------------------------===//
/// Operator overloads.
///
//===----------------------------------------------------------------------===//
reference operator*() { return *pointee; }
pointer operator->() { return pointee; }
vector_iterator &operator++() {
++pointee;
return *this;
}
///
vector_iterator operator++(int) {
vector_iterator tmp = *this;
++pointee;
return tmp;
}
///
vector_iterator &operator--() {
--pointee;
return *this;
}
vector_iterator operator--(int) {
vector_iterator tmp = *this;
--pointee;
return tmp;
}
difference_type operator-(vector_iterator &other) {
return pointee - other.pointee;
}
vector_iterator operator+(difference_type n) {
return vector_iterator(pointee + n);
}
vector_iterator operator-(difference_type n) {
return vector_iterator(pointee - n);
}
vector_iterator &operator+=(difference_type n) {
pointee += n;
return *this;
}
vector_iterator &operator-=(difference_type n) {
pointee -= n;
return *this;
}
//===----------------------------------------------------------------------===//
/// logical operators
///
//===----------------------------------------------------------------------===//
bool operator==(const vector_iterator &other) const {
return pointee == other.pointee;
}
bool operator!=(const vector_iterator &other) const {
return pointee != other.pointee;
}
bool operator<(const vector_iterator &other) {
return pointee < other.pointee;
}
bool operator>(const vector_iterator &other) {
return pointee > other.pointee;
}
bool operator<=(const vector_iterator &other) {
return pointee <= other.pointee;
}
bool operator>=(const vector_iterator &other) {
return pointee >= other.pointee;
}
};
} // end stevemac