Recent

Author Topic: "set of": order of elements  (Read 2802 times)

440bx

  • Hero Member
  • *****
  • Posts: 5078
Re: "set of": order of elements
« Reply #15 on: February 06, 2025, 11:32:45 am »
I am not a mod, but suggest that being gratuitously rude to another member of the community does not help answer the question raised by the OP.
If there is a forum member who has turned being "gratuitously rude" into an art form, that member is Thaddy. It really should come as no surprise that he occasionally gets a stern response.  Actually, the surprise is how much his, too often unpleasant, attitude is tolerated.

As far as element ordering in sets, a set is by mathematical definition, unordered.  However, for a compiler to implement sets _efficiently_ ordering the set elements is required, that is necessary for the compiler to represent a set element with a single bit.  Of course, the criteria used by the compiler to order the elements is implementation dependent.

Other implementations that do not depend on ordering are possible but, they are significantly less efficient in terms of speed and memory usage, e.g., dictionaries or other hashing dependent structures.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8334
Re: "set of": order of elements
« Reply #16 on: February 06, 2025, 11:36:35 am »
Are you talking about me or the other person to whom I addressed my words? If you mean me, I am surprised, because this second person is not shy in his choice of words on the forum. I just told him not to talk to me like that. If someone likes his tone - let them listen to him, I don't want to.

I'm talking to you. To summarise: not in here please.

Quote
And about everything else you said - I agree, in fact in several posts earlier I said the same thing. That there is an order in terms of internal storage, but this is the details of implementation and you should not rely on it, and that the order in which values were assigned to the set - this order is not preserved

In that case we're in broad agreement. I'd add that I regularly put startup checks in units, in particular that data structures that are supposed to mimic an OS parameter has the expected size.

Which I suppose takes us onto another detail. It's very common in C programming to see an integer parameter defined as individual bits shifted left by a bit number. It's tempting to define that in Pascal as a set based on an enumeration etc., but in my opinion very unwise.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

ALLIGATOR

  • Full Member
  • ***
  • Posts: 136
Re: "set of": order of elements
« Reply #17 on: February 06, 2025, 11:42:50 am »
If we shift our focus to real work instead of endless discussions and arguments, I have a piece of code that I’d be happy to recommend to the topic starter. I believe they’ll find it both useful and interesting—at least, I’d like to think so. No false modesty here!

Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. uses SysUtils, TypInfo;
  4.  
  5. type
  6.   tColour = (cBlack,cBlue,cRed,cWhite,cYellow,cGreen,cOrange);
  7.   tFlag = set of tColour;
  8.   tFlagArray = array of tColour;
  9.  
  10. procedure ShowFlag(const ThisCountry: string; const ThisFlag: tFlagArray);
  11. var
  12.   s,t: string;
  13.   c: tColour;
  14. begin
  15.   s:= 'The flag of ' + ThisCountry + ' has ' + IntToStr(Length(ThisFlag)) + ' colours: ';
  16.   for c in ThisFlag do
  17.   begin
  18.     t:=GetEnumName(TypeInfo(ThisFlag[0]), ord(c));
  19.     s:=s + RightStr(t, Length(t)-1) + ' ';
  20.   end;
  21.   WriteLn(s);
  22. end;
  23.  
  24. begin
  25.   ShowFlag('Ireland',    [cGreen,cWhite,cOrange]);
  26.   ShowFlag('Ivory Coast',[cOrange,cWhite,cGreen]);
  27.  
  28.   ReadLn;
  29. end.

Code: Pascal  [Select][+][-]
  1. The flag of Ireland has 3 colours: Green White Orange
  2. The flag of Ivory Coast has 3 colours: Orange White Green
« Last Edit: February 06, 2025, 11:53:36 am by ALLIGATOR »

MarkMLl

  • Hero Member
  • *****
  • Posts: 8334
Re: "set of": order of elements
« Reply #18 on: February 06, 2025, 11:59:17 am »
If there is a forum member who has turned being "gratuitously rude" into an art form, that member is Thaddy. It really should come as no surprise that he occasionally gets a stern response.  Actually, the surprise is how much his, too often unpleasant, attitude is tolerated.

I can assure you that with the combination of Th. (rushing into areas where angels fear to tread) and J. (using every opportunity to try to entice people into her parlour on IRC) I've come very close on occasion to throwing in the towel and abandoning not only this forum but also Pascal. Both of them, in my opinion, are immoderately fond of the sound of their own voice and confuse the "Posts:" count with objective correctness and subjective self-worth.

But with a bit of schooling and a few decades practice it's possible to be rude politely.

Quote
As far as element ordering in sets, a set is by mathematical definition, unordered.  However, for a compiler to implement sets _efficiently_ ordering the set elements is required, that is necessary for the compiler to represent a set element with a single bit.  Of course, the criteria used by the compiler to order the elements is implementation dependent.

Other implementations that do not depend on ordering are possible but, they are significantly less efficient in terms of speed and memory usage, e.g., dictionaries or other hashing dependent structures.

