pub struct RcComparator<T> { /* private fields */ }Expand description
An Rc-based single-threaded comparator with shared ownership.
RcComparator wraps a comparator function in an Rc, providing
single-threaded shared ownership semantics. It is cloneable and uses
&self in composition operations.
§Type Parameters
T- The type of values being compared
§Examples
use qubit_function::comparator::{Comparator, RcComparator};
use std::cmp::Ordering;
let cmp = RcComparator::new(|a: &i32, b: &i32| a.cmp(b));
let cloned = cmp.clone();
assert_eq!(cmp.compare(&5, &3), Ordering::Greater);
assert_eq!(cloned.compare(&5, &3), Ordering::Greater);§Author
Haixing Hu
Implementations§
Source§impl<T> RcComparator<T>
impl<T> RcComparator<T>
Sourcepub fn new<F>(f: F) -> Self
pub fn new<F>(f: F) -> Self
Creates a new RcComparator from a closure.
§Parameters
f- The closure to wrap
§Returns
A new RcComparator instance.
§Examples
use qubit_function::comparator::RcComparator;
let cmp = RcComparator::new(|a: &i32, b: &i32| a.cmp(b));Examples found in repository?
57fn demo_shared_comparators() {
58 println!("--- Shared comparators ---");
59
60 let ascending = ArcComparator::new(|left: &i32, right: &i32| left.cmp(right));
61 let descending = ascending.reversed();
62 println!(
63 "ArcComparator descending 10 vs 3: {:?}",
64 descending.compare(&10, &3)
65 );
66
67 let by_length = RcComparator::new(|left: &String, right: &String| left.len().cmp(&right.len()));
68 let alphabetic = RcComparator::new(|left: &String, right: &String| left.cmp(right));
69 let combined = by_length.then_comparing(&alphabetic);
70 println!(
71 "RcComparator length/name 'aa' vs 'b': {:?}",
72 combined.compare(&String::from("aa"), &String::from("b"))
73 );
74}Sourcepub fn reversed(&self) -> Selfwhere
T: 'static,
pub fn reversed(&self) -> Selfwhere
T: 'static,
Returns a comparator that imposes the reverse ordering.
§Returns
A new RcComparator that reverses the comparison order.
§Examples
use qubit_function::comparator::{Comparator, RcComparator};
use std::cmp::Ordering;
let cmp = RcComparator::new(|a: &i32, b: &i32| a.cmp(b));
let rev = cmp.reversed();
assert_eq!(rev.compare(&5, &3), Ordering::Less);
assert_eq!(cmp.compare(&5, &3), Ordering::Greater); // cmp still worksSourcepub fn then_comparing(&self, other: &Self) -> Selfwhere
T: 'static,
pub fn then_comparing(&self, other: &Self) -> Selfwhere
T: 'static,
Returns a comparator that uses this comparator first, then another comparator if this one considers the values equal.
§Parameters
other- The comparator to use for tie-breaking
§Returns
A new RcComparator that chains this comparator with another.
§Examples
use qubit_function::comparator::{Comparator, RcComparator};
use std::cmp::Ordering;
let cmp1 = RcComparator::new(|a: &i32, b: &i32| {
(a % 2).cmp(&(b % 2))
});
let cmp2 = RcComparator::new(|a: &i32, b: &i32| a.cmp(b));
let chained = cmp1.then_comparing(&cmp2);
assert_eq!(chained.compare(&4, &2), Ordering::Greater);Examples found in repository?
57fn demo_shared_comparators() {
58 println!("--- Shared comparators ---");
59
60 let ascending = ArcComparator::new(|left: &i32, right: &i32| left.cmp(right));
61 let descending = ascending.reversed();
62 println!(
63 "ArcComparator descending 10 vs 3: {:?}",
64 descending.compare(&10, &3)
65 );
66
67 let by_length = RcComparator::new(|left: &String, right: &String| left.len().cmp(&right.len()));
68 let alphabetic = RcComparator::new(|left: &String, right: &String| left.cmp(right));
69 let combined = by_length.then_comparing(&alphabetic);
70 println!(
71 "RcComparator length/name 'aa' vs 'b': {:?}",
72 combined.compare(&String::from("aa"), &String::from("b"))
73 );
74}Sourcepub fn comparing<K, F>(key_fn: F) -> Self
pub fn comparing<K, F>(key_fn: F) -> Self
Returns a comparator that compares values by a key extracted by the given function.
§Parameters
key_fn- A function that extracts a comparable key from values
§Returns
A new RcComparator that compares by the extracted key.
§Examples
use qubit_function::comparator::{Comparator, RcComparator};
use std::cmp::Ordering;
#[derive(Debug)]
struct Person {
name: String,
age: i32,
}
let by_age = RcComparator::comparing(|p: &Person| &p.age);
let p1 = Person { name: "Alice".to_string(), age: 30 };
let p2 = Person { name: "Bob".to_string(), age: 25 };
assert_eq!(by_age.compare(&p1, &p2), Ordering::Greater);Sourcepub fn into_fn(self) -> impl Fn(&T, &T) -> Ordering
pub fn into_fn(self) -> impl Fn(&T, &T) -> Ordering
Converts this comparator into a closure.
§Returns
A closure that implements Fn(&T, &T) -> Ordering.
§Examples
use qubit_function::comparator::{Comparator, RcComparator};
use std::cmp::Ordering;
let cmp = RcComparator::new(|a: &i32, b: &i32| a.cmp(b));
let func = cmp.into_fn();
assert_eq!(func(&5, &3), Ordering::Greater);Trait Implementations§
Source§impl<T: Clone> Clone for RcComparator<T>
impl<T: Clone> Clone for RcComparator<T>
Source§fn clone(&self) -> RcComparator<T>
fn clone(&self) -> RcComparator<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T> Comparator<T> for RcComparator<T>
impl<T> Comparator<T> for RcComparator<T>
Source§fn compare(&self, a: &T, b: &T) -> Ordering
fn compare(&self, a: &T, b: &T) -> Ordering
Source§fn into_box(self) -> BoxComparator<T>where
Self: Sized + 'static,
fn into_box(self) -> BoxComparator<T>where
Self: Sized + 'static,
BoxComparator. Read moreSource§fn into_arc(self) -> ArcComparator<T>
fn into_arc(self) -> ArcComparator<T>
ArcComparator. Read more