Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit USearchMap;
- interface
- uses
- UList;
- type
- TSearchMap<T: class> = class
- private type
- TBlock = class
- public
- Key: string;
- Values: TList<T>;
- constructor Create(Key: string; Value: T);
- end;
- TBlockList = TList<TBlock>;
- private
- Map: array of TBlockList;
- function Hash(Key: string): Integer;
- public
- constructor Create(Size: Integer);
- procedure Add(Key: string; Value: T);
- function Get(Key: string): TList<T>;
- procedure Remove(Key: string);
- end;
- implementation
- { TMap<K, V> }
- function TSearchMap<T>.Hash(Key: string): Integer;
- var
- Value: char;
- begin
- Result := 0;
- for Value in Key do
- Result := Result + Ord(Value);
- end;
- procedure TSearchMap<T>.Remove(Key: string);
- var
- Iterator: TBlockList.TIterator;
- Index: Integer;
- Temp: TBlock;
- begin
- Index := Hash(Key) mod Length(Map);
- Iterator := Map[Index].GetIterator;
- while Iterator.HasNext do
- begin
- Temp := Iterator.Next;
- if Temp.Key = Key then
- Map[Index].Remove(Temp);
- end;
- end;
- procedure TSearchMap<T>.Add(Key: string; Value: T);
- var
- Iterator: TBlockList.TIterator;
- Index: Integer;
- Block: TBlock;
- begin
- Index := Hash(Key) mod Length(Map);
- Block := Nil;
- Iterator := Map[Index].GetIterator;
- while Iterator.HasNext do
- Block := Iterator.Next;
- if Block = Nil then
- Map[Index].Append(TBlock.Create(Key, Value))
- else
- Block.Values.Append(Value);
- end;
- constructor TSearchMap<T>.Create(Size: Integer);
- var
- I: Integer;
- begin
- SetLength(Map, Size);
- for I := Low(Map) to High(Map) do
- Map[I] := TList<TBlock>.Create;
- end;
- function TSearchMap<T>.Get(Key: string): TList<T>;
- var
- Iterator: TBlockList.TIterator;
- Index: Integer;
- Temp: TBlock;
- begin
- Result := Nil;
- Index := Hash(Key) mod Length(Map);
- Iterator := Map[Index].GetIterator;
- while Iterator.HasNext do
- begin
- Temp := Iterator.Next;
- if Temp.Key = Key then
- Result := Temp.Values;
- end;
- end;
- { TMap<K, V>.TBlock }
- constructor TSearchMap<T>.TBlock.Create(Key: string; Value: T);
- begin
- Self.Key := Key;
- if Values = Nil then
- Values := TList<T>.Create;
- Values.Append(Value);
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement