4
%% Copyright Ericsson AB 1998-2010. All Rights Reserved.
6
%% The contents of this file are subject to the Erlang Public License,
7
%% Version 1.1, (the "License"); you may not use this file except in
8
%% compliance with the License. You should have received a copy of the
9
%% Erlang Public License along with this software. If not, it can be
10
%% retrieved online at http://www.erlang.org/.
12
%% Software distributed under the License is distributed on an "AS IS"
13
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
14
%% the License for the specific language governing rights and limitations
21
%% Copyright (C) 1991, Ellemtel Telecommunications Systems Laboratories
23
%% Author : Robert Virding
24
%% Purpose : Functions for manipulating sets as ordered lists.
26
%% As yet some of these are not very efficiently written.
30
-export([new_set/0,is_set/1,set_to_list/1,list_to_set/1]).
31
-export([is_element/2,add_element/2,del_element/2]).
32
-export([union/2,union/1,intersection/2,intersection/1]).
33
-export([subtract/2,subset/2]).
36
%% Return a new empty ordered set.
42
%% Return 'true' if Set is an ordered set of elements, else 'false'.
49
is_set([E2|Es], E1) when E1 < E2 ->
51
is_set([E2|Es], E1) ->
56
%% set_to_list(OrdSet)
57
%% Return the elements in OrdSet as a list.
63
%% Build an ordered set from the elements in List.
65
list_to_set([E|Es]) ->
66
add_element(E, list_to_set(Es));
70
%% is_element(Element, OrdSet)
71
%% Return 'true' if Element is an element of OrdSet, else 'false'.
73
is_element(E, [H|Es]) when E < H ->
75
is_element(E, [H|Es]) when E == H ->
77
is_element(E, [H|Es]) when E > H ->
82
%% add_element(Element, OrdSet)
83
%% Return OrdSet with Element inserted in it.
85
add_element(E, [H|Es]) when E < H ->
87
add_element(E, [H|Es]) when E == H ->
89
add_element(E, [H|Es]) when E > H ->
90
[H|add_element(E, Es)];
94
%% del_element(Element, OrdSet)
95
%% Return OrdSet but with Element removed.
97
del_element(E, [H|Es]) when E < H ->
99
del_element(E, [H|Es]) when E == H ->
101
del_element(E, [H|Es]) when E > H ->
102
[H|del_element(E, Es)];
103
del_element(E, []) ->
107
%% Return the union of Set1 and Set2.
109
union([H1|Es1], [H2|Es2]) when H1 < H2 ->
110
[H1|union(Es1, [H2|Es2])];
111
union([H1|Es1], [H2|Es2]) when H1 == H2 ->
112
[H1|union(Es1, Es2)];
113
union([H1|Es1], [H2|Es2]) when H1 > H2 ->
114
[H2|union([H1|Es1], Es2)];
121
%% Return the union of the list of sets.
124
union1(union(S1,S2), Ss);
130
union1(S1, [S2|Ss]) ->
131
union1(union(S1, S2), Ss);
135
%% intersection(Set1, Set2)
136
%% Return the intersection of Set1 and Set2.
138
intersection([H1|Es1], [H2|Es2]) when H1 < H2 ->
139
intersection(Es1, [H2|Es2]);
140
intersection([H1|Es1], [H2|Es2]) when H1 == H2 ->
141
[H1|intersection(Es1, Es2)];
142
intersection([H1|Es1], [H2|Es2]) when H1 > H2 ->
143
intersection([H1|Es1], Es2);
144
intersection([], Es2) ->
146
intersection(Es1, []) ->
149
%% intersection(OrdSets)
150
%% Return the intersection of the list of sets.
152
intersection([S1,S2|Ss]) ->
153
intersection1(intersection(S1,S2), Ss);
159
intersection1(S1, [S2|Ss]) ->
160
intersection1(intersection(S1, S2), Ss);
161
intersection1(S1, []) ->
164
%% subtract(Set1, Set2)
165
%% Return all and only the elements of Set1 which are not also in Set2.
167
subtract([H1|Es1], [H2|Es2]) when H1 < H2 ->
168
[H1|subtract(Es1, [H2|Es2])];
169
subtract([H1|Es1], [H2|Es2]) when H1 == H2 ->
171
subtract([H1|Es1], [H2|Es2]) when H1 > H2 ->
172
subtract([H1|Es1], Es2);
178
%% subset(Set1, Set2)
179
%% Return 'true' when every element of Set1 is also a member of Set2,
182
subset([H1|Es1], [H2|Es2]) when H1 < H2 -> %H1 not in Set2
184
subset([H1|Es1], [H2|Es2]) when H1 == H2 ->
186
subset([H1|Es1], [H2|Es2]) when H1 > H2 ->
187
subset([H1|Es1], Es2);