@@ -102,6 +102,12 @@ impl<T> Clone for Rawlink<T> {
102
102
}
103
103
}
104
104
105
+ impl < T > Node < T > {
106
+ fn new ( v : T ) -> Node < T > {
107
+ Node { value : v, next : None , prev : Rawlink :: none ( ) }
108
+ }
109
+ }
110
+
105
111
/// Set the .prev field on `next`, then return `Some(next)`
106
112
fn link_with_prev < T > ( mut next : ~Node < T > , prev : Rawlink < Node < T > > ) -> Link < T > {
107
113
next. prev = prev;
@@ -131,6 +137,66 @@ impl<T> Mutable for DList<T> {
131
137
}
132
138
}
133
139
140
+ // private methods
141
+ impl < T > DList < T > {
142
+ /// Add a Node first in the list
143
+ #[ inline]
144
+ fn push_front_node ( & mut self , mut new_head : ~Node < T > ) {
145
+ match self . list_head {
146
+ None => {
147
+ self . list_tail = Rawlink :: some ( new_head) ;
148
+ self . list_head = link_with_prev ( new_head, Rawlink :: none ( ) ) ;
149
+ }
150
+ Some ( ref mut head) => {
151
+ new_head. prev = Rawlink :: none ( ) ;
152
+ head. prev = Rawlink :: some ( new_head) ;
153
+ util:: swap ( head, & mut new_head) ;
154
+ head. next = Some ( new_head) ;
155
+ }
156
+ }
157
+ self . length += 1 ;
158
+ }
159
+
160
+ /// Remove the first Node and return it, or None if the list is empty
161
+ #[ inline]
162
+ fn pop_front_node ( & mut self ) -> Option < ~Node < T > > {
163
+ do self . list_head . take ( ) . map_consume |mut front_node| {
164
+ self . length -= 1 ;
165
+ match front_node. next . take ( ) {
166
+ Some ( node) => self . list_head = link_with_prev ( node, Rawlink :: none ( ) ) ,
167
+ None => self . list_tail = Rawlink :: none ( )
168
+ }
169
+ front_node
170
+ }
171
+ }
172
+
173
+ /// Add a Node last in the list
174
+ #[ inline]
175
+ fn push_back_node ( & mut self , mut new_tail : ~Node < T > ) {
176
+ match self . list_tail . resolve ( ) {
177
+ None => return self . push_front_node ( new_tail) ,
178
+ Some ( tail) => {
179
+ self . list_tail = Rawlink :: some ( new_tail) ;
180
+ tail. next = link_with_prev ( new_tail, Rawlink :: some ( tail) ) ;
181
+ }
182
+ }
183
+ self . length += 1 ;
184
+ }
185
+
186
+ /// Remove the last Node and return it, or None if the list is empty
187
+ #[ inline]
188
+ fn pop_back_node ( & mut self ) -> Option < ~Node < T > > {
189
+ do self . list_tail . resolve ( ) . map_consume |tail| {
190
+ self . length -= 1 ;
191
+ self . list_tail = tail. prev ;
192
+ match tail. prev . resolve ( ) {
193
+ None => self . list_head . take_unwrap ( ) ,
194
+ Some ( tail_prev) => tail_prev. next . take_unwrap ( )
195
+ }
196
+ }
197
+ }
198
+ }
199
+
134
200
impl < T > Deque < T > for DList < T > {
135
201
/// Provide a reference to the front element, or None if the list is empty
136
202
#[ inline]
@@ -156,66 +222,32 @@ impl<T> Deque<T> for DList<T> {
156
222
self . list_tail . resolve ( ) . map_mut ( |tail| & mut tail. value )
157
223
}
158
224
159
- /// Add an element last in the list
225
+ /// Add an element first in the list
160
226
///
161
227
/// O(1)
162
- fn push_back ( & mut self , elt : T ) {
163
- match self . list_tail . resolve ( ) {
164
- None => return self . push_front ( elt) ,
165
- Some ( tail) => {
166
- let mut new_tail = ~Node { value : elt, next : None , prev : self . list_tail } ;
167
- self . list_tail = Rawlink :: some ( new_tail) ;
168
- tail. next = Some ( new_tail) ;
169
- }
170
- }
171
- self . length += 1 ;
228
+ fn push_front ( & mut self , elt : T ) {
229
+ self . push_front_node ( ~Node :: new ( elt) )
172
230
}
173
231
174
- /// Remove the last element and return it, or None if the list is empty
232
+ /// Remove the first element and return it, or None if the list is empty
175
233
///
176
234
/// O(1)
177
- fn pop_back ( & mut self ) -> Option < T > {
178
- do self . list_tail . resolve ( ) . map_consume |tail| {
179
- self . length -= 1 ;
180
- self . list_tail = tail. prev ;
181
- match tail. prev . resolve ( ) {
182
- None => self . list_head . take_unwrap ( ) . value ,
183
- Some ( tail_prev) => tail_prev. next . take_unwrap ( ) . value
184
- }
185
- }
235
+ fn pop_front ( & mut self ) -> Option < T > {
236
+ self . pop_front_node ( ) . map_consume ( |~Node { value, _} | value)
186
237
}
187
238
188
- /// Add an element first in the list
239
+ /// Add an element last in the list
189
240
///
190
241
/// O(1)
191
- fn push_front ( & mut self , elt : T ) {
192
- let mut new_head = ~Node { value : elt, next : None , prev : Rawlink :: none ( ) } ;
193
- match self . list_head {
194
- None => {
195
- self . list_tail = Rawlink :: some ( new_head) ;
196
- self . list_head = Some ( new_head) ;
197
- }
198
- Some ( ref mut head) => {
199
- head. prev = Rawlink :: some ( new_head) ;
200
- util:: swap ( head, & mut new_head) ;
201
- head. next = Some ( new_head) ;
202
- }
203
- }
204
- self . length += 1 ;
242
+ fn push_back ( & mut self , elt : T ) {
243
+ self . push_back_node ( ~Node :: new ( elt) )
205
244
}
206
245
207
- /// Remove the first element and return it, or None if the list is empty
246
+ /// Remove the last element and return it, or None if the list is empty
208
247
///
209
248
/// O(1)
210
- fn pop_front ( & mut self ) -> Option < T > {
211
- do self . list_head . take ( ) . map_consume |~Node { value, next, _} | {
212
- self . length -= 1 ;
213
- match next {
214
- Some ( node) => self . list_head = link_with_prev ( node, Rawlink :: none ( ) ) ,
215
- None => self . list_tail = Rawlink :: none ( )
216
- }
217
- value
218
- }
249
+ fn pop_back ( & mut self ) -> Option < T > {
250
+ self . pop_back_node ( ) . map_consume ( |~Node { value, _} | value)
219
251
}
220
252
}
221
253
@@ -289,7 +321,7 @@ impl<T> DList<T> {
289
321
if take_a {
290
322
it. next ( ) ;
291
323
} else {
292
- it. insert_next ( other. pop_front ( ) . unwrap ( ) ) ;
324
+ it. insert_next_node ( other. pop_front_node ( ) . unwrap ( ) ) ;
293
325
}
294
326
}
295
327
}
@@ -433,25 +465,34 @@ pub trait ListInsertion<A> {
433
465
fn peek_next < ' a > ( & ' a mut self ) -> Option < & ' a mut A > ;
434
466
}
435
467
436
- impl < ' self , A > ListInsertion < A > for MutDListIterator < ' self , A > {
437
- fn insert_next ( & mut self , elt : A ) {
438
- // Insert an element before `self.head` so that it is between the
468
+ // private methods for MutDListIterator
469
+ impl < ' self , A > MutDListIterator < ' self , A > {
470
+ fn insert_next_node ( & mut self , mut ins_node : ~Node < A > ) {
471
+ // Insert before `self.head` so that it is between the
439
472
// previously yielded element and self.head.
473
+ //
474
+ // The inserted node will not appear in further iteration.
440
475
match self . head . resolve ( ) {
441
- None => { self . list . push_back ( elt ) ; }
476
+ None => { self . list . push_back_node ( ins_node ) ; }
442
477
Some ( node) => {
443
478
let prev_node = match node. prev . resolve ( ) {
444
- None => return self . list . push_front ( elt ) ,
479
+ None => return self . list . push_front_node ( ins_node ) ,
445
480
Some ( prev) => prev,
446
481
} ;
447
- let mut ins_node = ~Node { value : elt, next : None , prev : Rawlink :: none ( ) } ;
448
482
let node_own = prev_node. next . take_unwrap ( ) ;
449
483
ins_node. next = link_with_prev ( node_own, Rawlink :: some ( ins_node) ) ;
450
484
prev_node. next = link_with_prev ( ins_node, Rawlink :: some ( prev_node) ) ;
451
485
self . list . length += 1 ;
452
486
}
453
487
}
454
488
}
489
+ }
490
+
491
+ impl < ' self , A > ListInsertion < A > for MutDListIterator < ' self , A > {
492
+ #[ inline]
493
+ fn insert_next ( & mut self , elt : A ) {
494
+ self . insert_next_node ( ~Node :: new ( elt) )
495
+ }
455
496
456
497
#[ inline]
457
498
fn peek_next < ' a > ( & ' a mut self ) -> Option < & ' a mut A > {
@@ -929,7 +970,6 @@ mod tests {
929
970
m. push( 0 ) ;
930
971
}
931
972
}
932
-
933
973
#[ bench]
934
974
fn bench_push_back_pop_back( b: & mut test:: BenchHarness ) {
935
975
let mut m = DList :: new:: <int>( ) ;
@@ -947,6 +987,16 @@ mod tests {
947
987
}
948
988
}
949
989
990
+ #[ bench]
991
+ fn bench_push_front_pop_front( b: & mut test:: BenchHarness ) {
992
+ let mut m = DList :: new:: <int>( ) ;
993
+ do b. iter {
994
+ m. push_front( 0 ) ;
995
+ m. pop_front( ) ;
996
+ }
997
+ }
998
+
999
+
950
1000
#[ bench]
951
1001
fn bench_iter( b: & mut test:: BenchHarness ) {
952
1002
let v = & [ 0 , ..128 ] ;
0 commit comments