Changeset 24 for branches/Xvcl/Xvcl.Generics.pas
- Timestamp:
- May 8, 2013, 2:53:22 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/Xvcl/Xvcl.Generics.pas
r23 r24 2 2 3 3 interface 4 5 uses 6 SysUtils; 4 7 5 8 type … … 24 27 function GetEnumerator: TEnumerator; 25 28 function Add(Item: T): Integer; 26 procedure Remove(Item: T); 29 function Remove(Item: T): Integer; 30 procedure Delete(Index: Integer); 31 procedure Move(FromIndex, ToIndex: Integer); 32 function IndexOf(Item: T): Integer; 33 function Compare(Item1, Item2: T): Integer; 34 procedure Exchange(Index1, Index2: Integer); 27 35 property Count: Integer read FCount write SetCount; 28 36 property Items[Index: Integer]: T read GetItem write SetItem; default; … … 55 63 end; 56 64 65 function TList<T>.Compare(Item1, Item2: T): Integer; 66 begin 67 if CompareMem(Pointer(@Item1), Pointer(@Item2), SizeOf(T)) then Result := 0 68 else Result := -1; 69 end; 70 71 procedure TList<T>.Delete(Index: Integer); 72 var 73 I: Integer; 74 begin 75 if (Index >= 0) and (Index < Count) then 76 for I := Index to Count - 2 do 77 Items[I] := Items[I + 1]; 78 end; 79 80 procedure TList<T>.Exchange(Index1, Index2: Integer); 81 var 82 Temp: T; 83 begin 84 Temp := Items[Index2]; 85 Items[Index2] := Items[Index1]; 86 Items[Index1] := Temp; 87 end; 88 57 89 function TList<T>.GetEnumerator: TEnumerator; 58 90 begin … … 67 99 end; 68 100 69 procedure TList<T>.Remove(Item: T); 101 function TList<T>.IndexOf(Item: T): Integer; 102 var 103 I: Integer; 70 104 begin 105 I := 0; 106 while (I < Count) and (Compare(Items[I], Item) <> 0) do 107 Inc(I); 108 if I < Count then Result := I 109 else Result := -1; 110 end; 71 111 112 procedure TList<T>.Move(FromIndex, ToIndex: Integer); 113 var 114 I: Integer; 115 Temp: T; 116 begin 117 if (FromIndex >= 0) and (FromIndex < Count) and 118 (ToIndex >= 0) and (ToIndex < Count) then begin 119 if FromIndex > ToIndex then begin 120 Temp := Items[ToIndex]; 121 for I := ToIndex to FromIndex - 1 do 122 Items[I] := Items[I + 1]; 123 Items[FromIndex] := Temp; 124 end else 125 if FromIndex < ToIndex then begin 126 Temp := Items[FromIndex]; 127 for I := FromIndex to ToIndex - 1 do 128 Items[I] := Items[I + 1]; 129 Items[ToIndex] := Temp; 130 end; 131 end; 132 end; 133 134 function TList<T>.Remove(Item: T): Integer; 135 begin 136 Result := IndexOf(Item); 137 if Result >= 0 then Delete(Result); 72 138 end; 73 139
Note:
See TracChangeset
for help on using the changeset viewer.