TFPObjectHashTable more efficient way to filter objects lazarus / 21-General ---------------------------------------------------------------------- alpine · 2021-09-20 12:55 I'm trying to sweep the contents of a TFPObjectHashTable according to some filtering condition. Since it can't be iterated in a linear manner (it is a hash table), the only way I've realized so far is the following: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TTagCodeHash = class(TFPObjectHashTable)  protected    procedure IsValid(Item: TObject; const Key: string; var Continue: Boolean);  public    procedure Sweep1;    procedure Sweep2;  end; ... { TTagCodeHash } procedure TTagCodeHash.IsValid(Item: TObject; const Key: string;  var Continue: Boolean);begin  Continue := some_check_for_validity(Item);end; procedure TTagCodeHash.Sweep1;var  Item: THTObjectNode;begin  repeat    Item := ForEachCall(IsValid);    if Assigned(Item) then      Delete(Item.Key);  until not Assigned(Item);end; But this will call ForEachCall() from the beginning each time after item was deleted. The next thing I have tried was: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TTagCodeHash.Sweep2;var  I, J: LongWord;  Valid: Boolean;begin  for I := Pred(HashTableSize) downto 0 do    if Assigned(Chain(I)) then       for J := Pred(Chain(I).Count) downto 0 do       begin         IsValid(THTObjectNode(Chain(I)[J]).Data, '', Valid);         if not Valid then         begin           {$IF 0}           Chain(I).Delete(J);           Dec(FCount); // <-- Can't access private member FCount           {$ELSE}           Delete(THTObjectNode(Chain(I)[J]).Key);           {$ENDIF}         end;       end;end;But the Count property is read-only and FCount is private, so the only way to decrement is to call Delete(). Both ForEachCall() and Delete() will start again from the beginning of the table/chain, which is not the most efficient way for filtering. Does anyone have a better idea how to filter the hash table more efficiently? ---------------------------------------------------------------------- ASerge · 2021-09-20 21:14 Quote from: y.ivanov on September 20, 2021, 02:55:26 pmBut the Count property is read-only and FCount is private, so the only way to decrement is to call Delete(). Well, if you really need it : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$MODE OBJFPC}{$LONGSTRINGS ON}{$APPTYPE CONSOLE} uses Contnrs; function GetAddressOfReadFieldForProperty(constref Prop: LongWord): PLongWord; inline;begin  Result := @Prop;end; procedure Test;var  Hash: TFPObjectHashTable;begin  Hash := TFPObjectHashTable.Create;  try    Hash.Add('Some', nil);    Writeln('Hash count: ', Hash.Count);    GetAddressOfReadFieldForProperty(Hash.Count)^ := 0;    Writeln('Hash count: ', Hash.Count);  finally    Hash.Free;  end;end; begin  Test;  Readln;end. ---------------------------------------------------------------------- PascalDragon · 2021-09-21 07:05 Quote from: y.ivanov on September 20, 2021, 02:55:26 pmDoes anyone have a better idea how to filter the hash table more efficiently? Best right now is to filter those you want to keep into a separate list and then readd them. Otherwise use some other container type that's more efficiently filterable than a TFPObjectHashTable. If you go the route of ASerge I'm going to feel the urge to change the property from a field getter to a method getter in trunk just to spite anyone relying on implementation details. ---------------------------------------------------------------------- alpine · 2021-09-22 08:18 Quote from: ASerge on September 20, 2021, 11:14:47 pmWell, if you really need it : I won't need it that much  I can add another property along with the Count to track deletes or simply ignore it. Quote from: PascalDragon on September 21, 2021, 09:05:43 amBest right now is to filter those you want to keep into a separate list and then readd them. Otherwise use some other container type that's more efficiently filterable than a TFPObjectHashTable. Perhaps you're right. Looking at the code it comes to mind that clearing the hash table and re-adding will be less costly than searching and deleting from chains/lists. I can splice TFPObjectHashTable with TFPObjectList and use the latter just for the iteration. My question was more whether there is another more suitable map container with a reverse iterator or something like that. I'm a bit confused by the abundance of different types of containers for the same thing, i.e. hashtables, maps, dictionaries, etc. Thanks for the replies,