Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.22 KB | None | 0 0
  1. unit USearchMap;
  2.  
  3. interface
  4.  
  5. uses
  6.   UList;
  7.  
  8. type
  9.   TSearchMap<T: class> = class
  10.   private type
  11.     TBlock = class
  12.     public
  13.       Key: string;
  14.       Values: TList<T>;
  15.       constructor Create(Key: string; Value: T);
  16.     end;
  17.  
  18.     TBlockList = TList<TBlock>;
  19.  
  20.   private
  21.     Map: array of TBlockList;
  22.  
  23.     function Hash(Key: string): Integer;
  24.   public
  25.     constructor Create(Size: Integer);
  26.     procedure Add(Key: string; Value: T);
  27.     function Get(Key: string): TList<T>;
  28.     procedure Remove(Key: string);
  29.   end;
  30.  
  31. implementation
  32.  
  33. { TMap<K, V> }
  34.  
  35. function TSearchMap<T>.Hash(Key: string): Integer;
  36. var
  37.   Value: char;
  38. begin
  39.   Result := 0;
  40.   for Value in Key do
  41.     Result := Result + Ord(Value);
  42. end;
  43.  
  44. procedure TSearchMap<T>.Remove(Key: string);
  45. var
  46.   Iterator: TBlockList.TIterator;
  47.   Index: Integer;
  48.   Temp: TBlock;
  49. begin
  50.   Index := Hash(Key) mod Length(Map);
  51.   Iterator := Map[Index].GetIterator;
  52.   while Iterator.HasNext do
  53.   begin
  54.     Temp := Iterator.Next;
  55.     if Temp.Key = Key then
  56.       Map[Index].Remove(Temp);
  57.   end;
  58. end;
  59.  
  60. procedure TSearchMap<T>.Add(Key: string; Value: T);
  61. var
  62.   Iterator: TBlockList.TIterator;
  63.   Index: Integer;
  64.   Block: TBlock;
  65. begin
  66.   Index := Hash(Key) mod Length(Map);
  67.  
  68.   Block := Nil;
  69.   Iterator := Map[Index].GetIterator;
  70.   while Iterator.HasNext do
  71.     Block := Iterator.Next;
  72.  
  73.   if Block = Nil then
  74.     Map[Index].Append(TBlock.Create(Key, Value))
  75.   else
  76.     Block.Values.Append(Value);
  77. end;
  78.  
  79. constructor TSearchMap<T>.Create(Size: Integer);
  80. var
  81.   I: Integer;
  82. begin
  83.   SetLength(Map, Size);
  84.   for I := Low(Map) to High(Map) do
  85.     Map[I] := TList<TBlock>.Create;
  86. end;
  87.  
  88. function TSearchMap<T>.Get(Key: string): TList<T>;
  89. var
  90.   Iterator: TBlockList.TIterator;
  91.   Index: Integer;
  92.   Temp: TBlock;
  93. begin
  94.   Result := Nil;
  95.   Index := Hash(Key) mod Length(Map);
  96.   Iterator := Map[Index].GetIterator;
  97.   while Iterator.HasNext do
  98.   begin
  99.     Temp := Iterator.Next;
  100.     if Temp.Key = Key then
  101.       Result := Temp.Values;
  102.   end;
  103. end;
  104.  
  105. { TMap<K, V>.TBlock }
  106.  
  107. constructor TSearchMap<T>.TBlock.Create(Key: string; Value: T);
  108. begin
  109.   Self.Key := Key;
  110.   if Values = Nil then
  111.     Values := TList<T>.Create;
  112.   Values.Append(Value);
  113. end;
  114.  
  115. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement