forked from msinilo/rdestl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.h
109 lines (91 loc) · 2.72 KB
/
set.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
#ifndef RDESTL_SET_H
#define RDESTL_SET_H
#include "rb_tree.h"
#include "iterator.h"
namespace rde
{
template<typename T, class TAllocator = rde::allocator>
class set: private rb_tree<T, TAllocator>
{
typedef rb_tree<T, TAllocator> Base;
typedef typename Base::node BaseNode;
template<typename TNodePtr, typename TPtr, typename TRef>
class node_iterator
{
public:
typedef forward_iterator_tag iterator_category;
explicit node_iterator(TNodePtr node, set* set_)
: m_node(node),
m_set(set_)
{
}
template<typename UNodePtr, typename UPtr, typename URef>
node_iterator(const node_iterator<UNodePtr, UPtr, URef>& rhs)
: m_node(rhs.node()),
m_set(rhs.get_set())
{
}
TRef operator*() const { RDE_ASSERT(m_node != 0); return m_node->value.key; }
TPtr operator->() const { return &m_node->value.key; }
TNodePtr node() const { return m_node; }
node_iterator& operator++()
{
RDE_ASSERT(m_node != 0);
TNodePtr next = m_node->next;
if (next == 0)
next = find_next_node(m_node);
m_node = next;
return *this;
}
node_iterator operator++(int)
{
node_iterator copy(*this);
++(*this);
return copy;
}
bool operator==(const node_iterator& rhs) const { return rhs.m_node == m_node && m_set == rhs.m_set; }
bool operator!=(const node_iterator& rhs) const { return !(rhs == *this); }
set* get_set() const { return m_set; }
private:
TNodePtr find_next_node(TNodePtr node) const
{
return 0;
}
TNodePtr m_node;
set* m_set;
};
public:
typedef T value_type;
typedef node_iterator<BaseNode*, value_type*, value_type&> iterator;
typedef node_iterator<BaseNode*, const value_type*, const value_type&> const_iterator;
typedef TAllocator allocator_type;
explicit set(const allocator_type& allocator = allocator_type())
: Base(allocator) {}
iterator begin() { return iterator(Base::get_begin_node(), this); }
const_iterator begin() const { return const_iterator(Base::get_begin_node(), this); }
iterator end() { return iterator(0, this); }
const_iterator end() const { return iterator(0, this); }
iterator find(const value_type& v)
{
return iterator(Base::find_node(v), this);
}
const_iterator find(const value_type& v) const
{
return const_iterator(Base::find_node(v), this);
}
void erase(const value_type& v)
{
erase(find(v));
}
void erase(iterator it)
{
RDE_ASSERT(it != end());
Base::erase(it.node());
}
using Base::insert;
using Base::empty;
using Base::size;
};
} // namespace rde
//-----------------------------------------------------------------------------
#endif // #ifndef RDESTL_SET_H