From 872980bb1d46ac85cbb76cd2c32f453878cfd3ea Mon Sep 17 00:00:00 2001 From: DanArmor <39347109+DanArmor@users.noreply.github.com> Date: Sat, 23 Sep 2023 20:23:28 +0300 Subject: [PATCH 1/4] fix: return 1 for gcd if all elements are 0 --- src/util.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/util.rs b/src/util.rs index dba0011..c4e6074 100644 --- a/src/util.rs +++ b/src/util.rs @@ -15,7 +15,7 @@ pub mod math { } let first = slice[0]; - slice.iter().fold( + let gcd = slice.iter().fold( first, |acc, cur| { if *cur == 0 { @@ -24,7 +24,12 @@ pub mod math { gcd(*cur, acc) } }, - ) + ); + if gcd == 0 { + 1 + } else { + gcd + } } } From dfcd133cf3c79d01fe076d426911bc235a844c93 Mon Sep 17 00:00:00 2001 From: DanArmor <39347109+DanArmor@users.noreply.github.com> Date: Tue, 26 Sep 2023 20:57:30 +0300 Subject: [PATCH 2/4] fix: return gcd 1 for vec of 0 --- Cargo.lock | 2 +- src/util.rs | 15 ++++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 45c7c94..8ff6801 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -628,7 +628,7 @@ dependencies = [ [[package]] name = "weighted_rand" -version = "0.3.2" +version = "0.4.1" dependencies = [ "criterion", "rand", diff --git a/src/util.rs b/src/util.rs index c4e6074..bbe7d14 100644 --- a/src/util.rs +++ b/src/util.rs @@ -14,8 +14,13 @@ pub mod math { return 0; } - let first = slice[0]; - let gcd = slice.iter().fold( + let mut iter = slice.iter().skip_while(|x| x == &&0); + let first = match iter.next() { + Some(v) => *v, + None => return 1 + }; + + let gcd = iter.fold( first, |acc, cur| { if *cur == 0 { @@ -25,11 +30,7 @@ pub mod math { } }, ); - if gcd == 0 { - 1 - } else { - gcd - } + gcd } } From 9fa642664a562a2360c6a4b492f5f34aec549523 Mon Sep 17 00:00:00 2001 From: DanArmor <39347109+DanArmor@users.noreply.github.com> Date: Tue, 26 Sep 2023 20:58:03 +0300 Subject: [PATCH 3/4] Revert "fix: return gcd 1 for vec of 0" This reverts commit dfcd133cf3c79d01fe076d426911bc235a844c93. --- Cargo.lock | 2 +- src/util.rs | 15 +++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8ff6801..45c7c94 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -628,7 +628,7 @@ dependencies = [ [[package]] name = "weighted_rand" -version = "0.4.1" +version = "0.3.2" dependencies = [ "criterion", "rand", diff --git a/src/util.rs b/src/util.rs index bbe7d14..c4e6074 100644 --- a/src/util.rs +++ b/src/util.rs @@ -14,13 +14,8 @@ pub mod math { return 0; } - let mut iter = slice.iter().skip_while(|x| x == &&0); - let first = match iter.next() { - Some(v) => *v, - None => return 1 - }; - - let gcd = iter.fold( + let first = slice[0]; + let gcd = slice.iter().fold( first, |acc, cur| { if *cur == 0 { @@ -30,7 +25,11 @@ pub mod math { } }, ); - gcd + if gcd == 0 { + 1 + } else { + gcd + } } } From 123f4e96caf173782b586a91ea3336f074072836 Mon Sep 17 00:00:00 2001 From: DanArmor <39347109+DanArmor@users.noreply.github.com> Date: Tue, 26 Sep 2023 21:03:40 +0300 Subject: [PATCH 4/4] style: handling of vec of 0 for gcd_for_slice --- Cargo.lock | 2 +- src/util.rs | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 45c7c94..8ff6801 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -628,7 +628,7 @@ dependencies = [ [[package]] name = "weighted_rand" -version = "0.3.2" +version = "0.4.1" dependencies = [ "criterion", "rand", diff --git a/src/util.rs b/src/util.rs index c4e6074..f454393 100644 --- a/src/util.rs +++ b/src/util.rs @@ -15,7 +15,13 @@ pub mod math { } let first = slice[0]; - let gcd = slice.iter().fold( + let mut iter = slice.iter().skip_while(|x| x == &&0); + let first = match iter.next() { + Some(v) => *v, + None => return 1 + }; + + let gcd = iter.fold( first, |acc, cur| { if *cur == 0 { @@ -25,11 +31,7 @@ pub mod math { } }, ); - if gcd == 0 { - 1 - } else { - gcd - } + gcd } }