slice-group-by
An implementation of the groupBy
Haskell function, providing tools for efficiently iterating over slice
and str
by groups defined by a function that specifies if two elements are in the same group.
Differences with Itertools::group_by
The Itertools::group_by
method use a key to compare elements, this library works like, say, slice::sort_by
, it uses a comparison function. It works on every Iterator
type, slice-group-by
work only with slice
and str
, which is the power of this library, it is fast thanks to data locality.
Also slice-group-by
support multiple search algorithms (i.e. linear, binary and exponential search) and can return groups starting from the end.
Examples
Linear Searched Immutable Groups
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 function.
use GroupBy;
let slice = &;
let mut iter = slice.linear_group_by;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Linear Searched Immutable Str Groups
You will only need to define a function that returns true
if two char
are in the same group.
The LinearStrGroupBy
iterator will always gives contiguous char
to the predicate function.
use StrGroupBy;
let string = "aaaabbbbb饰饰cccc";
let mut iter = string.linear_group_by;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Binary Searched Mutable Groups
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 implement
an order consistent with the sort order of the slice.
use GroupByMut;
let slice = &mut ;
let mut iter = slice.binary_group_by_mut;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Exponential Searched Mutable Groups starting from the End
It is also possible to get mutable non overlapping groups of a slice even starting from end of it.
use GroupByMut;
let slice = &mut ;
let mut iter = slice.exponential_group_by_mut.rev;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;