I want to know the actual implementation of the set type in pascal, provided by the language. Specially, I would like to know the one used in the freepascal runtime library, but I'm interested in any pascal implementation.
I care about the run-time complexity of it. The best implementations of Disjoint-set data structure are in O(log*n), and I wish to know if pascal implementation has this one.
The doc for the fpc rtl is found here: ftp://ftp.freepascal.org/pub/fpc/docs-pdf/rtl.pdf , but it's too large (>1700 pages) for looking for this without knowing if it's even there. The freepascal wiki doesn't shed any light on this.
According to this explanation, Pascal internally represents sets as bit strings. However, the article apparently does not refer to a specific implementation of Pascal. In this documentation, it is also stated that bitstrings are used for representation. More precisely, this documentation explicitly mentions 32 bytes for storage of a set.
The Free Pascal language documentation is the "reference" guide, the rtl is the runtime guide. Compiler options and directives are in the programmers guide, see the $pack* links below.
Sets are bitfields, differing in size depending on options. (e.g.$packset and $packenum ), to maximum size of 256 bits, 32 bytes (this is an old TP limit).
IIRC in (obj)FPC mode, sets grow with wordsize, and in Delphi mode with byte sized granularity, but that is a bit x86 centric. The size=3 bytes is however not possible, and will be rounded up to 4.
The lower bound is always 0, so a set of 8..10 is 2 bytes (0..15) even though it can only hold 3 values (8,9,10).
Besides this there is also a little endian vs big endian issue. On big endian systems you can't access set fields alternately bytewise and wordwise. Afaik FPC mostly accesses them wordwise, but it is a while I ago that I last checked that.
来源:https://stackoverflow.com/questions/27897172/what-is-the-implementation-of-sets-used-in-pascal