Crates.io | count-invert |
lib.rs | count-invert |
version | 0.1.0 |
source | src |
created_at | 2024-07-18 18:34:37.795867 |
updated_at | 2024-07-18 18:34:37.795867 |
description | A Rust crate providing utility functions for counting elements in a vector and inverting a HashMap based on those counts. |
homepage | |
repository | https://github.com/klebs6/klebs-general |
max_upload_size | |
id | 1307852 |
size | 10,595 |
count-invert
is a Rust crate providing utility functions for counting the occurrences of elements in a vector and inverting a HashMap
based on those counts. This crate leverages the itertools
crate to offer robust and efficient functionalities for common data manipulation tasks.
HashMap
with element counts.HashMap
to map counts to vectors of elements.HashMap
mapping counts to vectors of elements directly from a vector.Add count-invert
to your Cargo.toml
:
[dependencies]
count-invert = "0.1.0"
Here's how to use the count-invert crate:
into_counts
Convert a vector into a HashMap where the keys are the elements and the values are their counts.
use count_invert::into_counts;
use std::collections::HashMap;
let vec = vec![1, 2, 2, 3, 3, 3];
let counts: HashMap<i32, usize> = into_counts(vec);
println!("{:?}", counts); // Output: {1: 1, 2: 2, 3: 3}
invert_map
Invert a HashMap such that the keys become the values and the values become the keys.
use count_invert::invert_map;
use std::collections::HashMap;
let mut map = HashMap::new();
map.insert(1, 2);
map.insert(2, 2);
map.insert(3, 3);
let inverted: HashMap<usize, Vec<i32>> = invert_map(map);
println!("{:?}", inverted); // Output: {2: [1, 2], 3: [3]}
into_count_map
Convert a vector into a HashMap where the keys are the counts of elements and the values are vectors of elements with those counts.
use count_invert::into_count_map;
use std::collections::HashMap;
let vec = vec![1, 2, 2, 3, 3, 3];
let count_map: HashMap<usize, Vec<i32>> = into_count_map(vec);
println!("{:?}", count_map); // Output: {1: [1], 2: [2], 3: [3]}
Contributions are welcome! Please submit pull requests or open issues to suggest improvements or report bugs.
This project is licensed under the MIT License. See the LICENSE file for details.