[fpc-pascal]How to get the values stored in a set

memsom at interalpha.co.uk memsom at interalpha.co.uk
Tue Dec 3 11:47:39 CET 2002


> I'd like to know how to get the values of a set.
> For example:
> Type TSet = set of 1..50;
> Var Test:TSet;
> 
> begin
>      Test:=[12,25,15]; //set with some alleatory values
>      ?? //How can i know that this set has 3 itens and the itens are 12, 
> 25 and 15?
> end;


Test:=[12, 25, 15, 24, 30, 10, 18, 6]; 

// member operator 'in'
// A is a member of [B]
if (12 in Test) then begin
  //you got 12...
end;

//subset operator '<='
// [A] is a subset of [B] ([B] contains [A])
if ([12, 25, 15] <= Test) then begin
  //12, 25, and 15 are in the set...
end;

//superset operator '>='
// [B] is a superset of [A]
// ([B] contains (amoungst others) the contents of [A])
if (Test >= [12, 25, 15]) then begin
  //Test contains 12, 25, 15, amoungst others..
end;

Please note theat in my explanations I use '[]' where the variable must be a 
set (or a set variable) and no '[]' where is must be a variable of the type the 
set contains. I don't know of is simple way to work out the count, though you 
could write a routine to do this yourself:

function TSetCount(x: TSet): word;
type
  TSetBounds = 1..50;
var
  i: TSetBounds;
begin
  Result := 0;
  for i := low(TSetBounds) to high (TSetBounds) do begin
    if i in x then inc(result);
  end;
end;

However, this will not tell you what the contents is. 

Hope that helps,

Matt



---------------------------------------------
This message was sent using Mistral WebMail.
http://www.mistral.co.uk/






More information about the fpc-pascal mailing list