@@ -297,17 +297,19 @@ impl<T: Ord + Copy> Relation<T> {
297
297
{
298
298
extend_with:: ExtendWith :: from ( self , key_func)
299
299
}
300
- }
301
300
302
- impl < Key : Ord , Val : Ord > Relation < ( Key , Val ) > {
303
301
/// Extend with `Val` using the complement of the relation.
304
- pub fn extend_anti < ' leap , Tuple : Ord , Func : Fn ( & Tuple ) -> Key > (
305
- & ' leap self ,
306
- key_func : Func ,
307
- ) -> extend_anti:: ExtendAnti < ' leap , Key , Val , Tuple , Func >
308
- where
309
- Key : ' leap ,
310
- Val : ' leap ,
302
+ ///
303
+ /// This leaper *removes* all proposed values that appear as a suffix in `self` for the prefix
304
+ /// extracted from the source tuple via `key_func`.
305
+ ///
306
+ ///
307
+ ///
308
+ /// ```prolog
309
+ /// step_sibling(X, Y) :- parent(X, P), parent(Y, P), !biological_sibling(X, Y).
310
+ /// ```
311
+ pub fn extend_anti < P , F , S > ( & self , key_func : F ) -> extend_anti:: ExtendAnti < ' _ , P , T , F >
312
+ where F : Fn ( & S ) -> P
311
313
{
312
314
extend_anti:: ExtendAnti :: from ( self , key_func)
313
315
}
@@ -419,63 +421,45 @@ pub(crate) mod extend_anti {
419
421
420
422
use super :: { binary_search, Leaper , Relation } ;
421
423
use crate :: join:: gallop;
424
+ use crate :: Split ;
422
425
423
426
/// Wraps a Relation<Tuple> as a leaper.
424
- pub struct ExtendAnti < ' leap , Key , Val , Tuple , Func >
425
- where
426
- Key : Ord + ' leap ,
427
- Val : Ord + ' leap ,
428
- Tuple : Ord ,
429
- Func : Fn ( & Tuple ) -> Key ,
430
- {
431
- relation : & ' leap Relation < ( Key , Val ) > ,
432
- key_func : Func ,
433
- old_key : Option < ( Key , Range < usize > ) > ,
434
- phantom : :: std:: marker:: PhantomData < Tuple > ,
427
+ pub struct ExtendAnti < ' a , P , T , F > {
428
+ relation : & ' a Relation < T > ,
429
+ key_func : F ,
430
+ old_key : Option < ( P , Range < usize > ) > ,
435
431
}
436
432
437
- impl < ' leap , Key , Val , Tuple , Func > ExtendAnti < ' leap , Key , Val , Tuple , Func >
438
- where
439
- Key : Ord + ' leap ,
440
- Val : Ord + ' leap ,
441
- Tuple : Ord ,
442
- Func : Fn ( & Tuple ) -> Key ,
443
- {
433
+ impl < ' a , P , T , F > ExtendAnti < ' a , P , T , F > {
444
434
/// Constructs a ExtendAnti from a relation and key and value function.
445
- pub fn from ( relation : & ' leap Relation < ( Key , Val ) > , key_func : Func ) -> Self {
446
- ExtendAnti {
447
- relation,
448
- key_func,
449
- old_key : None ,
450
- phantom : :: std:: marker:: PhantomData ,
451
- }
435
+ pub fn from ( relation : & ' a Relation < T > , key_func : F ) -> Self {
436
+ ExtendAnti { relation, key_func, old_key : None }
452
437
}
453
438
}
454
439
455
- impl < ' leap , Key : Ord , Val : Ord + ' leap , Tuple : Ord , Func > Leaper < Tuple , Val >
456
- for ExtendAnti < ' leap , Key , Val , Tuple , Func >
440
+ impl < P , T , S , F > Leaper < S , T :: Suffix > for ExtendAnti < ' _ , P , T , F >
457
441
where
458
- Key : Ord + ' leap ,
459
- Val : Ord + ' leap ,
460
- Tuple : Ord ,
461
- Func : Fn ( & Tuple ) -> Key ,
442
+ T : Copy + Split < P > ,
443
+ P : Ord ,
444
+ T :: Suffix : Ord ,
445
+ F : Fn ( & S ) -> P ,
462
446
{
463
- fn count ( & mut self , _prefix : & Tuple ) -> usize {
447
+ fn count ( & mut self , _prefix : & S ) -> usize {
464
448
usize:: max_value ( )
465
449
}
466
- fn propose ( & mut self , _prefix : & Tuple , _values : & mut Vec < Val > ) {
450
+ fn propose ( & mut self , _prefix : & S , _values : & mut Vec < T :: Suffix > ) {
467
451
panic ! ( "ExtendAnti::propose(): variable apparently unbound." ) ;
468
452
}
469
- fn intersect ( & mut self , prefix : & Tuple , values : & mut Vec < Val > ) {
453
+ fn intersect ( & mut self , prefix : & S , values : & mut Vec < T :: Suffix > ) {
470
454
let key = ( self . key_func ) ( prefix) ;
471
455
472
456
let range = match self . old_key . as_ref ( ) {
473
457
Some ( ( old, range) ) if old == & key => range. clone ( ) ,
474
458
475
459
_ => {
476
- let start = binary_search ( & self . relation . elements , |x| & x. 0 < & key) ;
460
+ let start = binary_search ( & self . relation . elements , |x| & x. prefix ( ) < & key) ;
477
461
let slice1 = & self . relation [ start..] ;
478
- let slice2 = gallop ( slice1, |x| & x. 0 <= & key) ;
462
+ let slice2 = gallop ( slice1, |x| & x. prefix ( ) <= & key) ;
479
463
let range = start..self . relation . len ( ) -slice2. len ( ) ;
480
464
481
465
self . old_key = Some ( ( key, range. clone ( ) ) ) ;
@@ -487,8 +471,8 @@ pub(crate) mod extend_anti {
487
471
let mut slice = & self . relation [ range] ;
488
472
if !slice. is_empty ( ) {
489
473
values. retain ( |v| {
490
- slice = gallop ( slice, |kv| & kv. 1 < v) ;
491
- slice. get ( 0 ) . map ( |kv| & kv. 1 ) != Some ( v)
474
+ slice = gallop ( slice, |kv| & kv. suffix ( ) < v) ;
475
+ slice. get ( 0 ) . map ( |kv| kv. suffix ( ) ) . as_ref ( ) != Some ( v)
492
476
} ) ;
493
477
}
494
478
}
0 commit comments