Board index » delphi » name-value pair lookup in TstringList

name-value pair lookup in TstringList

I just looked at the source code, and the lookup:
aStringList.Values['mykeystring']
is done as a linear search. Considering that TStringList is _the_ Delphi
class, shouldn't the lookup function check whether the string list is
sorted and then use a boolean search for better performance? Now that I
know about this inefficiency, the "associative array" functionality of
TStringList seems pretty useless.

I know this is not recommended, but instead of rewriting a bunch of my
own code, I'd like to change this lookup implementation in the Borland
code. What steps should I take to ensure that my changes are included by
the linker?

Paul

 

Re:name-value pair lookup in TstringList


Quote
"Pawel Slusarz" <REMOVEwiedz...@yahoo.comME> wrote in message
> I know this is not recommended, but instead of rewriting a bunch of my
> own code, I'd like to change this lookup implementation in the Borland
> code. What steps should I take to ensure that my changes are included by
> the linker?

unit StringListUnit;

interface

uses
  Classes;

type
  TStringList = class(Classes.TStringList)
    function IndexOfName(const Name: string): Integer; override;
  end;

implementation

function TStringList.IndexOfName(const Name: string): Integer;
begin
  Result := inherited IndexOfName(Name);
end;

end.

If two units has the same class name, the linker takes the code in the
last unit in the uses clause, therefore it will use this class (code)
if StringListUnit comes after Classes. You only have to add this unit
to the units you are creating this custom version of TStringList in.

--
Regards
Torjei Kvinen

Re:name-value pair lookup in TstringList


Two points :-

a) Fiddling with the Delphi libraries is very dangerous.

b) It is quite possible to have more than one Name=Value pair in a
chunk of TStringList.Text
Doing a chop on a sorted list will *probably* bring up inconsistent
results - TStringList.Find is aware of this

If you really want to 'improve' the TStringList then you should write
a descendant that overrides the methods/properties that you don't
like.

That may not be so easy as the 'culprit' is
   function IndexOfName(const Name: string): Integer;
in
   TStrings

and this is not a virtual method

I shudder at the thought of the problems that could be introduced.

On Sat, 19 Oct 2002 00:27:44 GMT, "Pawel Slusarz"

Quote
<REMOVEwiedz...@yahoo.comME> wrote:
>I just looked at the source code, and the lookup:
>aStringList.Values['mykeystring']
>is done as a linear search. Considering that TStringList is _the_ Delphi
>class, shouldn't the lookup function check whether the string list is
>sorted and then use a boolean search for better performance? Now that I
>know about this inefficiency, the "associative array" functionality of
>TStringList seems pretty useless.

>I know this is not recommended, but instead of rewriting a bunch of my
>own code, I'd like to change this lookup implementation in the Borland
>code. What steps should I take to ensure that my changes are included by
>the linker?

>Paul

Re:name-value pair lookup in TstringList


I am pretty sure that simply adding another (later) Unit will not have
much effect on the OP's original 'problem'

As far as I understand, he wants to 'redefine' the basic TStringList
So things like TMemo etc use the 'faster' TStringList

I dont't think he wants to just have a 'different' TStringList that is
used by any Units that have this 'special unit' last in its uses
clause.

Anyway I get the following error :-

    [Error] Unit2.pas(10): Cannot override a static method

Removing the Override makes it work fine, when one calls the
   IndexOfName function

However
   S := SL.Values[ 'test' ]

does NOT call this 'redefined thing' - since the code in Classes.pas
has no idea of the code in StringListUnit.pas

What this has done is to create a 'special' thing that replaces the
basic TStringList
- but only for Units that 'know' about this 'special' Unit

This means that there will be two types of object floating around the
App - the core TStringList
- and something with the same name
- but is not the same thing.

On Sat, 19 Oct 2002 05:40:45 GMT, "Torjei Kvinen"

Quote
<torjei@hahaha_bluezone.no> wrote:
>"Pawel Slusarz" <REMOVEwiedz...@yahoo.comME> wrote in message

>> I know this is not recommended, but instead of rewriting a bunch of my
>> own code, I'd like to change this lookup implementation in the Borland
>> code. What steps should I take to ensure that my changes are included by
>> the linker?

>unit StringListUnit;

>interface

>uses
>  Classes;

>type
>  TStringList = class(Classes.TStringList)
>    function IndexOfName(const Name: string): Integer; override;
>  end;

>implementation

>function TStringList.IndexOfName(const Name: string): Integer;
>begin
>  Result := inherited IndexOfName(Name);
>end;

>end.

>If two units has the same class name, the linker takes the code in the
>last unit in the uses clause, therefore it will use this class (code)
>if StringListUnit comes after Classes. You only have to add this unit
>to the units you are creating this custom version of TStringList in.

>--
>Regards
>Torjei Kvinen

Re:name-value pair lookup in TstringList


Quote
"J French" <Bounce_It_je...@iss.u-net.com_.bin> wrote in message
>     [Error] Unit2.pas(10): Cannot override a static method

In Delphi 6, TStrings.IndexOfName is a virtual method.

--
Regards
Torjei Kvinen

Re:name-value pair lookup in TstringList


Ah - in D4 it is not

That makes a major difference, but will it help the OP with his
original problem :-

   that he wanted to *replace* the basic TStringList class
   throughout *all* of his App

Surely what you suggested would only 'out scope' the TStringList Class
in the Classes Unit for units that had your TStringListUnit in their
Uses clause

On Sat, 19 Oct 2002 08:02:39 GMT, "Torjei Kvinen"

Quote
<torjei@hahaha_bluezone.no> wrote:
>"J French" <Bounce_It_je...@iss.u-net.com_.bin> wrote in message

>>     [Error] Unit2.pas(10): Cannot override a static method

>In Delphi 6, TStrings.IndexOfName is a virtual method.

>--
>Regards
>Torjei Kvinen

Re:name-value pair lookup in TstringList


Quote
"Torjei Kvinen" <torjei@hahaha_bluezone.no> wrote in message

news:zk8s9.1191$QT5.28090@news2.ulv.nextra.no...

Quote
> "J French" <Bounce_It_je...@iss.u-net.com_.bin> wrote in message

> >     [Error] Unit2.pas(10): Cannot override a static method

> In Delphi 6, TStrings.IndexOfName is a virtual method.

But is it virtual in TStringList, which you derived from in your code?

Dan

Quote
> --
> Regards
> Torjei Kvinen

Re:name-value pair lookup in TstringList


Quote
"Dan F" <Da...@removeme.Bigfoot.Com> wrote in message
> But is it virtual in TStringList, which you derived from in your code?

TStringList doesn't implement IndexOfName, so it's overriding the virtual
method in TStrings.

--
Regards
Torjei Kvinen

Other Threads