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.
_______________________________________________
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply via email to