Ignore:
Timestamp:
Feb 7, 2012, 2:03:20 PM (13 years ago)
Author:
chronos
Message:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Generics/NativeGenerics/Units/GenericDictionary.pas

    r132 r320  
    1414  end;
    1515
    16   TGDictionary<TPair> = class(TGList<TPair>)
     16  TGDictionary<TKey, TValue> = class
    1717  private
    1818  type
    19     TGDictionaryIndex = Integer;
     19    TIndex = NativeInt;
     20    TDictionaryPair = TGPair<TKey, TValue>;
    2021  var
    21     function GetKey(Index: TGDictionaryIndex): TPair.TKey;
    22     function GetValue(Key: TPair.TKey): TPair.TValue;
    23     procedure PutKey(Index: TGDictionaryIndex; const AValue: TPair.TKey);
    24     procedure PutValue(Key: TPair.TKey; const AValue: TPair.TValue);
     22    FList: TGList<TDictionaryPair>;
     23    function GetKey(Index: TIndex): TKey;
     24    function GetValue(Key: TKey): TValue;
     25    procedure PutKey(Index: TIndex; const AValue: TKey);
     26    procedure PutValue(Key: TKey; const AValue: TValue);
    2527  public
    26     function SearchKey(Key: TPair.TKey): TGDictionaryIndex;
    27     procedure Add(Key: TPair.TKey; Value: TPair.TValue);
    28     property Values[Index: TPair.TKey]: TPair.TValue
     28    constructor Create;
     29    destructor Destroy; override;
     30    function SearchKey(Key: TKey): TIndex;
     31    procedure Add(Key: TKey; Value: TValue);
     32    property Values[Index: TKey]: TValue
    2933      read GetValue write PutValue;
    30     property Keys[Index: TGDictionaryIndex]: TPair.TKey
     34    property Keys[Index: TIndex]: TKey
    3135      read GetKey write PutKey;
     36    property List: TGList<TDictionaryPair> read FList;
    3237  end;
    3338
     
    3540
    3641
    37 function TGDictionary<TPair>.GetKey(Index: TGDictionaryIndex): TPair.TKey;
     42constructor TGDictionary<TKey, TValue>.Create;
    3843begin
    39   Result := Items[Index].Key;
     44  FList := TGList<TDictionaryPair>.Create;
    4045end;
    4146
    42 function TGDictionary<TPair>.GetValue(Key: TPair.TKey): TPair.TValue;
     47destructor TGDictionary<TKey, TValue>.Destroy;
    4348begin
    44   Result := Items[SearchKey(Key)].Value;
     49  FList.Free;
    4550end;
    4651
    47 procedure TGDictionary<TPair>.PutKey(Index: TGDictionaryIndex;
    48   const AValue: TPair.TKey);
     52function TGDictionary<TKey, TValue>.GetKey(Index: TIndex): TKey;
     53begin
     54  Result := FList.Items[Index].Key;
     55end;
     56
     57function TGDictionary<TKey, TValue>.GetValue(Key: TKey): TValue;
     58begin
     59  Result := FList.Items[SearchKey(Key)].Value;
     60end;
     61
     62procedure TGDictionary<TKey, TValue>.PutKey(Index: TIndex;
     63  const AValue: TKey);
    4964var
    50   Item: TPair;
     65  Item: TDictionaryPair;
    5166begin
    5267  //Items[Index].Key := AValue;
    53   Item := Items[Index];
     68  Item := FList.Items[Index];
    5469  Item.Key := AValue;
    55   Items[Index] := Item;
     70  FList.Items[Index] := Item;
    5671end;
    5772
    58 procedure TGDictionary<TPair>.PutValue(Key: TPair.TKey;
    59   const AValue: TPair.TValue);
     73procedure TGDictionary<TKey, TValue>.PutValue(Key: TKey;
     74  const AValue: TValue);
    6075var
    61   Item: TPair;
    62   Index: TGDictionaryIndex;
     76  Item: TDictionaryPair;
     77  Index: TIndex;
    6378begin
    6479  //Items[SearchKey(Index)].Value := AValue;
    6580  Index := SearchKey(Key);
    66   Item := Items[Index];
     81  Item := FList.Items[Index];
    6782  Item.Value := AValue;
    68   Items[Index] := Item;
     83  FList.Items[Index] := Item;
    6984end;
    7085
    71 function TGDictionary<TPair>.SearchKey(Key: TPair.TKey): TGDictionaryIndex;
     86function TGDictionary<TKey, TValue>.SearchKey(Key: TKey): TIndex;
    7287begin
    7388  Result := 0;
    74   while Result < Count do begin
    75     if Items[Result].Key = Key then begin
     89  while Result < FList.Count do begin
     90    if FList.Items[Result].Key = Key then begin
    7691      Break;
    7792    end;
     
    8095end;
    8196
    82 procedure TGDictionary<TPair>.Add(Key: TPair.TKey; Value: TPair.TValue);
     97procedure TGDictionary<TKey, TValue>.Add(Key: TKey; Value: TValue);
    8398var
    84   NewPair: TPair;
     99  NewPair: TDictionaryPair;
    85100begin
    86101  NewPair.Key := Key;
    87102  NewPair.Value := Value;
    88   inherited Add(NewPair);
     103  FList.Add(NewPair);
    89104end;
    90105
Note: See TracChangeset for help on using the changeset viewer.