如何在Delphi中创建多维关联数组?

时间:2015-08-07 09:32:40

标签: delphi multidimensional-array

我想在Delphi中创建一个像多维关联数组的数据结构。

我知道有TObjectDictionary。但是我认为用TObjectDictionary制作多维数组是如此困难和无效。

在Delphi中有没有其他好的方法来制作多维关联数组?

1 个答案:

答案 0 :(得分:5)

TDictionary是基于哈希的,因此对于任何数量的维度,性能都不会严重降低。二维版本的字典可以像这样实现:

  T2DimDictionary<TDim1,TDim2,TValue> = class
  private
    function GetValue(x: TDim1; y: TDim2): TValue;
    procedure SetValue(x: TDim1; y: TDim2; const Value: TValue);
  protected
    FDictionary: TObjectDictionary<TDim1, TDictionary<TDim2, TValue>>;
  public
    constructor Create;
    destructor Destroy; override;
    property Values[x: TDim1; y: TDim2]: TValue read GetValue write SetValue; default;
  end;

{ T2DimDictionary }

constructor T2DimDictionary<TDim1,TDim2,TValue>.Create;
begin
  FDictionary := TObjectDictionary<TDim1, TDictionary<TDim2, TValue>>.Create;
end;

destructor T2DimDictionary<TDim1,TDim2,TValue>.Destroy;
begin
  FreeAndNil(FDictionary);
  inherited;
end;

function T2DimDictionary<TDim1,TDim2,TValue>.GetValue(x: TDim1; y: TDim2): TValue;
var
  SubArray: TDictionary<TDim2, TValue>;
begin
  if not FDictionary.TryGetValue(x, SubArray) or
    not SubArray.TryGetValue(y, result)
  then
    raise exception.Create('Error');
end;

procedure T2DimDictionary<TDim1,TDim2,TValue>.SetValue(x: TDim1; y: TDim2;
  const Value: TValue);
var
  SubArray: TDictionary<TDim2, TValue>;
begin
  if not FDictionary.TryGetValue(x, SubArray) then
  begin
    SubArray := TDictionary<TDim2, TValue>.Create;
    FDictionary.Add(x, SubArray);
  end;
  SubArray.AddOrSetValue(y, Value);
end;

var
  d: T2DimDictionary<integer, string, string>;
begin
  d := T2DimDictionary<integer, string, string>.Create;
  d[1,'january'] := 'test1';
  d[1,'march'] := 'test2';
  assert(d[1,'january']='test1');
  FreeAndNil(d);
end;

或者,您可以使用基于所有&#34;尺寸&#34;的单个TDictionary和复杂的键。