Recent

Author Topic: IntArray - optional default parameters for limited array segment ?  (Read 1784 times)

FrankBKK

  • New Member
  • *
  • Posts: 41
To calculate some values for an integer array I  use a function like this: 
Code: Pascal  [Select][+][-]
  1. function  IntArrayGetPeak(aiArray: Array of Integer; iLowerLimit, iUpperLimit: Integer) : Integer ;

now I would like to use iLowerLimit and iUpperLimit as optional parameters, using Low(aiArray) and High(aiArray) as default parameters, like so:
Code: Pascal  [Select][+][-]
  1. function  IntArrayGetPeak(aiArray: Array of Integer; iLowerLimit :Integer=Low(aiArray); iUpperLimit:Integer= High(aiArray) ) : Integer ;
which does not work (Error: Illegal expression).
I use {$mode objfpc}{$H+}


How can I achieve something similar ? 






FrankBKK

  • New Member
  • *
  • Posts: 41
Re: IntArray - optional default parameters for limited array segment ?
« Reply #1 on: September 24, 2024, 06:35:05 am »
Never mind, I found a solution:

I use -1 as default value for iLowerLimit and iUpperLimit.

Within the function then check on these values and if they are < 0 replace them with Low(aiArray) / High(aiArray).

ASerge

  • Hero Member
  • *****
  • Posts: 2376
Re: IntArray - optional default parameters for limited array segment ?
« Reply #2 on: September 24, 2024, 02:55:50 pm »
I use -1 as default value for iLowerLimit and iUpperLimit.
Just the upper bound is enough. The lower one is always non-negative.
Code: Pascal  [Select][+][-]
  1. function IntArrayGetPeak(const aiArray: array of Integer; iLowerLimit: Integer = 0; iUpperLimit: Integer = -1): Integer;
  2. begin
  3.   if iUpperLimit < 0 then
  4.     iUpperLimit := High(aiArray);
  5.   //...
  6. end;

PascalDragon

  • Hero Member
  • *****
  • Posts: 5855
  • Compiler Developer
Re: IntArray - optional default parameters for limited array segment ?
« Reply #3 on: September 26, 2024, 10:39:03 pm »
How can I achieve something similar ?

Why don't you simply drop the i*Limit parameters and restrict the array upon call using the slice syntax?

Code: Pascal  [Select][+][-]
  1. IntArrayGetPeak(SomeArray[LowerLimit..UpperLimit])

dbannon

  • Hero Member
  • *****
  • Posts: 3212
    • tomboy-ng, a rewrite of the classic Tomboy
Re: IntArray - optional default parameters for limited array segment ?
« Reply #4 on: September 27, 2024, 07:03:41 am »
Why don't you simply drop the i*Limit parameters and restrict the array upon call using the slice syntax?
Code: Pascal  [Select][+][-]
  1. IntArrayGetPeak(SomeArray[LowerLimit..UpperLimit])

PD, could you please enlarge on "Slice syntax" ?   Do you mean the Slice() intrinsic (which has limited usefullness) or the A := AnArray[2..4] which, for me at least, does not compile. Copy() does but it creates a array when the OP (and I) would like to just pass a subarray ?

Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$mode objfpc}{$H+}
  3.  
  4. type  TIntegerArray = Array of Integer;  
  5.  
  6. procedure TestArray(const AArray : TIntegerArray);
  7. var    
  8.         X : integer;
  9. begin
  10.         for x in AArray do
  11.                 writeln(X);
  12.         // or
  13.         for x := low(AArray) to high(AArray) do
  14.                 writeln(AArray[x]);
  15. end;
  16.  
  17. var
  18.     AnArray : TIntegerArray = (1,2,3,4,5,6,7,8,9);
  19. begin
  20.         TestArray(Copy(AnArray, 2, 4));  // take 4 elements, starting at index 2
  21. //  TestArray(slice(AnArray, 4));    // Internal error 2005101501 https://www.freepascal.org/docs-html/rtl/system/slice.html
  22. //  TestArray(AnArray[2..6]);        // Error: Illegal expression
  23. end.

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

bytebites

  • Hero Member
  • *****
  • Posts: 698
