Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2020-10

TFPGMapObject duplicate errors

6 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Ryan Joseph

I expected to get an assertion when I added the duplicate key "foo". What am I doing wrong?

type
  TSomeMap = specialize TFPGMapObject<String, TObject>;
var
  map: TSomeMap;
begin
  map := TSomeMap.Create(true);
  map.Duplicates := dupError;
  map.Add('foo', TObject.Create);
  map.Add('foo', TObject.Create);

Regards,
Ryan Joseph

Reply by email

Vojtěch Čihák
to Ryan Joseph
I looked to code, it gives error only when map is Sorted=True.
 
function TFPSMap.Add(AKey: Pointer): Integer;
begin
  if Sorted then
  begin
    if Find(AKey, Result) then
      case Duplicates of
        dupIgnore: exit;
        dupError: Error(SDuplicateItem, 0)
      end;
  end else
    Result := Count;
  CopyKey(AKey, inherited Insert(Result));
end;  
 
V.
______________________________________________________________
Show quoted text 5 lines
Od: "Ryan Joseph via fpc-pascal" Komu: "FPC-Pascal users discussions" Datum: 02.10.2020 02:24 Předmět: [fpc-pascal] TFPGMapObject duplicate errors

I expected to get an assertion when I added the duplicate key "foo". What am I doing wrong?

 type
  TSomeMap = specialize TFPGMapObject<String, TObject>;
 var
  map: TSomeMap;
 begin
  map := TSomeMap.Create(true);
  map.Duplicates := dupError;
  map.Add('foo', TObject.Create);
  map.Add('foo', TObject.Create);

Regards,
Ryan Joseph

Reply by email

Ryan Joseph
to Vojtěch Čihák
On Oct 1, 2020, at 7:00 PM, Vojtěch Čihák via fpc-pascal wrote: I looked to code, it gives error only when map is Sorted=True.

Thanks. Sneaky that sorted had to be set to true. Not sure why that would be.

Regards,
Ryan Joseph

Reply by email

Michael Van Canneyt
to Ryan Joseph
Ryan Joseph wrote 8 lines
> On Oct 1, 2020, at 7:00 PM, Vojtěch Čihák via fpc-pascal wrote: > > I looked to code, it gives error only when map is Sorted=True. > Thanks. Sneaky that sorted had to be set to true. Not sure why that would be.

Because otherwise it would take a linear search to do IndexOf whenever you insert/append an element in the list.

It is in fact documented, so you know:
https://www.freepascal.org/docs-html/current/rtl/fgl/tfpsmap.duplicates.html

Michael.

Reply by email

Mattias Gaertner
to Michael Van Canneyt

On Sat, 3 Oct 2020 15:53:58 +0200 (CEST)
Michael Van Canneyt via fpc-pascal

Show quoted text 7 lines
On Fri, 2 Oct 2020, Ryan Joseph via fpc-pascal wrote: [...] > Thanks. Sneaky that sorted had to be set to true. Not sure why that > would be. Because otherwise it would take a linear search to do IndexOf whenever you insert/append an element in the list.

Insert element is linear anyway.

Why not set Duplicate to dupAccept as default?

Mattias

Reply by email

Michael Van Canneyt
to Mattias Gaertner
Mattias Gaertner wrote 13 lines
On Sat, 3 Oct 2020 15:53:58 +0200 (CEST) Michael Van Canneyt via fpc-pascal wrote: > On Fri, 2 Oct 2020, Ryan Joseph via fpc-pascal wrote: > [...] > > Thanks. Sneaky that sorted had to be set to true. Not sure why that > > would be. > > Because otherwise it would take a linear search to do IndexOf > whenever you insert/append an element in the list. Insert element is linear anyway.

But it does not need a search, I suppose ?

Why not set Duplicate to dupAccept as default?

Just guessing:
Because it is a map, and usually you don't want a map to contain duplicates ?

If any change is needed, I would set the 'Sorted' to true by default.

Michael.

Reply by email