Recent

Author Topic: case of  (Read 9256 times)

jamie

  • Hero Member
  • *****
  • Posts: 4346
Re: case of
« Reply #15 on: March 06, 2021, 04:34:16 pm »
if you are an old C/C++ coder, you can use what looks like the C syntax for the short hand of things..

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2.  Var
  3.    W:Word;
  4. begin
  5.    W:= 10001;
  6.    Case w of 10000..20000:Beep;End;
  7. end;                                                
  8.  

 I use single liners like that many times since the range types seem somewhat crippled at times.
The only true wisdom is knowing you know nothing

winni

  • Hero Member
  • *****
  • Posts: 2287
Re: case of
« Reply #16 on: March 06, 2021, 07:35:27 pm »
Hi!

Nice trick for all situations where a set (again and again) is too small.

And

Code: Pascal  [Select][+][-]
  1. (w >= 10000) and (w<=20000)

is inconvenient.

Winni



jamie

  • Hero Member
  • *****
  • Posts: 4346
Re: case of
« Reply #17 on: March 06, 2021, 08:04:46 pm »
Now I do code something like this using the class operators at times... it really depends on the case.

It would be nice if this would work with Cass pointers without the Inline hack that apparently is not up to everyone's standards.
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4. {$modeswitch AdvancedRecords}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  10.  
  11. type
  12.  
  13.  TMyRangeType= Record
  14.     L,H:Integer;
  15.   class Operator in(A:Integer;B:TMyRangeType):Boolean;
  16.  end;
  17.  
  18.   { TForm1 }
  19.  
  20.   TForm1 = class(TForm)
  21.     Button1: TButton;
  22.     procedure Button1Click(Sender: TObject);
  23.   private
  24.  
  25.   public
  26.  
  27.   end;
  28.  
  29. var
  30.   Form1: TForm1;
  31.  
  32. implementation
  33.  
  34. {$R *.lfm}
  35.  
  36. { TForm1 }
  37. class Operator TMyRangeType.in(A:Integer;B:TMyRangeType):Boolean;Inline;
  38. Begin
  39.    Result := (A>=B.L)and(A<=B.H);
  40. end;
  41.  
  42. procedure TForm1.Button1Click(Sender: TObject);
  43.  Var
  44.    R:TMyRangeType;
  45. begin
  46.    //Assume R is set somewhere else;
  47.   If 123 in R Then Beep;
  48. end;
  49.  
  50. end.
  51.  
  52.  
  53.  

SO making a type that lives in a Record that can be used to log properties of an operation you can shorten your code typing..
The only true wisdom is knowing you know nothing

winni

  • Hero Member
  • *****
  • Posts: 2287
Re: case of
« Reply #18 on: March 06, 2021, 09:36:40 pm »
Hi!

Question at the edge of this topic.

I can do

Code: Pascal  [Select][+][-]
  1. case frequency of
  2. 16..800: ...
  3. 801..2000: ...
  4. 2001..20000: ...
  5. end; //case
  6.  

So there is a set like mechanism for a range between 16 and 20000.
Why is the same mechanism not used for sets?

I know that old UCSD Pascal used the same mechanism for sets and case. Sets "only" for 0..4095 and case for 0..32767.

So why is fpc not going that way to enlargen sets?

Winni



Kays

  • Sr. Member
  • ****
  • Posts: 299
  • Whasup!?
    • KaiBurghardt.de
Re: case of
« Reply #19 on: March 06, 2021, 09:51:44 pm »
[…] So why is fpc not going that way to enlargen sets?
Delhpi compatibility.

[…]
Code: Pascal  [Select][+][-]
  1. []
  2. class Operator TMyRangeType.in(A:Integer;B:TMyRangeType):Boolean;Inline;
  3. []
Well, “fun fact”: At one point something like 1337 in -42..5040 was possible.
Yours Sincerely
Kai Burghardt

winni

  • Hero Member
  • *****
  • Posts: 2287
