key-node-list

github crates.io docs.rs build status

Doubly-linked list that stores key-node pairs.

KeyNodeList is a doubly-linked list, it uses a hash map to maintain correspondence between keys and nodes, and records the previous key and the next key of the current node in the node itself. There is no pointer operations during this process, so key_node_list is all implemented in safe Rust.

You can complete key lookups, key-node pair updates, key-node pair deletions and other operations of KeyNodeList in O(1)~ time. You can also use cursor-based interface to traverse or edit the linked list.

Usage

key_node_list is available on crates.io, so:

toml [dependencies] key-node-list = "0.0.4"

Example

```rust use keynodelist::KeyValueList;

// construct key-value list from tuple array let mut list = KeyValueList::from([(1, "Reimu"), (2, "Marisa")]);

// or pushing other key-value pairs to the front/back of list list.pushfront(0, "Alice").unwrap(); list.pushback(3, "Patchouli").unwrap();

// query nodes by key asserteq!(list[&1].value(), &"Reimu"); asserteq!(list[&0].value(), &"Alice");

// also you can update nodes by key *list.nodemut(&3).unwrap().valuemut() = "Youmu"; *list.frontnodemut().unwrap().valuemut() = "Mokou"; asserteq!(list[&3].value(), &"Youmu"); asserteq!(list[&0].value(), &"Mokou"); asserteq!(list.front_node().unwrap().value(), &"Mokou");

// remove some key-node pairs assert!(list.popfront().issome()); assert!(list.remove(&2).is_some());

// all key-node pairs are in order list.pushback(5, "Yuyuko"); let vec: Vec<_> = list.intoiter().map(|(k, n)| (k, n.intovalue())).collect(); asserteq!(vec, [(1, "Reimu"), (3, "Youmu"), (5, "Yuyuko")]); ```

For more details, visit key_node_list on docs.rs.

Changelog

See CHANGELOG.md.

License

Copyright (C) 2010-2021 MaxXing. License GPLv3.