Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add is_unique_sorted_by and is_sorted_by #75

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Loading