-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintrusive_list.rs
144 lines (122 loc) · 2.96 KB
/
intrusive_list.rs
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
use std::managed;
struct IntrusiveNode<Node> {
next: Option<@mut Node>,
prev: Option<@mut Node>,
}
struct IntrusiveList<Node> {
first: Option<@mut Node>,
last: Option<@mut Node>,
f: @fn<'r>(&'r mut Node) -> &'r mut IntrusiveNode<Node>
}
impl<Node> IntrusiveNode<Node> {
pub fn new() -> IntrusiveNode<Node> {
IntrusiveNode { next: None, prev: None }
}
}
impl<Node> IntrusiveList<Node> {
pub fn new(f: @fn<'r>(&'r mut Node) -> &'r mut IntrusiveNode<Node>) -> IntrusiveList<Node> {
IntrusiveList { first: None, last: None, f: f }
}
pub fn front(&mut self) -> Option<@mut Node> {
self.first
}
pub fn last(&mut self) -> Option<@mut Node> {
self.last
}
pub fn push_front(&mut self, node: @mut Node) {
self.insert_before(node, self.last);
}
pub fn push_back(&mut self, node: @mut Node) {
self.insert_after(node, self.first);
}
pub fn remove(&mut self, node: @mut Node) {
let ptrs = (self.f)(node);
match self.first {
Some(first) => {
if managed::mut_ptr_eq(first, node) {
self.first = ptrs.next;
}
},
None => {}
}
match self.last {
Some(last) => {
if managed::mut_ptr_eq(last, node) {
self.last = ptrs.prev;
}
},
None => {}
}
match ptrs.next {
Some(next) => {
(self.f)(next).prev = ptrs.prev;
ptrs.next = None;
},
None => {}
}
match ptrs.prev {
Some(prev) => {
(self.f)(prev).next = ptrs.prev;
ptrs.prev = None;
},
None => {}
}
}
pub fn insert_after(&mut self, node: @mut Node, prev: Option<@mut Node>) {
let next = self.next(prev);
self.insert(node, prev, next);
}
pub fn insert_before(&mut self, node: @mut Node, next: Option<@mut Node>) {
let prev = self.prev(next);
self.insert(node, prev, next);
}
pub fn pop_back(&mut self) -> Option<@mut Node> {
let last = self.last;
match last {
None => {},
Some(l) => { self.remove(l); }
};
last
}
pub fn pop_front(&mut self) -> Option<@mut Node> {
let first = self.first;
match first {
None => {},
Some(f) => { self.remove(f); }
};
first
}
fn next(&mut self, node: Option<@mut Node>) -> Option<@mut Node> {
match node {
Some(n) => (self.f)(n).next,
None => None
}
}
fn prev(&mut self, node: Option<@mut Node>) -> Option<@mut Node> {
match node {
Some(n) => (self.f)(n).prev,
None => None
}
}
fn insert(&mut self, node: @mut Node, prev: Option<@mut Node>, next: Option<@mut Node>) {
self.remove(node);
match prev {
None => {
self.first = Some(node);
},
Some(p) => {
(self.f)(p).next = Some(node);
(self.f)(node).prev = prev;
}
}
match next {
None => {
self.last = Some(node);
},
Some(n) => {
(self.f)(n).prev = Some(node);
(self.f)(node).next = next;
}
}
}
}