Changeset 423 for Generics/NativeGenerics/Units/GenericList.pas
- Timestamp:
- Sep 18, 2012, 8:17:09 AM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Generics/NativeGenerics/Units/GenericList.pas
r379 r423 20 20 TFromStringConverter = function(Text: string): TItem; 21 21 TItemArray = array of TItem; 22 pr ivate23 function GetLast: TItem; virtual; abstract;24 procedure SetLast( AValue: TItem); virtual; abstract;25 function GetFirst: TItem; virtual; abstract;26 procedure SetFirst( AValue: TItem); virtual; abstract;22 protected 23 function GetLast: TItem; virtual; 24 procedure SetLast(const AValue: TItem); virtual; 25 function GetFirst: TItem; virtual; 26 procedure SetFirst(const AValue: TItem); virtual; 27 27 function GetCount: TIndex; virtual; abstract; 28 28 procedure SetCount(const AValue: TIndex); virtual; abstract; … … 33 33 public 34 34 constructor Create; virtual; 35 function Add(Item: TItem): TIndex; virtual; abstract; 35 procedure Clear; virtual; 36 function Add(const Item: TItem): TIndex; virtual; 36 37 property Count: TIndex read GetCount write SetCount; 37 38 property Capacity: TIndex read GetCapacity write SetCapacity; … … 47 48 FCount: TIndex; 48 49 FItems: array of TItem; 50 protected 49 51 function Get(Index: TIndex): TItem; override; 50 52 function GetCapacity: TIndex; override; 51 function GetFirst: TItem; override;52 function GetLast: TItem; override;53 53 function GetCount: TIndex; override; 54 54 procedure SetCapacity(const AValue: TIndex); override; 55 55 procedure SetCapacityOptimized(const NewCapacity: TIndex); 56 56 procedure SetCount(const AValue: TIndex); override; 57 procedure SetFirst(AValue: TItem); override;58 procedure SetLast(AValue: TItem); override;59 57 procedure Put(Index: TIndex; const AValue: TItem); override; 60 58 procedure QuickSort(L, R : TIndex; Compare: TSortCompare); 61 59 public 62 function Add( Item: TItem): TIndex; override;60 function Add(const Item: TItem): TIndex; override; 63 61 procedure AddArray(Values: array of TItem); 64 62 procedure AddList(List: TGList<TItem>); 65 63 procedure AddListPart(List: TGList<TItem>; ItemIndex, ItemCount: TIndex); 66 64 procedure Assign(Source: TGList<TItem>); virtual; 67 procedure Clear; virtual;68 65 procedure Delete(Index: TIndex); virtual; 69 66 procedure DeleteItems(Index, Count: TIndex); … … 95 92 96 93 TGObjectList<TItem> = class(TGList<TItem>) 97 pr ivate94 protected 98 95 procedure Put(Index: Integer; const AValue: TItem); override; 99 96 public … … 112 109 procedure Clear; override; 113 110 procedure Assign(Source: TGList<TItem>); override; 111 constructor Create; override; 112 destructor Destroy; override; 113 end; 114 115 { TGFileList } 116 117 TGFileList<TItem> = class(TGList<TItem>) 118 private 119 FHandle: THandle; 120 protected 121 function GetCount: TIndex; override; 122 procedure SetCount(const AValue: TIndex); override; 123 procedure SetCapacity(const AValue: TIndex); override; 124 function GetCapacity: TIndex; override; 125 function Get(Index: TIndex): TItem; override; 126 procedure Put(Index: TIndex; const AValue: TItem); override; 127 public 128 procedure Open(FileName: string; Mode: Integer); 129 procedure Close; 114 130 constructor Create; 115 131 destructor Destroy; override; … … 117 133 118 134 135 resourcestring 136 SListIndexError = 'List index (%d) out of bounds'; 137 SListCapacityError = 'List capacity (%d) exceeded.'; 138 SListCountError = 'List count (%d) out of bounds.'; 139 119 140 implementation 120 121 uses122 RtlConsts;123 141 124 142 { TGList<TItem> } … … 298 316 end; 299 317 300 function TGList<TItem>.GetLast: TItem;301 begin302 if FCount = 0 then303 raise EListError.CreateFmt(SListIndexError, [0])304 else305 Result := FItems[FCount - 1];306 end;307 308 318 function TGList<TItem>.GetCount: TIndex; 309 319 begin 310 320 Result := FCount; 311 end;312 313 procedure TGList<TItem>.SetLast(AValue: TItem);314 begin315 if FCount = 0 then316 raise EListError.CreateFmt(SListIndexError, [0])317 else318 FItems[FCount - 1] := AValue;319 end;320 321 function TGList<TItem>.GetFirst: TItem;322 begin323 if FCount = 0 then324 raise EListError.CreateFmt(SListIndexError, [0])325 else326 Result := FItems[0];327 end;328 329 procedure TGList<TItem>.SetFirst(AValue: TItem);330 begin331 if FCount = 0 then332 raise EListError.CreateFmt(SListIndexError, [0])333 else334 FItems[0] := AValue;335 321 end; 336 322 … … 521 507 end; 522 508 523 function TGList<TItem>.Add( Item: TItem): TIndex;509 function TGList<TItem>.Add(const Item: TItem): TIndex; 524 510 begin 525 511 Count := Count + 1; 526 512 Result := FCount - 1; 527 FItems[Result] := Item;513 Items[Result] := Item; 528 514 end; 529 515 … … 552 538 J := J + 1; 553 539 end; 554 end;555 556 procedure TGList<TItem>.Clear;557 begin558 Count := 0;559 Capacity := 0;560 540 end; 561 541 … … 690 670 { TGAbstractList<TItem> } 691 671 672 function TGAbstractList<TItem>.GetLast: TItem; 673 begin 674 if Count = 0 then 675 raise EListError.CreateFmt(SListIndexError, [0]) 676 else 677 Result := Items[Count - 1]; 678 end; 679 680 procedure TGAbstractList<TItem>.SetLast(const AValue: TItem); 681 begin 682 if Count = 0 then 683 raise EListError.CreateFmt(SListIndexError, [0]) 684 else 685 Items[Count - 1] := AValue; 686 end; 687 688 function TGAbstractList<TItem>.GetFirst: TItem; 689 begin 690 if Count = 0 then 691 raise EListError.CreateFmt(SListIndexError, [0]) 692 else 693 Result := Items[0]; 694 end; 695 696 procedure TGAbstractList<TItem>.SetFirst(const AValue: TItem); 697 begin 698 if Count = 0 then 699 raise EListError.CreateFmt(SListIndexError, [0]) 700 else 701 Items[0] := AValue; 702 end; 703 692 704 constructor TGAbstractList<TItem>.Create; 693 705 begin 694 706 end; 695 707 708 procedure TGAbstractList<TItem>.Clear; 709 begin 710 Count := 0; 711 Capacity := 0; 712 end; 713 714 function TGAbstractList<TItem>.Add(const Item: TItem): TIndex; 715 begin 716 Count := Count + 1; 717 Result := Count - 1; 718 Items[Result] := Item; 719 end; 720 721 { TGFileList<TItem> } 722 723 function TGFileList<TItem>.GetCount: TIndex; 724 var 725 OldPos: TIndex; 726 begin 727 OldPos := FileSeek(FHandle, 0, 1); 728 Result := FileSeek(FHandle, 0, 2); 729 FileSeek(FHandle, OldPos, 0); 730 end; 731 732 procedure TGFileList<TItem>.SetCount(const AValue: TIndex); 733 begin 734 FileTruncate(FHandle, AValue); 735 end; 736 737 procedure TGFileList<TItem>.SetCapacity(const AValue: TIndex); 738 begin 739 inherited SetCapacity(AValue); 740 end; 741 742 function TGFileList<TItem>.GetCapacity: TIndex; 743 begin 744 Result := inherited GetCapacity; 745 end; 746 747 function TGFileList<TItem>.Get(Index: TIndex): TItem; 748 begin 749 FileSeek(FHandle, Index, 0); 750 FileRead(FHandle, Result, SizeOf(Result)); 751 end; 752 753 procedure TGFileList<TItem>.Put(Index: TIndex; const AValue: TItem); 754 begin 755 FileSeek(FHandle, Index, 0); 756 FileWrite(FHandle, AValue, SizeOf(AValue)); 757 end; 758 759 procedure TGFileList<TItem>.Open(FileName: string; Mode: Integer); 760 begin 761 Close; 762 if Mode = fmCreate then FHandle := FileCreate(FileName, Mode) 763 else FileOpen(FileName, Mode); 764 end; 765 766 procedure TGFileList<TItem>.Close; 767 begin 768 if FHandle <> feInvalidHandle then FileClose(FHandle); 769 end; 770 771 constructor TGFileList<TItem>.Create; 772 begin 773 FHandle := feInvalidHandle; 774 end; 775 776 destructor TGFileList<TItem>.Destroy; 777 begin 778 Close; 779 inherited; 780 end; 781 696 782 end.
Note:
See TracChangeset
for help on using the changeset viewer.