Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2013-04

Generics - how to rewrite TOjectDictionary

14 messages · 7 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Marius

Hello,

It was many years ago i tried fpc/lazarus so i'm not up to speed with eveything. At this moment I'm trying to rewrite a piece of software from delphi to fpc/laz and i'm having trouble rewriting generics and in special the TObjectDictionary. Below shows how it is implemented in delphi.

Is there a native fpc solution for this and if there is not what are the alternatives to rewrite this? Or would it even be better to avoid generics in general?

TMyDic = TObjectDictionary <string, TMyObject>;

I would welcome any tips to solve this

Thanks,
Marius

I'm using fpc 2.7.1, laz 1.1(svn 4/7/13)

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Generics-how-to-rewrite-TOjectDictionary-tp5714029.html Sent from the Free Pascal - General mailing list .
Dimitri Smits
to Marius

Hi,

Ever tried TStringList with Strings[] and Objects[] properties?

kind regards,
Dimitri Smits

----- Oorspronkelijk e-mail -----

Show quoted text 42 lines
Van: "Marius" Aan: Verzonden: Zondag 7 april 2013 18:48:02 Onderwerp: [fpc-pascal] Generics - how to rewrite TOjectDictionary Hello, It was many years ago i tried fpc/lazarus so i'm not up to speed with eveything. At this moment I'm trying to rewrite a piece of software from delphi to fpc/laz and i'm having trouble rewriting generics and in special the TObjectDictionary. Below shows how it is implemented in delphi. Is there a native fpc solution for this and if there is not what are the alternatives to rewrite this? Or would it even be better to avoid generics in general? TMyDic = TObjectDictionary <string, TMyObject>; I would welcome any tips to solve this Thanks, Marius I'm using fpc 2.7.1, laz 1.1(svn 4/7/13) -- View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Generics-how-to-rewrite-TOjectDictionary-tp5714029.html Sent from the Free Pascal - General mailing list archive at Nabble.com. _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Marius2
to Dimitri Smits

Thanks Dimitri,

