slice_group_by/binary_group/binary_group.rs
1use crate::{BinaryGroupBy, BinaryGroupByMut};
2
3/// An iterator that will return non-overlapping groups of equal elements, according to
4/// the [`PartialEq::eq`] function in the slice using *binary search*.
5///
6/// It will not necessarily gives contiguous elements to the predicate function.
7/// The predicate function should implement an order consistent with the sort order of the slice.
8///
9/// [`PartialEq::eq`]: https://doc.rust-lang.org/std/cmp/trait.PartialEq.html#tymethod.eq
10pub struct BinaryGroup<'a, T: 'a>(BinaryGroupBy<'a, T, fn(&T, &T) -> bool>);
11
12impl<'a, T: 'a> BinaryGroup<'a, T>
13where T: PartialEq,
14{
15 pub fn new(slice: &'a [T]) -> BinaryGroup<'a, T> {
16 BinaryGroup(BinaryGroupBy::new(slice, PartialEq::eq))
17 }
18}
19
20group_by_wrapped!{ struct BinaryGroup, &'a [T] }
21
22/// An iterator that will return non-overlapping *mutable* groups of equal elements, according to
23/// the [`PartialEq::eq`] function in the slice using *binary search*.
24///
25/// It will not necessarily gives contiguous elements to the predicate function.
26/// The predicate function should implement an order consistent with the sort order of the slice.
27///
28/// [`PartialEq::eq`]: https://doc.rust-lang.org/std/cmp/trait.PartialEq.html#tymethod.eq
29pub struct BinaryGroupMut<'a, T: 'a>(BinaryGroupByMut<'a, T, fn(&T, &T) -> bool>);
30
31impl<'a, T: 'a> BinaryGroupMut<'a, T>
32where T: PartialEq,
33{
34 pub fn new(slice: &'a mut [T]) -> BinaryGroupMut<'a, T> {
35 BinaryGroupMut(BinaryGroupByMut::new(slice, PartialEq::eq))
36 }
37}
38
39group_by_wrapped!{ struct BinaryGroupMut, &'a mut [T] }