Repository navigation
Expand file tree
/
Copy pathxor_list_iter.hpp
More file actions
88 lines (69 loc) · 2.34 KB
/
Copy pathxor_list_iter.hpp
File metadata and controls
88 lines (69 loc) · 2.34 KB
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
#ifndef _XOR_LIST_ITER_HPP_
#define _XOR_LIST_ITER_HPP_
#include "xor_list_node.hpp"
#include <iterator>
#include "get_xor.hpp"
//TODO: make iterator STL-compatible
template<typename T>
class xor_base_iterator {
public:
using iterator_category = std::bidirectional_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = T;
using pointer = T*;
using reference = T&;
using node = xor_list_node<std::remove_cv_t<T>>;
node *prev,*curr;
constexpr xor_base_iterator() : prev(nullptr), curr(nullptr) {}
constexpr xor_base_iterator(node* prev,node *curr) : prev(prev),curr(curr) {}
constexpr xor_base_iterator(const xor_base_iterator &other) : prev(other.prev),curr(other.curr) {}
constexpr operator bool() const noexcept { return curr != nullptr; }
constexpr friend bool operator==(const xor_base_iterator &a, const xor_base_iterator &b) noexcept {
return a.curr==b.curr;
}
constexpr reference operator*() {
return (curr->value);
}
constexpr pointer operator->() {
return &curr->value;
}
constexpr xor_base_iterator& operator++() {
node *tmp = curr;
curr = reinterpret_cast<node*>(detail::get_xor(curr->both,prev));
prev = tmp;
return *this;
}
constexpr xor_base_iterator& operator--() {
node *tmp = prev;
prev = reinterpret_cast<node*>(detail::get_xor(prev->both, curr));
curr = tmp;
return *this;
}
constexpr xor_base_iterator operator++(int) {
auto res = *this;
++*this;
return res;
}
constexpr xor_base_iterator operator--(int) {
auto res = *this;
--*this;
return res;
}
};
template<typename T>
class xor_list_iterator : public xor_base_iterator<T> {
public:
using typename xor_base_iterator<T>::node;
constexpr xor_list_iterator() : xor_base_iterator<T>() {}
constexpr xor_list_iterator(node* prev,node *curr) : xor_base_iterator<T>(prev,curr) {}
constexpr xor_list_iterator(const xor_list_iterator &other) : xor_base_iterator<T>(other) {}
};
template<typename T>
class xor_list_const_iterator : public xor_base_iterator<const T> {
public:
using typename xor_base_iterator<const T>::node;
constexpr xor_list_const_iterator() : xor_base_iterator<const T>() {}
constexpr xor_list_const_iterator(node* prev,node *curr) : xor_base_iterator<const T>(prev,curr) {}
constexpr xor_list_const_iterator(const xor_base_iterator<T> &other) : xor_base_iterator<const T>(other.prev,other.curr) {}
};
#endif