4 个版本 (2 个破坏性更新)
0.2.0 | 2024年4月9日 |
---|---|
0.1.2 | 2024年2月27日 |
0.1.1 | 2024年2月1日 |
0.0.1 | 2024年1月8日 |
#1178 in 数据结构
每月21次下载
73KB
1.5K SLoC
Lookups


改进集合的数据检索操作。
概述
lookups 是一个用于扩展现有集合(std::vec::Vec
,std::collections::HashMap
,...)以附加查找功能,以便您能够以与 iterator
或 search algorithm
相似的方式快速访问数据。此包装器与给定的(原始)集合一样易于使用。
可以通过使用不同的方法实现快速访问,例如;
- 哈希表
- 索引
- ...
缺点
- 它需要更多的内存。除了用户数据外,还需要存储 hash,index 数据。
- 写操作较慢,因为每次写操作都需要另一个操作(用于存储查找数据)。
我如何使用它?
use lookups::{LkupHashMap, IndexLookup, Lookup};
#[derive(PartialEq, Debug)]
struct Car {
id: usize,
name: String,
}
// create a new Lookup HashMap with a unique lookup `Key`: `Car::id` (usize)
let mut map = LkupHashMap::new(IndexLookup::with_unique_key() ,|c: &Car| c.id);
map.insert(String::from("BMW"), Car{id: 0, name: "BMW".into()});
map.insert(String::from("Audi"), Car{id: 5, name: "Au".into()});
map.insert(String::from("VW"), Car{id: 2, name: "VW".into()});
// conventionally HashMap access with Key (name: String)
assert!(map.contains_key("VW"));
// lookup with Key (id: usize)
assert!(map.contains_lkup_key(2));
// update one entry by lookup-key
assert_eq!(1, map.update_by_key(5, |c| c.name = "Audi".into()));
// get a Car by an given: id
assert_eq!(Some(&Car{id: 5, name: "Audi".into()}), map.get_by_lkup_key(5).next());
// create a View: a subset from the Lookups (defined by the given Keys)
let view = map.create_lkup_view([0, 2]);
assert_eq!(Some(&Car{id: 0, name: "BMW".into()}), view.get_by_key(0).next());
assert_eq!(vec![&Car{id: 0, name: "BMW".into()}, &Car{id: 2, name: "VW".into()}],
view.items().collect::<Vec<_>>());
// get min and max key
assert_eq!(Some(0), view.min_key());
assert_eq!(Some(2), view.max_key());
依赖项
~0–415KB