sorted-groups 0.1.1

Implement a data structure to store elements in sorted groups while maintaining the order of elements in each group
Documentation
  • Coverage
  • 10%
    1 out of 10 items documented1 out of 10 items with examples
  • Size
  • Source code size: 22.85 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.2 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 6s Average build duration of successful builds.
  • all releases: 9s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • dax/sorted-groups
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • dax

sorted-groups

Crates.io Version Docs.rs Latest Build Status

sorted-groups implement a data structure to store elements in sorted groups while maintaining the order of elements in each group.

Usage

First, add the sorted_groups crate as a dependency:

cargo add sorted_groups
use sorted_groups::SortedGroups;

#[derive(PartialEq, Eq, Ord, Debug)]
struct Element {
   group: i32,
   value: i32,
}

impl PartialOrd for Element {
   fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
       Some(self.cmp(other))
   }
}

// Elements will be grouped by the `group` field
let mut sorted_groups = SortedGroups::<i32, Element, _>::new(|e| e.group);
sorted_groups.insert(Element { group: 1, value: 1 });
sorted_groups.insert(Element { group: 1, value: 2 });
sorted_groups.insert(Element { group: 2, value: 3 });

// `len` returns the total number of elements
assert_eq!(sorted_groups.len(), 3);
// `groups_len` returns the number of groups
assert_eq!(sorted_groups.groups_len(), 2);
// `iter` returns an iterator over groups and elements
let mut iter = sorted_groups.iter();
assert_eq!(iter.next(), Some((&1, &Element { group: 1, value: 1 })));
assert_eq!(iter.next(), Some((&1, &Element { group: 1, value: 2 })));
assert_eq!(iter.next(), Some((&2, &Element { group: 2, value: 3 })));
assert_eq!(iter.next(), None);

License

This project is distributed under the terms of the Apache License (Version 2.0).

See LICENSE