Ignore:
Timestamp:
Jan 9, 2012, 2:22:31 PM (13 years ago)
Author:
chronos
Message:
  • Modified: TGStream is now implemented. TMemoryStreamByte have additional methods usable for byte streams.
  • Added: Few other missing methods in TGList.
File:
1 edited

Legend:

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

    r308 r312  
    11{$IFDEF INTERFACE}
     2
     3  // TGList implemented using templates
     4  // - item operations (Add, Insert, ReplaceArray, Get, Set, IndexOf,
     5  //   Extract, Delete, Exchange)
     6  // - item range operations (DeleteItems, InsertItems, ReplaceItems,
     7  //   Move, Fill)
     8  // - other TGList operations (AddList, InsertList,
     9  //   ReplaceList, GetList, IndexOfList)
     10  // - dynamic array operations (AddArray, InsertArray,
     11  //   ReplaceArray, GetArray, IndexOfArray)
     12  // - all items operations (Clear, Reverse, Sort)
    213
    314  TGList = class;
     
    4152    property First: TGListItem read GetFirst write SetFirst;
    4253    procedure Fill(Start, Count: TGListIndex; Value: TGListItem);
    43     function GetArray: TGListItemArray;
     54    function GetArray(Index, ACount: TGListIndex): TGListItemArray;
     55    procedure GetList(List: TGList; Index, ACount: TGListIndex);
    4456    function Implode(Separator: string; Converter: TGListToStringConverter): string;
    4557    function IndexOf(Item: TGListItem; Start: TGListIndex = 0): TGListIndex; virtual;
    4658    function IndexOfList(List: TGList; Start: TGListIndex = 0): TGListIndex;
     59    function IndexOfArray(Values: array of TGListItem; Start: TGListIndex = 0): TGListIndex;
    4760    procedure Insert(Index: TGListIndex; Item: TGListItem);
    4861    procedure InsertList(Index: TGListIndex; List: TGList);
    4962    procedure InsertArray(Index: TGListIndex; Values: array of TGListItem);
     63    procedure InsertCount(Index: TGListIndex; ACount: TGListIndex);
    5064    procedure Move(CurIndex, NewIndex: TGListIndex);
    5165    procedure MoveItems(CurIndex, NewIndex, Count: TGListIndex);
    5266    function Remove(Item: TGListItem): TGListIndex;
    5367    procedure Reverse;
     68    procedure ReplaceArray(Index: TGListIndex; Values: array of TGListItem);
    5469    procedure ReplaceList(Index: TGListIndex; Source: TGList);
    5570    procedure ReplaceListPart(Index: TGListIndex; Source: TGList;
     
    8398end;
    8499
     100procedure TGList.ReplaceArray(Index: TGListIndex; Values: array of TGListItem);
     101var
     102  I: TGListIndex;
     103begin
     104  I := 0;
     105  while I < Length(Values) do begin
     106    Items[Index + I] := Values[I];
     107    I := I + 1;
     108  end;
     109end;
     110
    85111procedure TGList.ReplaceList(Index: TGListIndex; Source: TGList);
    86112var
     
    165191end;
    166192
    167 function TGList.GetArray: TGListItemArray;
     193function TGList.GetArray(Index, ACount: TGListIndex): TGListItemArray;
    168194var
    169195  I: Integer;
    170196begin
    171   SetLength(Result, Count);
     197  SetLength(Result, ACount);
    172198  I := 0;
    173199  while I < Count do begin
    174     Result[I] := FItems[I];
    175     I := I + 1;
    176   end;
     200    Result[I] := FItems[Index + I];
     201    I := I + 1;
     202  end;
     203end;
     204
     205procedure TGList.GetList(List: TGList; Index, ACount: TGListIndex);
     206begin
     207  List.Clear;
     208  List.AddListPart(Self, Index, ACount);
    177209end;
    178210
     
    258290procedure TGList.Insert(Index: TGListIndex; Item: TGListItem);
    259291begin
    260   if (Index < 0) or (Index > FCount ) then
     292  if (Index < 0) or (Index > FCount) then
    261293    raise EListError.CreateFmt(SListIndexError, [Index]);
    262   if FCount = Capacity then SetCapacityOptimized(Capacity + 1);
     294  InsertCount(Index, 1);
     295  FItems[Index] := Item;
     296end;
     297
     298procedure TGList.InsertList(Index: TGListIndex; List: TGList);
     299begin
     300  if (Index < 0) or (Index > FCount) then
     301    raise EListError.CreateFmt(SListIndexError, [Index]);
     302  InsertCount(Index, List.Count);
     303  ReplaceList(Index, List);
     304end;
     305
     306procedure TGList.InsertArray(Index: TGListIndex; Values: array of TGListItem);
     307begin
     308  if (Index < 0) or (Index > FCount) then
     309    raise EListError.CreateFmt(SListIndexError, [Index]);
     310  InsertCount(Index, Length(Values));
     311  ReplaceArray(Index, Values);
     312end;
     313
     314procedure TGList.InsertCount(Index: TGListIndex; ACount: TGListIndex);
     315begin
     316  if (Index < 0) or (Index > FCount) then
     317    raise EListError.CreateFmt(SListIndexError, [Index]);
     318  Count := Count + ACount;
    263319  if Index < FCount then
    264     System.Move(FItems[Index], FItems[Index + 1], (FCount - Index) * SizeOf(TGListItem));
    265   FItems[Index] := Item;
    266   FCount := FCount + 1;
    267 end;
    268 
    269 procedure TGList.InsertList(Index: TGListIndex; List: TGList);
    270 var
    271   I: TGListIndex;
    272 begin
    273   I := 0;
    274   while (I < List.Count) do begin
    275     Insert(Index + I, List[I]);
    276     I := I + 1;
    277   end;
     320    System.Move(FItems[Index], FItems[Index + ACount], (FCount - ACount - Index) * SizeOf(TGListItem));
    278321end;
    279322
     
    297340end;
    298341
     342function TGList.IndexOfArray(Values: array of TGListItem; Start: TGListIndex): TGListIndex;
     343var
     344  I: TGListIndex;
     345begin
     346  if Length(Values) > 0 then begin
     347    Result := IndexOf(Values[0], Start);
     348    if Result <> -1 then begin
     349      I := 1;
     350      while I < Length(Values) do begin
     351        if not CompareMem(Addr(FItems[Result + I]), Addr(Values[I]), SizeOf(TGListItem)) then begin
     352          Result := -1;
     353          Break;
     354        end;
     355        I := I + 1;
     356      end;
     357    end;
     358  end else Result := -1;
     359end;
     360
    299361function TGList.GetLast: TGListItem;
    300362begin
     
    438500end;
    439501
    440 procedure TGList.InsertArray(Index: TGListIndex; Values: array of TGListItem);
    441 var
    442   I: TGListIndex;
    443 begin
    444   I := 0;
    445   while I <= High(Values) do begin
    446     Insert(Index + I, Values[I]);
    447     I := I + 1;
    448   end;
    449 end;
    450 
    451502function TGList.Implode(Separator: string; Converter: TGListToStringConverter): string;
    452503var
Note: See TracChangeset for help on using the changeset viewer.