Is it possible to allocate memory to dynamic array with specific indexes?

痞子三分冷 提交于 2020-03-05 04:07:10

问题


Let suppose I have dynamic array

type TCharArr = Array of byte;
type PcharArr = ^TCharArr;
var charArr: PcharArr;

Which I want to allocate memory in Heap in the way of New(charArr);

However, how can I specify size and indexes? Is it possible dynamic array to have indexes eg. from 512.. to 1024?


回答1:


Dynamic arrays are always zero based. If you want to use array indices with a different base, then you would need to encapsulate the array access accounting for the offset to the indices. Something like this:

const
  Offset = 512;

function GetValue(Index: Integer): Byte;
begin
  Result := Arr[Index - Offset];
end;

procedure SetValue(Index: Integer; Value: Byte);
begin
  Arr[Index - Offset] := Value;
end;



回答2:


Assuming a more recent Delphi version, you can mimic that with a generic record:

type
  TDynArray<T> = record
  private
    FData: TArray<T>;
    FOffset: Integer;
    function GetData(Index: Integer): T;
    function GetHigh: Integer;
    function GetLength: Integer;
    function GetLow: Integer;
    procedure SetData(Index: Integer; const Value: T);
  public
    constructor Create(ALow, AHigh: Integer);
    property Data[Index: Integer]: T read GetData write SetData; default;
    property High: Integer read GetHigh;
    property Length: Integer read GetLength;
    property Low: Integer read GetLow;
  end;

constructor TDynArray<T>.Create(ALow, AHigh: Integer);
begin
  FOffset := ALow;
  SetLength(FData, AHigh - ALow + 1);
end;

function TDynArray<T>.GetData(Index: Integer): T;
begin
  Result := FData[Index - FOffset];
end;

function TDynArray<T>.GetHigh: Integer;
begin
  Result := FOffset + System.High(FData);
end;

function TDynArray<T>.GetLength: Integer;
begin
  Result := System.Length(FData);
end;

function TDynArray<T>.GetLow: Integer;
begin
  Result := FOffset;
end;

procedure TDynArray<T>.SetData(Index: Integer; const Value: T);
begin
  FData[Index - FOffset] := Value;
end;

The usage could look then like this:

var
  arr: TDynArray<Integer>;
  I: Integer;
begin
  arr := TDynArray<Integer>.Create(512, 1024);
  for I := arr.Low to arr.High do
    arr[I] := I;

  for I := arr.Low to arr.High do
    Writeln(I, '=', arr[I]);
  Readln;
end;



回答3:


In addition there is the concept of a sparse array (sparse matrix). Delphi does not support it out of the box, but there were implementations in TurboPower SysTools, if I remember correctly.

The source was put on SourceForge, when the company closed about 15 years ago: https://sourceforge.net/projects/tpsystools/

But these have not been updated for a looooong time.

This also seems to be the same library, maybe a bit more up to date: https://github.com/TurboPack/SysTools



来源:https://stackoverflow.com/questions/51592254/is-it-possible-to-allocate-memory-to-dynamic-array-with-specific-indexes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!