-
Notifications
You must be signed in to change notification settings - Fork 1
/
iterator.hpp
77 lines (72 loc) · 1.63 KB
/
iterator.hpp
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
#ifndef ITERATOR_H
#define ITERATOR_H
#include <iterator>
template<typename T, typename pred_t>
class segment_tree;
namespace seg_tree{
template<typename T, typename pred_t>
class iterator: public std::iterator<std::bidirectional_iterator_tag, T>
{
private:
const segment_tree<T, pred_t> *tree_;
int index_;
public:
iterator(const segment_tree<T, pred_t> &tree_, int index):tree_(&tree_), index_(index)
{ }
iterator(const iterator& rhs):tree_(rhs.tree_), index_(rhs.index_)
{ }
iterator& operator=(const iterator& rhs)
{
tree_ = rhs.tree_;
index_ = rhs.index_;
return *this;
}
iterator& operator++()
{
++index_;
return *this;
}
iterator operator++(int)
{
iterator temp(*this);
++(*this);
return temp;
}
iterator& operator--()
{
--index_;
return *this;
}
iterator operator--(int)
{
iterator temp(*this);
--(*this);
return temp;
}
T operator*()
{
return tree_->get(index_);
}
bool operator==(const iterator &rhs)
{
return index_ == rhs.index_;
}
bool operator!=(const iterator &rhs)
{
return !(*this == rhs);
}
int get_index()
{
return index_;
}
int operator-(const iterator &rhs)
{
return index_ - rhs.index_;
}
int operator+(const iterator &rhs)
{
return index_ + rhs.index_;
}
};
}
#endif