Board index » delphi » Setting tstringlist.sorted:=true without sorting?

Setting tstringlist.sorted:=true without sorting?

Yes, how to do that? Cause I know my strings are already sorted. and
Sorted:=true will do a sorting again before setting FSorted to true.
cu
 

Re:Setting tstringlist.sorted:=true without sorting?


I usually leave it as Sorted := false and then call the Sort method directly
whenever I need a sort to be performed.

Regards

Stan.

Re:Setting tstringlist.sorted:=true without sorting?


Stan,

all good and well, but by doing so you will not have the benefits of a
binary search
when searching your list. Every search you do will be sequential thus giving
you
a time penalty.

Lieven

Quote
> I usually leave it as Sorted := false and then call the Sort method
directly
> whenever I need a sort to be performed.

Re:Setting tstringlist.sorted:=true without sorting?


for TStringList.Find, always a binary search is done regardless if
FSorted=true or false. This is good when i know that my stringlist is
already sorted but dont want to sort a already sorted stringlist by setting
sorted:=true.
Its only that I am not on the save side by that "hack" because
-IndexOf will search lineary and
-I have to remember if the list is already sorted if I have a big program
with many such cases.
Unfortunately FSorted is a private attribute so in a workaround derivation
of tstringlist i have to introduce a property "AssumeSorted" or something.

Re:Setting tstringlist.sorted:=true without sorting?


Yeah,  sorry about that.  It works for me because I wrote my own search
routine which always binary searches.

Regards

Stan.

Re:Setting tstringlist.sorted:=true without sorting?


Friedrich,

just checked. You are right.
I could have used that knowledge already <g>

I remember reading a mail once (think it was posted
by Peter Below) where he explained how to alter
a private field of a class. Perhaps you can search
google for that. He does warn though that it's far
from good programming. If Borland decides to switch
the order of private fields in TStringlist or ancestor class,
you'll have to rewrite your code also.

Regards,
Lieven

Quote
> for TStringList.Find, always a binary search is done regardless if
> FSorted=true or false. This is good when i know that my stringlist is

Re:Setting tstringlist.sorted:=true without sorting?


Friedrich,

following approach seems to work (although I wouldn't recommend it)
The idea was picked up from following thread
http://groups.google.com/groups?hl=nl&lr=&threadm=VA.00008519.00d7d96...
ispam.compuserve.com&rnum=46&prev=/groups%3Fq%3Dprivate%2Bchange%2Bclass%2Bg
roup:borland.public.delphi*%2Bauthor:Peter%2Bauthor:Below%26hl%3Dnl%26lr%3D%
26start%3D40%26sa%3DN

type
  TStringListCracker = class(TStrings)
  private
    FList: PStringItemList;
    FCount: Integer;
    FCapacity: Integer;
    FSorted: Boolean;
  end;

procedure TForm1.Button1Click(Sender: TObject);
const
  CYesNo: array[Boolean] of ShortString = ('No', 'Yes');
var
  AStringList: TStringList;
begin
  AStringList := TStringList.Create;
  try
    ShowMessage(CYesNo[AStringList.Sorted]);
    TStringListCracker(AStringList).FSorted := True;
    ShowMessage(CYesNo[AStringList.Sorted]);
  finally
    AStringList.Free;
  end;
end;

Quote
> I remember reading a mail once (think it was posted
> by Peter Below) where he explained how to alter
> a private field of a class. Perhaps you can search

Re:Setting tstringlist.sorted:=true without sorting?


"Lieven Keersmaekers" <lieven.keersmaek...@removenospam.skynet.be>
wrote in message news:3cefd651_1@dnews...

Quote
> type
>   TStringListCracker = class(TStrings)
>   private
>     FList: PStringItemList;
>     FCount: Integer;
>     FCapacity: Integer;
>     FSorted: Boolean;
>   end;

There is also another way to change the value of "FSorted" without
doing a Sort:

Boolean((@AStringList.Sorted)^) := True;

This is also a dirty hack that works with properties that have a
data-member in their "read" directive and a procedure in their "write
directive or no "write" directive at all. Needless to say that it may
stop working in some future version of Delphi.

Re:Setting tstringlist.sorted:=true without sorting?


This workaround looks nice. Its wondering why Borland permits the @
infrontof properties (eiter they really forgot it or its a way to leave ways
open).
The asm of these approaches:

*TStringListCracker(Self).FSorted := True;
mov byte ptr [ebx+$1c],$01

*Boolean((@Sorted)^):=TRUE;
lea eax,[ebx+$1c]
mov byte ptr [eax],$01

So they forgot it I think..
Unfortunately this needs an extra instruction (which is equal to the first
version)

Other Threads