with Text_io; use Text_io; package body INTEGER_SETS is -- implemented by amorphous set(Botting 78) -- LINK is access DATA, INTEGER_SETS renames LINK -- NULL_SET = INTEGER_SET(NULL) -- DANGER - equality betwwen LINKS and INTEGER_SETs is defined locally. package IIO is new Text_IO.Integer_io(integer); --need integer output use iio; n:constant INTEGER:=200; type ITEMS is array(1..N) of INTEGER; type DATA is record LASTI:INTEGER range 0..N:=0; ITEM: ITEMS:=ITEMS'(1..N=>0); end record; procedure Make_Empty(S:out INTEGER_SET) is -- after S=NULL_SET begin S:=NULL; end; function Member(I:integer; ISIN:INTEGER_SET) return boolean is begin if Empty(ISIN) then return false; end if; for P in 1..ISIN.LASTI loop if ISIN.ITEM(P)=I then return true; end if; end loop; return false; end Member; function Empty(ISIN:INTEGER_SET) return boolean is begin return ISIN.LASTI=0; exception when CONSTRAINT_ERROR => return true; -- ISIN=NULL end Empty; function Full(ISIN:INTEGER_SET) return boolean is begin return not Empty(ISIN) and then ISIN.LASTI>=N; end Full; function Max(S:INTEGER_SET) return integer is M:INTEGER; begin if Empty(S) then raise EMPTY_GET; else M:=S.ITEM(S.ITEM'FIRST); for P in INTEGER'SUCC(S.ITEM'FIRST)..S.LASTI loop if MS.ITEM(P) then M:=S.ITEM(P); end if; end loop; return M; end if; end MIN; procedure Put(S:in INTEGER_SET) is -- output set to standard output begin Put('{'); if not Empty(S) then -- one or more items (S.ITEN(1...)) Put(S.ITEM(S.ITEM'FIRST)); for I in INTEGER'SUCC(S.ITEM'FIRST)..S.LASTI loop Put(", "); Put(S.ITEM(I)); end loop; end if; Put('}'); end Put; procedure Put(I:integer; INTO:in out INTEGER_SET) is -- put I into set S begin declare DUMMY:DATA; begin DUMMY:=INTO.all; exception when CONSTRAINT_ERROR => INTO:=new DATA; end; if INTO.LASTI=N then raise FULL_PUT; end if; INTO.LASTI:=INTO.LASTI+1; INTO.ITEM(INTO.LASTI):=I; end Put; procedure Take(I: integer; FROM:in out INTEGER_SET) is--take I out begin if Empty(FROM) then return; end if; for P in FROM.ITEM'FIRST..FROM.LASTI loop if FROM.ITEM(P)=I then FROM.ITEM(P):=FROM.ITEM(FROM.LASTI); FROM.LASTI:=FROM.LASTI-1; return; end if; end loop; end Take; procedure Getmin(I: out integer; FROM:in out INTEGER_SET) is M:integer:=MIN(FROM); begin I:=M; TAKE(M,FROM); end Getmin; procedure GetMax(I: out integer; FROM: in out INTEGER_SET) is M:integer:=MAX(FROM); begin I:=M; TAKE(M,FROM); end GetMax; procedure Get(I: out integer; FROM:in out INTEGER_SET) is begin if Empty(FROM) then raise EMPTY_GET; else I:=FROM.ITEM(FROM.LASTI); FROM.LASTI:=FROM.LASTI-1; end if; if FROM.LASTI=0 then FROM:=NULL; end if; end Get; function "="(LEFT,RIGHT:INTEGER_SET) return boolean is begin if Empty(LEFT) or Empty(RIGHT) then return Empty(LEFT) and EMPTY(RIGHT); end if; if LEFT.LASTI /= RIGHT.LASTI then return FALSE; end if; -- here, LEFT and RIGHT have the same number of elements -- they may be in different orders. for i in LEFT.ITEM'FIRST..LEFT.LASTI loop if not member(LEFT.ITEM(I), RIGHT) or not member(RIGHT.ITEM(I), LEFT) then return false; end if; end loop; end "="; end INTEGER_SETS;