#sorting #sets #collection #elements #vector #maintain #unique

sorted-vec

Create and maintain sorted vectors and vector-backed sets

20 releases

0.8.5 Oct 24, 2024
0.8.3 Sep 28, 2023
0.8.2 Jan 4, 2023
0.8.1 Nov 13, 2022
0.3.1 Mar 19, 2019

#84 in Data structures

Download history 14677/week @ 2024-08-19 13906/week @ 2024-08-26 17040/week @ 2024-09-02 17700/week @ 2024-09-09 14754/week @ 2024-09-16 19830/week @ 2024-09-23 17711/week @ 2024-09-30 18512/week @ 2024-10-07 19661/week @ 2024-10-14 20014/week @ 2024-10-21 18619/week @ 2024-10-28 18401/week @ 2024-11-04 14631/week @ 2024-11-11 18026/week @ 2024-11-18 19419/week @ 2024-11-25 24477/week @ 2024-12-02

77,609 downloads per month
Used in 68 crates (18 directly)

Apache-2.0

49KB
1K SLoC

sorted_vec

Create and maintain collections of sorted elements.

Documentation

let mut v = SortedVec::new();
assert_eq!(v.insert (5), 0);
assert_eq!(v.insert (3), 0);
assert_eq!(v.insert (4), 1);
assert_eq!(v.insert (4), 1);
assert_eq!(v.len(), 4);
v.dedup();
assert_eq!(v.len(), 3);
assert_eq!(v.binary_search (&3), Ok (0));
assert_eq!(*SortedVec::from_unsorted (
  vec![5, -10, 99, -11, 2, 17, 10]),
  vec![-11, -10, 2, 5, 10, 17, 99]);

Also provides sorted set containers only containing unique elements.

serde support

serde de/serialization is an optional feature.

By default, deserializing an unsorted container is an error.

To sort on deserialization, tag the field with #[serde(deserialize_with = "SortedVec::deserialize_unsorted")]:

#[derive(Debug, Eq, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
pub struct Foo {
  #[serde(deserialize_with = "SortedVec::deserialize_unsorted")]
  pub v : SortedVec <u64>
}

lib.rs:

Sorted vectors.

Repository

  • SortedVec -- sorted from least to greatest, may contain duplicates
  • SortedSet -- sorted from least to greatest, unique elements
  • ReverseSortedVec -- sorted from greatest to least, may contain duplicates
  • ReverseSortedSet -- sorted from greatest to least, unique elements

The partial module provides sorted vectors of types that only implement PartialOrd where comparison of incomparable elements results in runtime panic.

Dependencies

~160KB