library(ordsets)
In this group of predicates, sets are represented by ordered lists with no
duplicates. Thus {c,r,a,f,t} would be [a,c,f,r,t]
. The ordering
is defined by the @<
family of term comparison predicates, and
is the ordering used by the built-in predicates sort/2
and setof/3
.
Note that
sort/2
and setof/3
produce
ordered sets as their results. See ref-all for more
information.
The benefit of the ordered representation is that the elementary set operations can be done in time proportional to the sum of the argument sizes rather than their product.
A number of predicates described elsewhere can be used on unordered sets.
Examples are length/2
(built-in; see ref-lte),
member/2
(from library(basics)
; see lib-lis-basics-member),
subseq1/2
(from library(lists)
; see lib-lis-lists), select/3
(from
library(sets)
; see lib-lis-set-sets), and sublist/3
(from library(maplist)
; see lib-abs).
is_ordset(
+List)
T1 @< T2 @< ... @< Tn
. The
output of sort/2
and setof/3
always satisfies this test.
Anything that satisfies this test can be given to the predicates in
library(ordsets)
, regardless of how it was generated.
list_to_ord_set(
+List,
?Set)
sort/2
.)
ord_add_element(
+Set1,
+Element,
?Set2)
Set2 =
Set1 U {
Element}
.
It only works this way
around. ord_add_element/3
is the ordered equivalent of add_element/3
(lib-lis-set-sets).
ord_del_element(
+Set1,
+Element,
?Set2)
ord_del_element/3
is the ordered equivalent of del_element/3
(lib-lis-set-sets).
ord_disjoint(
+Set1,
+Set2)
ord_intersect(
+Set1,
+Set2)
==
rather than =
.
ord_intersect(
+Set1,
+Set2,
?Intersection)
ord_intersection/3
.
It should not be used in new programs.
ord_intersection(
+Set1,
+Set2,
?Intersection)
ord_intersection(
+Sets,
?Intersection)
ord_seteq(
+Set1,
+Set2)
ord_setproduct(
+Set1,
+Set2,
?Product)
| ?- list_to_ord_set([t,o,y], Set1), | list_to_ord_set([d,o,g], Set2), | ord_setproduct(Set1, Set2, Product). Set1 = [o,t,y], Set2 = [d,g,o], Product = [o-d,o-g,o-o,t-d,t-g,t-o,y-d,y-g,y-o] | ?- % but with unordered arguments: | ord_setproduct([t,o,y], [d,o,g], Product). Product = [t-d,t-o,t-g,o-d,o-o,o-g,y-d,y-o,y-g]
ord_subset(
+Set1,
+Set2)
subseq0/2
family from library(lists)
(lib-lis-lists).
ord_subtract(
+Set1,
+Set2,
?Difference)
ord_symdiff(
+Set1,
+Set2,
?Difference)
ord_union(
+Set1,
+Set2,
?Union)
ord_union(
+Sets,
?Union)
ord_union/2
can be seen as
a generalization of sort/2
.
ord_union(
+OldSet,
+NewSet,
?Union,
?ReallyNew)
If operations on ordered sets or ordered lists are useful to you,
you may also find library(ordered)
(lib-abs)
or library(ordprefix)
(lib-abs)
of interest.