Re: IntArray - optional default parameters for limited array segment ?
« Reply #5 on: September 27, 2024, 07:15:17 am »
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$mode objfpc}{$H+}
  3.  
  4. type  TIntegerArray = Array of Integer;  
  5.  
  6. procedure TestArray(const AArray : Array of integer); //Open array here
  7. var    
  8.         X : integer;
  9. begin
  10.         for x in AArray do
  11.                 writeln(X);
  12.         // or
  13.         for x := low(AArray) to high(AArray) do
  14.                 writeln(AArray[x]);
  15. end;
  16.  
  17. var
  18.     AnArray : TIntegerArray = (1,2,3,4,5,6,7,8,9);
  19. begin
  20.         TestArray(Copy(AnArray, 2, 4));  // take 4 elements, starting at index 2
  21. //  TestArray(slice(AnArray, 4));    // Internal error 2005101501 https://www.freepascal.org/docs-html/rtl/system/slice.html
  22. //  TestArray(AnArray[2..6]);        // Error: Illegal expression
  23. end.

TestArray function has open array parameter.

dbannon

  • Hero Member
  • *****
  • Posts: 3212
    • tomboy-ng, a rewrite of the classic Tomboy
Re: IntArray - optional default parameters for limited array segment ?
« Reply #6 on: September 27, 2024, 07:31:22 am »
TestArray function has open array parameter.

Ah, yes bytebites, thanks !  I did not try that because the slice intrinsic is still pretty useless, it always starts from index 0.  It was the nice syntax of

Code: Pascal  [Select][+][-]
  1. A := AnArray[2..4];


I was really hoping to hear more about. Hmm, what about -

Code: Pascal  [Select][+][-]
  1. A := slice((AnArray+3), 4);    //??

Apart from looking horribly C like, I doubt it will compile.    :D

Davo 

Edit : typo
« Last Edit: September 27, 2024, 07:33:24 am by dbannon »
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

Zvoni

  • Hero Member
  • *****
  • Posts: 2821
Re: IntArray - optional default parameters for limited array segment ?
« Reply #7 on: September 27, 2024, 10:45:50 am »
I use -1 as default value for iLowerLimit and iUpperLimit.
Just the upper bound is enough. The lower one is always non-negative.
Code: Pascal  [Select][+][-]
  1. function IntArrayGetPeak(const aiArray: array of Integer; iLowerLimit: Integer = 0; iUpperLimit: Integer = -1): Integer;
  2. begin
  3.   if iUpperLimit < 0 then
  4.     iUpperLimit := High(aiArray);
  5.   //...
  6. end;
I never noticed, that if you pass an Array to a Procedure/Function, that the (local) Array always starts at 0
Is there a specific mode which allows to keep the "original" Low/High of a passed Array?

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. Var
  4.   aa:Array[-2..3] Of Integer;
  5.  
  6. Procedure Test(a:Array Of Integer);
  7. Begin
  8.   Writeln('Local Low=',Low(a));
  9.   Writeln('Local High=',High(a));
  10. End;
  11.  
  12. begin
  13.   Writeln('Original Low=',Low(aa));
  14.   Writeln('Original High=',High(aa));
  15.   Test(aa);
  16. end.

Returns
Code: [Select]
Original Low=-2
Original High=3
Local Low=0
Local High=5
« Last Edit: September 27, 2024, 10:49:04 am by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Josh

  • Hero Member
  • *****
  • Posts: 1361
Re: IntArray - optional default parameters for limited array segment ?
« Reply #8 on: September 27, 2024, 11:11:50 am »
Hi
Would overloading the function be better?
ie
x:=IntArrayGetPeak(MyArray,MyMin,MyMax); // for part of array
or
x:=IntArrayGetPeak(MyArray);// for whole array

Code: Pascal  [Select][+][-]
  1. function IntArrayGetPeak(aiArray: Array of Integer; iLowerLimit, iUpperLimit: Integer) : Integer ;
  2. function IntArrayGetPeak(aiArray: Array of Integer) : Integer ; overload;
  3. ........
  4. implementation
  5. ........
  6. function IntArrayGetPeak(aiArray: Array of Integer; iLowerLimit, iUpperLimit: Integer) : Integer ;
  7. var i:integer;
  8. begin
  9.   // Sanity Check
  10.   If iLowerLimit<Low(aiArray) then iLowerLimit:=Low(aiArray);
  11.   If iUpperLimit>High(aiArray) then iUpperLimit:=High(aiArray);
  12.   // Get Highest Value in Array Range
  13.   result:=aiArray[iLowerLimit];
  14.   for i:=iLowerLimit+1 to iUpperLimit do if aiArray[i]>Result then Result:=aiArray[i];
  15. end;
  16.  
  17. function IntArrayGetPeak(aiArray: Array of Integer) : Integer ;
  18. begin
  19.   result:=IntArrayGetPeak(aiArray,low(aiArray),High(AiArray));
  20. end;            
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

