#math #vec #combine #jobs #lib #permutate #combinate

combination

A lib to do math jobs like permutate and combinate data from vec

8 releases

0.2.2 May 7, 2022
0.2.0 Apr 27, 2022
0.1.5 Jan 20, 2021
0.1.2 Apr 11, 2020

#564 in Algorithms

Download history 387/week @ 2024-07-19 885/week @ 2024-07-26 772/week @ 2024-08-02 1074/week @ 2024-08-09 1471/week @ 2024-08-16 963/week @ 2024-08-23 1105/week @ 2024-08-30 1402/week @ 2024-09-06 1556/week @ 2024-09-13 1888/week @ 2024-09-20 1069/week @ 2024-09-27 1280/week @ 2024-10-04 1197/week @ 2024-10-11 1000/week @ 2024-10-18 926/week @ 2024-10-25 1099/week @ 2024-11-01

4,423 downloads per month
Used in 56 crates (2 directly)

MIT/Apache

24KB
420 lines

Combination

what it is

combination is a lib to do math jobs like permutate and combinate data from vec.

example

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);
    }
}

API

combine

Get combination data from a vec

  • for example
use combination::*;
let val = combine::from_vec(&vec![10, 20, 30, 40], 2);
for item in val {
    println!("{:?}", item);
}
  • and will get:
[10, 20]
[10, 30]
[10, 40]
[20, 30]
[20, 40]
[30, 40]

permutate

Get permutation data from a vec

  • for example
extern crate combination;
use combination::*;
let val = permutate::from_vec(&vec![10, 20, 30, 40]);
for item in val {
    println!("{:?}", item);
}
  • and will get:
    [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]

Recommend v2 module

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.

Example

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.

Selector

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.

Example

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"]
]

No runtime deps

Features