program Project47; {$APPTYPE CONSOLE} uses SysUtils; type TByteSet=set of Byte; function NextElementFrom(const ByteSet: TByteSet; fromV:smallint=0; toVal:byte=255 ): smallint; var bptr:AnsiChar absolute ByteSet; b: Byte; dwSize,dwordIt, dwStart:integer; curVal, mindwrd:Cardinal; label done; begin dwSize:= ( SizeOf(ByteSet)+3) shr 2; result:=(fromV); dwStart:=result shr 5; result:= result and $FFE0; fromV:=fromV and 31; mindwrd:= $FFFFFFFF shl fromV; for dwordIt := dwStart to dwSize do begin curVal:=PCardinal( PAnsiChar(@bptr)+dwordIt*4)^; if (fromV>0) then curval:=curVal and mindwrd; if curVal=0 then begin Inc(result,4*8); fromV:=0; Continue end; if (Word(curVal)=0)then begin curVal:=curVal shr 16; Inc(result, 16); end; b:=Lo(curVal); if (b=0) then begin b:=Hi(curVal); Inc(result, 8); end; if (b and $F=0)then begin b:=b shr 4; Inc(result, 4); end; if Result>toVal then begin result:=-1; Exit; end; if (b and $3<>0) then begin//and if (b and $1=0) then inc(Result); end else begin if b and $C<>0 then begin if b and $4=0 then Inc(result,3) else Inc(Result,2); end else result:=-1 end; done: if result>toVal then result:=-1; Assert( (fromV<>0) or (result>=0) ); exit; end;//for // No elements set result:=-1; end; var bs:TByteSet; bt:smallint; i,j:SmallInt; begin try bs:=[2]; bt:=NextElementFrom(bs,0); for i := 0 to 255 do begin for j := 0 to 255 do begin bs:=[i]; bt:=NextElementFrom(bs,j); Assert( ( (j>i) and (bt=-1) ) xor (bt=i) ) ; end; end; bs:=[]; for i := 0 to 255 do Include(bs,i); for i := 0 to 255 do begin bt:=NextElementFrom(bs,i); Assert(i=bt); end; writeln(bt); { TODO -oUser -cConsole Main : Insert code here } except on E:Exception do Writeln(E.Classname, ': ', E.Message); end; end. |