Recent

Author Topic: Slice through a pointer  (Read 1605 times)

zamtmn

  • Hero Member
  • *****
  • Posts: 609
Slice through a pointer
« on: May 19, 2024, 12:25:15 pm »
There is a great Slice function , but its use is clouded by the need to have Array type. Often Array this type is superfluous and is not used anywhere, only for slice calling. I think it would be great to add the ability to call through a pointer to a type. Or is there such a possibility and I don’t know it?
Code: Pascal  [Select][+][-]
  1. program project1;
  2. {$mode delphi}{$H+}
  3. uses
  4.   gvector;
  5. type
  6.   TTest2=TVector<integer>;
  7.   TDummyArr=array[0..0]of integer;
  8.   PTDummyArr=^TDummyArr;
  9. procedure WriteArgs(const Args:array of integer);
  10. var
  11.   i:integer;
  12. begin
  13.   for i:=low(args) to high(args) do
  14.     writeln(args[i]);
  15. end;
  16. var
  17.   arr:TTest2;
  18. begin
  19.   arr:=TTest2.Create;
  20.   arr.PushBack(1);
  21.   arr.PushBack(2);
  22.   arr.PushBack(3);
  23.   arr.PushBack(4);
  24.   WriteArgs(PTDummyArr(arr.Mutable[1])^[0..1]);//this work
  25.   WriteArgs(slice(PTDummyArr(arr.Mutable[1])^,2));//this work
  26.   WriteArgs(slice(PInteger(arr.Mutable[1]),2));//this better, bun not work((
  27.   WriteArgs(slice(arr.Mutable[1]),2);//ideal))
  28.   readln;
  29. end.

zamtmn

  • Hero Member
  • *****
  • Posts: 609
Re: Slice through a pointer
« Reply #1 on: May 19, 2024, 06:46:40 pm »
No function will help here, it's the compiler magic

zamtmn

  • Hero Member
  • *****
  • Posts: 609
Re: Slice through a pointer
« Reply #2 on: May 19, 2024, 07:48:45 pm »
>>Oh really?
Yes.
Drinking coffee and thinking about how it works will be very good))

zamtmn

  • Hero Member
  • *****
  • Posts: 609
Re: Slice through a pointer
« Reply #3 on: May 19, 2024, 10:35:21 pm »
You're copying the data. It's an easy but slow way.
I never do superfluous work and I don't recommend this to you))

zamtmn

  • Hero Member
  • *****
  • Posts: 609
Re: Slice through a pointer
« Reply #4 on: May 19, 2024, 10:43:07 pm »
jamie
Code: Pascal  [Select][+][-]
  1.  Function TVectorWithSlice<T>.Slice(aStart,ACount:Integer):TArray<T>;
  2.   Begin
  3.    SetLength(Result, ACount);
  4.    Result := Copy(fData,aStart,ACount);
  5.   end;
Try it on trunk fpc, you will not be pleasantly surprised((

zamtmn

  • Hero Member
  • *****
  • Posts: 609
Re: Slice through a pointer
« Reply #5 on: May 19, 2024, 10:53:54 pm »
Please note that there is no data copying in the source program, this is a mandatory requirement, because speed is very important
>>using 3.2.2
>>that should be close enough.
unfortunately not. There have been big changes - fData it private field, and cannot be used in heir

zamtmn

  • Hero Member
  • *****
  • Posts: 609
Re: Slice through a pointer
« Reply #6 on: May 19, 2024, 11:08:38 pm »
No, just passing an open array argument by PType and Count instead Array and Count

zamtmn

  • Hero Member
  • *****
  • Posts: 609
Re: Slice through a pointer
« Reply #7 on: May 20, 2024, 07:32:19 am »
I don't understand what you're arguing about. your approach is good for a textbook, but in real life it will lead to huge memory and execution time overruns. Please have some more coffee and understand the difference between dynamic arrays and open arrays in function arguments

jamie

  • Hero Member
  • *****
  • Posts: 6301
Re: Slice through a pointer
« Reply #8 on: May 20, 2024, 12:39:35 pm »
It appears u have other plans and they don't benifit the community

I withdrew as much of the bad code I could. So others don't get bothered

Have a good time.
The only true wisdom is knowing you know nothing

zamtmn

  • Hero Member
  • *****
  • Posts: 609
Re: Slice through a pointer
« Reply #9 on: May 20, 2024, 01:10:09 pm »
jamie
You shouldn't be offended. I immediately said that I was not interested in copying. Copying is good when there are 4 elements in the array and you need to get 2 of them. But there may be megabytes in the array, and recursion in the algorithm

Erasing messages that have answers is bad))

runewalsh

  • Jr. Member
  • **
  • Posts: 84
Re: Slice through a pointer
« Reply #10 on: May 20, 2024, 02:14:04 pm »
Little-known feature, but always has been.

Code: Pascal  [Select][+][-]
  1. procedure F(const x: array of int32);
  2. begin
  3. end;
  4.  
  5. var
  6.         x: pInt32;
  7.  
  8. begin
  9.         F(x[5 .. 10]);
  10. end.

zamtmn

  • Hero Member
  • *****
  • Posts: 609
Re: Slice through a pointer
« Reply #11 on: May 20, 2024, 02:22:38 pm »
runewalsh
just
Code: Pascal  [Select][+][-]
  1. WriteArgs(arr.Mutable[1][0..1]);
thanks!

 

TinyPortal © 2005-2018