#1 2015-01-27 08:54:32

jaclas
Member
Registered: 2014-09-12
Posts: 215

TDynArrayHashed.FindHashedForAdding - ambiguity

In the description of the method is written:


    // - warning: in contrast to the Add() method, if an entry is added to the
    // array (wasAdded=true), the entry is left VOID: you must set the field
    // content to expecting value - in short, Elem is used only for searching,
    // not for filling the newly created entry in the array

    function FindHashedForAdding(const Elem; out wasAdded: boolean; aHashCode: cardinal=0): integer;

1. What do you mean? Is this value is added to the array or not?
2. What is aHashCode param?

It's hard for me to understand how it works TDynArrayHashed, it is very complicated to use.
There is also no good documentation of these classes / records :-(

best regards
jacek

Offline

#2 2015-01-27 11:56:15

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

Re: TDynArrayHashed.FindHashedForAdding - ambiguity

The most obvious example is TSynCache, in the very same unit.

function TSynCache.Find(const aKey: RawUTF8; aResultTag: PPtrInt): RawUTF8;
var added: boolean;
begin
  result := '';
  if Self<>nil then
    if aKey='' then
      fFindLastAddedIndex := -1 else begin
      fFindLastAddedIndex := fNameValue.fDynArray.FindHashedForAdding(aKey,added);
      if added then
        fNameValue.List[fFindLastAddedIndex].Name := aKey else
        // match key found
        with fNameValue.List[fFindLastAddedIndex] do begin
          result := Value;
          if aResultTag<>nil then
            aResultTag^ := Tag;
          fFindLastAddedIndex := -1;
        end;
    end;
end;

See also TSynNameValue class:

procedure TSynNameValue.Add(const aName, aValue: RawUTF8; aTag: PtrInt);
var added: boolean;
    i: Integer;
begin
  i := fDynArray.FindHashedForAdding(aName,added);
  with List[i] do begin
    if added then
      Name := aName;
    Value := aValue;
    Tag := aTag;
  end;
  if Assigned(fOnAdd) then
    fOnAdd(List[i],i);
end;

The aHashCode parameter can be supplied, if the hash is already computed.
I've updated the documentation:

   ...
    // - optional aHashCode parameter can be supplied with an already hashed
    // value of the item, to be used e.g. after a call to HashFind() - default
    // 0 will use HashOneFromTypeInfo(Elem)
    function FindHashedForAdding(const Elem; out wasAdded: boolean;
      aHashCode: cardinal=0): integer;

Offline

Board footer

Powered by FluxBB