#collection #error #order #automatic #ord #insert #pre-sorted

ord-collections

A library offering collections which are pre-sorted automatically

5 releases

0.2.0 Aug 19, 2024
0.1.3 Aug 19, 2024
0.1.2 Feb 16, 2024
0.1.1 Feb 16, 2024
0.1.0 Feb 16, 2024

#850 in Data structures

Download history 117/week @ 2024-08-13 115/week @ 2024-08-20 20/week @ 2024-09-10 11/week @ 2024-09-17 16/week @ 2024-09-24 17/week @ 2024-10-01

183 downloads per month

MIT/Apache

13KB
198 lines

ord-collections

Status Crates.io Documentation Dependency status

Examples

OrdVec

use ord_collections:: {Error,OrdVec};

let mut index: OrdVec<u64> = OrdVec::default();

// insert `1`, `2`, `3` in wrong order
assert!(index.insert(1).is_ok());
assert!(index.insert(3).is_ok());
assert!(index.insert(2).is_ok());

// check that we cannot push `1` again
assert!(matches!(
    index.insert(1),
    Err(Error::Duplicate(_))
));

// check that iteration is in expected order
let mut sorted = String::new();
for i in index.iter() {
    sorted += &i.to_string()
}
assert_eq!(sorted, "123");

assert!(index.insert(1).is_err());

OrdMap

use ord_collections::{OrdMap,Indexed,Error};

let mut index: OrdMap<char, u64> = OrdMap::default();

// insert `A`, `B`, `C` in wrong order
assert!(index.insert(Indexed::new('C', 0)).is_ok());
assert!(index.insert(Indexed::new('A', 0)).is_ok());
assert!(index.insert(Indexed::new('B', 0)).is_ok());

// check that we cannot insert `A` again
assert!(matches!(
    index.insert(Indexed::new('A', 0)),
    Err(Error::Duplicate(_))
));

check that iteration is in expected order
let mut sorted = String::new();
for i in index.iter() {
    sorted += &i.index().to_string()
}
assert_eq!(sorted, "ABC");

assert!(index.insert(Indexed::new('A', 1)).is_err());

Dependencies

~105KB