forked from gfrd/egfrd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorted_list.hpp
182 lines (155 loc) · 4.87 KB
/
sorted_list.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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#ifndef _SORTED_LIST
#define _SORTED_LIST
#include <algorithm>
#include <functional>
#include <boost/range/size.hpp>
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
#include <boost/range/rbegin.hpp>
#include <boost/range/rend.hpp>
#include <boost/range/value_type.hpp>
#include <boost/range/size_type.hpp>
#include <boost/range/difference_type.hpp>
#include <boost/range/iterator.hpp>
#include <boost/range/const_iterator.hpp>
#include <boost/range/reverse_iterator.hpp>
#include <boost/range/const_reverse_iterator.hpp>
#include "utils/fun_composition.hpp"
template<typename Tcntnr_, typename TweakOrdering_ = std::less<typename boost::range_value<Tcntnr_>::type> >
class sorted_list
{
public:
typedef Tcntnr_ container_type;
typedef typename boost::range_value<container_type>::type value_type;
typedef typename boost::range_size<container_type>::type size_type;
typedef typename boost::range_difference<container_type>::type difference_type;
typedef typename boost::range_iterator<container_type>::type iterator;
typedef typename boost::range_const_iterator<container_type>::type const_iterator;
typedef typename boost::range_reverse_iterator<container_type>::type reverse_iterator;
typedef typename boost::range_const_reverse_iterator<container_type>::type const_reverse_iterator;
typedef value_type* pointer;
typedef value_type const* const_pointer;
typedef value_type& reference;
typedef value_type const& const_reference;
size_type size() const
{
return boost::size(cntnr_);
}
iterator begin()
{
return boost::begin(cntnr_);
}
const_iterator begin() const
{
return boost::begin(cntnr_);
}
iterator end()
{
return boost::end(cntnr_);
}
const_iterator end() const
{
return boost::end(cntnr_);
}
reverse_iterator rbegin()
{
return boost::rbegin(cntnr_);
}
const_reverse_iterator rbegin() const
{
return boost::rbegin(cntnr_);
}
reverse_iterator rend()
{
return boost::rend(cntnr_);
}
const_reverse_iterator rend() const
{
return boost::end(cntnr_);
}
void push(value_type const& v)
{
iterator i(std::upper_bound(cntnr_.begin(), cntnr_.end(), v,
static_cast<TweakOrdering_ const&>(ord_)));
cntnr_.insert(i, v);
}
bool push_no_duplicate(value_type const& v)
{
iterator i(std::upper_bound(cntnr_.begin(), cntnr_.end(), v,
static_cast<TweakOrdering_ const&>(ord_)));
if (i != cntnr_.begin())
{
if (*--i == v)
return false;
++i;
}
cntnr_.insert(i, v);
return true;
}
bool update(value_type const& v)
{
iterator i(std::upper_bound(cntnr_.begin(), cntnr_.end(), v,
static_cast<TweakOrdering_ const&>(ord_)));
if (i != cntnr_.begin())
{
if (*--i == v)
{
value_type _v(v);
std::swap(*i, _v);
return false;
}
++i;
}
cntnr_.insert(i, v);
return true;
}
void erase(iterator const& i)
{
cntnr_.erase(i);
}
iterator find(value_type const& v)
{
iterator i(std::lower_bound(begin(), end(), v,
static_cast<TweakOrdering_ const&>(ord_)));
return i != end() && *i == v ? i: end();
}
const_iterator find(value_type const& v) const
{
const_iterator i(std::lower_bound(begin(), end(), v,
static_cast<TweakOrdering_ const&>(ord_)));
return i != end() && *i == v ? i: end();
}
reverse_iterator rfind(value_type const& v)
{
reverse_iterator i(std::upper_bound(rbegin(), rend(), v,
compose_binary(std::logical_not<bool>(),
static_cast<TweakOrdering_ const&>(ord_))));
return i != rend() && *i == v ? i: rend();
}
const_reverse_iterator rfind(value_type const& v) const
{
const_reverse_iterator i(std::upper_bound(rbegin(), rend(), v,
compose_binary(std::logical_not<bool>(),
static_cast<TweakOrdering_ const&>(ord_))));
return i != rend() && *i == v ? i: rend();
}
size_type erase(value_type const& v)
{
iterator e(cntnr_.end());
std::pair<iterator, iterator> i(std::equal_range(cntnr_.begin(), e, v,
static_cast<TweakOrdering_ const&>(ord_)));
const size_type retval(i.second - i.first);
cntnr_.erase(i.first, i.second);
return retval;
}
void clear()
{
cntnr_.clear();
}
sorted_list(typename boost::call_traits<TweakOrdering_>::param_type ord): ord_(ord) {}
sorted_list(): ord_() {}
private:
container_type cntnr_;
TweakOrdering_ ord_;
};
#endif /* SORTED_LIST */