tags:

views:

57

answers:

1

Hi experts:

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 for 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  A: 

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 }
gabr
Dear gabr, Thank you so much for 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.
No, no, no and no :(
gabr
Update: example/demo is now stored in the SVN.
gabr
Thank you so much. I just used your code but there is the following error prompt. Or I made some mistakes:
why? format of our paste code changed.
procedure TForm1.ab;vari,j, fr:integer;k: string;enlist: TGpStringHashenumerator;kv: TGpStringHashkv;begina:=TGpStringHash.Create ;kv:=TGpStringHashkv.Create;enlist:= TGpStringHashenumerator.Create(a);for j:=1 to 10 do beginif a.HasKey(inttostr(j)) then beginfr:=a.ValueOf(inttostr(j));a.Update(inttostr(j),fr+1);end else begina.Add(inttostr(j),1);end;end;for i:=0 to a.Count -1 do beginkv:=enlist.GetCurrent ;memo1.Lines.Add(kv.Key + inttostr(kv.value) );end;end;
Division by Zero ERROr ///FindBucket(const key: string): cardinal;
We use delphi 7.0.
It works now though we do not know why not instantiate before numbering. Thank you again for your help and kindness.