-
Notifications
You must be signed in to change notification settings - Fork 0
/
adaptive.rs
206 lines (159 loc) · 5.68 KB
/
adaptive.rs
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/// Adaptive set intersection algorithms.
// Some of these implementations are inspired by works by Daniel Lemire:
// https://github.com/lemire/SIMDCompressionAndIntersection
// https://github.com/lemire/SIMDIntersections
use std::fmt::{Display, Debug};
use smallvec::{SmallVec, smallvec};
use crate::{
intersect::galloping::binary_search,
visitor::Visitor,
};
/// Recursively intersects the two sets.
/// Baeza-Yates, R., & Salinger, A. (2010, April). Fast Intersection Algorithms
/// for Sorted Sequences. In Algorithms and Applications (pp. 45-61).
pub fn baezayates<T, V>(small_set: &[T], large_set: &[T], visitor: &mut V)
where
T: Ord + Copy,
V: Visitor<T>,
{
if small_set.is_empty() || large_set.is_empty() {
return;
}
if small_set.len() > large_set.len() {
return baezayates(large_set, small_set, visitor);
}
let small_partition = small_set.len() / 2;
let target = small_set[small_partition];
let large_partition = binary_search(large_set, target, 0, large_set.len() as isize - 1);
baezayates(&small_set[..small_partition],
&large_set[..large_partition], visitor);
if large_partition >= large_set.len() {
return;
}
if large_set[large_partition] == target {
visitor.visit(target);
}
baezayates(&small_set[small_partition+1..],
&large_set[large_partition..], visitor)
}
// Experimental extension of above algorithm into k sets. Very slow.
pub fn baezayates_k<T, S, V>(sets: &[S], visitor: &mut V)
where
T: Ord + Copy + Display + Debug,
S: AsRef<[T]>,
V: Visitor<T>,
{
debug_assert!(sets.len() >= 2);
for set in sets {
if set.as_ref().is_empty() {
return;
}
}
let smallest = sets[0].as_ref();
let small_partition = smallest.len() / 2;
let target = smallest[small_partition];
let mut lowers: SmallVec<[&[T]; 8]> = SmallVec::new();
let mut uppers: SmallVec<[&[T]; 8]> = SmallVec::new();
lowers.push(&smallest[..small_partition]);
uppers.push(&smallest[small_partition+1..]);
let mut match_count = 0;
for large_set in &sets[1..] {
let large_set = large_set.as_ref();
let large_partition = binary_search(large_set, target, 0, large_set.len() as isize - 1);
if large_partition >= large_set.len() {
return;
}
if large_set[large_partition] == target {
match_count += 1;
}
lowers.push(&large_set[..large_partition]);
uppers.push(&large_set[large_partition..]);
}
if match_count == sets.len() - 1 {
visitor.visit(target);
}
baezayates_k(&lowers, visitor);
baezayates_k(&uppers, visitor);
}
/// Demaine, E. D., López-Ortiz, A., & Ian Munro, J. (2001). Experiments on
/// adaptive set intersections for text retrieval systems. In Algorithm
/// Engineering and Experimentation: Third International Workshop, ALENEX 2001
/// Washington, DC, USA, January 5–6, 2001 Revised Papers 3 (pp. 91-104).
/// Springer Berlin Heidelberg.
pub fn small_adaptive<T, S, V>(sets: &[S], visitor: &mut V)
where
T: Ord + Copy + Display + Debug,
S: AsRef<[T]>,
V: Visitor<T>,
{
assert!(sets.len() >= 2);
debug_assert!(
sets.iter().all(|set| set.as_ref().windows(2).all(|w| w[0] < w[1]))
);
// TODO: check if this optimisation is meaningful
let mut positions_vec: SmallVec<[usize; 8]> = smallvec![0; sets.len()];
let positions = &mut positions_vec[..];
'outer: for &element in sets[0].as_ref() {
let other_sets = sets.iter().map(|s| s.as_ref()).enumerate().skip(1);
for (i, set) in other_sets {
let base = positions[i];
let mut offset = 1;
while base + offset < set.len() && set[base + offset] <= element {
offset *= 2;
}
let lo = base as isize;
let hi = (set.len() as isize - 1).min((base + offset) as isize);
let new_base = binary_search(set, element, lo, hi);
positions[i] = new_base;
if new_base >= set.len() || set[new_base] != element {
continue 'outer;
}
}
visitor.visit(element);
}
}
// Experiment: sort sets each iteration. Result: always slower than standard Small Adaptive.
pub fn small_adaptive_sorted<T, S, V>(given_sets: &[S], visitor: &mut V)
where
T: Ord + Copy + Display + Debug,
S: AsRef<[T]>,
V: Visitor<T>,
{
assert!(given_sets.len() >= 2);
debug_assert!(
given_sets.iter().all(|set| set.as_ref().windows(2).all(|w| w[0] < w[1]))
);
let mut sets_vec: SmallVec<[&[T]; 8]> = SmallVec::from_iter(
given_sets.iter().map(|s| s.as_ref())
);
let sets = &mut sets_vec[..];
'outer: loop {
sets.sort_by_key(|a| a.len());
let (first, other_sets) = sets.split_at_mut(1);
let primary_set = &mut first[0];
if primary_set.is_empty() {
break;
}
let element = primary_set[0];
for set in other_sets {
let mut offset = 1;
while offset < set.len() && set[offset] <= element {
offset *= 2;
}
let lo = 0;
let hi = (set.len() as isize - 1).min(offset as isize);
let new_base = binary_search(set, element, lo, hi);
if new_base >= set.len() {
break 'outer;
}
*set = &set[new_base..];
if set[0] != element {
// Not found, start again with next element.
*primary_set = &primary_set[1..];
continue 'outer;
}
}
*primary_set = &primary_set[1..];
visitor.visit(element);
}
}