[][src]Crate slice_group_by

Crate slice-group-by is a library for efficiently iterating on a slice by groups defined by a function that specifies if two elements are in the same group.

Example: 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 slice_group_by::GroupBy;

let slice = &[1, 1, 1, 3, 3, 2, 2, 2];

let mut iter = slice.linear_group_by(|a, b| a == b);

assert_eq!(iter.next(), Some(&[1, 1, 1][..]));
assert_eq!(iter.next(), Some(&[3, 3][..]));
assert_eq!(iter.next(), Some(&[2, 2, 2][..]));
assert_eq!(iter.next(), None);

Example: Linear Searched Immutable Str Slices

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 slice_group_by::StrGroupBy;

let string = "aaaabbbbb饰饰cccc";

let mut iter = string.linear_group_by(|a, b| a == b);

assert_eq!(iter.next(), Some("aaaa"));
assert_eq!(iter.next(), Some("bbbbb"));
assert_eq!(iter.next(), Some("饰饰"));
assert_eq!(iter.next(), Some("cccc"));
assert_eq!(iter.next(), None);

Example: 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 slice_group_by::GroupByMut;

let slice = &mut [1, 1, 1, 2, 2, 2, 3, 3];

let mut iter = slice.binary_group_by_mut(|a, b| a == b);

assert_eq!(iter.next(), Some(&mut [1, 1, 1][..]));
assert_eq!(iter.next(), Some(&mut [2, 2, 2][..]));
assert_eq!(iter.next(), Some(&mut [3, 3][..]));
assert_eq!(iter.next(), None);

Example: Exponential Searched Mutable Groups starting from the End

It is also possible to get mutable non overlapping groups of a slice even starting from the end of it.

use slice_group_by::GroupByMut;

let slice = &mut [1, 1, 1, 2, 2, 2, 3, 3];

let mut iter = slice.exponential_group_by_mut(|a, b| a == b).rev();

assert_eq!(iter.next(), Some(&mut [3, 3][..]));
assert_eq!(iter.next(), Some(&mut [2, 2, 2][..]));
assert_eq!(iter.next(), Some(&mut [1, 1, 1][..]));
assert_eq!(iter.next(), None);

Structs

BinaryGroup

An iterator that will return non-overlapping groups of equal elements, according to the PartialEq::eq function in the slice using binary search.

BinaryGroupBy

An iterator that will return non-overlapping groups in the slice using binary search.

BinaryGroupByMut

An iterator that will return non-overlapping mutable groups in the slice using binary search.

BinaryGroupMut

An iterator that will return non-overlapping mutable groups of equal elements, according to the PartialEq::eq function in the slice using binary search.

ExponentialGroup

An iterator that will return non-overlapping groups of equal elements, according to the PartialEq::eq function in the slice using exponential search.

ExponentialGroupBy

An iterator that will reutrn non-overlapping groups in the slice using exponential search.

ExponentialGroupByMut

An iterator that will reutrn non-overlapping mutable groups in the slice using exponential search.

ExponentialGroupMut

An iterator that will return non-overlapping mutable groups of equal elements, according to the PartialEq::eq function in the slice using exponential search.

LinearGroup

An iterator that will return non-overlapping groups of equal elements in the slice using linear/sequential search.

LinearGroupBy

An iterator that will return non-overlapping groups in the slice using linear/sequential search.

LinearGroupByMut

An iterator that will return non-overlapping mutable groups in the slice using linear/sequential search.

LinearGroupMut

An iterator that will return non-overlapping mutable groups of equal elements in the slice using linear/sequential search.

LinearStrGroup

An iterator that will return non-overlapping groups of equal char in the str using linear/sequential search.

LinearStrGroupBy

An iterator that will return non-overlapping groups in the str using linear/sequential search.

LinearStrGroupByMut

An iterator that will return non-overlapping mutable groups in the str using linear/sequential search.

LinearStrGroupMut

An iterator that will return non-overlapping mutable groups of equal char in the str using linear/sequential search.

Traits

GroupBy

A convenient trait to construct an iterator returning non-overlapping groups defined by a predicate.

GroupByMut

A convenient trait to construct an iterator returning non-overlapping mutable groups defined by a predicate.

StrGroupBy

A convenient trait to construct an iterator returning non-overlapping str slices defined by a predicate.

StrGroupByMut

A convenient trait to construct an iterator returning non-overlapping mutable str slices defined by a predicate.

Functions

exponential_search

Exponential searches this sorted slice for a given element.

exponential_search_by

Binary searches this sorted slice with a comparator function.

exponential_search_by_key

Binary searches this sorted slice with a key extraction function.