Crates.io | combination |
lib.rs | combination |
version | 0.2.2 |
source | src |
created_at | 2020-04-11 12:33:41.955368 |
updated_at | 2022-05-07 08:25:12.03463 |
description | A lib to do math jobs like permutate and combinate data from vec. |
homepage | |
repository | https://github.com/shawroger/combination.git |
max_upload_size | |
id | 228638 |
size | 23,116 |
combination is a lib to do math jobs like permutate and combinate data from vec.
extern crate combination;
use combination::*;
#[test]
#[cfg(test)]
fn test_permutation() {
let data = vec![10, 20, 30, 40];
let val = permutate::from_vec(&data);
for item in val {
println!("{:?}", item);
}
}
Get combination data from a vec
use combination::*;
let val = combine::from_vec(&vec![10, 20, 30, 40], 2);
for item in val {
println!("{:?}", item);
}
[10, 20]
[10, 30]
[10, 40]
[20, 30]
[20, 40]
[30, 40]
Get permutation data from a vec
extern crate combination;
use combination::*;
let val = permutate::from_vec(&vec![10, 20, 30, 40]);
for item in val {
println!("{:?}", item);
}
[30, 10, 40, 20]
[30, 10, 20, 40]
[40, 10, 30, 20]
[10, 40, 30, 20]
[10, 30, 40, 20]
[10, 30, 20, 40]
[40, 10, 20, 30]
[10, 40, 20, 30]
[10, 20, 40, 30]
[10, 20, 30, 40]
[40, 30, 20, 10]
[30, 40, 20, 10]
[30, 20, 40, 10]
[30, 20, 10, 40]
[40, 20, 30, 10]
[20, 40, 30, 10]
[20, 30, 40, 10]
[20, 30, 10, 40]
[40, 20, 10, 30]
[20, 40, 10, 30]
[20, 10, 40, 30]
[20, 10, 30, 40]
In 2022, this package is using edition 2021
now and do more work.
In v2, no longer needs Clone
trait.
And Permutate now allow two params.
What is more, it use Optional value and any input won’t occur error.
Anyway, old api has not changed, don't worry.
Any you are going to use v2 module, just add v2
in the feature, and it is used by default.
use combination::v2::\*;
let str_list = ["hi", "i", "am", "roger", "and", "you"];
let combine = Combine::new(6, 4);
let res = str_list.try_select(&combine).unwrap();
for v in res {
println!("{:?}", v);
}
As long as use two traits Select and Selector it will work.
V2 module provides two structs which implement Selector
trait, they are Combine
and Permutate
By using them with trait Select
, type as &[T], Vec<T>, [T]
will be able to be selected.
By implementing this trait, then make any type as a selector.
Then using this for the list, it can select value from the list as custom mode.
struct CustomSelector;
impl Selector for CustomSelector {
fn select_mode(&self) -> Vec<Vec<usize>> {
vec![vec![0, 0, 0], vec![1, 1, 1], vec![2, 2, 2]]
}
}
fn test_custom_selector() {
let str_list = ["how", "are", "u"];
let custom_selector = CustomSelector;
let res = str_list.try_select(&custom_selector).unwrap();
for v in res {
println!("{:#?}", v);
}
}
it will be
[
["how", "how", "how"],
["are", "are", "are"],
["u", "u", "u"]
]