Recent

Author Topic: Trying to copy part of an array  (Read 2858 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: Trying to copy part of an array
« Reply #15 on: August 18, 2019, 08:52:59 pm »
Or you could simply write yourself a Copy Function that works the same way as the string Copy does...
Hmm, I can only envy the Delphi programmers, for some reason they always have the fastest processors, unlimited memory and the most understandable code.
You just killed me with laughter..... ;D ;D
« Last Edit: August 18, 2019, 08:56:08 pm by Thaddy »
Specialize a type, not a var.

Abelisto

  • Jr. Member
  • **
  • Posts: 91
Re: Trying to copy part of an array
« Reply #16 on: August 18, 2019, 09:56:44 pm »
Additionally, citation from your link: "but this array is not assignment compatible to any other array, and can therefor only be used in open array arguments to functions."
So it is impossible to use slice() function in a construction like
Code: Pascal  [Select][+][-]
  1. a := slice(b, c);
Can you explain what you mean? Of course that is possible. That's part of my demo .The devil is in the detail.

I mean what I mean (or, precisely, what documentation mean): result of function slice() is can not be assigned to any variable.

Probably I completely lost, but which demo you are talking about? I see only the code snippets from the doc.

Very simple test:

Code: Pascal  [Select][+][-]
  1. type
  2.     TArraySingleDyn = array of single;
  3.     TArraySingleFixed = array[1..4] of single;
  4. var
  5.     ad: TArraySingleDyn;
  6.     af: TArraySingleFixed;
  7. begin
  8.     ad := slice(ad, 3);
  9.     ad := slice(af, 3);
  10. end.

And compilation result:
Code: [Select]
Free Pascal Compiler version 3.3.1-r42722 [2019/08/17] for x86_64
Copyright (c) 1993-2019 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling test.pas
test.pas(8,16) Error: SLICE cannot be used outside of parameter list
test.pas(9,16) Error: SLICE cannot be used outside of parameter list
test.pas(11) Fatal: There were 2 errors compiling module, stopping
Fatal: Compilation aborted

That is what I mean.
OS: Linux Mint + MATE, Compiler: FPC trunk (yes, I am risky!), IDE: Lazarus trunk

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Trying to copy part of an array
« Reply #17 on: August 18, 2019, 11:15:28 pm »
Arguing with Thaddy is like a battle of wits, I just hate fighting an unarmed person.

The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Trying to copy part of an array
« Reply #18 on: August 18, 2019, 11:47:56 pm »
I've never had much interest for generics in general because I think they are highly over rated however, in this case they could work out well using them for the Copy function I posted to change the different types as needed in code. This should only compile the functions with the types requested.
The only true wisdom is knowing you know nothing

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Trying to copy part of an array
« Reply #19 on: August 19, 2019, 12:00:37 am »
@Abelisto

If slice should be used, you get around the problem with slice result only as parameter that way:

Code: Pascal  [Select][+][-]
  1. Type ArrayOfSingle = array of single;
  2.  
  3. function singleS ( s : array of single) : ArrayOfSingle;
  4. begin
  5. setLength(result,length(s));
  6. for i := 0 to high (s) do result[i] := s[i];
  7. end;
  8.  
  9.  
  10. MyArray := SingelS (slice (InArray, 23));
  11.  
  12.  

That's another solution. Which is quicker I don't know

Winni

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Trying to copy part of an array
« Reply #20 on: August 19, 2019, 09:37:13 am »
Why does this return a substring:

Code: Pascal  [Select][+][-]
  1. procedure TGraphic.AddTriangles(points: array of single);
  2. var
  3.   i: integer;
  4.   triangle: TTriangle;
  5.   chunk: array of single;
  6.  
  7. begin
  8.   i := 0;
  9.   while i < Length(points) - 1 do
  10.   begin
  11.     triangle := TTriangle.Create;
  12.     chunk := Copy(points, i, 9);     // HERE!
  13.     triangle.SetPoints(chunk);
  14.     inc(i, 9)
  15.   end;
  16. end;
  17.  

The error I get is:

Error: Incompatible types: got "ShortString" expected "{Dynamic} Array Of Single"

triangle.SetPoints expects array of single, why does Copy convert an array of single to shortstring?

Copy only supports strings and dynamic arrays, but not static or open array. So if you don't rely on AddTriangles to have an open array as parameter you can use this:

Code: Pascal  [Select][+][-]
  1. type
  2.   TSingleArray = array of Single;
  3.  
  4. procedure TGraphic.AddTriangles(points: TSingleArray);
  5. var
  6.   i: integer;
  7.   triangle: TTriangle;
  8.   chunk: TSingleArray;
  9.  
  10. begin
  11.   i := 0;
  12.   while i < Length(points) - 1 do
  13.   begin
  14.     triangle := TTriangle.Create;
  15.     chunk := Copy(points, i, 9);     // HERE!
  16.     triangle.SetPoints(chunk);
  17.     inc(i, 9)
  18.   end;
  19. end;

The main difference regarding open and dynamic array is that you can't call AddTriangles like this if you use a dynamic array:
Code: Pascal  [Select][+][-]
  1. AddTriangle([4.5452, 2.4523, 1.5342]);
You have to use a dynamic array variable instead.

However with 3.2 and newer that works due to the dynamic array constructors.

 

TinyPortal © 2005-2018