-- ordered sets (linked list in decreasing order) by R J Botting, CSUSB with Text_io; use Text_io; with IIO; use IIO; --generic type ELEMENT is private; with "<" and Put. package body ORDERED_SETS is -- The elements are kept in decreasing order with no duplicates -- For all S, S=empty or S.first>S.rest.first>S.rest.rest.first>... -- by R J Botting, CSUSB -- OSET is access DATA and NULL_SET=OSET(NULL). type DATA is record FIRST:ELEMENT; REST:OSET:=NULL; end record; procedure Empty(S:out OSET) is begin S:=NULL; end; function EQ(I:ELEMENT; T:OSET) return boolean is begin return T/=null and then T.FIRST=I; end EQ; function FIND(I:ELEMENT; S:OSET) return OSET is--before place for I T1: OSET; T2: OSET; begin --AFTER: if Empty(S) then T1=NULL -- if S={J} then T1=(FIRST=>J, rest=>NULL) -- else T1/=NULL/=T1.rest and -- T1.FIRST > I >= T1.rest.FIRST T1:=S; T2:=T1; while T2/=null and then I=T2.rest.first and II, REST=>null); elsif INTO.FIRST = I then null; -- I is already in the set elsif INTO.FIRST < I then -- I belongs at the start of the set INTO:=new DATA'(FIRST=>I, REST=>INTO); else -- need to find T1 before place for I T1:=INTO; T1:=FIND(I,T1); T2:=T1.REST; if not EQ(I,T2) then T1.REST:=new DATA'(FIRST=>I, REST=>T2); end if; end if; end; procedure Take(I:ELEMENT; FROM:in out OSET) is T1,T2:OSET; begin if Empty(FROM) then null; elsif FROM.FIRST=I then FROM:=OSET(FROM.REST); else T1:=FROM; T2:=T1.REST; while T2/=null and then I