diff --git a/library/ordsets.yap b/library/ordsets.yap index cf5d26497..72ee3e9e4 100644 --- a/library/ordsets.yap +++ b/library/ordsets.yap @@ -28,6 +28,7 @@ ord_intersect/2, % Set x Set -> ord_intersect/3, % Set x Set -> Set ord_intersection/3, % Set x Set -> Set + ord_intersection/4, % Set x Set -> Set x Set ord_seteq/2, % Set x Set -> ord_setproduct/3, % Set x Set -> Set ord_subset/2, % Set x Set -> @@ -170,6 +171,26 @@ ord_intersection(>, Head1, Tail1, _, Tail2, Intersection) :- + +% ord_intersection(+Set1, +Set2, ?Intersection, ?Difference) +% is true when Intersection is the ordered representation of Set1 +% and Set2, provided that Set1 and Set2 are ordered sets. + +ord_intersection(L, [], [], L) :- !. +ord_intersection([], L, [], L) :- !. +ord_intersection([Head1|Tail1], [Head2|Tail2], Intersection, Difference) :- + compare(Order, Head1, Head2), + ord_intersection(Order, Head1, Tail1, Head2, Tail2, Intersection, Difference). + +ord_intersection(=, Head, Tail1, _, Tail2, [Head|Intersection], Difference) :- + ord_intersection(Tail1, Tail2, Intersection, Difference). +ord_intersection(<, Head1, Tail1, Head2, Tail2, Intersection, [Head1|Difference]) :- + ord_intersection(Tail1, [Head2|Tail2], Intersection, Difference). +ord_intersection(>, Head1, Tail1, Head2, Tail2, Intersection, [Head2|Difference]) :- + ord_intersection([Head1|Tail1], Tail2, Intersection, Difference). + + + % ord_seteq(+Set1, +Set2) % is true when the two arguments represent the same set. Since they % are assumed to be ordered representations, they must be identical.