Recent

Author Topic: Have you ever used Set?  (Read 3211 times)

darksky666

  • New Member
  • *
  • Posts: 38
Have you ever used Set?
« on: April 05, 2018, 03:42:18 pm »
1. Can you please give examples of real world scenarios where SETs could be best used?

Have you ever used it? What were you developing? What did you use it for? Did it make things easier?

Other languages, for eg. C don't have this feature, imo Subrange and SET are indeed great features of Pascal, but i don't have any idea where it could be used?

Thanks

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Have you ever used Set?
« Reply #1 on: April 05, 2018, 03:49:28 pm »
One simple example is the event onKeydown of any control. It has a Shift parameter that is a set of control keys (Ctrl, Alt, Shift etc) that can have any combination of the keys pressed, as an extension to that logic you could apply to phone types, you might have the following phone types  landline, mobile, house, work, warehouse, personal and a single phone can be be part of [mobile,personal] or [landline,work,warehouse] or [mobile,work] etc.
« Last Edit: April 05, 2018, 03:50:59 pm by taazz »
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: Have you ever used Set?
« Reply #2 on: April 05, 2018, 03:49:46 pm »
Sets are a great way to filter, for instance. Set theory is basic computer science and fundamental mathematics.
The whole internet is based on it (especially big data). But Pascal sets - just the compilers - are currently limited to 256 elements.
See https://en.wikipedia.org/wiki/Set_theory
Note e.g. SQL is basically sets.

Basically: you have a population (like a real population, but say enumeration) and you can manipulate that population based on membership with specific treats.
The last part of the first chapter of http://wiki.freepascal.org/Defensive_programming_techniques (which I wrote) gives an example, but the official manuals have even more examples.
As does any book about  Pascal.

Any targeted advertising on your internet profile is based on set theory......Easy...

« Last Edit: April 05, 2018, 04:04:50 pm by Thaddy »
Specialize a type, not a var.

darksky666

  • New Member
  • *
  • Posts: 38
Re: Have you ever used Set?
« Reply #3 on: April 05, 2018, 04:11:03 pm »
Oh, thank you... but internally, how is it implemented by compiler? I think Bit Manipulation used internally .. assuming it's so is there a table that matches with certain bit patterns as readonly memory?
Code: Pascal  [Select][+][-]
  1. type
  2.   en = (sun = 55, mon = 77, tue = 85, wed = 91);  (* Manual shows := but = also works? *)
  3.  
  4. var
  5.   e : set of en;
  6.   f : en;
  7.  
  8. begin
  9. e := [sun, mon, wed];
  10.  
  11.   for f in e do
  12.       Writeln (ord(f));
  13. end.
  14.  

In this case, is a "dictionary" like table prepared by the compiler with those ordinal values and corresponding bit patterns?

Thanks
« Last Edit: April 05, 2018, 04:14:00 pm by darksky666 »

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: Have you ever used Set?
« Reply #4 on: April 05, 2018, 04:30:40 pm »
Yes they are based on Boolean logic. (But do not confuse internal implementation with it: bit manipulation is just an efficient manifestation/implementation of such logic given a certain family of processors: e.q. quantum processors or even much older architectures work very different. Don't assume state.)
« Last Edit: April 05, 2018, 04:37:51 pm by Thaddy »
Specialize a type, not a var.

darksky666

  • New Member
  • *
  • Posts: 38
Re: Have you ever used Set?
« Reply #5 on: April 05, 2018, 06:26:33 pm »
While overriding the default value of an Enum element, does := operator also work fine in Delphi?

Code: Pascal  [Select][+][-]
  1. { I mixed both := and =, but it compiled fine in FPC, does Delphi give any error? }
  2.  
  3. TMyEnum = (sun, mon, tue := 7, wed = 17, thu, fri, sat);
  4.  

I am asking this because everywhere else (other than FPC Docs, as far as i remember) shows use of the = operator.

Quote from: Object Pascal Language Guide, Embarcardero - pg. 5-7
You can override this by explicitly assigning ordinalities to some or all of the values in the declaration. To assign an ordinality to a value, follow its identifier with = constantExpression

Quote from: FPC Reference Manual - pg. 28
A C-style enumeration type looks as follows:
Code: Pascal  [Select][+][-]
  1. Type
  2. EnumType = (one, two, three, forty := 40,fortyone);
  3.  

(While it doesn't matter since it's all translated to Assembly anyway, but still asking just out of curiosity.. which one is the original (as Writh designed it)?

Thanks

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Have you ever used Set?
« Reply #6 on: April 05, 2018, 08:54:29 pm »
1. Can you please give examples of real world scenarios where SETs could be best used?
Flags, mostly. The IDE, LCL and compiler source code is full of it.
Have you ever used it? What were you developing? What did you use it for? Did it make things easier?

Other languages, for eg. C don't have this feature, imo Subrange and SET are indeed great features of Pascal, but i don't have any idea where it could be used?
Yes. GUI wrappers, games, some other "common" apps. For example, in games, sets are useful for player's bad status (poisoned, paralyzed, petrified, etc.). Empty set denotes normal / healthy condition. It not only makes things easier, but set operations are damn fast at instruction level. Those who aren't used to sets will probably do this:
Code: Pascal  [Select][+][-]
  1. function TPlayer.CanMove: Boolean;
  2. begin
  3.   Result := (Player.Status = psPoisoned) or (Player.Status = psParalyzed) or (Player.Status = psPetrified);
  4. end;
  5.  
That's a few CMP + JZ instructions (without optimizer). With sets, it can be turned into:
Code: Pascal  [Select][+][-]
  1. function TPlayer.CanMove: Boolean;
  2. begin
  3.   Result := Player.Status in [psPoisoned,psParalyzed,psPetrified];
  4. end;
  5.  
Which is not only shorter at source code level, but at instruction level only generates (probably) single AND + JNZ instruction. Shorter and faster, double benefit. C might not have sets, but C programmers can have it by carefully using bit operations against typedef-ed integer type and probably some enum or #define. Still, the programmer's responsibility is way higher (ensuring no value outside allowed range) and having 256 elements set will not be a trivial operation.
(While it doesn't matter since it's all translated to Assembly anyway, but still asking just out of curiosity.. which one is the original (as Writh designed it)?
The original Pascal has no way to override the ordinal value. Wirth designs the type as a high level type, as how you would use it in math, not computer interfacing.

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: Have you ever used Set?
« Reply #7 on: April 05, 2018, 08:59:04 pm »
Specialize a type, not a var.

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Have you ever used Set?
« Reply #8 on: April 06, 2018, 12:26:21 am »
I use Set to make UI states.

ButtonNormal ButtonHover ButtonActive.

Well these are just independent.

If you add focused then a set is needed to pass data, instead of a Boolean.

but in custom drawn package all states of all controls are in a set.
« Last Edit: April 06, 2018, 12:29:59 am by lainz »

 

TinyPortal © 2005-2018