Skip to content

Commit

Permalink
Add is_unique_sorted_by and is_sorted_by (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
thibault-martinez authored Jan 23, 2024
1 parent db8b3d7 commit 7317d01
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
6 changes: 6 additions & 0 deletions iterator-sorted/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Security -->

## 0.2.0 - 2024-01-23

### Added

- `is_unique_sorted_by` and `is_sorted_by`;

## 0.1.0 - 2022-01-17

### Added
Expand Down
6 changes: 3 additions & 3 deletions iterator-sorted/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[package]
name = "iterator-sorted"
version = "0.1.0"
authors = [ "IOTA Stiftung" ]
version = "0.2.0"
authors = ["IOTA Stiftung"]
edition = "2021"
description = "Stable functions for checking iterator sorting."
readme = "README.md"
repository = "https://github.com/iotaledger/common-rs"
license = "Apache-2.0"
keywords = [ "iota", "iterator", "sorted" ]
keywords = ["iota", "iterator", "sorted"]
homepage = "https://www.iota.org"
25 changes: 19 additions & 6 deletions iterator-sorted/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@

use core::{cmp::Ordering, iter::Iterator};

/// Checks if an iterator yields ordered and unique values.
pub fn is_unique_sorted<T: Ord, I: Iterator<Item = T>>(mut iterator: I) -> bool {
/// Checks if an iterator yields ordered and unique values based on a given comparator.
pub fn is_unique_sorted_by<T: Ord, I: Iterator<Item = T>, F: FnMut(&T, &T) -> Ordering>(
mut iterator: I,
mut cmp: F,
) -> bool {
let mut previous = match iterator.next() {
Some(e) => e,
None => return true,
};

for curr in iterator {
if previous.cmp(&curr) != Ordering::Less {
if cmp(&previous, &curr) != Ordering::Less {
return false;
}
previous = curr;
Expand All @@ -25,19 +28,29 @@ pub fn is_unique_sorted<T: Ord, I: Iterator<Item = T>>(mut iterator: I) -> bool
true
}

/// Checks if an iterator yields ordered values.
pub fn is_sorted<T: Ord, I: Iterator<Item = T>>(mut iterator: I) -> bool {
/// Checks if an iterator yields ordered and unique values.
pub fn is_unique_sorted<T: Ord, I: Iterator<Item = T>>(iterator: I) -> bool {
is_unique_sorted_by(iterator, T::cmp)
}

/// Checks if an iterator yields ordered values based on a given comparator.
pub fn is_sorted_by<T: Ord, I: Iterator<Item = T>, F: FnMut(&T, &T) -> Ordering>(mut iterator: I, mut cmp: F) -> bool {
let mut previous = match iterator.next() {
Some(e) => e,
None => return true,
};

for curr in iterator {
if previous.cmp(&curr) == Ordering::Greater {
if cmp(&previous, &curr) == Ordering::Greater {
return false;
}
previous = curr;
}

true
}

/// Checks if an iterator yields ordered values.
pub fn is_sorted<T: Ord, I: Iterator<Item = T>>(iterator: I) -> bool {
is_sorted_by(iterator, T::cmp)
}

0 comments on commit 7317d01

Please sign in to comment.