#vec #sorted #vector #sorting

sorted-vec

Create and maintain sorted vectors and vector-backed sets

17 releases

0.8.2 Jan 4, 2023
0.8.1 Nov 13, 2022
0.8.0 Apr 10, 2022
0.7.0 Dec 8, 2021
0.3.1 Mar 19, 2019

#226 in Data structures

Download history 1020/week @ 2022-12-05 818/week @ 2022-12-12 512/week @ 2022-12-19 278/week @ 2022-12-26 709/week @ 2023-01-02 668/week @ 2023-01-09 1326/week @ 2023-01-16 811/week @ 2023-01-23 1152/week @ 2023-01-30 988/week @ 2023-02-06 863/week @ 2023-02-13 942/week @ 2023-02-20 770/week @ 2023-02-27 622/week @ 2023-03-06 952/week @ 2023-03-13 1056/week @ 2023-03-20

3,450 downloads per month
Used in 15 crates (6 directly)

Apache-2.0

47KB
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

~220KB