Einzelnen Beitrag anzeigen

Benutzerbild von Stevie
Stevie

Registriert seit: 12. Aug 2003
Ort: Soest
4.009 Beiträge
 
Delphi 10.1 Berlin Enterprise
 
#4

AW: Case Syntax

  Alt 14. Feb 2017, 14:29
So?

Delphi-Quellcode:
uses
  Generics.Collections;

type
  TMultiMap<TKey,TValue> = class
  private
    fHashMap: TDictionary<TKey,TList<TValue>>;
  public
    constructor Create;
    destructor Destroy; override;

    procedure Add(const key: TKey; const value: TValue);
  end;

constructor TMultiMap<TKey, TValue>.Create;
begin
  inherited Create;
  fHashMap := TObjectDictionary<TKey,TList<TValue>>.Create([doOwnsValues]);
end;

destructor TMultiMap<TKey, TValue>.Destroy;
begin
  fHashMap.Free;
  inherited;
end;

procedure TMultiMap<TKey, TValue>.Add(const key: TKey; const value: TValue);
var
  list: TList<TValue>;
begin
  if not fHashMap.TryGetValue(key, list) then
  begin
    list := TList<TValue>.Create;
    fHashMap.Add(key, list);
  end;
  list.Add(value);
end;
Stefan
“Simplicity, carried to the extreme, becomes elegance.” Jon Franklin

Delphi Sorcery - DSharp - Spring4D - TestInsight

Geändert von Stevie (14. Feb 2017 um 16:14 Uhr)
  Mit Zitat antworten Zitat