Thaddy

  • Hero Member
  • *****
  • Posts: 16520
  • Kallstadt seems a good place to evict Trump to.
Re: IntArray - optional default parameters for limited array segment ?
« Reply #9 on: September 27, 2024, 12:11:31 pm »
Dynamic arrays and open arrays always start at index zero. Only fixed length arrays can start somewhere else.
But I am sure they don't want the Trumps back...

Zvoni

  • Hero Member
  • *****
  • Posts: 2821
Re: IntArray - optional default parameters for limited array segment ?
« Reply #10 on: September 27, 2024, 12:29:09 pm »
Dynamic arrays and open arrays always start at index zero. Only fixed length arrays can start somewhere else.
I know that.
My Question was if there is a specific switch or mode (or whatever), for a "passed on" array to keep its low/high as the original
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Thaddy

  • Hero Member
  • *****
  • Posts: 16520
  • Kallstadt seems a good place to evict Trump to.
Re: IntArray - optional default parameters for limited array segment ?
« Reply #11 on: September 27, 2024, 12:57:52 pm »
No
But I am sure they don't want the Trumps back...

PascalDragon

  • Hero Member
  • *****
  • Posts: 5855
  • Compiler Developer
Re: IntArray - optional default parameters for limited array segment ?
« Reply #12 on: September 28, 2024, 04:20:56 pm »
Why don't you simply drop the i*Limit parameters and restrict the array upon call using the slice syntax?
Code: Pascal  [Select][+][-]
  1. IntArrayGetPeak(SomeArray[LowerLimit..UpperLimit])

PD, could you please enlarge on "Slice syntax" ?   Do you mean the Slice() intrinsic (which has limited usefullness) or the A := AnArray[2..4] which, for me at least, does not compile. Copy() does but it creates a array when the OP (and I) would like to just pass a subarray ?

The slice syntax only works if the array is passed to open array parameters.

I never noticed, that if you pass an Array to a Procedure/Function, that the (local) Array always starts at 0

Does it really matter for a function if it gets an open array with indices from 0 to x or from y to z as long as z - y = x?

Is there a specific mode which allows to keep the "original" Low/High of a passed Array?

Currently there is not, but once support for ISO (Extended) Pascal is improved there would be conformant array parameters which are declared like array[a..b: IndexType] of ElementType where a and b would take on the boundaries of the passed in array (slice). But as I said: is it really necessary?

dbannon

  • Hero Member
  • *****
  • Posts: 3212
    • tomboy-ng, a rewrite of the classic Tomboy
Re: IntArray - optional default parameters for limited array segment ?
« Reply #13 on: September 30, 2024, 01:50:38 am »
The slice syntax only works if the array is passed to open array parameters.

Yes, thanks PascalDragon, I found it in section 14.4.5 of the language reference. The doc does not use the word 'slice' so searching failed. Probably a pity, 'slice' sums it up nicely.  Yes, the requirement to accept an "Open Array" was what was blocking me.

Thanks
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

Zvoni

  • Hero Member
  • *****
  • Posts: 2821
Re: IntArray - optional default parameters for limited array segment ?
« Reply #14 on: September 30, 2024, 08:28:07 am »
Currently there is not, but once support for ISO (Extended) Pascal is improved there would be conformant array parameters which are declared like array[a..b: IndexType] of ElementType where a and b would take on the boundaries of the passed in array (slice). But as I said: is it really necessary?
Let's not start an argument about this.

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. Type
  4.   TMyEnum = (meFirst=-2,meSecond=-1,meThird=0,meFourth=1);
  5.  
  6. Var
  7.   a:Array[TMyEnum] Of Integer;
  8.  
  9. Procedure test(aa:Array Of Integer);
  10. Begin
  11.   Writeln('Local Low=',Low(aa));
  12.   Writeln('Local High=',High(aa));
  13. End;
  14.  
  15. begin
  16.   Writeln('Original Low=',Ord(Low(a)));
  17.   Writeln('Original High=',Ord(High(a)));
  18.   Test(a);
  19. end.

And let's not start an argument, that an enum always starts at 0.
I can let it start wherever i like
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

 

TinyPortal © 2005-2018