3

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.

Akronix
  • 1,858
  • 2
  • 19
  • 32
  • By logic i'd say that sets are stored as bit in a 32 bit integer type. Why ? because a set cannot be defined on an enumeration which have more than 32 elements (e.g Set Of ...). This is a comon implementation. Retrieving, setting values is always O(1) or O(2),: shift/xor, there is no ten Billions ways to do this. – Abstract type Jan 12 '15 at 13:32
  • 2
    @BBaz: I don't know about Free Pascal, but in Turbo Pascal/Borland Pascal/Delphi, the limit was always 256 elements in a set (32 bytes). – 500 - Internal Server Error Jan 12 '15 at 17:02
  • Sorry, big-mouth syndrom..., let me explain the origin of the misconception: it comes from the fact that a **published** set cannot have more than 32 elements, otherwise FPC outputs: `Error: This kind of property can't be published`. The more crazy thing about that is that i've written a static library for another language based on this misconception !! – Abstract type Jan 13 '15 at 06:53
  • Probably published sets are always 4 bytes wide (one integer), so there is only one set type for RTTI. Probably Delphi does this too. – Marco van de Voort Jan 19 '15 at 11:10

2 Answers2

2

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.

Codor
  • 17,447
  • 9
  • 29
  • 56
  • A maximum of 32 bytes (256 bits) for a set. Smaller sets use fewer bytes. A `set of 0..10` needs 11 bits, so is implemented as 2 bytes. – Rudy Velthuis Oct 23 '16 at 18:38
1

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.

Marco van de Voort
  • 25,628
  • 5
  • 56
  • 89