Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2025-01

Set limitations

7 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Hairy Pixels

I was curious, is there any practical limit on the size of a set (32 bits and 256 values) and why they couldn’t be 64 bit or any other arbitrary size? The floor seems to be 4 bytes too but why not allow smaller sizes to save memory? Perhaps just a historical artifact but I was curious.

Regards,
Ryan Joseph

Reply by email

Hairy Pixels
to Hairy Pixels
Hairy Pixels wrote 5 lines
I was curious, is there any practical limit on the size of a set (32 bits and 256 values) and why they couldn’t be 64 bit or any other arbitrary size? The floor seems to be 4 bytes too but why not allow smaller sizes to save memory? Perhaps just a historical artifact but I was curious.

Sorry that was wrong, the biggest size is 32 bytes and the largest size of an enum is 256 elements. I was talking about sets of enums also if that wasn ’t clear.

I still don’t see it’s possible to make a set which is smaller than 4 bytes, even in the 0..7 range.

Regards,
Ryan Joseph

Reply by email

Martin Frb
to Hairy Pixels
Hairy Pixels wrote 14 lines
On Jan 11, 2025 at 8:17:33 PM, Hairy Pixels wrote: > I was curious, is there any practical limit on the size of a set (32 > bits and 256 values) and why they couldn’t be 64 bit or any other > arbitrary size? The floor seems to be 4 bytes too but why not allow > smaller sizes to save memory? Perhaps just a historical artifact but > I was curious. Sorry that was wrong, the biggest size is 32 bytes and the largest size of an enum is 256 elements. I was talking about sets of enums also if that wasn’t clear. I still don’t see it’s possible to make a set which is smaller than 4 bytes, even in the 0..7 range.
program Project1;
{$PackSet 1}
var
   a: set of byte;
   b: set of 0..7;
begin
   writeln(sizeof(a));
   writeln(sizeof(b));
   readln;
end.

Reply by email

Sven Barth
to Hairy Pixels: I was curious, is there any practical limit on the size of a set (32 bits and 256 values)…

Hairy Pixels via fpc-pascal schrieb am
Sa., 11. Jan. 2025, 14:17:

Show quoted text 5 lines
I was curious, is there any practical limit on the size of a set (32 bits and 256 values) and why they couldn’t be 64 bit or any other arbitrary size? The floor seems to be 4 bytes too but why not allow smaller sizes to save memory? Perhaps just a historical artifact but I was curious.

Set are a minimum of 4 Byte by default cause that's how Delphi does them. Ifets you want smaller sets then use the $PackSets directive as Martin had written.

The maximum is also based on what TP and Delphi allow. ISO Pascal for example suggests a maximum of 1024 elements instead, though they define this number to be implementation specific.
In the end it's a decision of where to do the cut as at some size the majority of the set will be sparsely populated so it makes more sense to use a dedicated type instead (e.g. a hash set or something like that) that wastes less memory.

Regards,
Sven

Reply by email

Thomas Kurz
to Alexey T.

I always use the one from generics.collections

Earlier message 20 lines
----- Original Message ----- From: Alexey T. via fpc-pascal To: FPC-Pascal users discussions Sent: Saturday, January 11, 2025, 17:32:49 Subject: [fpc-pascal] Set limitations > so it makes more sense > to use a dedicated type instead (e.g. a hash set or something like > that) I found these implementations of hash set (origial is from Java, as search shows): https://wiki.freepascal.org/LGenerics - hashset (unit lgHashSet) - fine-grained concurrent hashset (unit lgHashSet) - disjoint set (unit lgHashSet) Alex

Reply by email

Hairy Pixels
to Sven Barth: Hairy Pixels via fpc-pascal schrieb am Sa., 11. Jan. 2025, 14:17: Set are a minimum of 4…

On Jan 11, 2025 at 10:53:36 PM, Sven Barth via fpc-pascal <

Show quoted text 6 lines
wrote: Set are a minimum of 4 Byte by default cause that's how Delphi does them. Ifets you want smaller sets then use the $PackSets directive as Martin had written.

I did not know this existed. I don’t see any reason they force the set to 4 bytes unless it was being used to pass to C libraries maybe?

Show quoted text 8 lines
The maximum is also based on what TP and Delphi allow. ISO Pascal for example suggests a maximum of 1024 elements instead, though they define this number to be implementation specific. In the end it's a decision of where to do the cut as at some size the majority of the set will be sparsely populated so it makes more sense to use a dedicated type instead (e.g. a hash set or something like that) that wastes less memory.

Theoretically if you added one more element to 257 the size would double to 64 bytes right? That’s still only 50% of a short string both of which can be sparsely populate so how are they different?

Regards,
Ryan Joseph

Reply by email