#vec #math #data #combine #jobs #lib #permutate

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

#1014 in Algorithms

Download history 273/week @ 2023-11-20 565/week @ 2023-11-27 354/week @ 2023-12-04 439/week @ 2023-12-11 365/week @ 2023-12-18 148/week @ 2023-12-25 249/week @ 2024-01-01 333/week @ 2024-01-08 290/week @ 2024-01-15 425/week @ 2024-01-22 954/week @ 2024-01-29 819/week @ 2024-02-05 638/week @ 2024-02-12 780/week @ 2024-02-19 814/week @ 2024-02-26 719/week @ 2024-03-04

2,992 downloads per month
Used in 26 crates (via ssi-json-ld)

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