开发者

How to fast copy from hash to listview?

开发者 https://www.devze.com 2023-01-21 20:21 出处:网络
There is a hash pas file http://gpdelphiunits.googlecode.com/svn-history/r4/trunk/src/GpStringHash.pas

There is a hash pas file http://gpdelphiunits.googlecode.com/svn-history/r4/trunk/src/GpStringHash.pas

We can create hash and add key - value

Question 1: We want to know how to iterate key - value and copy data to listview.

Question 2: is there a way to fast copy like assign method to it?

Thank you very much in advance.

Dear gabr, Thank you so much 开发者_如何学Cfor your immediate reply and your hash file. Is there doc or help files or examples or demo for your code ? Thank you so much again.


Just test, I do not know where i did wrong Thank you so much. I just used your code but there is the following error prompt. Or I made some mistakes:

procedure TForm8.ab;
var
  a: TGpStringHash;
  i,j, fr:integer;
  k: string;
  enlist: TGpStringHashenumerator;
  kv: TGpStringHashKV;
begin
  a:=TGpStringHash.Create;
  kv:=TGpStringHashKV.Create;
  enlist:= TGpStringHashenumerator.Create(a);
  for j:=1 to 10 do begin
    if a.HasKey(inttostr(j)) then begin
      fr:=a.ValueOf(inttostr(j));
      a.Update(inttostr(j),fr+1);
    end
    else begin
      a.Add(inttostr(j),1);
    end;
  end;
  for i:=0 to a.Count -1 do begin
    kv:=enlist.GetCurrent;
    memo1.Lines.Add(kv.Key + inttostr(kv.value) );
  end;
end; /// Division by Zero ERROr ///FindBucket(const key: string): cardinal;

ANSWER: You're using enumerator improperly. Don't instantiate it in front and always use MoveNext to move to the next element.

// fill 'a' as above
enlist := TGpStringHashenumerator.Create(a);
while enList.MoveNext do begin
  kv:=enlist.GetCurrent;
  memo1.Lines.Add(kv.Key + inttostr(kv.value) );
end;


1) Use the latest version. It implements enumerators for all containers.

2) No.

EDIT:

I have committed my internal GpStringHash test app to the repository. It can server as a demo on how to use GpStringHash classes.

To enumerate TGpStringHash you would use

var
  hash: TGpStringHash;
  kv: TGpStringHashKV;

for kv in hash do
  // do something with kv.Key and kv.Value

If you're using an older Delphi without support for enumerators, you can use ForEach method with an external callback method.

procedure TGpStringHash.ForEach(enumerator: TGpStringHashEnumMethod);
var
  enum: TGpStringHashEnumerator;
begin
  enum := GetEnumerator;
  try
    while enum.MoveNext do
      enumerator(enum.Current);
  finally FreeAndNil(enum); end;
end; { TGpStringHash.ForEach }
0

精彩评论

暂无评论...
验证码 换一张
取 消