-
Notifications
You must be signed in to change notification settings - Fork 8
/
linked_list.hpp
113 lines (93 loc) · 1.61 KB
/
linked_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
#pragma once
#include <cstddef>
#include <utility>
/**
* We define a common linked-list interface, to make writing benchmarks easier:
* this implementation doesn't really do much other than delegate to the
* underlying implementation
*
* We try to use an API similar to a subset of the std::list API, with a
* few non-standard functions which make more sense in a multi-threaded
* environment.
*/
template <typename T, typename Impl>
class linked_list {
public:
typedef T value_type;
typedef T & reference;
typedef const T & const_reference;
typedef typename Impl::iterator iterator;
linked_list() : impl_() {}
inline bool
empty() const
{
return impl_.size() == 0;
}
inline size_t
size() const
{
return impl_.size();
}
inline reference
front()
{
return impl_.front();
}
inline const_reference
front() const
{
return impl_.front();
}
inline reference
back()
{
return impl_.back();
}
inline const_reference
back() const
{
return impl_.back();
}
inline void
pop_front()
{
impl_.pop_front();
}
inline void
push_back(const value_type &val)
{
impl_.push_back(val);
}
inline void
remove(const value_type &val)
{
impl_.remove(val);
}
inline iterator
begin()
{
return impl_.begin();
}
inline iterator
end()
{
return impl_.end();
}
inline void
clear()
{
for (;;) {
auto ret = try_pop_front();
if (!ret.second)
break;
}
}
// begin non-standard API
std::pair<bool, T>
try_pop_front()
{
return impl_.try_pop_front();
}
private:
Impl impl_;
};