1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
//! 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. //! //! ```rust //! 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: 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. //! //! ```rust //! 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. //! //! ```rust //! 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); //! ``` //! #![cfg_attr(feature = "nightly", feature(ptr_offset_from))] #![cfg_attr(feature = "nightly", feature(test))] mod linear_group_by; mod binary_group_by; mod exponential_group_by; pub use self::linear_group_by::{LinearGroupBy, LinearGroupByMut}; pub use self::binary_group_by::{BinaryGroupBy, BinaryGroupByMut}; pub use self::exponential_group_by::{ExponentialGroupBy, ExponentialGroupByMut}; #[cfg(feature = "nightly")] #[inline] unsafe fn offset_from<T>(to: *const T, from: *const T) -> usize { to.offset_from(from) as usize } #[cfg(not(feature = "nightly"))] #[inline] unsafe fn offset_from<T>(to: *const T, from: *const T) -> usize { use std::mem; (to as usize - from as usize) / mem::size_of::<T>() } /// A convenient trait to construct an iterator returning non-overlapping groups /// defined by a predicate. pub trait GroupBy<T, P> where P: FnMut(&T, &T) -> bool { /// Returns an iterator on slice groups using the *linear search* method. fn linear_group_by(&self, predicate: P) -> LinearGroupBy<T, P>; /// Returns an iterator on slice groups using the *binary search* method. /// /// The predicate function should implement an order consistent with /// the sort order of the slice. fn binary_group_by(&self, predicate: P) -> BinaryGroupBy<T, P>; /// Returns an iterator on slice groups using the *exponential search* method. /// /// The predicate function should implement an order consistent with /// the sort order of the slice. fn exponential_group_by(&self, predicate: P) -> ExponentialGroupBy<T, P>; } /// A convenient trait to construct an iterator returning non-overlapping mutable /// groups defined by a predicate. pub trait GroupByMut<T, P> where P: FnMut(&T, &T) -> bool { /// Returns an iterator on slice groups using the *linear search* method. fn linear_group_by_mut(&mut self, predicate: P) -> LinearGroupByMut<T, P>; /// Returns an iterator on slice groups using the *binary search* method. /// /// The predicate function should implement an order consistent with /// the sort order of the slice. fn binary_group_by_mut(&mut self, predicate: P) -> BinaryGroupByMut<T, P>; /// Returns an iterator on slice groups using the *exponential search* method. /// /// The predicate function should implement an order consistent with /// the sort order of the slice. fn exponential_group_by_mut(&mut self, predicate: P) -> ExponentialGroupByMut<T, P>; } impl<T, P> GroupBy<T, P> for [T] where P: FnMut(&T, &T) -> bool { fn linear_group_by(&self, predicate: P) -> LinearGroupBy<T, P> { LinearGroupBy::new(self, predicate) } fn binary_group_by(&self, predicate: P) -> BinaryGroupBy<T, P> { BinaryGroupBy::new(self, predicate) } fn exponential_group_by(&self, predicate: P) -> ExponentialGroupBy<T, P> { ExponentialGroupBy::new(self, predicate) } } impl<T, P> GroupByMut<T, P> for [T] where P: FnMut(&T, &T) -> bool { fn linear_group_by_mut(&mut self, predicate: P) -> LinearGroupByMut<T, P> { LinearGroupByMut::new(self, predicate) } fn binary_group_by_mut(&mut self, predicate: P) -> BinaryGroupByMut<T, P> { BinaryGroupByMut::new(self, predicate) } fn exponential_group_by_mut(&mut self, predicate: P) -> ExponentialGroupByMut<T, P> { ExponentialGroupByMut::new(self, predicate) } }