#indexed #container #btree-map #hash-map #range #vector

indexed-vector

A Rust library that implements vector container that can be indexed with specified function

1 unstable release

0.1.0 Mar 6, 2023

#2440 in Data structures

Custom license

8KB
117 lines

indexed-vector

A Rust library that implements vector container that can be indexed with specified function.

Implemented Structs

  • HashIndexedVector implements a simple indexed vector with HashMap
  • BTreeIndexedVector implements an indexed vector with BTreeMap. It also supports range query.

Example

use indexed_vector::{BTreeIndexedVector, HashIndexedVector, IndexedVector};

#[derive(Clone, Debug)]
struct User {
    name: String,
    age: u16,
}


fn main() {
    let users = vec![
        User {
            name: "Tom".to_owned(),
            age: 20,
        },
        User {
            name: "Jane".to_owned(),
            age: 20,
        },
        User {
            name: "Ivan".to_owned(),
            age: 30,
        },
    ];

    let hash_vec = HashIndexedVector::new(users.clone(), |user: &User| user.age);
    // Tom and Jane
    dbg!(hash_vec.search(&20).collect::<Vec<_>>());
    // Ivan
    dbg!(hash_vec.search(&30).collect::<Vec<_>>());

    let btree_vec = BTreeIndexedVector::new(users, |user: &User| user.age);
    // Tom, Jane and Ivan
    dbg!(btree_vec.search_range(10..40).collect::<Vec<_>>());
}

No runtime deps