[fpc-pascal] Re: Generics - how to rewrite TOjectDictionary

leledumbo leledumbo_cool at yahoo.co.id
Mon Apr 8 13:48:03 CEST 2013


use the following unit:

unit StringMyObjectMap;

{$mode objfpc}{$H+}

interface

uses ghashmap;

type
  TStringHash = class
    class function hash(s: String; n: Integer): Integer;
  end;

  TMyObject = class ... end; // define yourself

  TStringMyObjectMap = class(specialize
THashMap<String,TMyObject,TStringHash>)
    destructor Destroy; override;
  end;

implementation

class function TStringHash.hash(s: String; n: Integer): Integer;
var
  c: Char;
begin
  Result := 0;
  for c in s do Inc(Result,Ord(LowerCase(c))); // remove LowerCase if you
want the key search to be case sensitive
  Result := Result mod n;
end;

destructor TStringMyObjectMap.Destroy;
var
  It: TIterator;
begin
  if Size > 0 then begin
    It := Iterator;
    repeat
      It.Value.Free;
    until not It.Next;
    It.Free;
  end;
  inherited Destroy;
end;

end.

WARNING: untested code, though I'm quite sure it works fine



--
View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Generics-how-to-rewrite-TOjectDictionary-tp5714029p5714045.html
Sent from the Free Pascal - General mailing list archive at Nabble.com.



More information about the fpc-pascal mailing list