Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knowing how many elements a set have?

I'm programming in Pascal and I'm trying to find if is there any already created function/procedure in the standard library or similar that allows me to know how many elements a set has?

I know how to do it by using a for loop and a counter increasing +1 when it finds an element (similar to what it has to be done when you want to print a set) but I was wondering if is there any other easier way of doing it, like I said, some predefined standard function/procedure?

like image 447
mp19uy Avatar asked Oct 28 '25 16:10

mp19uy


1 Answers

If your sets are 1,2,4 or 8 byte, one can use the recently introduced popcnt intrinsic.

This intrinsic is in system, but only in trunk (2.7.1)

function PopCnt(Const AValue: Byte): Byte;[internproc:fpc_in_popcnt_x];
function PopCnt(Const AValue: Word): Word;[internproc:fpc_in_popcnt_x];
function PopCnt(Const AValue : DWord): DWord;[internproc:fpc_in_popcnt_x];
function PopCnt(Const AValue : QWord): QWord;[internproc:fpc_in_popcnt_x];

The reason must probably be sought more in speeding up encryption/compression algorithms though, rather than classic sets.

like image 61
Marco van de Voort Avatar answered Oct 31 '25 05:10

Marco van de Voort



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!