View Issue Details

IDProjectCategoryView StatusLast Update
0038748pas2jsrtlpublic2021-04-13 09:16
Reporterhenrique Assigned ToMichael Van Canneyt  
PrioritynormalSeverityminorReproducibilityalways
Status resolvedResolutionfixed 
PlatformPas2JsOSWindows 
Fixed in Versiontrunk 
Summary0038748: Generic TList with removal problem.
DescriptionI found a comparison problem in the generic TList, which does not remove the past parameter item in "Remove".

The problem is because the created comparator does not support objects in the correct way.

I made a correction to this, that if the comparator is not provided, I simply compare one item with another.
TagsNo tags attached.
Fixed in Revision1143
Attached Files

Activities

henrique

2021-04-12 22:47

reporter  

0001-Ajustado-problema-de-compara-o-no-TList.patch (1,885 bytes)   
From 746e40785b2c7b491e1bd512ea197307a759dd1d Mon Sep 17 00:00:00 2001
From: Henrique Gottardi Werlang <henriquewerlang@hotmail.com>
Date: Mon, 12 Apr 2021 16:05:05 -0300
Subject: [PATCH] =?UTF-8?q?Ajustado=20problema=20de=20compara=C3=A7=C3=A3o?=
 =?UTF-8?q?=20no=20TList.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 packages/rtl/generics.collections.pas | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/packages/rtl/generics.collections.pas b/packages/rtl/generics.collections.pas
index 96ccac2e..c8d7170b 100644
--- a/packages/rtl/generics.collections.pas
+++ b/packages/rtl/generics.collections.pas
@@ -152,6 +152,7 @@ type
   TList<T> = class(TCustomList<T>)
   private
     FComparer: IComparer<T>;
+    function SameValue(const Left, Right: T): Boolean;
   protected
     procedure SetCapacity(AValue: SizeInt); override;
     procedure SetCount(AValue: SizeInt);
@@ -812,6 +813,11 @@ end;
 
 { TList }
 
+function TList<T>.SameValue(const Left, Right: T): Boolean;
+begin
+  Result := Assigned(FComparer) and (FComparer.Compare(Left, Right) = 0) or (Left = Right);
+end;
+
 procedure TList<T>.SetCapacity(AValue: SizeInt);
 begin
   if AValue < Count then
@@ -877,7 +883,6 @@ end;
 constructor TList<T>.Create;
 begin
   InitializeList;
-  FComparer := TComparer<T>.Default;
 end;
 
 constructor TList<T>.Create(const AComparer: IComparer<T>);
@@ -1111,7 +1116,7 @@ var
   i: SizeInt;
 begin
   for i := 0 to Count - 1 do
-    if FComparer.Compare(AValue, FItems[i]) = 0 then
+    if SameValue(AValue, FItems[i]) then
       Exit(i);
   Result:=-1;
 end;
@@ -1121,7 +1126,7 @@ var
   i: SizeInt;
 begin
   for i := Count - 1 downto 0 do
-    if FComparer.Compare(AValue, FItems[i]) = 0 then
+    if SameValue(AValue, FItems[i]) then
       Exit(i);
   Result:=-1;
 end;
-- 
2.31.1.windows.1

Michael Van Canneyt

2021-04-13 09:16

administrator   ~0130338

Slightly adjusted patch applied.

Issue History

Date Modified Username Field Change
2021-04-12 22:47 henrique New Issue
2021-04-12 22:47 henrique File Added: 0001-Ajustado-problema-de-compara-o-no-TList.patch
2021-04-13 09:16 Michael Van Canneyt Assigned To => Michael Van Canneyt
2021-04-13 09:16 Michael Van Canneyt Status new => resolved
2021-04-13 09:16 Michael Van Canneyt Resolution open => fixed
2021-04-13 09:16 Michael Van Canneyt Fixed in Version => trunk
2021-04-13 09:16 Michael Van Canneyt Fixed in Revision => 1143
2021-04-13 09:16 Michael Van Canneyt Note Added: 0130338