Re: case of
« Reply #20 on: March 06, 2021, 10:59:14 pm »
[…] So why is fpc not going that way to enlargen sets?
Delhpi compatibility.


Hi!

I know that "Delphi compatibility" is always the alibi for nearly every bug or restriction.
But this time not.

If you build a superset for the range of a set it does not disturb Delphi compatitbility, because a Delphi mini set would fit into a bigger fpc set. The other way round there might be problems. But this way is seldom.

And it might convince Delphi-Users to switch to  fpc/Lazarus.
( Or UCSD-Pascal).

The 256 element set restriction is a relict of Turbo Pascal and was already old fashioned in these days.

Winni

Kays

  • Sr. Member
  • ****
  • Posts: 299
  • Whasup!?
    • KaiBurghardt.de
Re: case of
« Reply #21 on: March 06, 2021, 11:50:03 pm »
I know that "Delphi compatibility" is always the alibi for nearly every bug or restriction.
But this time not.

If you build a superset for the range of a set it does not disturb Delphi compatitbility, […]
Well, then it isn’t so much about creating conflicts but there just were “more important” Delphi-compatibility-related things, so those were attended first, before creating something new, you know.
The 256 element set restriction is a relict of Turbo Pascal and was already old fashioned in these days.
I absolutely agree with you. The compiler and RTL do contain code for what’s called “Varset”. I understand the current default set implementations are “small set” and “normal set” in FPC terms.

I don’t know how to enable/use those “varset”. In revision 8515 Jonas wrote there were still problems, though.
Yours Sincerely
Kai Burghardt

winni

  • Hero Member
  • *****
  • Posts: 2287
Re: case of
« Reply #22 on: March 07, 2021, 12:32:34 am »

I don’t know how to enable/use those “varset”. In revision 8515 Jonas wrote there were still problems, though.

Hi
Thanx  for that info.

Code: Text  [Select][+][-]
  1. Revision 8515 (13 years, 5 months ago)
  2.  


Ok. It just hit puberty and that is always a time of big changes.
So let's hope the best!

What else can I say????

Winni


PascalDragon

  • Hero Member
  • *****
  • Posts: 2898
  • Compiler Developer
Re: case of
« Reply #23 on: March 07, 2021, 02:48:34 pm »
I know that "Delphi compatibility" is always the alibi for nearly every bug or restriction.
But this time not.

If you build a superset for the range of a set it does not disturb Delphi compatitbility, […]
Well, then it isn’t so much about creating conflicts but there just were “more important” Delphi-compatibility-related things, so those were attended first, before creating something new, you know.

The problem is less Delphi-compatibility, but more size. If you have a set of 1024 elements you need 1024 bits aka 128 Byte aka an array of 32 UInt32. Of course one might argue that in today's time that is nothing, but one shouldn't scoff at that either.

That said I'd guess that we'll increase the limit of sets in one of the next major releases.

And case-statements have no restrictions regarding this.

The 256 element set restriction is a relict of Turbo Pascal and was already old fashioned in these days.
I absolutely agree with you. The compiler and RTL do contain code for what’s called “Varset”. I understand the current default set implementations are “small set” and “normal set” in FPC terms.

I don’t know how to enable/use those “varset”. In revision 8515 Jonas wrote there were still problems, though.

The varset helpers are used for any set with more than 32 elements as a bit array will be used for those instead of primitive types.

winni

  • Hero Member
  • *****
  • Posts: 2287
Re: case of
« Reply #24 on: March 07, 2021, 03:21:46 pm »

The problem is less Delphi-compatibility, but more size. If you have a set of 1024 elements you need 1024 bits aka 128 Byte aka an array of 32 UInt32. Of course one might argue that in today's time that is nothing, but one shouldn't scoff at that either.

That said I'd guess that we'll increase the limit of sets in one of the next major releases.


Hi!

Let's have a look at the Apple II and UCSD Pascal.
We had an OS that fitted in 64 kB (KILO!!).
We had segment procedures for swaping in  and out of the RAM to make a 250 kB executable to fit to that 64kB (for example Adimens DB).
We had two floppy drives and everything was awful slow.

