#hash-map #hash-set #collection #set #b-tree #map #hash

collectables

收藏品:用于 BTreeMap、BTreeSet、HashMapSet 等集合辅助的 Rust 库。由 SixArm.com 提供。

2 个稳定版本

2.0.1 2021 年 4 月 4 日
2.0.0 2021 年 4 月 3 日

#35 in #hash-set

MIT OR Apache-2.0 OR GPL-2.0-only

30KB
357

收藏品:Rust 集合辅助库

collectables Rust 库为 BTreeMap、BTreeSet、HashMapSet 等提供辅助工具。

该库提供两个通用集合辅助工具

  • BTreeMapToSet<K, V> 基于 BTreeMap<K, BTreeSet>

  • HashMapToSet<K, V> 基于 HashMap<K, HashSet>

示例

将单词映射到其他单词集合的示例

// Use this crate
use collectables::*;

// Create an empty hash map, where each vaue is a hash set.
let mut a: HashMapToSet<String, String> = HashMapToSet::new();

/// Create some example strings
let hello   = String::from("hello");   // English example
let ninhao  = String::from("nǐn hǎo"); // Chinese example
let hola    = String::from("hola");    // Spanish example

// Insert items, such as mapping the word "hello" to various tranlations.
// The first parameter is a map key; the second parameter is a set item.
a.sub_insert(hello.to_string(), ninhao.to_string());
a.sub_insert(hello.to_string(), hola.to_string());

// Does the collection contain a word map key to a word set item?
assert_eq!(a.sub_contains(&hello, &ninhao), true);

// Remove an item from the map key set.
a.sub_remove(&hello, &ninhao);

// These collections helpers are implemented as trait extensions,
// thus all HashMap functions and HashSet functions are available,
// and can be intermixed with the HashMapSet trait extensions.
assert_eq!(a.get(&hello).unwrap().contains(&hola), true);


## Specific-purpose collections helpers

This crate provides two specific-purpose collections helpers:

* BTreeMapOfFileLenToSetOfPathBuf is based on BTreeMap<u64, BTreeSet<PathBuf>>

* HashMapOfFileLenToSetOfPathBuf is based on HashMap<u64, HashSet<PathBuf>>

The helpers are implemented as trait extensions i.e. the helpers add 
functions to existing Rust std::collections code.

```rust
// Use this crate
use collectables::*;

// Create a path to an existing file 
let alpha = std::path::PathBuf::from("alpha.txt");

// Create a collection to map the file length to a set of path buffers.
let mut a: HashMapOfFileLenToSetOfPathBuf = HashMapOfFileLenToSetOfPathBuf::new();

// Insert the path, which uses the path metadata file length as a map key.
a.sub_insert_path(alpha.clone());

// Does the collection contain a path?
assert_eq!(a.sub_contains_path(&alpha), true);

// Remove an item from the map key set
a.sub_remove_path(alpha.clone());

跟踪

联系方式:Joel Parker Henderson [email protected]

许可证:MIT 或 Apache-2.0 或 GPL-2.0-only

依赖项

~1MB
~14K SLoC