In stringlist can we get addorset method lazarus / 21-General ---------------------------------------------------------------------- Packs · 2024-11-28 05:30 In tstringlist can we add new method addorset ---------------------------------------------------------------------- cdbc · 2024-11-28 06:24 Hi What do you mean, what would that do, how should it look...?!? If You show me, I can rather quickly add it to my "IStringList" implementation, which btw. is found here. Regards Benny ---------------------------------------------------------------------- Zvoni · 2024-11-28 07:28 Quote from: Packs on November 28, 2024, 06:30:41 amIn tstringlist can we add new method addorset Class Helper? Derived Class? ---------------------------------------------------------------------- Thaddy · 2024-11-28 09:04 [editted version, covers more cases] Yes, something like this (untested):Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$mode objfpc}{$modeswitch typehelpers}{$H+}uses classes; type  TAddOrSet = type helper for Tstringlist  function AddOrSet(const value:string;obj:TObject = nil):integer;inline;  function AddOrSet(const name,value:string; obj:TObject = nil):integer;inline;  end;    function TAddOrSet.AddOrSet(const value:string;obj:TObject):integer;  begin    Result:= self.indexof(value);    if result = -1 then      result:=self.addobject(value,obj)    else    begin      self[result]:= value;      self.objects[result]:=obj;    end;  end;    function TAddOrSet.AddOrSet(const name,value:string;obj:TObject):integer;inline;  begin    Result:= self.indexof(name);    if result = -1 then    begin      self.addpair(name,value,obj);      result := self.indexof(name);    end    else    begin      self.values[name]:= value;      self.objects[result] := obj;    end;  end;  beginend. ---------------------------------------------------------------------- Packs · 2024-11-28 09:38 @taddy Thank you 🙏 ---------------------------------------------------------------------- Thaddy · 2024-11-28 09:51 I just posted a better version, editted my post. Covers more cases. ---------------------------------------------------------------------- egsuh · 2024-11-28 09:52 If you set TStringList.Sorted = true and TStringList.Duplicates = dupIgnore then it will do what we guess you want to achieve. https://www.freepascal.org/docs-html/rtl/classes/tstringlist.add.html Or if you want to set value for a name, e.g. Name=Value format, than simply   Values [name] := newvalue; does it. ---------------------------------------------------------------------- Thaddy · 2024-11-28 09:57 Nope. For both cases. If you want to do it properly and without relying on sorted, the type helper I showed is a better option. It does not make any assumption about options. The latter suggestion is plain wrong because the item may not exist. (Look how I handle addpair with object) Note my code is not fast, I intentionally chose the most universal cases. comparing the value and obj parts is really overkill, so even if value and obj are equal these will be overwritten anyway. ---------------------------------------------------------------------- silvercoder70 · 2024-11-28 10:20 dumb question ... if you happen to call this "add or set" with the same key value but different objects, or keeps a list in sync with database, what happens to the existing element that might be already stored. For example: initially... list.AddOrSet(serial-number, object-associated-with-serial-number); on loading from dataset (say 10 minutes later)... list.AddOrSet(serial-number, object-associated-with-serial-number- updated); what happens with the object associated with serial-number initially? ---------------------------------------------------------------------- Thaddy · 2024-11-28 10:36 If objects are equal, that means they are the same object instance, hence no need to take provisions for that... You did not consider that, did you? It is a pointer comparison, the reference is stored. And so it does not hurt to overwrite same valued pointers. Overwrite a pointer is likely also faster than introducing extra branching. This is also the way the separate methods work already. In general AddOrSet is quite a common pattern and is supported by most generics libraries for fpc. ---------------------------------------------------------------------- jamie · 2024-11-28 12:27 TFPGMap has what is needed already. ---------------------------------------------------------------------- Packs · 2024-11-28 12:32 TFPGMap this is third party library or it stand library ---------------------------------------------------------------------- Thaddy · 2024-11-28 12:44 It is standard: it is in the fgl unit which is part of the rtl, it is not a separate package. Note that it does not support everything, though: name-value pairs + objects are afaik not supported. But if you don't need that it is a good option. It is also lightweight. Other generics libraries also supports what you want in that case. ---------------------------------------------------------------------- Thaddy · 2024-11-28 13:13 Quote from: jamie on November 28, 2024, 01:27:30 pmTFPGMap has what is needed already. Nope. addpair(name,value,obj); is not covered in TFpgMap. It is a strict map. But in the simple cases it would be enough. ---------------------------------------------------------------------- cdbc · 2024-11-28 13:21 Hi Thaddy Nice code, do you mind if I use it in 'IStringlist'? With credits Ofc.  Regards Benny ---------------------------------------------------------------------- Thaddy · 2024-11-28 13:23 Of course not... But as I wrote it is not "fast" although it covers most cases. ---------------------------------------------------------------------- cdbc · 2024-11-28 13:27 Hi I can live with that, stringlist being the goto workhorse and all, I think, covering more cases is better than speed... Thanks - Benny