@@ -50,44 +50,22 @@ impl<T: PartialEq> TinyList<T> {
50
50
51
51
#[ inline]
52
52
pub fn insert ( & mut self , data : T ) {
53
- let current_head = mem:: replace ( & mut self . head , None ) ;
54
-
55
- if let Some ( current_head) = current_head {
56
- let current_head = Box :: new ( current_head) ;
57
- self . head = Some ( Element {
58
- data,
59
- next : Some ( current_head)
60
- } ) ;
61
- } else {
62
- self . head = Some ( Element {
63
- data,
64
- next : None ,
65
- } )
66
- }
53
+ self . head = Some ( Element {
54
+ data,
55
+ next : mem:: replace ( & mut self . head , None ) . map ( Box :: new) ,
56
+ } ) ;
67
57
}
68
58
69
59
#[ inline]
70
60
pub fn remove ( & mut self , data : & T ) -> bool {
71
- let remove_head = if let Some ( ref mut head) = self . head {
72
- if head. data == * data {
73
- Some ( mem:: replace ( & mut head. next , None ) )
74
- } else {
75
- None
61
+ self . head = match self . head {
62
+ Some ( ref mut head) if head. data == * data => {
63
+ mem:: replace ( & mut head. next , None ) . map ( |x| * x)
76
64
}
77
- } else {
78
- return false
65
+ Some ( ref mut head ) => return head . remove_next ( data ) ,
66
+ None => return false ,
79
67
} ;
80
-
81
- if let Some ( remove_head) = remove_head {
82
- if let Some ( next) = remove_head {
83
- self . head = Some ( * next) ;
84
- } else {
85
- self . head = None ;
86
- }
87
- return true
88
- }
89
-
90
- self . head . as_mut ( ) . unwrap ( ) . remove_next ( data)
68
+ true
91
69
}
92
70
93
71
#[ inline]
@@ -156,6 +134,8 @@ impl<T: PartialEq> Element<T> {
156
134
#[ cfg( test) ]
157
135
mod test {
158
136
use super :: * ;
137
+ extern crate test;
138
+ use self :: test:: Bencher ;
159
139
160
140
#[ test]
161
141
fn test_contains_and_insert ( ) {
@@ -248,4 +228,41 @@ mod test {
248
228
249
229
assert_eq ! ( list. len( ) , 0 ) ;
250
230
}
231
+
232
+ #[ bench]
233
+ fn bench_insert_empty ( b : & mut Bencher ) {
234
+ b. iter ( || {
235
+ let mut list = TinyList :: new ( ) ;
236
+ list. insert ( 1 ) ;
237
+ } )
238
+ }
239
+
240
+ #[ bench]
241
+ fn bench_insert_one ( b : & mut Bencher ) {
242
+ b. iter ( || {
243
+ let mut list = TinyList :: new_single ( 0 ) ;
244
+ list. insert ( 1 ) ;
245
+ } )
246
+ }
247
+
248
+ #[ bench]
249
+ fn bench_remove_empty ( b : & mut Bencher ) {
250
+ b. iter ( || {
251
+ TinyList :: new ( ) . remove ( & 1 )
252
+ } ) ;
253
+ }
254
+
255
+ #[ bench]
256
+ fn bench_remove_unknown ( b : & mut Bencher ) {
257
+ b. iter ( || {
258
+ TinyList :: new_single ( 0 ) . remove ( & 1 )
259
+ } ) ;
260
+ }
261
+
262
+ #[ bench]
263
+ fn bench_remove_one ( b : & mut Bencher ) {
264
+ b. iter ( || {
265
+ TinyList :: new_single ( 1 ) . remove ( & 1 )
266
+ } ) ;
267
+ }
251
268
}
0 commit comments