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
#![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>()
}
pub trait GroupBy<T, P> {
fn linear_group_by(&self, predicate: P) -> LinearGroupBy<T, P>
where P: FnMut(&T, &T) -> bool;
fn binary_group_by(&self, predicate: P) -> BinaryGroupBy<T, P>
where P: FnMut(&T, &T) -> bool;
fn exponential_group_by(&self, predicate: P) -> ExponentialGroupBy<T, P>
where P: FnMut(&T, &T) -> bool;
}
pub trait GroupByMut<T, P> {
fn linear_group_by_mut(&mut self, predicate: P) -> LinearGroupByMut<T, P>
where P: FnMut(&T, &T) -> bool;
fn binary_group_by_mut(&mut self, predicate: P) -> BinaryGroupByMut<T, P>
where P: FnMut(&T, &T) -> bool;
fn exponential_group_by_mut(&mut self, predicate: P) -> ExponentialGroupByMut<T, P>
where P: FnMut(&T, &T) -> bool;
}
impl<T, P, S> GroupBy<T, P> for S where S: AsRef<[T]>
{
fn linear_group_by(&self, predicate: P) -> LinearGroupBy<T, P>
where P: FnMut(&T, &T) -> bool,
{
LinearGroupBy::new(self.as_ref(), predicate)
}
fn binary_group_by(&self, predicate: P) -> BinaryGroupBy<T, P>
where P: FnMut(&T, &T) -> bool,
{
BinaryGroupBy::new(self.as_ref(), predicate)
}
fn exponential_group_by(&self, predicate: P) -> ExponentialGroupBy<T, P>
where P: FnMut(&T, &T) -> bool,
{
ExponentialGroupBy::new(self.as_ref(), predicate)
}
}
impl<T, P, S> GroupByMut<T, P> for S
where S: AsMut<[T]>,
{
fn linear_group_by_mut(&mut self, predicate: P) -> LinearGroupByMut<T, P>
where P: FnMut(&T, &T) -> bool,
{
LinearGroupByMut::new(self.as_mut(), predicate)
}
fn binary_group_by_mut(&mut self, predicate: P) -> BinaryGroupByMut<T, P>
where P: FnMut(&T, &T) -> bool,
{
BinaryGroupByMut::new(self.as_mut(), predicate)
}
fn exponential_group_by_mut(&mut self, predicate: P) -> ExponentialGroupByMut<T, P>
where P: FnMut(&T, &T) -> bool,
{
ExponentialGroupByMut::new(self.as_mut(), predicate)
}
}