Browse Source

* Implemented IndexOfItem, reversed search again in IndexOf

git-svn-id: trunk@19279 -
michael 14 years ago
parent
commit
4a8914cbb0
2 changed files with 36 additions and 3 deletions
  1. 10 0
      rtl/objpas/classes/classesh.inc
  2. 26 3
      rtl/objpas/classes/lists.inc

+ 10 - 0
rtl/objpas/classes/classesh.inc

@@ -179,6 +179,11 @@ type
     function MoveNext: Boolean;
     property Current: Pointer read GetCurrent;
   end;
+  
+{$ifdef VER2_4}  
+type
+  TDirection = (FromBeginning, FromEnd);
+{$endif}          
 
   TFPList = class(TObject)
   private
@@ -200,6 +205,10 @@ type
     procedure SetCount(NewCount: Integer);
     Procedure RaiseIndexError(Index: Integer);
   public
+{$IFNDEF VER2_4}  
+    Type
+      TDirection = (FromBeginning, FromEnd);
+{$ENDIF}
     destructor Destroy; override;
     Procedure AddList(AList : TFPList);
     function Add(Item: Pointer): Integer; {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
@@ -212,6 +221,7 @@ type
     function First: Pointer;
     function GetEnumerator: TFPListEnumerator;
     function IndexOf(Item: Pointer): Integer;
+    function IndexOfItem(Item: Pointer; Direction: TDirection): Integer;
     procedure Insert(Index: Integer; Item: Pointer); {$ifdef CLASSESINLINE} inline; {$endif CLASSESINLINE}
     function Last: Pointer;
     procedure Move(CurIndex, NewIndex: Integer);

+ 26 - 3
rtl/objpas/classes/lists.inc

@@ -197,10 +197,33 @@ begin
 end;
 
 function TFPList.IndexOf(Item: Pointer): Integer;
+
+Var
+  C : Integer;
+
 begin
-  Result:=Count-1;
-  while (Result >=0) and (Flist^[Result]<>Item) do
-    Result:=Result - 1;
+  Result:=0;
+  C:=Count;
+  while (Result<C) and (Flist^[Result]<>Item) do
+    Inc(Result);
+  If Result>=C then
+    Result:=-1;
+end;
+
+function TFPList.IndexOfItem(Item: Pointer; Direction: TDirection): Integer;
+
+Var
+  C : Integer;
+
+begin
+  if Direction=fromBeginning then
+    Result:=IndexOf(Item)
+  else
+    begin
+    Result:=Count-1;
+    while (Result >=0) and (Flist^[Result]<>Item) do
+      Result:=Result - 1;
+    end;      
 end;
 
 procedure TFPList.Insert(Index: Integer; Item: Pointer);