numerosity of a set
Author Message
numerosity of a set

Is there a way (possibly a built in function) to know the number of
elements in a set in Turbo Pascal ?

Thanks Lauro

Wed, 18 Jun 1902 08:00:00 GMT
numerosity of a set

Quote:
>Subject: numerosity of a set

>Date: Tue, Oct 14, 1997 17:08 EDT

>Is there a way (possibly a built in function) to know the number of
>elements in a set in Turbo Pascal ?

>Thanks Lauro

The proper terminology for "numerosity" is the set's cardinal number or
cardinality.

Unfortunately, there isn't really anyway to do so except to test for each of
the element in the range of the type of set and see if they are in the set.
For example, if the set is of type SET OF MyType where MyType is defined as
5..17, then you'll have to write a loop and test if each of 5 through 17 is in
the set.

A slightly faster trick is also available (can be improve using asm but I don't
have the time to do so):

[untested]
(TYPE  SetPtrType = RECORD
CASE Boolean OF
TRUE :  (Pntr = ^Byte);
FALSE : (PtrPart = RECORD

Offset : WORD;

Segment : WORD;

END);
END;

VAR SetPtr : SetPtrType;
SByte : BYTE;)

Cardinality := 0;

FOR ByteCount := 1 TO SizeOf (MySet) DO

BEGIN
SByte  := SetPtr.Pntr^;
FOR BitCount := 0 TO 7 DO
BEGIN
Cardinality := Cardinality + (SByte AND 1);
SByte := SByte SHR 2
END;
INC (SByte.PtrPart.Offset := SByte.PtrPart.Offset)
END;

Wed, 18 Jun 1902 08:00:00 GMT
numerosity of a set

In your letter, you wrote following instructions. The loop FOR BitCount...
must be executed 8 times. Try my instructions, my loop executes for each
bit set in SByte. Instruction SByte := SByte AND (SByte-1) cuts off the
lower bit set in byte.

Quote:
> FOR ByteCount := 1 TO SizeOf (MySet) DO

>     BEGIN
>          SByte  := SetPtr.Pntr^;
>          FOR BitCount := 0 TO 7 DO
>               BEGIN
>                    Cardinality := Cardinality + (SByte AND 1);
>                    SByte := SByte SHR 2
>               END;
>          INC (SByte.PtrPart.Offset := SByte.PtrPart.Offset)
>     END;

My program:
FOR ByteCount := 1 TO SizeOf (MySet) DO
BEGIN
SByte  := SetPtr.Pntr^;
WHILE SByte<>0 DO
BEGIN
Inc(Cardinality);
SByte := SByte AND (SByte-1);
END;
END;

--
***************************************
* Jan Blaszczynski

* http://orion.nl.com.pl/~jbl

Wed, 18 Jun 1902 08:00:00 GMT

 Page 1 of 1 [ 3 post ]

Relevant Pages