Regrettably, that is something that is regularly overlooked by the CS fraternity who are so intent on generalising that they're quite happy to e.g. implement even a small set using a linked list (and then expend multiple research programs optimising the operations). That was, of course, one of the bad things about APL.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

BrunoK

  • Hero Member
  • *****
  • Posts: 684
  • Retired programmer
Re: "set of": order of elements
« Reply #19 on: February 06, 2025, 12:48:05 pm »
If we shift our focus to real work instead of endless discussions and arguments, I have a piece of code that I’d be happy to recommend to the topic starter. I believe they’ll find it both useful and interesting—at least, I’d like to think so. No false modesty here!

Mostly what I submitted with the additional complication of   
Code: Pascal  [Select][+][-]
  1. t:=GetEnumName(TypeInfo(ThisFlag[0]), ord(c));

Thaddy

  • Hero Member
  • *****
  • Posts: 16653
  • Kallstadt seems a good place to evict Trump to.
Re: "set of": order of elements
« Reply #20 on: February 06, 2025, 01:53:49 pm »
There is no order in a set, only membership. Period.
So test for membership and do that with a mask, that is one cycle.
Alligatorinae seem not to be the smartest of all animals, all be it the longest living species.
« Last Edit: February 06, 2025, 01:57:44 pm by Thaddy »
But I am sure they don't want the Trumps back...

BrunoK

  • Hero Member
  • *****
  • Posts: 684
  • Retired programmer
Re: "set of": order of elements
« Reply #21 on: February 06, 2025, 01:56:54 pm »
As Marcov wrote already: sets are by definition not ordered.
Don't look stupid.

I have absolutely no interest in talking to you or proving anything to you. Why? Because you have the
...

I am not a mod, but suggest that being gratuitously rude to another member of the community does not help ...
When dealing with Grumpy posts, never forget to quote what he wrote. He is very good at disappearing his most useless contibutions or edit then to avoid excessive embarrassment.

Thaddy

  • Hero Member
  • *****
  • Posts: 16653
  • Kallstadt seems a good place to evict Trump to.
Re: "set of": order of elements
« Reply #22 on: February 06, 2025, 01:58:47 pm »
I did, post crossed. There is no point in being stupid.
But I am sure they don't want the Trumps back...

ALLIGATOR

  • Full Member
  • ***
  • Posts: 136
Re: "set of": order of elements
« Reply #23 on: February 06, 2025, 03:02:11 pm »
Mostly what I submitted with the additional complication of   
Code: Pascal  [Select][+][-]
  1. t:=GetEnumName(TypeInfo(ThisFlag[0]), ord(c));

👍Indeed, sorry, didn't notice your post
(while I was dealing with a very important person (as he apparently thinks he is 😂)).

440bx

  • Hero Member
  • *****
  • Posts: 5078
Re: "set of": order of elements
« Reply #24 on: February 06, 2025, 03:16:35 pm »
Just in case someone is interested, here is a little bit of code that shows the element ordering done by the compiler and the mask associated with each set element.

Code: Pascal  [Select][+][-]
  1.  
  2. {$MODE OBJFPC}
  3. {$IFDEF WINDOWS}
  4.   {$APPTYPE CONSOLE}
  5. {$ENDIF}
  6.  
  7. program _SetElementsOrder;
  8.  
  9. uses StrUtils;
  10.  
  11. type
  12.   TColour = (cBlack, cBlue, cRed, cWhite, cYellow, cGreen, cOrange);
  13.   TFlag = set of TColour;
  14.  
  15. procedure PrintSetMasks();
  16. var
  17.   AFlag     : TFLAG = [];
  18.   AFlagBits : bitpacked array[0..bitsizeof(longint) - 1] of boolean
  19.               absolute AFlag;
  20.  
  21.   C     : TColour;
  22.  
  23. begin
  24.   writeln('sizeof TColor: ', sizeof(TColour));
  25.   writeln('sizeof AFlag : ', sizeof(AFlag));
  26.   writeln;
  27.  
  28.   { figure out how the compiler orders the elements, i.e., from LSB to MSB or }
  29.   { MSB to LSB                                                                }
  30.  
  31.   write('bit ordering is ');
  32.   AFlag := [low(TColour)];
  33.   case AFlagBits[ord(low(TColour))] of
  34.     TRUE : writeln('LSB to MSB');
  35.     FALSE: writeln('MSB to LSB');
  36.   end;
  37.   writeln;
  38.  
  39.   for C in TColour do
  40.   begin
  41.     AFlag := [C];
  42.     writeln('Colour: ', C:8, ' binary mask: ', IntToBin(longint(AFlag),
  43.                                                bitsizeof(longint),
  44.                                                1));
  45.   end;
  46. end;
  47.  
  48. begin
  49.   repeat
  50.     { make sure the code can handle a set of TColour                          }
  51.  
  52.     if sizeof(TFlag) <> sizeof(longint) then
  53.     begin
  54.       writeln('This program cannot handle sets with more than ',
  55.               bitsizeof(longint), ' bits');
  56.       break;
  57.     end;
  58.  
  59.     writeln;
  60.     PrintSetMasks();
  61.  
  62.     break;
  63.   until TRUE;
  64.  
  65.   writeln;
  66.   writeln('press ENTER/RETURN to end this program.');
  67.   readln;
  68. end.                          
  69.  
