Recent

Author Topic: In stringlist can we get addorset method  (Read 1262 times)

Packs

  • Sr. Member
  • ****
  • Posts: 410
In stringlist can we get addorset method
« on: November 28, 2024, 06:30:41 am »
In tstringlist can we add new method addorset

cdbc

  • Hero Member
  • *****
  • Posts: 1786
    • http://www.cdbc.dk
Re: In stringlist can we get addorset method
« Reply #1 on: November 28, 2024, 07:24:45 am »
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
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Zvoni

  • Hero Member
  • *****
  • Posts: 2798
Re: In stringlist can we get addorset method
« Reply #2 on: November 28, 2024, 08:28:19 am »
In tstringlist can we add new method addorset
Class Helper?
Derived Class?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Thaddy

  • Hero Member
  • *****
  • Posts: 16413
  • Censorship about opinions does not belong here.
Re: In stringlist can we get addorset method
« Reply #3 on: November 28, 2024, 10:04:34 am »
[editted version, covers more cases]
Yes, something like this (untested):
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$modeswitch typehelpers}{$H+}
  2. uses classes;
  3.  
  4. type
  5.   TAddOrSet = type helper for Tstringlist
  6.   function AddOrSet(const value:string;obj:TObject = nil):integer;inline;
  7.   function AddOrSet(const name,value:string; obj:TObject = nil):integer;inline;
  8.   end;
  9.  
  10.   function TAddOrSet.AddOrSet(const value:string;obj:TObject):integer;
  11.   begin
  12.     Result:= self.indexof(value);
  13.     if result = -1 then
  14.       result:=self.addobject(value,obj)
  15.     else
  16.     begin
  17.       self[result]:= value;
  18.       self.objects[result]:=obj;
  19.     end;
  20.   end;
  21.  
  22.   function TAddOrSet.AddOrSet(const name,value:string;obj:TObject):integer;inline;
  23.   begin
  24.     Result:= self.indexof(name);
  25.     if result = -1 then
  26.     begin
  27.       self.addpair(name,value,obj);
  28.       result := self.indexof(name);
  29.     end
  30.     else
  31.     begin
  32.       self.values[name]:= value;
  33.       self.objects[result] := obj;
  34.     end;
  35.   end;
  36.  
  37. begin
  38. end.
« Last Edit: November 28, 2024, 10:50:25 am by Thaddy »
There is nothing wrong with being blunt. At a minimum it is also honest.

Packs

  • Sr. Member
  • ****
  • Posts: 410
Re: In stringlist can we get addorset method
« Reply #4 on: November 28, 2024, 10:38:35 am »
@taddy

Thank you 🙏

Thaddy

  • Hero Member
  • *****
  • Posts: 16413
  • Censorship about opinions does not belong here.
Re: In stringlist can we get addorset method
« Reply #5 on: November 28, 2024, 10:51:08 am »
I just posted a better version, editted my post. Covers more cases.
There is nothing wrong with being blunt. At a minimum it is also honest.

egsuh

  • Hero Member
  • *****
  • Posts: 1524
Re: In stringlist can we get addorset method
« Reply #6 on: November 28, 2024, 10:52:01 am »
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

  • Hero Member
  • *****
  • Posts: 16413
  • Censorship about opinions does not belong here.
Re: In stringlist can we get addorset method
« Reply #7 on: November 28, 2024, 10:57:24 am »
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.
« Last Edit: November 28, 2024, 11:12:35 am by Thaddy »
There is nothing wrong with being blunt. At a minimum it is also honest.

silvercoder70

  • Full Member
  • ***
  • Posts: 125
    • Tim Coates
Re: In stringlist can we get addorset method
« Reply #8 on: November 28, 2024, 11:20:21 am »
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?
Explore the beauty of modern Pascal programming with Delphi & Free Pascal - https://www.youtube.com/@silvercoder70

Thaddy

  • Hero Member
  • *****
  • Posts: 16413
  • Censorship about opinions does not belong here.
Re: In stringlist can we get addorset method
« Reply #9 on: November 28, 2024, 11:36:41 am »
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.
« Last Edit: November 28, 2024, 11:48:31 am by Thaddy »
There is nothing wrong with being blunt. At a minimum it is also honest.

jamie

  • Hero Member
  • *****
  • Posts: 6791
Re: In stringlist can we get addorset method
« Reply #10 on: November 28, 2024, 01:27:30 pm »
TFPGMap has what is needed already.
The only true wisdom is knowing you know nothing

Packs

  • Sr. Member
  • ****
  • Posts: 410
Re: In stringlist can we get addorset method
« Reply #11 on: November 28, 2024, 01:32:42 pm »
TFPGMap this is third party library or it stand library

Thaddy

  • Hero Member
  • *****
  • Posts: 16413
  • Censorship about opinions does not belong here.
Re: In stringlist can we get addorset method
« Reply #12 on: November 28, 2024, 01:44:36 pm »
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.
There is nothing wrong with being blunt. At a minimum it is also honest.

Thaddy

  • Hero Member
  • *****
  • Posts: 16413
  • Censorship about opinions does not belong here.
Re: In stringlist can we get addorset method
« Reply #13 on: November 28, 2024, 02:13:37 pm »
TFPGMap 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.
There is nothing wrong with being blunt. At a minimum it is also honest.

cdbc

  • Hero Member
  • *****
  • Posts: 1786
    • http://www.cdbc.dk
Re: In stringlist can we get addorset method
« Reply #14 on: November 28, 2024, 02:21:15 pm »
Hi Thaddy
Nice code, do you mind if I use it in 'IStringlist'?
With credits Ofc.  ;)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

 

TinyPortal © 2005-2018