1#############################################################################
2##
3##  This file is part of GAP, a system for computational discrete algebra.
4##  This file's authors include Martin Schönert.
5##
6##  Copyright of GAP belongs to its developers, whose names are too numerous
7##  to list here. Please refer to the COPYRIGHT file for details.
8##
9##  SPDX-License-Identifier: GPL-2.0-or-later
10##
11
12
13#############################################################################
14##
15#M  IsEqualSet( <list1>, <list2> )  . . . . . . . . . . . . . . for two lists
16##
17InstallMethod( IsEqualSet,
18    "for two lists",
19    true,
20    [ IsList, IsList ], 0,
21    function( list1, list2 )
22    return Set( list1 ) = Set( list2 );
23    end );
24
25
26#############################################################################
27##
28#M  IsEqualSet( <list1>, <list2> )  . .  for two internally represented lists
29##
30InstallMethod( IsEqualSet,
31    "for two internally represented lists",
32    true,
33    [ IsList and IsInternalRep, IsList and IsInternalRep ], 0,
34    IS_EQUAL_SET );
35
36
37#############################################################################
38##
39#M  IsSubsetSet( <list1>, <list2> ) . . . . . . . . . . . . . . for two lists
40##
41InstallMethod( IsSubsetSet,
42    "for two lists",
43    true,
44    [ IsList, IsList ], 0,
45    function( list1, list2 )
46    list1:= Set( list1 );
47    return ForAll( Set( list2 ), x -> x in list1 );
48    end );
49
50
51#############################################################################
52##
53#M  IsSubsetSet( <list1>, <list2> ) . .  for two internally represented lists
54##
55InstallMethod( IsSubsetSet,
56    "for two internally represented lists",
57    true,
58    [ IsList and IsInternalRep, IsList and IsInternalRep ], 0,
59    IS_SUBSET_SET );
60
61
62#############################################################################
63##
64#M  AddSet( <set>, <obj> )  . . . . . . . . . .  for mutable list, and object
65##
66InstallMethod( AddSet,
67    "for mutable list, and object",
68    true,
69    [ IsList and IsMutable, IsObject ], 0,
70    function( set, obj )
71    local pos, len;
72    if not IsSSortedList( set ) then
73      Error( "<set> must be a mutable proper set" );
74    fi;
75    pos:= PositionSorted( set, obj );
76    if pos>Length(set) then
77      set[ pos ]:= obj;
78    elif set[ pos ] <> obj then
79      len:= Length( set );
80      set{ [ pos+1 .. len+1 ] }:= set{ [ pos .. len ] };
81      set[ pos ]:= obj;
82    fi;
83    end );
84
85
86#############################################################################
87##
88#M  AddSet( <set>, <obj> )  . . . . . for mutable int. repr. list, and object
89##
90InstallMethod( AddSet,
91    "for mutable internally represented list, and object",
92    true,
93    [ IsList and IsInternalRep and IsMutable, IsObject ], 0,
94    ADD_SET );
95
96
97#############################################################################
98##
99#M  RemoveSet( <set>, <obj> ) . . . . for mutable int. repr. list, and object
100##
101InstallMethod( RemoveSet,
102    "for mutable internally represented list, and object",
103    true,
104    [ IsList and IsInternalRep and IsMutable, IsObject ], 0,
105    REM_SET );
106
107
108#############################################################################
109##
110#M  RemoveSet( <set>, <obj> ) . . . . . . . . .  for mutable list, and object
111##
112InstallMethod( RemoveSet,
113    "for mutable list, and object",
114    true,
115    [ IsList and IsMutable, IsObject ], 0,
116    function( set, obj )
117    local pos, len;
118    if not IsSSortedList( set ) then
119      Error( "<set> must be a mutable proper set" );
120    fi;
121    pos:= PositionSorted( set, obj );
122    len:= Length( set );
123    if pos <= len and set[ pos ] = obj then
124      set{ [ pos .. len-1 ] }:= set{ [ pos+1 .. len ] };
125      Unbind( set[ len ] );
126    fi;
127    end );
128
129
130#############################################################################
131##
132#M  UniteSet( <set>, <list> ) . . . . .  for two internally represented lists
133##
134InstallMethod( UniteSet,
135    "for two internally represented lists, the first being mutable",
136    true,
137    [ IsList and IsInternalRep and IsMutable, IsList and IsInternalRep ], 0,
138    UNITE_SET );
139
140
141#############################################################################
142##
143#M  UniteSet( <set>, <list> ) . . . . . . .  for two lists, the first mutable
144##
145InstallMethod( UniteSet,
146    "for two lists, the first being mutable",
147    true,
148    [ IsList and IsMutable, IsList ], 0,
149    function( set, list )
150    local obj;
151    if not IsSSortedList( set ) then
152      Error( "<set> must be a mutable proper set" );
153    fi;
154    for obj in list do
155      AddSet( set, obj );
156    od;
157    end );
158
159
160#############################################################################
161##
162#M  IntersectSet( <set>, <list> ) . . .  for two internally represented lists
163##
164InstallMethod( IntersectSet,
165    "for two internally represented lists, the first being mutable",
166    true,
167    [ IsList and IsInternalRep and IsMutable, IsList and IsInternalRep ], 0,
168    INTER_SET );
169
170
171#############################################################################
172##
173#M  IntersectSet( <set>, <list> ) . . . . .  for two lists, the first mutable
174##
175InstallMethod( IntersectSet,
176    "for two lists, the first being mutable",
177    true,
178    [ IsList and IsMutable, IsList ], 0,
179    function( set, list )
180    local obj,i;
181    if not IsSSortedList( set ) then
182      Error( "<set> must be a mutable proper set" );
183    fi;
184    i:= 1;
185    while i <= Length( set ) do
186      obj:= set[i];
187      if not obj in list then
188          RemoveSet( set, obj );
189      else
190          i:= i+1;
191      fi;
192    od;
193
194    end );
195
196
197#############################################################################
198##
199#M  SubtractSet( <set>, <list> )  . . .  for two internally represented lists
200##
201InstallMethod( SubtractSet,
202    "for two internally represented lists, the first being mutable",
203    true,
204    [ IsList and IsInternalRep and IsMutable, IsList and IsInternalRep ], 0,
205    SUBTR_SET );
206
207
208#############################################################################
209##
210#M  SubtractSet( <set>, <list> )  . . . . .  for two lists, the first mutable
211##
212InstallMethod( SubtractSet,
213    "for two lists, the first being mutable",
214    true,
215    [ IsList and IsMutable, IsList ], 0,
216    function( set, list )
217    local obj;
218    if not IsSSortedList( set ) then
219      Error( "<set> must be a mutable proper set" );
220    fi;
221    for obj in list do
222      RemoveSet( set, obj );
223    od;
224    end );
225