10
10
// Backlinks over List::prev are raw pointers that form a full chain in
11
11
// the reverse direction.
12
12
13
-
14
13
use std:: cast;
15
14
use std:: cmp;
16
15
use std:: ptr;
17
16
use std:: util;
18
17
use std:: iterator:: FromIterator ;
19
18
19
+ use container:: Deque ;
20
+
20
21
/// A doubly-linked list
21
22
pub struct List < T > {
22
23
priv length : uint ,
@@ -124,36 +125,30 @@ impl<T> Mutable for List<T> {
124
125
}
125
126
}
126
127
127
- impl < T > List < T > {
128
- /// Create an empty List
129
- #[ inline]
130
- pub fn new ( ) -> List < T > {
131
- List { list_head : None , list_tail : Rawlink :: none ( ) , length : 0 }
132
- }
133
-
128
+ impl < T > Deque < T > for List < T > {
134
129
/// Provide a reference to the front element, or None if the list is empty
135
- pub fn peek_front < ' a > ( & ' a self ) -> Option < & ' a T > {
130
+ fn front < ' a > ( & ' a self ) -> Option < & ' a T > {
136
131
self . list_head . chain_ref ( |x| Some ( & x. value ) )
137
132
}
138
133
139
134
/// Provide a mutable reference to the front element, or None if the list is empty
140
- pub fn peek_front_mut < ' a > ( & ' a mut self ) -> Option < & ' a mut T > {
135
+ fn front_mut < ' a > ( & ' a mut self ) -> Option < & ' a mut T > {
141
136
match self . list_head {
142
137
None => None ,
143
138
Some ( ref mut head) => Some ( & mut head. value ) ,
144
139
}
145
140
}
146
141
147
142
/// Provide a reference to the back element, or None if the list is empty
148
- pub fn peek_back < ' a > ( & ' a self ) -> Option < & ' a T > {
143
+ fn back < ' a > ( & ' a self ) -> Option < & ' a T > {
149
144
match self . list_tail . resolve_immut ( ) {
150
145
None => None ,
151
146
Some ( tail) => Some ( & tail. value ) ,
152
147
}
153
148
}
154
149
155
150
/// Provide a mutable reference to the back element, or None if the list is empty
156
- pub fn peek_back_mut < ' a > ( & ' a mut self ) -> Option < & ' a mut T > {
151
+ fn back_mut < ' a > ( & ' a mut self ) -> Option < & ' a mut T > {
157
152
match self . list_tail . resolve ( ) {
158
153
None => None ,
159
154
Some ( tail) => Some ( & mut tail. value ) ,
@@ -163,7 +158,7 @@ impl<T> List<T> {
163
158
/// Add an element last in the list
164
159
///
165
160
/// O(1)
166
- pub fn push_back ( & mut self , elt : T ) {
161
+ fn push_back ( & mut self , elt : T ) {
167
162
match self . list_tail . resolve ( ) {
168
163
None => return self . push_front ( elt) ,
169
164
Some ( tail) => {
@@ -179,7 +174,7 @@ impl<T> List<T> {
179
174
///
180
175
/// O(1)
181
176
#[ inline]
182
- pub fn pop_back ( & mut self ) -> Option < T > {
177
+ fn pop_back ( & mut self ) -> Option < T > {
183
178
match self . list_tail . resolve ( ) {
184
179
None => None ,
185
180
Some ( tail) => {
@@ -202,7 +197,7 @@ impl<T> List<T> {
202
197
/// Add an element first in the list
203
198
///
204
199
/// O(1)
205
- pub fn push_front ( & mut self , elt : T ) {
200
+ fn push_front ( & mut self , elt : T ) {
206
201
let mut new_head = ~Node { value : elt, next : None , prev : Rawlink :: none ( ) } ;
207
202
match self . list_head {
208
203
None => {
@@ -221,7 +216,7 @@ impl<T> List<T> {
221
216
/// Remove the first element and return it, or None if the list is empty
222
217
///
223
218
/// O(1)
224
- pub fn pop_front ( & mut self ) -> Option < T > {
219
+ fn pop_front ( & mut self ) -> Option < T > {
225
220
match util:: replace ( & mut self . list_head , None ) {
226
221
None => None ,
227
222
Some ( old_head) => {
@@ -239,6 +234,14 @@ impl<T> List<T> {
239
234
}
240
235
}
241
236
}
237
+ }
238
+
239
+ impl < T > List < T > {
240
+ /// Create an empty List
241
+ #[ inline]
242
+ pub fn new ( ) -> List < T > {
243
+ List { list_head : None , list_tail : Rawlink :: none ( ) , length : 0 }
244
+ }
242
245
243
246
/// Add all elements from `other` to the end of the list
244
247
///
@@ -292,7 +295,7 @@ impl<T> List<T> {
292
295
{
293
296
let mut it = self . mut_iter ( ) ;
294
297
loop {
295
- match ( it. next ( ) , other. peek_front ( ) ) {
298
+ match ( it. next ( ) , other. front ( ) ) {
296
299
( None , _ ) => break ,
297
300
( _ , None ) => return ,
298
301
( Some ( x) , Some ( y) ) => if f ( x, y) { loop }
@@ -462,7 +465,7 @@ impl<'self, A> ListInsertion<A> for MutForwardIterator<'self, A> {
462
465
463
466
fn peek_next < ' a > ( & ' a mut self ) -> Option < & ' a mut A > {
464
467
match self . curs . resolve ( ) {
465
- None => self . list . peek_front_mut ( ) ,
468
+ None => self . list . front_mut ( ) ,
466
469
Some ( curs) => match curs. next {
467
470
None => None ,
468
471
Some ( ref mut node) => Some ( & mut node. value ) ,
@@ -574,14 +577,14 @@ mod tests {
574
577
n. push_front ( 2 ) ;
575
578
n. push_front ( 3 ) ;
576
579
{
577
- assert_eq ! ( n. peek_front ( ) . unwrap( ) , & 3 ) ;
578
- let x = n. peek_front_mut ( ) . unwrap ( ) ;
580
+ assert_eq ! ( n. front ( ) . unwrap( ) , & 3 ) ;
581
+ let x = n. front_mut ( ) . unwrap ( ) ;
579
582
assert_eq ! ( * x, 3 ) ;
580
583
* x = 0 ;
581
584
}
582
585
{
583
- assert_eq ! ( n. peek_back ( ) . unwrap( ) , & 2 ) ;
584
- let y = n. peek_back_mut ( ) . unwrap ( ) ;
586
+ assert_eq ! ( n. back ( ) . unwrap( ) , & 2 ) ;
587
+ let y = n. back_mut ( ) . unwrap ( ) ;
585
588
assert_eq ! ( * y, 2 ) ;
586
589
* y = 1 ;
587
590
}
0 commit comments