Ignore:
Timestamp:
Oct 29, 2010, 1:26:32 PM (14 years ago)
Author:
george
Message:
  • Added: Generic range type.
  • Added: Specialized TListByte type which si simply memory block.
  • Added: New functionality to TGList.
Location:
Generics/TemplateGenerics/Generic
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • Generics/TemplateGenerics/Generic/ListImplementation.tpl

    r76 r77  
    109109end;
    110110
    111 function TGList.IndexOf(Item: TListItem): TListIndex;
    112 begin
    113   Result := 0;
     111function TGList.IndexOf(Item: TListItem; Start: TListIndex): TListIndex;
     112begin
     113  Result := Start;
    114114  while (Result < FCount) and
    115115  not CompareMem(Addr(FItems[Result]), Addr(Item), SizeOf(TListItem)) do
     
    138138    I := I + 1;
    139139  end;
     140end;
     141
     142function TGList.IndexOfList(List: TGList; Start: TListIndex): TListIndex;
     143var
     144  I: TListIndex;
     145begin
     146  if List.Count > 0 then begin
     147    Result := IndexOf(List[0], Start);
     148    if Result <> -1 then begin
     149      I := 1;
     150      while I < List.Count do begin
     151        if not CompareMem(Addr(FItems[Result + I]), Addr(List.FItems[I]), SizeOf(TListItem)) then begin
     152          Result := -1;
     153          Break;
     154        end;
     155        I := I + 1;
     156      end;
     157    end;
     158  end else Result := -1;
    140159end;
    141160
     
    200219end;
    201220
    202 (*function TGList.Equals(Obj: TObject): Boolean;
    203 var
    204   I: TListIndex;
    205 begin
    206   Result := Count = (Obj as TGList).Count;
     221function TGList.Equals(List: TGList): Boolean;
     222var
     223  I: TListIndex;
     224begin
     225  Result := Count = List.Count;
    207226  if Result then begin
    208227    I := 0;
    209228    while I < Count do begin
    210       if Items[I] <> (Obj as TGList)[I] then begin
     229      if not CompareMem(Addr(FItems[I]), Addr(List.FItems[I]), SizeOf(TListItem)) then begin
    211230        Result := False;
    212231        Break;
     
    215234    end;
    216235  end;
    217 end;*)
     236end;
    218237
    219238procedure TGList.Reverse;
     
    241260  while I <= High(Values) do begin
    242261    Add(Values[I]);
     262    I := I + 1;
     263  end;
     264end;
     265
     266procedure TGList.SetArray(Values: array of TListItem);
     267var
     268  I: TListIndex;
     269begin
     270  Clear;
     271  I := 0;
     272  while I <= High(Values) do begin
     273    Add(Values[I]);
     274    I := I + 1;
     275  end;
     276end;
     277
     278procedure TGList.InsertArray(Index: TListIndex; Values: array of TListItem);
     279var
     280  I: TListIndex;
     281begin
     282  I := 0;
     283  while I <= High(Values) do begin
     284    Insert(Index + I, Values[I]);
    243285    I := I + 1;
    244286  end;
     
    255297    if I < (Count - 1) then
    256298      Result := Result + Separator;
     299    I := I + 1;
     300  end;
     301end;
     302
     303procedure TGList.Perform(Operation: TGListOperation);
     304var
     305  I: TListIndex;
     306begin
     307  I := 0;
     308  while I < Count do begin
     309    Operation(Self, @FItems[I]);
    257310    I := I + 1;
    258311  end;
     
    309362end;
    310363
     364procedure TGList.Fill(Start, Count: TListIndex; Value: TListItem);
     365begin
     366  while Count > 0 do begin
     367    Items[Start] := Value;
     368    Count := Count - 1;
     369    Start := Start + 1;
     370  end;
     371end;
     372
    311373procedure TGList.Exchange(Index1, Index2: TListIndex);
    312374var
  • Generics/TemplateGenerics/Generic/ListInterface.tpl

    r74 r77  
     1
     2  PGListItem = ^TListItem;
     3  TGList = class;
    14
    25  TGListSortCompare = function(const Item1, Item2: TListItem): Integer of object;
    36  TGListStringConverter = function(Item: TListItem): string;
     7  TGListOperation = procedure(List: TGList; Item: PGListItem);
    48  //TGListNotification = (lnAdded, lnExtracted, lnDeleted);
    59
     
    1620    procedure SetCount(const AValue: TListIndex);
    1721    procedure QuickSort(L, R : TListIndex; Compare: TGListSortCompare);
    18     property Capacity: TListIndex read GetCapacity write SetCapacity;
    1922  public
    2023    // All items
     
    2427    procedure Sort(Compare: TGListSortCompare);
    2528    function Implode(Separator: string; Converter: TGListStringConverter): string;
     29    procedure Perform(Operation: TGListOperation);
    2630    // Many items
    2731    procedure MoveItems(CurIndex, NewIndex, Count: TListIndex);
    2832    procedure DeleteItems(Index, Count: TListIndex);
     33    procedure Fill(Start, Count: TListIndex; Value: TListItem);
    2934    // One item
    3035    function Add(Item: TListItem): TListIndex;
     
    3338    procedure Exchange(Index1, Index2: TListIndex);
    3439    function First: TListItem;
    35     function IndexOf(Item: TListItem): TListIndex;
     40    function IndexOf(Item: TListItem; Start: TListIndex = 0): TListIndex;
    3641    procedure Insert(Index: TListIndex; Item: TListItem);
    3742    function Last: TListItem;
     
    4247    procedure AddList(List: TGList);
    4348    procedure Assign(List: TGList);
    44     //function Equals(Obj: TObject): Boolean; override;
     49    function Equals(List: TGList): Boolean;
    4550    procedure InsertList(Index: TListIndex; List: TGList);
     51    function IndexOfList(List: TGList; Start: TListIndex = 0): TListIndex;
    4652    // Other
    4753    property Count: TListIndex read GetCount write SetCount;
    48     // Additional
     54    property Capacity: TListIndex read GetCapacity write SetCapacity;
     55    // Array
    4956    procedure AddArray(Values: array of TListItem);
     57    procedure SetArray(Values: array of TListItem);
     58    procedure InsertArray(Index: TListIndex; Values: array of TListItem);
    5059  end;
  • Generics/TemplateGenerics/Generic/SetImplementation.tpl

    r76 r77  
    33{ TGSet }
    44
     5function TGSet.IsIn(Item: TSetItem): Boolean;
     6begin
     7  Result := FList.IndexOf(Item) <> -1;
     8end;
    59
     10constructor TGSet.Create;
     11begin
     12  FList := TGList.Create;
     13end;
    614
     15destructor TGSet.Destroy;
     16begin
     17  FList.Free;
     18  inherited Destroy;
     19end;
Note: See TracChangeset for help on using the changeset viewer.