Changeset 321 for Generics/NativeGenerics/Units/GenericList.pas
- Timestamp:
- Feb 7, 2012, 3:35:32 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Generics/NativeGenerics/Units/GenericList.pas
r320 r321 9 9 10 10 type 11 12 { TGList } 13 11 14 TGList<TItem> = class 12 p rivate15 public 13 16 type 14 17 TIndex = NativeInt; … … 17 20 TFromStringConverter = function(Text: string): TItem; 18 21 TItemArray = array of TItem; 19 var 22 private 23 FCount: TIndex; 20 24 FItems: array of TItem; 21 FCount: TIndex;22 25 function Get(Index: TIndex): TItem; 23 26 function GetCapacity: TIndex; 27 function GetFirst: TItem; 24 28 function GetLast: TItem; 25 function GetFirst: TItem;26 29 procedure SetCapacity(const AValue: TIndex); 27 30 procedure SetCapacityOptimized(const NewCapacity: TIndex); 31 procedure SetCount(const AValue: TIndex); 32 procedure SetFirst(AValue: TItem); 28 33 procedure SetLast(AValue: TItem); 29 procedure SetFirst(AValue: TItem);30 34 procedure Put(Index: TIndex; const AValue: TItem); virtual; 31 procedure SetCount(const AValue: TIndex); virtual;32 35 procedure QuickSort(L, R : TIndex; Compare: TSortCompare); 33 36 public … … 52 55 procedure InsertList(Index: TIndex; List: TGList<TItem>); 53 56 procedure InsertArray(Index: TIndex; Values: array of TItem); 57 procedure InsertCount(Index: TIndex; ACount: TIndex); 54 58 procedure Move(CurIndex, NewIndex: TIndex); 55 59 procedure MoveItems(CurIndex, NewIndex, Count: TIndex); 56 60 function Remove(Item: TItem): TIndex; 57 61 procedure Reverse; 62 procedure ReplaceArray(Index: TIndex; Values: array of TItem); 63 procedure ReplaceList(Index: TIndex; Source: TGList<TItem>); 64 procedure ReplaceListPart(Index: TIndex; Source: TGList<TItem>; 65 SourceIndex, SourceCount: TIndex); 58 66 procedure Sort(Compare: TSortCompare); 59 67 procedure SetArray(Values: array of TItem); … … 84 92 constructor Create; 85 93 destructor Destroy; override; 86 87 94 end; 88 95 … … 230 237 procedure TGList<TItem>.Insert(Index: TIndex; Item: TItem); 231 238 begin 232 if (Index < 0) or (Index > FCount 239 if (Index < 0) or (Index > FCount) then 233 240 raise EListError.CreateFmt(SListIndexError, [Index]); 234 if FCount = Capacity then SetCapacityOptimized(Capacity + 1); 235 if Index < FCount then 236 System.Move(FItems[Index], FItems[Index + 1], (FCount - Index) * SizeOf(TItem)); 241 InsertCount(Index, 1); 237 242 FItems[Index] := Item; 238 FCount := FCount + 1;239 243 end; 240 244 241 245 procedure TGList<TItem>.InsertList(Index: TIndex; List: TGList<TItem>); 242 var 243 I: TIndex; 244 begin 245 I := 0; 246 while (I < List.Count) do begin 247 Insert(Index + I, List[I]); 248 I := I + 1; 249 end; 246 begin 247 if (Index < 0) or (Index > FCount) then 248 raise EListError.CreateFmt(SListIndexError, [Index]); 249 InsertCount(Index, List.Count); 250 ReplaceList(Index, List); 250 251 end; 251 252 … … 381 382 end; 382 383 384 procedure TGList<TItem>.ReplaceArray(Index: TIndex; 385 Values: array of TItem); 386 var 387 I: TIndex; 388 begin 389 I := 0; 390 while I < Length(Values) do begin 391 Items[Index + I] := Values[I]; 392 I := I + 1; 393 end; 394 end; 395 396 procedure TGList<TItem>.ReplaceList(Index: TIndex; Source: TGList<TItem>); 397 var 398 I: TIndex; 399 begin 400 I := 0; 401 while I < Source.Count do begin 402 Items[Index + I] := Source[I]; 403 I := I + 1; 404 end; 405 end; 406 407 procedure TGList<TItem>.ReplaceListPart(Index: TIndex; Source: TGList<TItem>; 408 SourceIndex, SourceCount: TIndex); 409 var 410 I: TIndex; 411 begin 412 I := 0; 413 while I < SourceCount do begin 414 Items[Index + I] := Source[SourceIndex + I]; 415 I := I + 1; 416 end; 417 end; 418 383 419 procedure TGList<TItem>.Sort(Compare: TSortCompare); 384 420 begin … … 411 447 412 448 procedure TGList<TItem>.InsertArray(Index: TIndex; Values: array of TItem); 413 var 414 I: TIndex; 415 begin 416 I := 0; 417 while I <= High(Values) do begin 418 Insert(Index + I, Values[I]); 419 I := I + 1; 420 end; 449 begin 450 if (Index < 0) or (Index > FCount) then 451 raise EListError.CreateFmt(SListIndexError, [Index]); 452 InsertCount(Index, Length(Values)); 453 ReplaceArray(Index, Values); 454 end; 455 456 procedure TGList<TItem>.InsertCount(Index: TIndex; ACount: TIndex); 457 begin 458 if (Index < 0) or (Index > FCount) then 459 raise EListError.CreateFmt(SListIndexError, [Index]); 460 Count := Count + ACount; 461 if Index < FCount then 462 System.Move(FItems[Index], FItems[Index + ACount], (FCount - ACount - Index) * SizeOf(TItem)); 421 463 end; 422 464
Note:
See TracChangeset
for help on using the changeset viewer.