1212 (assumed: all elements are >0)
1313
1414 Initialisation: ele=0, cnt=0
15- Loop beings .
15+ Loop begins .
1616
1717 loop 1: arr[0]=9
1818 ele = 9
4141
4242*/
4343
44- pub fn moore_voting ( arr : & [ i32 ] ) -> i32 {
45- let n = arr. len ( ) ;
46- let mut cnt = 0 ; // initializing cnt
47- let mut ele = 0 ; // initializing ele
44+ // boilerplate, because `==` isn't `const` yet
45+ const fn eq_s ( a : & [ u8 ] , b : & [ u8 ] ) -> bool {
46+ if a. len ( ) != b. len ( ) {
47+ return false ;
48+ }
49+ let mut i = 0 ;
50+ while i < a. len ( ) {
51+ if a[ i] != b[ i] {
52+ return false ;
53+ }
54+ i += 1 ;
55+ }
56+ true
57+ }
4858
49- for & item in arr. iter ( ) {
59+ pub fn moore_voting_2pass < T : Eq > ( arr : & [ T ] ) -> Option < & T > {
60+ let mut ele = arr. first ( ) ?;
61+ let mut cnt = 0 ;
62+
63+ for item in arr. iter ( ) {
5064 if cnt == 0 {
5165 cnt = 1 ;
5266 ele = item;
@@ -57,13 +71,107 @@ pub fn moore_voting(arr: &[i32]) -> i32 {
5771 }
5872 }
5973
60- let cnt_check = arr. iter ( ) . filter ( |& & x| x == ele) . count ( ) ;
74+ let cnt_check = arr. iter ( ) . filter ( |& x| x == ele) . count ( ) ;
6175
76+ let n = arr. len ( ) ;
6277 if cnt_check > ( n / 2 ) {
63- ele
78+ Some ( ele)
6479 } else {
65- -1
80+ None
81+ }
82+ }
83+
84+ pub const fn moore_voting_2pass_c < ' a > ( arr : & [ & ' a [ u8 ] ] ) -> Option < & ' a [ u8 ] > {
85+ let n = arr. len ( ) ;
86+ if n == 0 {
87+ return None ;
88+ }
89+ let mut cnt: usize = 1 ;
90+ let mut ele = arr[ 0 ] ;
91+ let mut i = 1 ;
92+ while i < n {
93+ if cnt == 0 {
94+ cnt = 1 ;
95+ ele = arr[ i] ;
96+ } else if eq_s ( arr[ i] , ele) {
97+ cnt += 1 ;
98+ } else {
99+ cnt -= 1 ;
100+ }
101+ i += 1 ;
102+ }
103+
104+ let mut cnt_check = 0 ;
105+ let mut i = 0 ;
106+ while i < n {
107+ if eq_s ( arr[ i] , ele) {
108+ cnt_check += 1 ;
109+ }
110+ i += 1 ;
111+ }
112+
113+ if cnt_check > ( n / 2 ) {
114+ Some ( ele)
115+ } else {
116+ None
117+ }
118+ }
119+
120+ /// Returns `None` only if `i` is empty.
121+ /// If there are multiple majorities, anyone could be returned.
122+ ///
123+ /// # Panics
124+ /// In debug-mode, if the internal majority-counter overflows.
125+ /// The counter is `usize`, so it'll **never** overlow if `i` is a slice.
126+ ///
127+ /// Even if `i` is infinite, the counter might never overflow;
128+ /// consider this:
129+ /// ```
130+ /// core::iter::successors(Some(false), |b| Some(!b));
131+ /// ```
132+ /// This is equivalent to the sequence `1-1+1-1...`
133+ pub fn moore_voting_it < T : Eq , I : IntoIterator < Item = T > > ( it : I ) -> Option < T > {
134+ let mut it = it. into_iter ( ) ;
135+ let first = it. next ( ) ?;
136+ Some (
137+ it. fold ( ( 1_usize , first) , |( cnt, ele) , item| {
138+ if cnt == 0 {
139+ ( 1 , item)
140+ } else if item == ele {
141+ ( cnt + 1 , ele)
142+ } else {
143+ ( cnt - 1 , ele)
144+ }
145+ } )
146+ . 1 ,
147+ )
148+ }
149+
150+ /// Returns `None` only if `s` is empty.
151+ /// Otherwise returns an arbitrary index where the majority was found.
152+ /// If there are multiple majorities, anyone could be returned
153+ pub const fn moore_voting_i ( s : & [ & [ u8 ] ] ) -> Option < usize > {
154+ if s. is_empty ( ) {
155+ return None ;
156+ }
157+ let mut i = 0 ;
158+ let mut cnt: usize = 1 ;
159+ let mut ele = s[ i] ;
160+ let mut index = i;
161+ i = 1 ;
162+ while i < s. len ( ) {
163+ if cnt == 0 {
164+ cnt = 1 ;
165+ ele = s[ i] ;
166+ index = i;
167+ } else if eq_s ( s[ i] , ele) {
168+ cnt += 1 ;
169+ } else {
170+ cnt -= 1 ;
171+ }
172+ i += 1 ;
66173 }
174+ Some ( index)
67175}
68176
69177#[ cfg( test) ]
@@ -73,8 +181,8 @@ mod tests {
73181 #[ test]
74182 fn test_moore_voting ( ) {
75183 let arr1: Vec < i32 > = vec ! [ 9 , 1 , 8 , 1 , 1 ] ;
76- assert ! ( moore_voting( & arr1) == 1 ) ;
184+ assert ! ( moore_voting( arr1) == Some ( 1 ) ) ;
77185 let arr2: Vec < i32 > = vec ! [ 1 , 2 , 3 , 4 ] ;
78- assert ! ( moore_voting( & arr2) == - 1 ) ;
186+ assert ! ( moore_voting( arr2) == None ) ;
79187 }
80188}
0 commit comments