- Timestamp:
- Oct 27, 2010, 9:28:58 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Generics/TemplateGenerics/List/GenericListImplementation.tpl
r68 r69 10 10 { TGList } 11 11 12 function TGList.GetCapacity: T IndexType;12 function TGList.GetCapacity: TListIndex; 13 13 begin 14 14 Result := Length(FItems); 15 15 end; 16 16 17 procedure TGList.SetCapacity(const AValue: T IndexType);17 procedure TGList.SetCapacity(const AValue: TListIndex); 18 18 begin 19 19 SetLength(FItems, AValue); 20 20 end; 21 21 22 function TGList.Get(Index: T IndexType): TItemType;22 function TGList.Get(Index: TListIndex): TListItem; 23 23 begin 24 24 Result := FItems[Index]; 25 25 end; 26 26 27 function TGList.GetCount: T IndexType;27 function TGList.GetCount: TListIndex; 28 28 begin 29 29 Result := FCount; 30 30 end; 31 31 32 procedure TGList.Put(Index: T IndexType; const AValue: TItemType);32 procedure TGList.Put(Index: TListIndex; const AValue: TListItem); 33 33 begin 34 34 FItems[Index] := AValue; 35 35 end; 36 36 37 procedure TGList.SetCount(const AValue: T IndexType);37 procedure TGList.SetCount(const AValue: TListIndex); 38 38 begin 39 39 SetLength(FItems, AValue); … … 41 41 end; 42 42 43 procedure TGList.QuickSort(L, R: T IndexType; Compare: TGListSortCompare);44 var 45 I, J: T IndexType;46 P, Q: T ItemType;43 procedure TGList.QuickSort(L, R: TListIndex; Compare: TGListSortCompare); 44 var 45 I, J: TListIndex; 46 P, Q: TListItem; 47 47 begin 48 48 repeat … … 84 84 procedure TGList.Expand; 85 85 var 86 IncSize: T IndexType;86 IncSize: TListIndex; 87 87 begin 88 88 if FCount = Capacity then begin … … 95 95 end; 96 96 97 function TGList.Extract(Item: T ItemType): TItemType;98 var 99 I: T IndexType;97 function TGList.Extract(Item: TListItem): TListItem; 98 var 99 I: TListIndex; 100 100 begin 101 101 I := IndexOf(Item); … … 107 107 end; 108 108 109 function TGList.ExtractList(Item: T ItemType; Count: TIndexType): TItemType;109 function TGList.ExtractList(Item: TListItem; Count: TListIndex): TListItem; 110 110 begin 111 111 raise Exception.Create(SNotImplemented); 112 112 end; 113 113 114 function TGList.First: T ItemType;114 function TGList.First: TListItem; 115 115 begin 116 116 if FCount = 0 then … … 120 120 end; 121 121 122 function TGList.IndexOf(Item: T ItemType): TIndexType;122 function TGList.IndexOf(Item: TListItem): TListIndex; 123 123 begin 124 124 Result := 0; … … 128 128 end; 129 129 130 procedure TGList.Insert(Index: T IndexType; Item: TItemType);130 procedure TGList.Insert(Index: TListIndex; Item: TListItem); 131 131 begin 132 132 if (Index < 0) or (Index > FCount ) then … … 134 134 if FCount = Capacity then Expand; 135 135 if Index < FCount then 136 SystemMove(FItems[Index], FItems[Index + 1], (FCount - Index) * SizeOf(T ItemType));136 SystemMove(FItems[Index], FItems[Index + 1], (FCount - Index) * SizeOf(TListItem)); 137 137 FItems[Index] := Item; 138 138 FCount := FCount + 1; 139 139 end; 140 140 141 procedure TGList.InsertList(Index: T IndexType; List: TGList);142 var 143 I: T IndexType;141 procedure TGList.InsertList(Index: TListIndex; List: TGList); 142 var 143 I: TListIndex; 144 144 begin 145 145 I := 0; … … 150 150 end; 151 151 152 function TGList.Last: T ItemType;152 function TGList.Last: TListItem; 153 153 begin 154 154 if FCount = 0 then … … 158 158 end; 159 159 160 procedure TGList.Move(CurIndex, NewIndex: T IndexType);161 var 162 Temp: T ItemType;160 procedure TGList.Move(CurIndex, NewIndex: TListIndex); 161 var 162 Temp: TListItem; 163 163 begin 164 164 if ((CurIndex < 0) or (CurIndex > Count - 1)) then … … 171 171 end; 172 172 173 procedure TGList.MoveItems(CurIndex, NewIndex, Count: T IndexType);173 procedure TGList.MoveItems(CurIndex, NewIndex, Count: TListIndex); 174 174 begin 175 175 raise Exception.Create(SNotImplemented); 176 176 end; 177 177 178 function TGList.Remove(Item: TItemType): TIndexType; 178 procedure TGList.Swap(Index1, Index2: TListIndex); 179 var 180 Temp: TListItem; 181 begin 182 Temp := Items[Index1]; 183 Items[Index1] := Items[Index2]; 184 Items[Index2] := Temp; 185 end; 186 187 function TGList.Remove(Item: TListItem): TListIndex; 179 188 begin 180 189 Result := IndexOf(Item); … … 183 192 end; 184 193 185 function TGList.Equals(Obj: TObject): Boolean;186 var 187 I: T IndexType;194 (*function TGList.Equals(Obj: TObject): Boolean; 195 var 196 I: TListIndex; 188 197 begin 189 198 Result := Count = (Obj as TGList).Count; … … 198 207 end; 199 208 end; 200 end; 209 end;*) 201 210 202 211 procedure TGList.Reverse; 203 begin 204 raise Exception.Create(SNotImplemented); 212 var 213 I: TListIndex; 214 begin 215 I := 0; 216 while I < (Count div 2) do begin 217 Swap(I, Count - 1 - I); 218 I := I + 1; 219 end; 205 220 end; 206 221 … … 211 226 end; 212 227 213 procedure TGList.SetArray(Values: array of T ItemType);214 var 215 I: T IndexType;228 procedure TGList.SetArray(Values: array of TListItem); 229 var 230 I: TListIndex; 216 231 begin 217 232 Clear; … … 223 238 end; 224 239 225 function TGList.Implode(Separator: string ): string;226 var 227 I: T IndexType;240 function TGList.Implode(Separator: string; Converter: TGListStringConverter): string; 241 var 242 I: TListIndex; 228 243 begin 229 244 Result := ''; 230 245 I := 0; 231 246 while I < Count do begin 232 Result := Result + string(Items[I]);247 Result := Result + Converter(Items[I]); 233 248 if I < (Count - 1) then 234 249 Result := Result + Separator; … … 237 252 end; 238 253 239 function TGList.Add(Item: T ItemType): TIndexType;254 function TGList.Add(Item: TListItem): TListIndex; 240 255 begin 241 256 if FCount = Capacity then … … 248 263 procedure TGList.AddList(List: TGList); 249 264 var 250 I: T IndexType;265 I: TListIndex; 251 266 begin 252 267 I := 0; … … 263 278 end; 264 279 265 procedure TGList.Delete(Index: T IndexType);280 procedure TGList.Delete(Index: TListIndex); 266 281 begin 267 282 if (Index < 0) or (Index >= FCount) then 268 283 raise EListError.CreateFmt(SListIndexError, [Index]); 269 284 FCount := FCount - 1; 270 SystemMove(FItems[Index + 1], FItems[Index], (FCount - Index) * SizeOf(T ItemType));285 SystemMove(FItems[Index + 1], FItems[Index], (FCount - Index) * SizeOf(TListItem)); 271 286 // Shrink the list if appropriate 272 287 if (Capacity > 256) and (FCount < Capacity shr 2) then … … 276 291 end; 277 292 278 procedure TGList.DeleteItems(Index, Count: T IndexType);279 var 280 I: T IndexType;293 procedure TGList.DeleteItems(Index, Count: TListIndex); 294 var 295 I: TListIndex; 281 296 begin 282 297 I := 0; … … 287 302 end; 288 303 289 procedure TGList.Exchange(Index1, Index2: T IndexType);290 var 291 Temp: T ItemType;304 procedure TGList.Exchange(Index1, Index2: TListIndex); 305 var 306 Temp: TListItem; 292 307 begin 293 308 if ((Index1 >= FCount) or (Index1 < 0)) then
Note:
See TracChangeset
for help on using the changeset viewer.