[fpc-devel] Aligned dynamic arrays

Anthony Walter sysrpl at gmail.com
Sat Mar 30 05:56:21 CET 2019


Inside the type I have:

  TAlignedArray<T> = record
  public
    type
      TReference = ^T;
      TValue = T;
    ...
    procedure Push(const Item: TValue);
    function Pop: TValue;
    property Length: Integer read FLength write SetLength;
    property Reference[Index: Integer]: TReference read GetReference;
default;
    property Item[Index: Integer]: TValue read GetItem write SetItem;
    ...
  end;

So the default property indexer returns references and not values. If you
want values specifically you can use the Item property indexer.

This way we can either access fields directly on the items withing the
array like so:

A[0].X := SomeValue;

Or if you need fast access to quickly populate the values, support we want
to write 3 component vertex data, you could write:

type
  TVertexBuffer = type TAlignedArray<TVector3>;
var
  Buffer: TVertexBuffer;
  V: TVertexBuffer.TReference;
begin
  Buffer.Length := VertexCount(ModelStream);
  V := Buffer[0];
  for I := 1 to Buffer.Length do
  begin
    V.X := ReadX(ModelStream);
    V.Y := ReadY(ModelStream);
    V.Z := ReadZ(ModelStream);
    Inc(V);
  end;
  ...
end;

Which probably ends up being faster than continually accessing array
dynamic values by index.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20190330/38b5eedd/attachment.html>


More information about the fpc-devel mailing list