Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Ockajak committed Jul 13, 2024
1 parent e5d5b50 commit 383de88
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 51 deletions.
25 changes: 11 additions & 14 deletions src/extensions/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1941,22 +1941,19 @@ pub trait Map<Key, Value> {
pub(crate) fn minmax_by_pairs<'a, K: 'a, V: 'a>(
mut iterator: impl Iterator<Item = (&'a K, &'a V)>, mut compare: impl FnMut((&K, &V), (&K, &V)) -> Ordering,
) -> Option<((&'a K, &'a V), (&'a K, &'a V))> {
match iterator.next() {
Some(item) => {
let mut min = item;
let mut max = min;
for item in iterator {
if compare(item, min) == Ordering::Less {
min = item;
}
if compare(item, max) == Ordering::Greater {
max = item;
}
iterator.next().map(|item| {
let mut min = item;
let mut max = min;
for item in iterator {
if compare(item, min) == Ordering::Less {
min = item;
}
if compare(item, max) == Ordering::Greater {
max = item;
}
Some((min, max))
}
None => None,
}
(min, max)
})
}

#[inline]
Expand Down
62 changes: 25 additions & 37 deletions src/extensions/ordered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub trait Ordered<Item> {
Item: Eq + Hash + 'a;

/// Find the position and value of the first element in this sequence satisfying a predicate.
///
///
/// # Example
///
/// ```
Expand Down Expand Up @@ -485,8 +485,7 @@ pub trait Ordered<Item> {

pub(crate) fn common_prefix_length<'a, Item: PartialEq + 'a>(
iterator: impl Iterator<Item = &'a Item>, elements: &'a impl Iterable<Item<'a> = &'a Item>,
) -> usize
{
) -> usize {
let mut result = 0_usize;
for (item, element) in iterator.zip(elements.iterator()) {
if item != element {
Expand All @@ -499,8 +498,7 @@ pub(crate) fn common_prefix_length<'a, Item: PartialEq + 'a>(

pub(crate) fn common_suffix_length<'a, Item: PartialEq + 'a, I: DoubleEndedIterator<Item = &'a Item>>(
reversed_iterator: impl Iterator<Item = &'a Item>, elements: &'a impl Iterable<Item<'a> = &'a Item, Iterator<'a> = I>,
) -> usize
{
) -> usize {
let mut result = 0_usize;
for (item, element) in reversed_iterator.zip(elements.iterator().rev()) {
if item != element {
Expand All @@ -519,8 +517,7 @@ pub(crate) fn count_unique<'a, Item: Eq + Hash + 'a>(iterator: impl Iterator<Ite

pub(crate) fn equivalent<'a, Item: Eq + Hash + 'a>(
iterator: impl Iterator<Item = &'a Item>, elements: &'a impl Iterable<Item<'a> = &'a Item>,
) -> bool
{
) -> bool {
let elements_iterator = elements.iterator();
let mut excluded: HashMap<&Item, usize> = HashMap::with_capacity(iterator.size_hint().0);
let mut remaining = 0_usize;
Expand All @@ -529,62 +526,53 @@ pub(crate) fn equivalent<'a, Item: Eq + Hash + 'a>(
remaining += 1;
}
for item in iterator {
match excluded.get_mut(item) {
Some(count) => {
if *count > 0 {
*count -= 1;
remaining = remaining.saturating_sub(1);
} else {
return false
}
},
None => return false
if let Some(count) = excluded.get_mut(item) {
if *count > 0 {
*count -= 1;
remaining = remaining.saturating_sub(1);
continue;
}
}
};
return false;
}
remaining == 0
}

pub(crate) fn frequencies_by<'a, Item: 'a, K: Eq + Hash>(
iterator: impl Iterator<Item = &'a Item>, mut to_key: impl FnMut(&Item) -> K,
) -> HashMap<K, usize>
{
) -> HashMap<K, usize> {
let mut result = HashMap::with_capacity(iterator.size_hint().0);
for item in iterator {
*result.entry(to_key(item)).or_default() += 1;
}
result
}

pub(crate) fn joined<'a, Item: Display + 'a>(
mut iterator: impl Iterator<Item = &'a Item>, separator: &str,
) -> String {
match iterator.next() {
Some(item) => {
let mut result = String::with_capacity(separator.len() * iterator.size_hint().0);
pub(crate) fn joined<'a, Item: Display + 'a>(mut iterator: impl Iterator<Item = &'a Item>, separator: &str) -> String {
if let Some(item) = iterator.next() {
let mut result = String::with_capacity(separator.len() * iterator.size_hint().0);
write!(&mut result, "{}", item).unwrap();
for item in iterator {
result.push_str(separator);
write!(&mut result, "{}", item).unwrap();
for item in iterator {
result.push_str(separator);
write!(&mut result, "{}", item).unwrap();
}
result.shrink_to_fit();
result
}
None => String::new(),
result.shrink_to_fit();
result
} else {
String::new()
}
}

#[inline]
pub(crate) fn positions<'a, Item: 'a>(
iterator: impl Iterator<Item = &'a Item>, mut predicate: impl FnMut(&Item) -> bool,
) -> Vec<usize>
{
) -> Vec<usize> {
iterator.enumerate().filter(|(_, item)| predicate(item)).map(|(index, _)| index).collect()
}

pub(crate) fn position_sequence<'a, Item: PartialEq + 'a>(
mut iterator: impl Iterator<Item = &'a Item>, elements: &'a impl Iterable<Item<'a> = &'a Item>,
) -> Option<usize>
{
) -> Option<usize> {
let mut elements_iterator = elements.iterator();
if let Some(first_element) = elements_iterator.next() {
if let Some(start_index) = iterator.position(|item| item == first_element) {
Expand Down

0 comments on commit 383de88

Please sign in to comment.