Ignore:
Timestamp:
Oct 31, 2010, 3:14:23 PM (14 years ago)
Author:
george
Message:
  • Modified: Thanks to compiler directives and simple macros specialized types was moved to one unit per base generic class. Directive $DEFINE is used to specify generic class template parameters which will be included to code. This approach enable to define more specialized types per unit.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Generics/TemplateGenerics/Generic/GenericList.inc

    r83 r84  
    11{$IFDEF INTERFACE}
    22
    3   PGListItem = ^TListItem;
    43  TGList = class;
    54
    6   TGListSortCompare = function(const Item1, Item2: TListItem): Integer of object;
    7   TGListStringConverter = function(Item: TListItem): string;
    8   TGListOperation = procedure(List: TGList; Item: PGListItem);
     5  TGListSortCompare = function(const Item1, Item2: TGListItem): Integer of object;
     6  TGListStringConverter = function(Item: TGListItem): string;
    97  //TGListNotification = (lnAdded, lnExtracted, lnDeleted);
    108
    11   // TGList<TListIndex, TListItem> = class
     9  // TGList<TGListIndex, TGListItem> = class
    1210  TGList = class
    1311  private
    14     FItems: array of TListItem;
    15     FCount: TListIndex;
    16     function Get(Index: TListIndex): TListItem;
    17     function GetCapacity: TListIndex;
    18     procedure SetCapacity(const AValue: TListIndex);
    19     procedure Put(Index: TListIndex; const AValue: TListItem); virtual;
    20     procedure SetCount(const AValue: TListIndex);
    21     procedure QuickSort(L, R : TListIndex; Compare: TGListSortCompare);
     12    FItems: array of TGListItem;
     13    FCount: TGListIndex;
     14    function Get(Index: TGListIndex): TGListItem;
     15    function GetCapacity: TGListIndex;
     16    procedure SetCapacity(const AValue: TGListIndex);
     17    procedure Put(Index: TGListIndex; const AValue: TGListItem); virtual;
     18    procedure SetCount(const AValue: TGListIndex);
     19    procedure QuickSort(L, R : TGListIndex; Compare: TGListSortCompare);
    2220  public
    2321    // All items
     
    2826    procedure Sort(Compare: TGListSortCompare);
    2927    function Implode(Separator: string; Converter: TGListStringConverter): string;
    30     procedure Perform(Operation: TGListOperation);
    3128    // Many items
    32     procedure MoveItems(CurIndex, NewIndex, Count: TListIndex);
    33     procedure DeleteItems(Index, Count: TListIndex);
    34     procedure Fill(Start, Count: TListIndex; Value: TListItem);
     29    procedure MoveItems(CurIndex, NewIndex, Count: TGListIndex);
     30    procedure DeleteItems(Index, Count: TGListIndex);
     31    procedure Fill(Start, Count: TGListIndex; Value: TGListItem);
    3532    // One item
    36     function Add(Item: TListItem): TListIndex;
    37     procedure Delete(Index: TListIndex); virtual;
    38     function Extract(Item: TListItem): TListItem;
    39     procedure Exchange(Index1, Index2: TListIndex);
    40     function First: TListItem;
    41     function IndexOf(Item: TListItem; Start: TListIndex = 0): TListIndex;
    42     procedure Insert(Index: TListIndex; Item: TListItem);
    43     function Last: TListItem;
    44     procedure Move(CurIndex, NewIndex: TListIndex);
    45     function Remove(Item: TListItem): TListIndex;
    46     property Items[Index: TListIndex]: TListItem read Get write Put; default;
     33    function Add(Item: TGListItem): TGListIndex;
     34    procedure Delete(Index: TGListIndex); virtual;
     35    function Extract(Item: TGListItem): TGListItem;
     36    procedure Exchange(Index1, Index2: TGListIndex);
     37    function First: TGListItem;
     38    function IndexOf(Item: TGListItem; Start: TGListIndex = 0): TGListIndex;
     39    procedure Insert(Index: TGListIndex; Item: TGListItem);
     40    function Last: TGListItem;
     41    procedure Move(CurIndex, NewIndex: TGListIndex);
     42    function Remove(Item: TGListItem): TGListIndex;
     43    property Items[Index: TGListIndex]: TGListItem read Get write Put; default;
    4744    // List
    4845    procedure AddList(List: TGList);
    4946    procedure Assign(List: TGList);
    5047    function Equals(List: TGList): Boolean;
    51     procedure InsertList(Index: TListIndex; List: TGList);
    52     function IndexOfList(List: TGList; Start: TListIndex = 0): TListIndex;
     48    procedure InsertList(Index: TGListIndex; List: TGList);
     49    function IndexOfList(List: TGList; Start: TGListIndex = 0): TGListIndex;
    5350    // Other
    54     property Count: TListIndex read FCount write SetCount;
    55     property Capacity: TListIndex read GetCapacity write SetCapacity;
     51    property Count: TGListIndex read FCount write SetCount;
     52    property Capacity: TGListIndex read GetCapacity write SetCapacity;
    5653    // Array
    57     procedure AddArray(Values: array of TListItem);
    58     procedure SetArray(Values: array of TListItem);
    59     procedure InsertArray(Index: TListIndex; Values: array of TListItem);
     54    procedure AddArray(Values: array of TGListItem);
     55    procedure SetArray(Values: array of TGListItem);
     56    procedure InsertArray(Index: TGListIndex; Values: array of TGListItem);
    6057  end;
    6158 
     
    6360{$ENDIF}
    6461
    65 {$IFDEF IMPLEMENTATION}
     62{$IFDEF IMPLEMENTATION_USES}
    6663
    6764uses
    6865  RtlConsts;
    6966
     67{$UNDEF IMPLEMENTATION_USES}
     68{$ENDIF}
     69
     70{$IFDEF IMPLEMENTATION}
     71
    7072{ TGList }
    7173
    72 function TGList.GetCapacity: TListIndex;
     74function TGList.GetCapacity: TGListIndex;
    7375begin
    7476  Result := Length(FItems);
    7577end;
    7678
    77 procedure TGList.SetCapacity(const AValue: TListIndex);
     79procedure TGList.SetCapacity(const AValue: TGListIndex);
    7880begin
    7981  SetLength(FItems, AValue);
    8082end;
    8183
    82 function TGList.Get(Index: TListIndex): TListItem;
     84function TGList.Get(Index: TGListIndex): TGListItem;
    8385begin
    8486  Result := FItems[Index];
    8587end;
    8688
    87 procedure TGList.Put(Index: TListIndex; const AValue: TListItem);
     89procedure TGList.Put(Index: TGListIndex; const AValue: TGListItem);
    8890begin
    8991  FItems[Index] := AValue;
    9092end;
    9193
    92 procedure TGList.SetCount(const AValue: TListIndex);
     94procedure TGList.SetCount(const AValue: TGListIndex);
    9395begin
    9496  SetLength(FItems, AValue);
     
    9698end;
    9799
    98 procedure TGList.QuickSort(L, R: TListIndex; Compare: TGListSortCompare);
    99 var
    100   I, J: TListIndex;
    101   P, Q: TListItem;
     100procedure TGList.QuickSort(L, R: TGListIndex; Compare: TGListSortCompare);
     101var
     102  I, J: TGListIndex;
     103  P, Q: TGListItem;
    102104begin
    103105 repeat
     
    139141procedure TGList.Expand;
    140142var
    141   IncSize: TListIndex;
     143  IncSize: TGListIndex;
    142144begin
    143145  if FCount = Capacity then begin
     
    158160end;
    159161
    160 function TGList.Extract(Item: TListItem): TListItem;
    161 var
    162   I: TListIndex;
     162function TGList.Extract(Item: TGListItem): TGListItem;
     163var
     164  I: TGListIndex;
    163165begin
    164166  I := IndexOf(Item);
     
    170172end;
    171173
    172 function TGList.First: TListItem;
     174function TGList.First: TGListItem;
    173175begin
    174176  if FCount = 0 then
     
    178180end;
    179181
    180 function TGList.IndexOf(Item: TListItem; Start: TListIndex): TListIndex;
     182function TGList.IndexOf(Item: TGListItem; Start: TGListIndex): TGListIndex;
    181183begin
    182184  Result := Start;
    183185  while (Result < FCount) and
    184   not CompareMem(Addr(FItems[Result]), Addr(Item), SizeOf(TListItem)) do
     186  not CompareMem(Addr(FItems[Result]), Addr(Item), SizeOf(TGListItem)) do
    185187    Result := Result + 1;
    186188  if Result = FCount then Result := -1;
    187189end;
    188190
    189 procedure TGList.Insert(Index: TListIndex; Item: TListItem);
     191procedure TGList.Insert(Index: TGListIndex; Item: TGListItem);
    190192begin
    191193  if (Index < 0) or (Index > FCount ) then
     
    193195  if FCount = Capacity then Expand;
    194196  if Index < FCount then
    195     System.Move(FItems[Index], FItems[Index + 1], (FCount - Index) * SizeOf(TListItem));
     197    System.Move(FItems[Index], FItems[Index + 1], (FCount - Index) * SizeOf(TGListItem));
    196198  FItems[Index] := Item;
    197199  FCount := FCount + 1;
    198200end;
    199201
    200 procedure TGList.InsertList(Index: TListIndex; List: TGList);
    201 var
    202   I: TListIndex;
     202procedure TGList.InsertList(Index: TGListIndex; List: TGList);
     203var
     204  I: TGListIndex;
    203205begin
    204206  I := 0;
     
    209211end;
    210212
    211 function TGList.IndexOfList(List: TGList; Start: TListIndex): TListIndex;
    212 var
    213   I: TListIndex;
     213function TGList.IndexOfList(List: TGList; Start: TGListIndex): TGListIndex;
     214var
     215  I: TGListIndex;
    214216begin
    215217  if List.Count > 0 then begin
     
    218220      I := 1;
    219221      while I < List.Count do begin
    220         if not CompareMem(Addr(FItems[Result + I]), Addr(List.FItems[I]), SizeOf(TListItem)) then begin
     222        if not CompareMem(Addr(FItems[Result + I]), Addr(List.FItems[I]), SizeOf(TGListItem)) then begin
    221223          Result := -1;
    222224          Break;
     
    228230end;
    229231
    230 function TGList.Last: TListItem;
     232function TGList.Last: TGListItem;
    231233begin
    232234  if FCount = 0 then
     
    236238end;
    237239
    238 procedure TGList.Move(CurIndex, NewIndex: TListIndex);
    239 var
    240   Temp: TListItem;
     240procedure TGList.Move(CurIndex, NewIndex: TGListIndex);
     241var
     242  Temp: TGListItem;
    241243begin
    242244  if ((CurIndex < 0) or (CurIndex > Count - 1)) then
     
    246248  Temp := FItems[CurIndex];
    247249  if NewIndex > CurIndex then begin
    248     System.Move(FItems[CurIndex + 1], FItems[CurIndex], (NewIndex - CurIndex) * SizeOf(TListItem));
     250    System.Move(FItems[CurIndex + 1], FItems[CurIndex], (NewIndex - CurIndex) * SizeOf(TGListItem));
    249251  end else
    250252  if NewIndex < CurIndex then begin
    251     System.Move(FItems[NewIndex], FItems[NewIndex + 1], (CurIndex - NewIndex) * SizeOf(TListItem));
     253    System.Move(FItems[NewIndex], FItems[NewIndex + 1], (CurIndex - NewIndex) * SizeOf(TGListItem));
    252254  end;
    253255  FItems[NewIndex] := Temp;
     
    256258end;
    257259
    258 procedure TGList.MoveItems(CurIndex, NewIndex, Count: TListIndex);
     260procedure TGList.MoveItems(CurIndex, NewIndex, Count: TGListIndex);
    259261var
    260262  S: Integer;
     
    281283end;
    282284
    283 function TGList.Remove(Item: TListItem): TListIndex;
     285function TGList.Remove(Item: TGListItem): TGListIndex;
    284286begin
    285287  Result := IndexOf(Item);
     
    290292function TGList.Equals(List: TGList): Boolean;
    291293var
    292   I: TListIndex;
     294  I: TGListIndex;
    293295begin
    294296  Result := Count = List.Count;
     
    296298    I := 0;
    297299    while I < Count do begin
    298       if not CompareMem(Addr(FItems[I]), Addr(List.FItems[I]), SizeOf(TListItem)) then begin
     300      if not CompareMem(Addr(FItems[I]), Addr(List.FItems[I]), SizeOf(TGListItem)) then begin
    299301        Result := False;
    300302        Break;
     
    307309procedure TGList.Reverse;
    308310var
    309   I: TListIndex;
     311  I: TGListIndex;
    310312begin
    311313  I := 0;
     
    322324end;
    323325
    324 procedure TGList.AddArray(Values: array of TListItem);
    325 var
    326   I: TListIndex;
     326procedure TGList.AddArray(Values: array of TGListItem);
     327var
     328  I: TGListIndex;
    327329begin
    328330  I := 0;
     
    333335end;
    334336
    335 procedure TGList.SetArray(Values: array of TListItem);
    336 var
    337   I: TListIndex;
     337procedure TGList.SetArray(Values: array of TGListItem);
     338var
     339  I: TGListIndex;
    338340begin
    339341  Clear;
     
    345347end;
    346348
    347 procedure TGList.InsertArray(Index: TListIndex; Values: array of TListItem);
    348 var
    349   I: TListIndex;
     349procedure TGList.InsertArray(Index: TGListIndex; Values: array of TGListItem);
     350var
     351  I: TGListIndex;
    350352begin
    351353  I := 0;
     
    358360function TGList.Implode(Separator: string; Converter: TGListStringConverter): string;
    359361var
    360   I: TListIndex;
     362  I: TGListIndex;
    361363begin
    362364  Result := '';
     
    370372end;
    371373
    372 procedure TGList.Perform(Operation: TGListOperation);
    373 var
    374   I: TListIndex;
    375 begin
    376   I := 0;
    377   while I < Count do begin
    378     Operation(Self, @FItems[I]);
    379     I := I + 1;
    380   end;
    381 end;
    382 
    383 function TGList.Add(Item: TListItem): TListIndex;
     374function TGList.Add(Item: TGListItem): TGListIndex;
    384375begin
    385376  if FCount = Capacity then
     
    392383procedure TGList.AddList(List: TGList);
    393384var
    394   I: TListIndex;
     385  I: TGListIndex;
    395386begin
    396387  I := 0;
     
    407398end;
    408399
    409 procedure TGList.Delete(Index: TListIndex);
     400procedure TGList.Delete(Index: TGListIndex);
    410401begin
    411402  if (Index < 0) or (Index >= FCount) then
    412403    raise EListError.CreateFmt(SListIndexError, [Index]);
    413404  FCount := FCount - 1;
    414   System.Move(FItems[Index + 1], FItems[Index], (FCount - Index) * SizeOf(TListItem));
     405  System.Move(FItems[Index + 1], FItems[Index], (FCount - Index) * SizeOf(TGListItem));
    415406  Contract;
    416407end;
    417408
    418 procedure TGList.DeleteItems(Index, Count: TListIndex);
    419 var
    420   I: TListIndex;
     409procedure TGList.DeleteItems(Index, Count: TGListIndex);
     410var
     411  I: TGListIndex;
    421412begin
    422413  I := Index;
     
    427418end;
    428419
    429 procedure TGList.Fill(Start, Count: TListIndex; Value: TListItem);
     420procedure TGList.Fill(Start, Count: TGListIndex; Value: TGListItem);
    430421begin
    431422  while Count > 0 do begin
     
    436427end;
    437428
    438 procedure TGList.Exchange(Index1, Index2: TListIndex);
    439 var
    440   Temp: TListItem;
     429procedure TGList.Exchange(Index1, Index2: TGListIndex);
     430var
     431  Temp: TGListItem;
    441432begin
    442433  if ((Index1 >= FCount) or (Index1 < 0)) then
Note: See TracChangeset for help on using the changeset viewer.