split_from: use REAL generics
This commit is contained in:
parent
8099326d63
commit
a2fe0daf28
82
Cargo.lock
generated
82
Cargo.lock
generated
@ -2,6 +2,88 @@
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
|
||||
|
||||
[[package]]
|
||||
name = "num"
|
||||
version = "0.4.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "35bd024e8b2ff75562e5f34e7f4905839deb4b22955ef5e73d2fea1b9813cb23"
|
||||
dependencies = [
|
||||
"num-bigint",
|
||||
"num-complex",
|
||||
"num-integer",
|
||||
"num-iter",
|
||||
"num-rational",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-bigint"
|
||||
version = "0.4.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9"
|
||||
dependencies = [
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-complex"
|
||||
version = "0.4.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495"
|
||||
dependencies = [
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-integer"
|
||||
version = "0.1.46"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f"
|
||||
dependencies = [
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-iter"
|
||||
version = "0.1.45"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1429034a0490724d0075ebb2bc9e875d6503c3cf69e235a8941aa757d83ef5bf"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-rational"
|
||||
version = "0.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824"
|
||||
dependencies = [
|
||||
"num-bigint",
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "othello"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"num",
|
||||
]
|
||||
|
||||
@ -4,3 +4,4 @@ version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
num = "0.4.3"
|
||||
|
||||
22
src/misc.rs
22
src/misc.rs
@ -2,11 +2,7 @@ use std::{iter::Rev, ops::RangeInclusive};
|
||||
|
||||
pub fn split_from<T>(min: T, max: T, x: T) -> Vec<Vec<T>>
|
||||
where
|
||||
T: std::cmp::PartialOrd
|
||||
+ std::ops::Add<usize, Output = T>
|
||||
+ std::ops::Sub<usize, Output = T>
|
||||
+ Copy
|
||||
+ Ord,
|
||||
T: num::Integer + Copy,
|
||||
RangeInclusive<T>: Iterator<Item = T> + DoubleEndedIterator,
|
||||
Rev<RangeInclusive<T>>: Iterator<Item = T>,
|
||||
{
|
||||
@ -14,15 +10,14 @@ where
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
let x_upper = (x + 1).min(max);
|
||||
|
||||
let mut output: Vec<Vec<T>> = Vec::new();
|
||||
if x > min + 1 {
|
||||
let x_lower = x - 1;
|
||||
let mut output: Vec<Vec<T>> = Vec::with_capacity(2);
|
||||
if x > min + T::one() {
|
||||
let x_lower = x - T::one();
|
||||
output.push((min..=x_lower).rev().collect());
|
||||
}
|
||||
if x_upper < max {
|
||||
output.push((x_upper..=max).collect());
|
||||
|
||||
if x + T::one() < max {
|
||||
output.push(((x + T::one())..=max).collect());
|
||||
}
|
||||
output
|
||||
}
|
||||
@ -38,5 +33,8 @@ mod test {
|
||||
assert_eq!(split_from(0, 6, 0), vec![vec![1, 2, 3, 4, 5, 6]]);
|
||||
|
||||
assert_eq!(split_from(0, 6, 6), vec![vec![5, 4, 3, 2, 1, 0]]);
|
||||
|
||||
// test out-of-bounds and also generics
|
||||
assert_eq!(split_from::<i16>(-1, 4, 10), Vec::<Vec<i16>>::new());
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user