Ignore:
Timestamp:
Sep 18, 2012, 8:17:09 AM (12 years ago)
Author:
chronos
Message:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Generics/NativeGenerics/Units/GenericList.pas

    r379 r423  
    2020    TFromStringConverter = function(Text: string): TItem;
    2121    TItemArray = array of TItem;
    22   private
    23     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;
    2727    function GetCount: TIndex; virtual; abstract;
    2828    procedure SetCount(const AValue: TIndex); virtual; abstract;
     
    3333  public
    3434    constructor Create; virtual;
    35     function Add(Item: TItem): TIndex; virtual; abstract;
     35    procedure Clear; virtual;
     36    function Add(const Item: TItem): TIndex; virtual;
    3637    property Count: TIndex read GetCount write SetCount;
    3738    property Capacity: TIndex read GetCapacity write SetCapacity;
     
    4748    FCount: TIndex;
    4849    FItems: array of TItem;
     50  protected
    4951    function Get(Index: TIndex): TItem; override;
    5052    function GetCapacity: TIndex; override;
    51     function GetFirst: TItem; override;
    52     function GetLast: TItem; override;
    5353    function GetCount: TIndex; override;
    5454    procedure SetCapacity(const AValue: TIndex); override;
    5555    procedure SetCapacityOptimized(const NewCapacity: TIndex);
    5656    procedure SetCount(const AValue: TIndex); override;
    57     procedure SetFirst(AValue: TItem); override;
    58     procedure SetLast(AValue: TItem); override;
    5957    procedure Put(Index: TIndex; const AValue: TItem); override;
    6058    procedure QuickSort(L, R : TIndex; Compare: TSortCompare);
    6159  public
    62     function Add(Item: TItem): TIndex; override;
     60    function Add(const Item: TItem): TIndex; override;
    6361    procedure AddArray(Values: array of TItem);
    6462    procedure AddList(List: TGList<TItem>);
    6563    procedure AddListPart(List: TGList<TItem>; ItemIndex, ItemCount: TIndex);
    6664    procedure Assign(Source: TGList<TItem>); virtual;
    67     procedure Clear; virtual;
    6865    procedure Delete(Index: TIndex); virtual;
    6966    procedure DeleteItems(Index, Count: TIndex);
     
    9592
    9693  TGObjectList<TItem> = class(TGList<TItem>)
    97   private
     94  protected
    9895    procedure Put(Index: Integer; const AValue: TItem); override;
    9996  public
     
    112109    procedure Clear; override;
    113110    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;
    114130    constructor Create;
    115131    destructor Destroy; override;
     
    117133
    118134
     135resourcestring
     136  SListIndexError               = 'List index (%d) out of bounds';
     137  SListCapacityError            = 'List capacity (%d) exceeded.';
     138  SListCountError               = 'List count (%d) out of bounds.';
     139
    119140implementation
    120 
    121 uses
    122   RtlConsts;
    123141
    124142{ TGList<TItem> }
     
    298316end;
    299317
    300 function TGList<TItem>.GetLast: TItem;
    301 begin
    302   if FCount = 0 then
    303     raise EListError.CreateFmt(SListIndexError, [0])
    304   else
    305     Result := FItems[FCount - 1];
    306 end;
    307 
    308318function TGList<TItem>.GetCount: TIndex;
    309319begin
    310320  Result := FCount;
    311 end;
    312 
    313 procedure TGList<TItem>.SetLast(AValue: TItem);
    314 begin
    315   if FCount = 0 then
    316     raise EListError.CreateFmt(SListIndexError, [0])
    317   else
    318     FItems[FCount - 1] := AValue;
    319 end;
    320 
    321 function TGList<TItem>.GetFirst: TItem;
    322 begin
    323   if FCount = 0 then
    324     raise EListError.CreateFmt(SListIndexError, [0])
    325   else
    326     Result := FItems[0];
    327 end;
    328 
    329 procedure TGList<TItem>.SetFirst(AValue: TItem);
    330 begin
    331   if FCount = 0 then
    332     raise EListError.CreateFmt(SListIndexError, [0])
    333   else
    334     FItems[0] := AValue;
    335321end;
    336322
     
    521507end;
    522508
    523 function TGList<TItem>.Add(Item: TItem): TIndex;
     509function TGList<TItem>.Add(const Item: TItem): TIndex;
    524510begin
    525511  Count := Count + 1;
    526512  Result := FCount - 1;
    527   FItems[Result] := Item;
     513  Items[Result] := Item;
    528514end;
    529515
     
    552538    J := J + 1;
    553539  end;
    554 end;
    555 
    556 procedure TGList<TItem>.Clear;
    557 begin
    558   Count := 0;
    559   Capacity := 0;
    560540end;
    561541
     
    690670{ TGAbstractList<TItem> }
    691671
     672function TGAbstractList<TItem>.GetLast: TItem;
     673begin
     674  if Count = 0 then
     675    raise EListError.CreateFmt(SListIndexError, [0])
     676  else
     677    Result := Items[Count - 1];
     678end;
     679
     680procedure TGAbstractList<TItem>.SetLast(const AValue: TItem);
     681begin
     682  if Count = 0 then
     683    raise EListError.CreateFmt(SListIndexError, [0])
     684  else
     685    Items[Count - 1] := AValue;
     686end;
     687
     688function TGAbstractList<TItem>.GetFirst: TItem;
     689begin
     690  if Count = 0 then
     691    raise EListError.CreateFmt(SListIndexError, [0])
     692  else
     693    Result := Items[0];
     694end;
     695
     696procedure TGAbstractList<TItem>.SetFirst(const AValue: TItem);
     697begin
     698  if Count = 0 then
     699    raise EListError.CreateFmt(SListIndexError, [0])
     700  else
     701    Items[0] := AValue;
     702end;
     703
    692704constructor TGAbstractList<TItem>.Create;
    693705begin
    694706end;
    695707
     708procedure TGAbstractList<TItem>.Clear;
     709begin
     710  Count := 0;
     711  Capacity := 0;
     712end;
     713
     714function TGAbstractList<TItem>.Add(const Item: TItem): TIndex;
     715begin
     716  Count := Count + 1;
     717  Result := Count - 1;
     718  Items[Result] := Item;
     719end;
     720
     721{ TGFileList<TItem> }
     722
     723function TGFileList<TItem>.GetCount: TIndex;
     724var
     725  OldPos: TIndex;
     726begin
     727  OldPos := FileSeek(FHandle, 0, 1);
     728  Result := FileSeek(FHandle, 0, 2);
     729  FileSeek(FHandle, OldPos, 0);
     730end;
     731
     732procedure TGFileList<TItem>.SetCount(const AValue: TIndex);
     733begin
     734  FileTruncate(FHandle, AValue);
     735end;
     736
     737procedure TGFileList<TItem>.SetCapacity(const AValue: TIndex);
     738begin
     739  inherited SetCapacity(AValue);
     740end;
     741
     742function TGFileList<TItem>.GetCapacity: TIndex;
     743begin
     744  Result := inherited GetCapacity;
     745end;
     746
     747function TGFileList<TItem>.Get(Index: TIndex): TItem;
     748begin
     749  FileSeek(FHandle, Index, 0);
     750  FileRead(FHandle, Result, SizeOf(Result));
     751end;
     752
     753procedure TGFileList<TItem>.Put(Index: TIndex; const AValue: TItem);
     754begin
     755  FileSeek(FHandle, Index, 0);
     756  FileWrite(FHandle, AValue, SizeOf(AValue));
     757end;
     758
     759procedure TGFileList<TItem>.Open(FileName: string; Mode: Integer);
     760begin
     761  Close;
     762  if Mode = fmCreate then FHandle := FileCreate(FileName, Mode)
     763    else FileOpen(FileName, Mode);
     764end;
     765
     766procedure TGFileList<TItem>.Close;
     767begin
     768  if FHandle <> feInvalidHandle then FileClose(FHandle);
     769end;
     770
     771constructor TGFileList<TItem>.Create;
     772begin
     773  FHandle := feInvalidHandle;
     774end;
     775
     776destructor TGFileList<TItem>.Destroy;
     777begin
     778  Close;
     779  inherited;
     780end;
     781
    696782end.
Note: See TracChangeset for help on using the changeset viewer.