#1 2017-03-13 14:06:11

Junior/RO
Member
Registered: 2011-05-13
Posts: 201

TSynDictionary: keys and values

Arnaud, what is the best approach to get keys and values from a TSynDictionary instance?

I have a dictionary where the keys are Double and the values are integers. My objective is get the count of integers by key. And I am using this:

My HugeList[] is a TDoubleDynArray:

  D := TSynDictionary.Create(TypeInfo(TDoubleDynArray), TypeInfo(TIntegerDynArray));
  try
    for I := 0 to High(HugeList) do begin
        if D.FindAndCopy(HugeList[I], N) then
            N := N + 1
        else
            N := 1;
        D.AddOrUpdate(HugeList[I], N);
    end;
    SetLength(Keys, D.Keys.Count);  // get a copy of the keys        <--- this works, but
    D.Keys.Slice(Keys, D.Keys.Count);
    SetLength(Counts, D.Values.Count);  // get a copy of the values  <--- there are a better way?
    D.Values.Slice(Counts, D.Values.Count);
  finally
    D.Free;
  end;

Offline

#2 2017-03-13 18:26:01

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 13,165
Website

Re: TSynDictionary: keys and values

I guess that the SetLength() are not mandatory.

Just call TDynArray.Slice, which will be very fast in your case.

Offline

#3 2017-03-13 20:42:59

Junior/RO
Member
Registered: 2011-05-13
Posts: 201

Re: TSynDictionary: keys and values

Yeah that works. Thank you.

Offline

Board footer

Powered by FluxBB