BUT: We had sets for 4096 elements!
That was over 40 years ago!!

I hope the the next development step for sets will not only reach 1024 elements.
That would be only 1 / 4 of UCSD Pascal 40 years ago!

Winni

 

Blaazen

  • Hero Member
  • *****
  • Posts: 3034
  • POKE 54296,15
    • Eye-Candy Controls
Re: case of
« Reply #25 on: March 07, 2021, 03:28:47 pm »
Sets for more than 256 are listed in future plans since I remember: https://www.freepascal.org/future.html
Lazarus 2.1.0 r64546 FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

avk

  • Sr. Member
  • ****
  • Posts: 407
    • my self-education project
Re: case of
« Reply #26 on: March 07, 2021, 04:23:58 pm »
Hi!

@winni, I've already seen you regret about the lack of sets in FPC more than 256 elements several times.
Are you only interested in the built-in set, or is a home-made one suitable?

winni

  • Hero Member
  • *****
  • Posts: 2287
Re: case of
« Reply #27 on: March 07, 2021, 07:46:37 pm »
@avk

No - I am interestested in a built-in set  inside the fpc ≫ 256 elements.

A workaround with an array of set I made myself.

Winni

avk

  • Sr. Member
  • ****
  • Posts: 407
    • my self-education project
Re: case of
« Reply #28 on: March 07, 2021, 08:01:03 pm »
The set, which I meant, almost completely mimics the built-in set and, moreover, can accept Subrange types, including those with a negative lower bound. But if no, then no.

flori

  • Full Member
  • ***
  • Posts: 183
Re: case of
« Reply #29 on: March 09, 2021, 12:09:00 pm »
Thank you everyone.
I managed to solve the problem

Code: Pascal  [Select][+][-]
  1. procedure TfrmLeaf.btnCalculateClick(Sender: TObject);
  2.  
  3.    // local functions visible inside the event method
  4.  
  5.   function IsEmpty(ATxt: TEdit): Boolean;
  6.   begin
  7.     Result := Length(Trim(ATxt.Text)) = 0;
  8.   end;
  9.  
  10.   function InRange(ALab, ALow, AHigh, AVH: TEdit): TValueRange;
  11.   begin
  12.     Result := vrNone;
  13.     if IsEmpty(ALab) or IsEmpty(ALow) or IsEmpty(AHigh) or IsEmpty(AVH) then
  14.       exit;
  15.     Result := vrMedium;
  16.     if Str2Float(ALab.Text) <= Str2Float(ALow.Text) then
  17.       Result := vrLow
  18.     else if Str2Float(ALab.Text) >= Str2Float(AHigh.Text) then
  19.       Result := vrHigh;
  20.   if Str2Float(ALab.Text) >= Str2Float(AVH.Text) then
  21.       Result := vvH;
  22.   end;
  23.  
  24.   procedure ClassifyRange(Range: TValueRange; Res: TEdit);
  25.   begin
  26.      case Range of
  27.        vrNone: begin Res.Text := '-none-'; Res.Color := clDefault end;
  28.        vrLow: begin Res.Text := 'Low'; Res.Color := $00D9FFCC end;
  29.        vrMedium: begin Res.Text := 'Medium'; Res.Color := $00FFC8C8 end;
  30.        vrHigh: begin Res.Text:= 'High'; Res.Color := $00B9B9FF end;
  31.        vvH: begin Res.Text:= 'Very High'; Res.Color := $00B9B9FF end;
  32.      end;
  33.   end;
  34.  
  35. begin
  36.   // calculate event
  37.   ClassifyRange(InRange(N_Lab,N_Low,N_High,Nvery),N_Res);
  38.   ClassifyRange(InRange(P_Lab,P_Low,P_High,Pvery),P_Res);
  39.   ClassifyRange(InRange(K_Lab,K_Low,K_High,Kvery),K_Res);

 

TinyPortal © 2005-2018