* * *

Author Topic: RTL-Generics question/suggestion regarding TCustomList<T>.ToArray  (Read 2749 times)

SlightlyOutOfPhase

  • New member
  • *
  • Posts: 8
ToArray seems to be useful if you actually want an entirely new, unique array containing the exact same values as the FItems protected property of the List, but there are many many circumstances where this is not the case (namely, anytime you're dealing with functions or procedures that take pointers to the first index of an array as an argument.... ToArray doesn't work for this, as it's not viewed as a variable.) Might I suggest the addition of a read-only property (not function) that simply provides direct read-only access to FItems? Something like this:

Code: Pascal  [Select]
  1. public
  2.   function ToArray: TArray<T>; override; final;
  3.   {RELEVANT CODE HERE ----> } property AsArray: TArray<T> read FItems;
  4. end;

Out of curiousity I added this to my RTL-Generics sources and recompiled FPC, and not only does it have the benefit of being "accepted" as a variable that I previously mentioned, but it is also much, much, much faster when dealing with extremely large lists (as one would expect, since it simply directs to data that already exists.)
« Last Edit: November 18, 2016, 03:19:54 am by SlightlyOutOfPhase »

Thaddy

  • Hero Member
  • *****
  • Posts: 4779
Re: RTL-Generics question/suggestion regarding TCustomList<T>.ToArray
« Reply #1 on: November 13, 2016, 07:22:12 am »
Instead of modifying an existing abstract class, you should derive a new class if you need that functionality.

Btw: it is of course only faster if the array is accessed sequentially. You loose the enumerator support.
« Last Edit: November 13, 2016, 07:33:54 am by Thaddy »
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

SlightlyOutOfPhase

  • New member
  • *
  • Posts: 8
Re: RTL-Generics question/suggestion regarding TCustomList<T>.ToArray
« Reply #2 on: November 15, 2016, 04:07:08 pm »
Well yes, it's certainly true that I could derive a new class. I was merely suggesting to add it to the TCustomList<T> class specifically since that is the class that the major commonly used Generic list classes (TList<T>, TObjectList<T>, e.t.c) descend from, so if it were added at that level they would all have it by default.

Thaddy

  • Hero Member
  • *****
  • Posts: 4779
Re: RTL-Generics question/suggestion regarding TCustomList<T>.ToArray
« Reply #3 on: November 15, 2016, 06:05:21 pm »
Well yes, it's certainly true that I could derive a new class. I was merely suggesting to add it to the TCustomList<T> class specifically since that is the class that the major commonly used Generic list classes (TList<T>, TObjectList<T>, e.t.c) descend from, so if it were added at that level they would all have it by default.
I knew that. I still stick to my answer. Because of the same reasoning.
In addition: if you need that kind of array access you chose the wrong structure.
But that is merely theory. I appreciate you want to see it in the base class.
I think that is wrong, but it won't hurt much for average users.
« Last Edit: November 15, 2016, 06:08:26 pm by Thaddy »
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

SlightlyOutOfPhase

  • New member
  • *
  • Posts: 8
Re: RTL-Generics question/suggestion regarding TCustomList<T>.ToArray
« Reply #4 on: November 16, 2016, 02:19:05 am »
In addition: if you need that kind of array access you chose the wrong structure.

Not sure I agree with you on that. Here's an OpenGL-oriented example that shows the usefulness of this kind of "low-level" access in an otherwise "high-level" list class.

Code: Pascal  [Select]
  1. unit glasarrayexample
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, gl, glext, Generics.Collections;
  9.  
  10. type
  11.   TVec3F = array[0..2] of GLFloat;
  12.   TVectorList = specialize TList<TVec3F>;
  13.  
  14. var
  15.   VecList: TVectorList;
  16.  
  17.   //So now you can add/remove/sort/insert/e.t.c. TVec3Fs at will using the high-level constructs of TList,
  18.   //instead of manually managing an "array of GLFloat" or "array of TVec3f". Then, when it comes time to draw,
  19.   //all you need to do is something like this...
  20.  
  21. procedure VeryIncompleteGLDrawingProc;
  22.  
  23. implementation
  24.  
  25. procedure VeryIncompleteGLDrawingProc;
  26. begin
  27.   glVertexPointer(3, GL_FLOAT, 0, @VecList.AsArray[0]);
  28.   glDrawArrays(GL_LINES, 0, VecList.Count);
  29. end;
  30.  
  31. initialization
  32.   MyVectorList := TVectorList.Create;
  33.  
  34. finalization
  35.   MyVectorList.Free;
  36.  
  37. end.
« Last Edit: February 11, 2017, 04:28:56 am by SlightlyOutOfPhase »

hnb

  • Full Member
  • ***
  • Posts: 202
Re: RTL-Generics question/suggestion regarding TCustomList<T>.ToArray
« Reply #5 on: November 24, 2016, 01:53:34 pm »
So many more views than when I last posted, but no one has any input?

TList has FItems in protected section. You can create "class helper" to expose FItems or you can create descendant class from TList. Situation is not bad :) FItems in Delphi is declared in private section.

If you really like to have AsArray please create bug report on bugtracker.
Checkout NewPascal initiative and donate beer - ready to use tuned FPC compiler + Lazarus for mORMot project

best regards,
Maciej Izak

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 5875
Re: RTL-Generics question/suggestion regarding TCustomList<T>.ToArray
« Reply #6 on: November 24, 2016, 02:49:17 pm »
Download the code in the  http://forum.lazarus.freepascal.org/index.php/topic,30556.msg194484.html#msg194484 topic, and look at the units glhelper/gloverlayhelp

Basically I exported the pointer value and the elementsize from my generic class.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus