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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
|
//! Join functionality.
use super::Relation;
/// Performs treefrog leapjoin using a list of leapers.
pub(crate) fn leapjoin<'leap, Tuple: Ord, Val: Ord + 'leap, Result: Ord>(
source: &[Tuple],
mut leapers: impl Leapers<'leap, Tuple, Val>,
mut logic: impl FnMut(&Tuple, &Val) -> Result,
) -> Relation<Result> {
let mut result = Vec::new(); // temp output storage.
let mut values = Vec::new(); // temp value storage.
for tuple in source {
// Determine which leaper would propose the fewest values.
let mut min_index = usize::max_value();
let mut min_count = usize::max_value();
leapers.for_each_count(tuple, |index, count| {
if min_count > count {
min_count = count;
min_index = index;
}
});
// We had best have at least one relation restricting values.
assert!(min_count < usize::max_value());
// If there are values to propose:
if min_count > 0 {
// Push the values that `min_index` "proposes" into `values`.
leapers.propose(tuple, min_index, &mut values);
// Give other leapers a chance to remove values from
// anti-joins or filters.
leapers.intersect(tuple, min_index, &mut values);
// Push remaining items into result.
for val in values.drain(..) {
result.push(logic(tuple, val));
}
}
}
Relation::from_vec(result)
}
/// Implemented for a tuple of leapers
pub trait Leapers<'leap, Tuple, Val> {
/// Internal method:
fn for_each_count(&mut self, tuple: &Tuple, op: impl FnMut(usize, usize));
/// Internal method:
fn propose(&mut self, tuple: &Tuple, min_index: usize, values: &mut Vec<&'leap Val>);
/// Internal method:
fn intersect(&mut self, tuple: &Tuple, min_index: usize, values: &mut Vec<&'leap Val>);
}
macro_rules! tuple_leapers {
($($Ty:ident)*) => {
#[allow(unused_assignments, non_snake_case)]
impl<'leap, Tuple, Val, $($Ty),*> Leapers<'leap, Tuple, Val> for ($($Ty,)*)
where
$($Ty: Leaper<'leap, Tuple, Val>,)*
{
fn for_each_count(&mut self, tuple: &Tuple, mut op: impl FnMut(usize, usize)) {
let ($($Ty,)*) = self;
let mut index = 0;
$(
let count = $Ty.count(tuple);
op(index, count);
index += 1;
)*
}
fn propose(&mut self, tuple: &Tuple, min_index: usize, values: &mut Vec<&'leap Val>) {
let ($($Ty,)*) = self;
let mut index = 0;
$(
if min_index == index {
return $Ty.propose(tuple, values);
}
index += 1;
)*
panic!("no match found for min_index={}", min_index);
}
fn intersect(&mut self, tuple: &Tuple, min_index: usize, values: &mut Vec<&'leap Val>) {
let ($($Ty,)*) = self;
let mut index = 0;
$(
if min_index != index {
$Ty.intersect(tuple, values);
}
index += 1;
)*
}
}
}
}
tuple_leapers!(A B);
tuple_leapers!(A B C);
tuple_leapers!(A B C D);
tuple_leapers!(A B C D E);
tuple_leapers!(A B C D E F);
tuple_leapers!(A B C D E F G);
/// Methods to support treefrog leapjoin.
pub trait Leaper<'leap, Tuple, Val> {
/// Estimates the number of proposed values.
fn count(&mut self, prefix: &Tuple) -> usize;
/// Populates `values` with proposed values.
fn propose(&mut self, prefix: &Tuple, values: &mut Vec<&'leap Val>);
/// Restricts `values` to proposed values.
fn intersect(&mut self, prefix: &Tuple, values: &mut Vec<&'leap Val>);
}
pub(crate) mod filters {
use super::Leaper;
use super::Leapers;
/// A treefrog leaper that tests each of the tuples from the main
/// input (the "prefix"). Use like `PrefixFilter::from(|tuple|
/// ...)`; if the closure returns true, then the tuple is
/// retained, else it will be ignored. This leaper can be used in
/// isolation in which case it just acts like a filter on the
/// input (the "proposed value" will be `()` type).
pub struct PrefixFilter<Tuple, Func: Fn(&Tuple) -> bool> {
phantom: ::std::marker::PhantomData<Tuple>,
predicate: Func,
}
impl<'leap, Tuple, Func> PrefixFilter<Tuple, Func>
where
Func: Fn(&Tuple) -> bool,
{
/// Creates a new filter based on the prefix
pub fn from(predicate: Func) -> Self {
PrefixFilter {
phantom: ::std::marker::PhantomData,
predicate,
}
}
}
impl<'leap, Tuple, Val, Func> Leaper<'leap, Tuple, Val> for PrefixFilter<Tuple, Func>
where
Func: Fn(&Tuple) -> bool,
{
/// Estimates the number of proposed values.
fn count(&mut self, prefix: &Tuple) -> usize {
if (self.predicate)(prefix) {
usize::max_value()
} else {
0
}
}
/// Populates `values` with proposed values.
fn propose(&mut self, _prefix: &Tuple, _values: &mut Vec<&'leap Val>) {
panic!("PrefixFilter::propose(): variable apparently unbound");
}
/// Restricts `values` to proposed values.
fn intersect(&mut self, _prefix: &Tuple, _values: &mut Vec<&'leap Val>) {
// We can only be here if we returned max_value() above.
}
}
impl<'leap, Tuple, Func> Leapers<'leap, Tuple, ()> for PrefixFilter<Tuple, Func>
where
Func: Fn(&Tuple) -> bool,
{
fn for_each_count(&mut self, tuple: &Tuple, mut op: impl FnMut(usize, usize)) {
if <Self as Leaper<'_, Tuple, ()>>::count(self, tuple) == 0 {
op(0, 0)
} else {
// we will "propose" the `()` value if the predicate applies
op(0, 1)
}
}
fn propose(&mut self, _: &Tuple, min_index: usize, values: &mut Vec<&'leap ()>) {
assert_eq!(min_index, 0);
values.push(&());
}
fn intersect(&mut self, _: &Tuple, min_index: usize, values: &mut Vec<&'leap ()>) {
assert_eq!(min_index, 0);
assert_eq!(values.len(), 1);
}
}
/// A treefrog leaper based on a predicate of prefix and value.
/// Use like `ValueFilter::from(|tuple, value| ...)`. The closure
/// should return true if `value` ought to be retained. The
/// `value` will be a value proposed elsewhere by an `extend_with`
/// leaper.
///
/// This leaper cannot be used in isolation, it must be combined
/// with other leapers.
pub struct ValueFilter<Tuple, Val, Func: Fn(&Tuple, &Val) -> bool> {
phantom: ::std::marker::PhantomData<(Tuple, Val)>,
predicate: Func,
}
impl<'leap, Tuple, Val, Func> ValueFilter<Tuple, Val, Func>
where
Func: Fn(&Tuple, &Val) -> bool,
{
/// Creates a new filter based on the prefix
pub fn from(predicate: Func) -> Self {
ValueFilter {
phantom: ::std::marker::PhantomData,
predicate,
}
}
}
impl<'leap, Tuple, Val, Func> Leaper<'leap, Tuple, Val> for ValueFilter<Tuple, Val, Func>
where
Func: Fn(&Tuple, &Val) -> bool,
{
/// Estimates the number of proposed values.
fn count(&mut self, _prefix: &Tuple) -> usize {
usize::max_value()
}
/// Populates `values` with proposed values.
fn propose(&mut self, _prefix: &Tuple, _values: &mut Vec<&'leap Val>) {
panic!("PrefixFilter::propose(): variable apparently unbound");
}
/// Restricts `values` to proposed values.
fn intersect(&mut self, prefix: &Tuple, values: &mut Vec<&'leap Val>) {
values.retain(|val| (self.predicate)(prefix, val));
}
}
}
/// Extension method for relations.
pub trait RelationLeaper<Key: Ord, Val: Ord> {
/// Extend with `Val` using the elements of the relation.
fn extend_with<'leap, Tuple: Ord, Func: Fn(&Tuple) -> Key>(
&'leap self,
key_func: Func,
) -> extend_with::ExtendWith<'leap, Key, Val, Tuple, Func>
where
Key: 'leap,
Val: 'leap;
/// Extend with `Val` using the complement of the relation.
fn extend_anti<'leap, Tuple: Ord, Func: Fn(&Tuple) -> Key>(
&'leap self,
key_func: Func,
) -> extend_anti::ExtendAnti<'leap, Key, Val, Tuple, Func>
where
Key: 'leap,
Val: 'leap;
/// Extend with any value if tuple is present in relation.
fn filter_with<'leap, Tuple: Ord, Func: Fn(&Tuple) -> (Key, Val)>(
&'leap self,
key_func: Func,
) -> filter_with::FilterWith<'leap, Key, Val, Tuple, Func>
where
Key: 'leap,
Val: 'leap;
/// Extend with any value if tuple is absent from relation.
fn filter_anti<'leap, Tuple: Ord, Func: Fn(&Tuple) -> (Key, Val)>(
&'leap self,
key_func: Func,
) -> filter_anti::FilterAnti<'leap, Key, Val, Tuple, Func>
where
Key: 'leap,
Val: 'leap;
}
impl<Key: Ord, Val: Ord> RelationLeaper<Key, Val> for Relation<(Key, Val)> {
fn extend_with<'leap, Tuple: Ord, Func: Fn(&Tuple) -> Key>(
&'leap self,
key_func: Func,
) -> extend_with::ExtendWith<'leap, Key, Val, Tuple, Func>
where
Key: 'leap,
Val: 'leap,
{
extend_with::ExtendWith::from(self, key_func)
}
fn extend_anti<'leap, Tuple: Ord, Func: Fn(&Tuple) -> Key>(
&'leap self,
key_func: Func,
) -> extend_anti::ExtendAnti<'leap, Key, Val, Tuple, Func>
where
Key: 'leap,
Val: 'leap,
{
extend_anti::ExtendAnti::from(self, key_func)
}
fn filter_with<'leap, Tuple: Ord, Func: Fn(&Tuple) -> (Key, Val)>(
&'leap self,
key_func: Func,
) -> filter_with::FilterWith<'leap, Key, Val, Tuple, Func>
where
Key: 'leap,
Val: 'leap,
{
filter_with::FilterWith::from(self, key_func)
}
fn filter_anti<'leap, Tuple: Ord, Func: Fn(&Tuple) -> (Key, Val)>(
&'leap self,
key_func: Func,
) -> filter_anti::FilterAnti<'leap, Key, Val, Tuple, Func>
where
Key: 'leap,
Val: 'leap,
{
filter_anti::FilterAnti::from(self, key_func)
}
}
pub(crate) mod extend_with {
use super::{binary_search, Leaper, Leapers, Relation};
use crate::join::gallop;
/// Wraps a Relation<Tuple> as a leaper.
pub struct ExtendWith<'leap, Key, Val, Tuple, Func>
where
Key: Ord + 'leap,
Val: Ord + 'leap,
Tuple: Ord,
Func: Fn(&Tuple) -> Key,
{
relation: &'leap Relation<(Key, Val)>,
start: usize,
end: usize,
key_func: Func,
phantom: ::std::marker::PhantomData<Tuple>,
}
impl<'leap, Key, Val, Tuple, Func> ExtendWith<'leap, Key, Val, Tuple, Func>
where
Key: Ord + 'leap,
Val: Ord + 'leap,
Tuple: Ord,
Func: Fn(&Tuple) -> Key,
{
/// Constructs a ExtendWith from a relation and key and value function.
pub fn from(relation: &'leap Relation<(Key, Val)>, key_func: Func) -> Self {
ExtendWith {
relation,
start: 0,
end: 0,
key_func,
phantom: ::std::marker::PhantomData,
}
}
}
impl<'leap, Key, Val, Tuple, Func> Leaper<'leap, Tuple, Val>
for ExtendWith<'leap, Key, Val, Tuple, Func>
where
Key: Ord + 'leap,
Val: Ord + 'leap,
Tuple: Ord,
Func: Fn(&Tuple) -> Key,
{
fn count(&mut self, prefix: &Tuple) -> usize {
let key = (self.key_func)(prefix);
self.start = binary_search(&self.relation[..], |x| &x.0 < &key);
let slice1 = &self.relation[self.start..];
let slice2 = gallop(slice1, |x| &x.0 <= &key);
self.end = self.relation.len() - slice2.len();
slice1.len() - slice2.len()
}
fn propose(&mut self, _prefix: &Tuple, values: &mut Vec<&'leap Val>) {
let slice = &self.relation[self.start..self.end];
values.extend(slice.iter().map(|&(_, ref val)| val));
}
fn intersect(&mut self, _prefix: &Tuple, values: &mut Vec<&'leap Val>) {
let mut slice = &self.relation[self.start..self.end];
values.retain(|v| {
slice = gallop(slice, |kv| &kv.1 < v);
slice.get(0).map(|kv| &kv.1) == Some(v)
});
}
}
impl<'leap, Key, Val, Tuple, Func> Leapers<'leap, Tuple, Val>
for ExtendWith<'leap, Key, Val, Tuple, Func>
where
Key: Ord + 'leap,
Val: Ord + 'leap,
Tuple: Ord,
Func: Fn(&Tuple) -> Key,
{
fn for_each_count(&mut self, tuple: &Tuple, mut op: impl FnMut(usize, usize)) {
op(0, self.count(tuple))
}
fn propose(&mut self, tuple: &Tuple, min_index: usize, values: &mut Vec<&'leap Val>) {
assert_eq!(min_index, 0);
Leaper::propose(self, tuple, values);
}
fn intersect(&mut self, _: &Tuple, min_index: usize, _: &mut Vec<&'leap Val>) {
assert_eq!(min_index, 0);
}
}
}
pub(crate) mod extend_anti {
use super::{binary_search, Leaper, Relation};
use crate::join::gallop;
/// Wraps a Relation<Tuple> as a leaper.
pub struct ExtendAnti<'leap, Key, Val, Tuple, Func>
where
Key: Ord + 'leap,
Val: Ord + 'leap,
Tuple: Ord,
Func: Fn(&Tuple) -> Key,
{
relation: &'leap Relation<(Key, Val)>,
key_func: Func,
phantom: ::std::marker::PhantomData<Tuple>,
}
impl<'leap, Key, Val, Tuple, Func> ExtendAnti<'leap, Key, Val, Tuple, Func>
where
Key: Ord + 'leap,
Val: Ord + 'leap,
Tuple: Ord,
Func: Fn(&Tuple) -> Key,
{
/// Constructs a ExtendAnti from a relation and key and value function.
pub fn from(relation: &'leap Relation<(Key, Val)>, key_func: Func) -> Self {
ExtendAnti {
relation,
key_func,
phantom: ::std::marker::PhantomData,
}
}
}
impl<'leap, Key: Ord, Val: Ord + 'leap, Tuple: Ord, Func> Leaper<'leap, Tuple, Val>
for ExtendAnti<'leap, Key, Val, Tuple, Func>
where
Key: Ord + 'leap,
Val: Ord + 'leap,
Tuple: Ord,
Func: Fn(&Tuple) -> Key,
{
fn count(&mut self, _prefix: &Tuple) -> usize {
usize::max_value()
}
fn propose(&mut self, _prefix: &Tuple, _values: &mut Vec<&'leap Val>) {
panic!("ExtendAnti::propose(): variable apparently unbound.");
}
fn intersect(&mut self, prefix: &Tuple, values: &mut Vec<&'leap Val>) {
let key = (self.key_func)(prefix);
let start = binary_search(&self.relation[..], |x| &x.0 < &key);
let slice1 = &self.relation[start..];
let slice2 = gallop(slice1, |x| &x.0 <= &key);
let mut slice = &slice1[..(slice1.len() - slice2.len())];
if !slice.is_empty() {
values.retain(|v| {
slice = gallop(slice, |kv| &kv.1 < v);
slice.get(0).map(|kv| &kv.1) != Some(v)
});
}
}
}
}
pub(crate) mod filter_with {
use super::{Leaper, Leapers, Relation};
/// Wraps a Relation<Tuple> as a leaper.
pub struct FilterWith<'leap, Key, Val, Tuple, Func>
where
Key: Ord + 'leap,
Val: Ord + 'leap,
Tuple: Ord,
Func: Fn(&Tuple) -> (Key, Val),
{
relation: &'leap Relation<(Key, Val)>,
key_func: Func,
phantom: ::std::marker::PhantomData<Tuple>,
}
impl<'leap, Key, Val, Tuple, Func> FilterWith<'leap, Key, Val, Tuple, Func>
where
Key: Ord + 'leap,
Val: Ord + 'leap,
Tuple: Ord,
Func: Fn(&Tuple) -> (Key, Val),
{
/// Constructs a FilterWith from a relation and key and value function.
pub fn from(relation: &'leap Relation<(Key, Val)>, key_func: Func) -> Self {
FilterWith {
relation,
key_func,
phantom: ::std::marker::PhantomData,
}
}
}
impl<'leap, Key, Val, Val2, Tuple, Func> Leaper<'leap, Tuple, Val2>
for FilterWith<'leap, Key, Val, Tuple, Func>
where
Key: Ord + 'leap,
Val: Ord + 'leap,
Tuple: Ord,
Func: Fn(&Tuple) -> (Key, Val),
{
fn count(&mut self, prefix: &Tuple) -> usize {
let key_val = (self.key_func)(prefix);
if self.relation.binary_search(&key_val).is_ok() {
usize::max_value()
} else {
0
}
}
fn propose(&mut self, _prefix: &Tuple, _values: &mut Vec<&'leap Val2>) {
panic!("FilterWith::propose(): variable apparently unbound.");
}
fn intersect(&mut self, _prefix: &Tuple, _values: &mut Vec<&'leap Val2>) {
// Only here because we didn't return zero above, right?
}
}
impl<'leap, Key, Val, Tuple, Func> Leapers<'leap, Tuple, ()>
for FilterWith<'leap, Key, Val, Tuple, Func>
where
Key: Ord + 'leap,
Val: Ord + 'leap,
Tuple: Ord,
Func: Fn(&Tuple) -> (Key, Val),
{
fn for_each_count(&mut self, tuple: &Tuple, mut op: impl FnMut(usize, usize)) {
if <Self as Leaper<Tuple, ()>>::count(self, tuple) == 0 {
op(0, 0)
} else {
op(0, 1)
}
}
fn propose(&mut self, _: &Tuple, min_index: usize, values: &mut Vec<&'leap ()>) {
assert_eq!(min_index, 0);
values.push(&());
}
fn intersect(&mut self, _: &Tuple, min_index: usize, values: &mut Vec<&'leap ()>) {
assert_eq!(min_index, 0);
assert_eq!(values.len(), 1);
}
}
}
pub(crate) mod filter_anti {
use super::{Leaper, Leapers, Relation};
/// Wraps a Relation<Tuple> as a leaper.
pub struct FilterAnti<'leap, Key, Val, Tuple, Func>
where
Key: Ord + 'leap,
Val: Ord + 'leap,
Tuple: Ord,
Func: Fn(&Tuple) -> (Key, Val),
{
relation: &'leap Relation<(Key, Val)>,
key_func: Func,
phantom: ::std::marker::PhantomData<Tuple>,
}
impl<'leap, Key, Val, Tuple, Func> FilterAnti<'leap, Key, Val, Tuple, Func>
where
Key: Ord + 'leap,
Val: Ord + 'leap,
Tuple: Ord,
Func: Fn(&Tuple) -> (Key, Val),
{
/// Constructs a FilterAnti from a relation and key and value function.
pub fn from(relation: &'leap Relation<(Key, Val)>, key_func: Func) -> Self {
FilterAnti {
relation,
key_func,
phantom: ::std::marker::PhantomData,
}
}
}
impl<'leap, Key: Ord, Val: Ord + 'leap, Val2, Tuple: Ord, Func> Leaper<'leap, Tuple, Val2>
for FilterAnti<'leap, Key, Val, Tuple, Func>
where
Key: Ord + 'leap,
Val: Ord + 'leap,
Tuple: Ord,
Func: Fn(&Tuple) -> (Key, Val),
{
fn count(&mut self, prefix: &Tuple) -> usize {
let key_val = (self.key_func)(prefix);
if self.relation.binary_search(&key_val).is_ok() {
0
} else {
usize::max_value()
}
}
fn propose(&mut self, _prefix: &Tuple, _values: &mut Vec<&'leap Val2>) {
panic!("FilterAnti::propose(): variable apparently unbound.");
}
fn intersect(&mut self, _prefix: &Tuple, _values: &mut Vec<&'leap Val2>) {
// Only here because we didn't return zero above, right?
}
}
impl<'leap, Key, Val, Tuple, Func> Leapers<'leap, Tuple, ()>
for FilterAnti<'leap, Key, Val, Tuple, Func>
where
Key: Ord + 'leap,
Val: Ord + 'leap,
Tuple: Ord,
Func: Fn(&Tuple) -> (Key, Val),
{
fn for_each_count(&mut self, tuple: &Tuple, mut op: impl FnMut(usize, usize)) {
if <Self as Leaper<Tuple, ()>>::count(self, tuple) == 0 {
op(0, 0)
} else {
op(0, 1)
}
}
fn propose(&mut self, _: &Tuple, min_index: usize, values: &mut Vec<&'leap ()>) {
// We only get here if `tuple` is *not* a member of `self.relation`
assert_eq!(min_index, 0);
values.push(&());
}
fn intersect(&mut self, _: &Tuple, min_index: usize, values: &mut Vec<&'leap ()>) {
// We only get here if `tuple` is not a member of `self.relation`
assert_eq!(min_index, 0);
assert_eq!(values.len(), 1);
}
}
}
fn binary_search<T>(slice: &[T], mut cmp: impl FnMut(&T) -> bool) -> usize {
// we maintain the invariant that `lo` many elements of `slice` satisfy `cmp`.
// `hi` is maintained at the first element we know does not satisfy `cmp`.
let mut hi = slice.len();
let mut lo = 0;
while lo < hi {
let mid = lo + (hi - lo) / 2;
if cmp(&slice[mid]) {
lo = mid + 1;
} else {
hi = mid;
}
}
lo
}
|