On an intel/Windows machine, the bit number is the ordinal value of the enumeration elements that comprise the set (no surprise there.)

ETA:

Is there a way to figure out the order (sequence) of the elements is a set?
The answer is yes and the program above shows how to do it.  Questions, if you have any, are welcome.
« Last Edit: February 06, 2025, 03:27:59 pm by 440bx »
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Rik

  • Jr. Member
  • **
  • Posts: 78
Re: "set of": order of elements
« Reply #25 on: February 06, 2025, 04:37:52 pm »
Thanks to all for responding.
At the end it turned out to be rather easy to get the order of the set elements (as given in the function call).
Combining
Quote
"set of ..."
and
Quote
"array of ..."
as suggested by BrunoK was (at least for what I intended) the way to go.

CM630

  • Hero Member
  • *****
  • Posts: 1297
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: "set of": order of elements
« Reply #26 on: February 06, 2025, 05:49:05 pm »
Almost, you do not need a set at all, all you need is an array.
I have commented a line (//  tFlag = set of tColour;) in @BrunoK's code, the app still works, I get he just forget to remove it.

Code: Pascal  [Select][+][-]
  1. program Country;
  2.  
  3. uses
  4.   SysUtils;
  5.  
  6. type
  7.   tColour = (cBlack, cBlue, cRed, cWhite, cYellow, cGreen, cOrange);
  8. //  tFlag = set of tColour;
  9.   tColors = array of tColour;
  10.  
  11.   procedure ShowFlag(ThisCountry: string; ThisFlag: tColors);
  12.   var
  13.     n: DWord;
  14.     lFlag: tColour;
  15.     lFlagStr: string;
  16.   begin
  17.     Write('<', ThisCountry, '> ');
  18.     for lFlag in ThisFlag do begin
  19.       Str(lFlag, lFlagStr);
  20.       Write(lFlagStr, ' ');
  21.     end;
  22.     WriteLn;
  23.   end;
  24.  
  25.   { TMainForm }
  26.  
  27. begin
  28.   ShowFlag('Ireland', [cGreen, cWhite, cOrange]);
  29.   ShowFlag('Ivory Coast', [cOrange, cWhite, cGreen]);
  30.   ReadLn;
  31. end.  
Besides that, I have no idea if dynamic sets exist.
« Last Edit: February 06, 2025, 06:23:05 pm by CM630 »
Лазар 4,0RC2 32 bit (sometimes 64 bit); FPC3,2,2

440bx

  • Hero Member
  • *****
  • Posts: 5078
Re: "set of": order of elements
« Reply #27 on: February 06, 2025, 07:27:57 pm »
Almost, you do not need a set at all, all you need is an array.
He can have both simultaneously by overlaying a bitpacked array of boolean on top of the variable of type TFlags, then he can use the values in tColour to index into the bitpacked array just as if it were an array of TColour.  That's what the code I posted does.

A potentially significant advantage of that method is being able to use the set operators.  Another potential advantage is that, for small sets (32 elements or less), the entire "array" fits in one register.  (it would be nice if that optimization was extended to 64 elements when creating a 64 bit app... but that's a different subject.)

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5906
  • Compiler Developer
Re: "set of": order of elements
« Reply #28 on: February 06, 2025, 10:14:14 pm »
If there is a forum member who has turned being "gratuitously rude" into an art form, that member is Thaddy. It really should come as no surprise that he occasionally gets a stern response.  Actually, the surprise is how much his, too often unpleasant, attitude is tolerated.

I can assure you that with the combination of Th. (rushing into areas where angels fear to tread) and J. (using every opportunity to try to entice people into her parlour on IRC) I've come very close on occasion to throwing in the towel and abandoning not only this forum but also Pascal. Both of them, in my opinion, are immoderately fond of the sound of their own voice and confuse the "Posts:" count with objective correctness and subjective self-worth.

I tend to just skip over their posts if there's something that needs to be corrected and otherwise ignore them... 🤷‍♀️

Alligatorinae seem not to be the smartest of all animals, all be it the longest living species.

The code that ALLIGATOR showed is essentially the same that BrunoK showed, both of which use an array of the enum type as parameter instead of a set and thus have an order.

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 866
Re: "set of": order of elements
« Reply #29 on: February 07, 2025, 08:25:29 am »
Core problem - is when programmers are taught to code via current popular languages like Java, JS or Python and as result become "monkey" coders, who don't understand what they do, cuz they don't know underlying mechanisms behind their code. I don't try to say, that each programmer should always learn Asm first in order to understand, what really happens in his code. Asm is really bad first language candidate. But I think, that good programmer should be interested in underlying mechanisms behind his code. And that Pascal is the best first language to learn. It's not as complicated, as C/C++, but it's powerful enough. And it's compilable, not interpretable.

Overall idea - each language feature doesn't exist just for sake of existence. It's usually just wrapper around some commonly used low level code. Set is wrapper around bitfield. If you need ordered data structure - use array.
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

 

TinyPortal © 2005-2018