An implementation of the group_by
Haskell function for slices only.
Crate slice-group-by
is a library for efficiently iterating over a slice by groups defined by a function that specifies if two elements are in the same group.
You will only need to define a function that returns true
if two elements are in the same group.
The LinearGroupBy
iterator will always gives contiguous elements to the predicate tion.
```rust use slicegroupby::GroupBy;
let slice = &[1, 1, 1, 3, 3, 2, 2, 2];
let mut iter = slice.lineargroupby(|a, b| a == b);
asserteq!(iter.next(), Some(&[1, 1, 1][..])); asserteq!(iter.next(), Some(&[3, 3][..])); asserteq!(iter.next(), Some(&[2, 2, 2][..])); asserteq!(iter.next(), None); ```
It is also possible to get mutable non overlapping groups of a slice.
The BinaryGroupBy/Mut
and ExponentialGroupBy/Mut
iterators will not necessarily
gives contiguous elements to the predicate function. The predicate function should ement
an order consistent with the sort order of the slice.
```rust use slicegroupby::GroupByMut;
let slice = &mut [1, 1, 1, 2, 2, 2, 3, 3];
let mut iter = slice.binarygroupby_mut(|a, b| a == b);
asserteq!(iter.next(), Some(&mut [1, 1, 1][..])); asserteq!(iter.next(), Some(&mut [2, 2, 2][..])); asserteq!(iter.next(), Some(&mut [3, 3][..])); asserteq!(iter.next(), None); ```
It is also possible to get mutable non overlapping groups of a slice even starting from end of the slice.
```rust use slicegroupby::GroupByMut;
let slice = &mut [1, 1, 1, 2, 2, 2, 3, 3];
let mut iter = slice.exponentialgroupby_mut(|a, b| a == b).rev();
asserteq!(iter.next(), Some(&mut [3, 3][..])); asserteq!(iter.next(), Some(&mut [2, 2, 2][..])); asserteq!(iter.next(), Some(&mut [1, 1, 1][..])); asserteq!(iter.next(), None); ```