@@ -20,7 +20,6 @@ pub struct TinyList<T: PartialEq> {
20
20
}
21
21
22
22
impl < T : PartialEq > TinyList < T > {
23
-
24
23
#[ inline]
25
24
pub fn new ( ) -> TinyList < T > {
26
25
TinyList {
@@ -60,20 +59,24 @@ impl<T: PartialEq> TinyList<T> {
60
59
61
60
#[ inline]
62
61
pub fn contains ( & self , data : & T ) -> bool {
63
- if let Some ( ref head) = self . head {
64
- head. contains ( data)
65
- } else {
66
- false
62
+ let mut elem = self . head . as_ref ( ) ;
63
+ while let Some ( ref e) = elem {
64
+ if & e. data == data {
65
+ return true ;
66
+ }
67
+ elem = e. next . as_ref ( ) . map ( |e| & * * e) ;
67
68
}
69
+ false
68
70
}
69
71
70
72
#[ inline]
71
73
pub fn len ( & self ) -> usize {
72
- if let Some ( ref head ) = self . head {
73
- head . len ( )
74
- } else {
75
- 0
74
+ let ( mut elem , mut count ) = ( self . head . as_ref ( ) , 0 ) ;
75
+ while let Some ( ref e ) = elem {
76
+ count += 1 ;
77
+ elem = e . next . as_ref ( ) . map ( |e| & * * e ) ;
76
78
}
79
+ count
77
80
}
78
81
}
79
82
@@ -84,40 +87,13 @@ struct Element<T: PartialEq> {
84
87
}
85
88
86
89
impl < T : PartialEq > Element < T > {
87
-
88
90
fn remove_next ( & mut self , data : & T ) -> bool {
89
- let new_next = if let Some ( ref mut next) = self . next {
90
- if next. data != * data {
91
- return next. remove_next ( data)
92
- } else {
93
- next. next . take ( )
94
- }
95
- } else {
96
- return false
91
+ let new_next = match self . next {
92
+ Some ( ref mut next) if next. data == * data => next. next . take ( ) ,
93
+ Some ( ref mut next) => return next. remove_next ( data) ,
94
+ None => return false ,
97
95
} ;
98
-
99
96
self . next = new_next;
100
-
101
97
true
102
98
}
103
-
104
- fn len ( & self ) -> usize {
105
- if let Some ( ref next) = self . next {
106
- 1 + next. len ( )
107
- } else {
108
- 1
109
- }
110
- }
111
-
112
- fn contains ( & self , data : & T ) -> bool {
113
- if self . data == * data {
114
- return true
115
- }
116
-
117
- if let Some ( ref next) = self . next {
118
- next. contains ( data)
119
- } else {
120
- false
121
- }
122
- }
123
99
}
0 commit comments