Yes, I have used it lots of times and its really the last choice if theres something available like the objectdictionary. In this case the dict is also the owner of the objects, it saves me the continious ugly typecasting, takes care of the extra administration with freeing objects and all the additional troubles you get over time with this solution (its not as bad as i'm telling now). In other words tstrings.objects are working fine, but i find it a bad strategy if you have to use it in flatout in the whole application.

I'm curious how to rewrite/solve dictionaries, indexed list and even sorted strings.objects to a neat solution while (if possible) working with both delphi and fpc/laz.

Greetings,
Marius

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Generics-how-to-rewrite-TOjectDictionary-tp5714029p5714031.html Sent from the Free Pascal - General mailing list .
Marco van de Voort
to Marius2: Yes, I have used it lots of times and its really the last choice if theres something…

In our previous episode, Marius2 said:

Show quoted text 11 lines
Yes, I have used it lots of times and its really the last choice if theres something available like the objectdictionary. In this case the dict is also the owner of the objects, it saves me the continious ugly typecasting, takes care of the extra administration with freeing objects and all the additional troubles you get over time with this solution (its not as bad as i'm telling now). In other words tstrings.objects are working fine, but i find it a bad strategy if you have to use it in flatout in the whole application. I'm curious how to rewrite/solve dictionaries, indexed list and even sorted strings.objects to a neat solution while (if possible) working with both delphi and fpc/laz.

There is fgl.tfglmap and variants.

Sven Barth
to Marco van de Voort
Marco van de Voort wrote 13 lines
In our previous episode, Marius2 said: > Yes, I have used it lots of times and its really the last choice if theres > something available like the objectdictionary. In this case the dict is also > the owner of the objects, it saves me the continious ugly typecasting, takes > care of the extra administration with freeing objects and all the additional > troubles you get over time with this solution (its not as bad as i'm telling > now). In other words tstrings.objects are working fine, but i find it a bad > strategy if you have to use it in flatout in the whole application. > > I'm curious how to rewrite/solve dictionaries, indexed list and even sorted > strings.objects to a neat solution while (if possible) working with both > delphi and fpc/laz. There is fgl.tfglmap and variants.

But AFAIK we don't have a TFGLObjectMap...

Regards,
Sven

dmitry boyarintsev
to Marius2: Yes, I have used it lots of times and its really the last choice if theres something…

You can always use functions, to fight typecasting.

function GetMyObject(dic: TStrings; const nm: string): TMyObject;
var
  i : integer;
begin
  if not Assigned(dic) then begin Result:=nil; Exit; end;
  i:=dic.indexof(nm); // replace with IndexOfName if necessary
  if (i<0) or (i>=dic.Count) or not (i.Objects[i] is TMyObject) then
Result:=nil
  else Result:=TMyObject(i.Objects[i]); // not using "as" , since "is" has
already been used
end;

Of course, people would use ClassHelpers these days to have nice syntax and avoid the sanity check.

thanks,
Dmitry

Marius2 wrote 33 lines
Thanks Dimitri, Yes, I have used it lots of times and its really the last choice if theres something available like the objectdictionary. In this case the dict is also the owner of the objects, it saves me the continious ugly typecasting, takes care of the extra administration with freeing objects and all the additional troubles you get over time with this solution (its not as bad as i'm telling now). In other words tstrings.objects are working fine, but i find it a bad strategy if you have to use it in flatout in the whole application. I'm curious how to rewrite/solve dictionaries, indexed list and even sorted strings.objects to a neat solution while (if possible) working with both delphi and fpc/laz. Greetings, Marius -- View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Generics-how-to-rewrite-TOjectDictionary-tp5714029p5714031.html Sent from the Free Pascal - General mailing list . _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
dmitry boyarintsev
to dmitry boyarintsev

please disregard my last note about "avoiding the sanity check"

On Mon, Apr 8, 2013 at 1:52 PM, dmitry boyarintsev <

Show quoted text 62 lines
wrote: You can always use functions, to fight typecasting. function GetMyObject(dic: TStrings; const nm: string): TMyObject; var i : integer; begin if not Assigned(dic) then begin Result:=nil; Exit; end; i:=dic.indexof(nm); // replace with IndexOfName if necessary if (i<0) or (i>=dic.Count) or not (i.Objects[i] is TMyObject) then Result:=nil else Result:=TMyObject(i.Objects[i]); // not using "as" , since "is" has already been used end; Of course, people would use ClassHelpers these days to have nice syntax and avoid the sanity check. thanks, Dmitry On Mon, Apr 8, 2013 at 1:42 AM, Marius2 wrote: > Thanks Dimitri, > > Yes, I have used it lots of times and its really the last choice if theres > something available like the objectdictionary. In this case the dict is > also > the owner of the objects, it saves me the continious ugly typecasting, > takes > care of the extra administration with freeing objects and all the > additional > troubles you get over time with this solution (its not as bad as i'm > telling > now). In other words tstrings.objects are working fine, but i find it a > bad > strategy if you have to use it in flatout in the whole application. > > I'm curious how to rewrite/solve dictionaries, indexed list and even > sorted > strings.objects to a neat solution while (if possible) working with both > delphi and fpc/laz. > > Greetings, > Marius > > > > > > -- > View this message in context: > http://free-pascal-general.1045716.n5.nabble.com/Generics-how-to-rewrite-TOjectDictionary-tp5714029p5714031.html > Sent from the Free Pascal - General mailing list . > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal >
leledumbo
to Marius: It was many years ago i tried fpc/lazarus so i'm not up to speed with eveything. At this…

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 .
Marius
to dmitry boyarintsev: You can always use functions, to fight typecasting. function GetMyObject(dic: TStrings;…
dmitry boyarintsev wrote
function GetMyObject(dic: TStrings; const nm: string): TMyObject;

It is one of the few solutions to share the code between delphi and fpc (and yes, i would probably encase it in another class to hide the typecasting). Still need to figure out how hashing compare to stringlist (it should be a lot faster but i can't find the link at this moment)

(Sorry I had some trouble with the mailing list, previous msg's are delayed)

leledumbo
to Marius

Oops, my bad. There's actually a bug in the search. It will still be case sensitive despite the hash function uses lowercase-d version of the key. It only affects the items distribution, but the key is still searched in case sensitive way through the selected bucket.

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