From 12df875131c2302f085ac2d6a69eff238c9fe04c Mon Sep 17 00:00:00 2001 From: Thibault Martinez Date: Tue, 23 Jan 2024 12:55:57 +0100 Subject: [PATCH] Add `is_unique_sorted_by` and `is_sorted_by` --- iterator-sorted/CHANGELOG.md | 6 ++++++ iterator-sorted/Cargo.toml | 6 +++--- iterator-sorted/src/lib.rs | 25 +++++++++++++++++++------ 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/iterator-sorted/CHANGELOG.md b/iterator-sorted/CHANGELOG.md index a011ae1..ec3bc2f 100644 --- a/iterator-sorted/CHANGELOG.md +++ b/iterator-sorted/CHANGELOG.md @@ -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 diff --git a/iterator-sorted/Cargo.toml b/iterator-sorted/Cargo.toml index 755f93b..63ad17a 100644 --- a/iterator-sorted/Cargo.toml +++ b/iterator-sorted/Cargo.toml @@ -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" diff --git a/iterator-sorted/src/lib.rs b/iterator-sorted/src/lib.rs index 64a2360..a82235f 100644 --- a/iterator-sorted/src/lib.rs +++ b/iterator-sorted/src/lib.rs @@ -8,15 +8,18 @@ use core::{cmp::Ordering, iter::Iterator}; -/// Checks if an iterator yields ordered and unique values. -pub fn is_unique_sorted>(mut iterator: I) -> bool { +/// Checks if an iterator yields ordered and unique values based on a given comparator. +pub fn is_unique_sorted_by, 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; @@ -25,15 +28,20 @@ pub fn is_unique_sorted>(mut iterator: I) -> bool true } -/// Checks if an iterator yields ordered values. -pub fn is_sorted>(mut iterator: I) -> bool { +/// Checks if an iterator yields ordered and unique values. +pub fn is_unique_sorted>(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, 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; @@ -41,3 +49,8 @@ pub fn is_sorted>(mut iterator: I) -> bool { true } + +/// Checks if an iterator yields ordered values. +pub fn is_sorted>(iterator: I) -> bool { + is_sorted_by(iterator, T::cmp) +}