1/* Part of SWI-Prolog 2 3 Author: Jon Jagger 4 E-mail: J.R.Jagger@shu.ac.uk 5 Copyright (c) 1993-2011, Jon Jagger 6 All rights reserved. 7 8 Redistribution and use in source and binary forms, with or without 9 modification, are permitted provided that the following conditions 10 are met: 11 12 1. Redistributions of source code must retain the above copyright 13 notice, this list of conditions and the following disclaimer. 14 15 2. Redistributions in binary form must reproduce the above copyright 16 notice, this list of conditions and the following disclaimer in 17 the documentation and/or other materials provided with the 18 distribution. 19 20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 24 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 30 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 POSSIBILITY OF SUCH DAMAGE. 32*/ 33 34:- module(oset, 35 [ oset_is/1, 36 oset_union/3, 37 oset_int/3, 38 oset_diff/3, 39 oset_dint/2, 40 oset_dunion/2, 41 oset_addel/3, 42 oset_delel/3, 43 oset_power/2 44 ]). 45:- autoload(library(lists), [reverse/2]). 46:- autoload(library(ordsets), 47 [ is_ordset/1, 48 ord_union/3, 49 ord_intersection/3, 50 ord_subtract/3, 51 ord_add_element/3, 52 ord_del_element/3, 53 ord_union/2, 54 ord_intersection/2 55 ]).
74oset_is(OSet) :-
75 is_ordset(OSet).
83oset_union(OSet1, OSet2, Union) :-
84 ord_union(OSet1, OSet2, Union).
90oset_int(Set1, Set2, Intersection) :-
91 ord_intersection(Set1, Set2, Intersection).
99oset_diff(InOSet, NotInOSet, Diff) :-
100 ord_subtract(InOSet, NotInOSet, Diff).
108oset_dunion(SetofSets, DUnion) :-
109 ord_union(SetofSets, DUnion).
117oset_dint(SetofSets, DInt) :-
118 ord_intersection(SetofSets, DInt).
125oset_power(S, PSet) :- 126 reverse(S, R), 127 pset(R, [[]], PSet0), 128 sort(PSet0, PSet). 129 130% The powerset of a set is the powerset of a set of one smaller, 131% together with the set of one smaller where each subset is extended 132% with the new element. Note that this produces the elements of the set 133% in reverse order. Hence the reverse in oset_power/2. 134 135pset([], PSet, PSet). 136pset([H|T], PSet0, PSet) :- 137 happ(PSet0, H, PSet1), 138 pset(T, PSet1, PSet). 139 140happ([], _, []). 141happ([S|Ss], H, [[H|S],S|Rest]) :- 142 happ(Ss, H, Rest).
150oset_addel(Set1, Element, Set2) :-
151 ord_add_element(Set1, Element, Set2).
159oset_delel(Set, Element, NewSet) :-
160 ord_del_element(Set, Element, NewSet)
Ordered set manipulation
This library defines set operations on sets represented as ordered lists. This current library is a thin wrapper around library(ordsets). Many of the implementations of library(ordsets) originate from the library.