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.
Location:
Generics/TemplateGenerics/Generic
Files:
5 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
  • Generics/TemplateGenerics/Generic/GenericListObject.inc

    r257 r312  
    2222    procedure Clear; override;
    2323    procedure Assign(Source: TGList); override;
    24     constructor Create;
     24    constructor Create; override;
    2525    destructor Destroy; override;
    2626  end;
  • Generics/TemplateGenerics/Generic/GenericListString.inc

    r258 r312  
    1919    procedure Assign(Source: TGList); override;
    2020    function IndexOf(Item: TGListItem; Start: TGListIndex = 0): TGListIndex; override;
    21     constructor Create;
     21    constructor Create; override;
    2222    destructor Destroy; override;
    2323  end;
  • Generics/TemplateGenerics/Generic/GenericQueue.inc

    r112 r312  
    1111{$I 'GenericList.inc'}
    1212
    13   // TGQueue<TSetIndex, TSetItem> = class(TGList)
     13  // TGQueue<TQueueIndex, TQueueItem> = class(TGList)
    1414  TGQueue = class
    1515  private
    1616    FList: TGList;
     17    function GetCount: TGQueueIndex;
    1718  public
    1819    procedure Enqueue(Value: TGQueueItem);
     20    procedure EnqueueArray(Values: array of TGQueueItem);
     21    procedure EnqueueList(List: TGList);
    1922    function Dequeue: TGQueueItem;
    2023    function Peek: TGQueueItem;
     
    2225    destructor Destroy; override;
    2326    property List: TGList read FList;
     27    property Count: TGQueueIndex read GetCount;
    2428  end;
    2529
     
    5458end;
    5559
     60procedure TGQueue.EnqueueArray(Values: array of TGQueueItem);
     61begin
     62  FList.AddArray(Values);
     63end;
     64
     65procedure TGQueue.EnqueueList(List: TGList);
     66begin
     67  FList.AddList(List);
     68end;
     69
    5670function TGQueue.Peek: TGQueueItem;
    5771begin
     
    7589end;
    7690
     91function TGQueue.GetCount: TGQueueIndex;
     92begin
     93  Result := FList.Count;
     94end;
     95
    7796{$UNDEF IMPLEMENTATION}
    7897{$ENDIF}
  • Generics/TemplateGenerics/Generic/GenericStream.inc

    r226 r312  
    11{$IFDEF INTERFACE}
    22
    3 TGStreamDataEvent = procedure (Item: TGStreamItem) of object;
     3TGStreamItemArray = array of TGStreamItem;
    44
    5 // TGStream<TStreamItem> = class
     5// TGStream<TGStreamIndex, TGStreamItem> = class
    66TGStream = class
    7   FOnData: TGStreamDataEvent;
    8   procedure Write(Item: TStreamItem);
    9   property OnData: TGStreamDataEvent read FOnData write FOnData;
     7  procedure SetSize(AValue: TGStreamIndex);
     8  function GetSize: TGStreamIndex;
     9  procedure SetPosition(AValue: TGStreamIndex);
     10  function GetPosition: TGStreamIndex;
     11public
     12  procedure Assign(Source: TGStream); virtual;
     13  procedure Write(Item: TGStreamItem); virtual; abstract;
     14  procedure WriteArray(Item: array of TGStreamItem); virtual; abstract;
     15  function Read: TGStreamItem; virtual; abstract;
     16  function ReadArray(Count: TGStreamIndex): TGStreamItemArray; virtual; abstract;
     17  function Insert(Count: TGStreamIndex): TGStreamIndex; virtual; abstract;
     18  function Remove(Count: TGStreamIndex): TGStreamIndex; virtual; abstract;
     19  function Seek(Offset: TGStreamIndex; Origin: TSeekOrigin = soCurrent):
     20    TGStreamIndex; virtual; abstract;
     21  constructor Create; virtual;
     22  property Position: TGStreamIndex read GetPosition write SetPosition;
     23  property Size: TGStreamIndex read GetSize write SetSize;
    1024end;
    1125
     
    1529{$IFDEF IMPLEMENTATION}
    1630
    17 procedure TGStream.Write(Item: TGStreamItem);
     31procedure TGStream.Assign(Source: TGStream);
    1832begin
    19   if Assigned(FOnData) then
    20     FOnData(Item);
    2133end;
    2234
     35procedure TGStream.SetPosition(AValue: TGStreamIndex);
     36begin
     37  Seek(AValue, soBegin);
     38end;
     39
     40function TGStream.GetPosition: TGStreamIndex;
     41begin
     42  Result := Seek(0, soCurrent);
     43end;
     44
     45procedure TGStream.SetSize(AValue: TGStreamIndex);
     46var
     47  StreamSize: TGStreamIndex;
     48  OldPosition: TGStreamIndex;
     49begin
     50  OldPosition := Seek(0, soCurrent);
     51  StreamSize := Size;
     52  if AValue > StreamSize then begin
     53    Seek(StreamSize, soBegin);
     54    Insert(AValue - StreamSize);
     55  end else
     56  if AValue < StreamSize then begin
     57    Seek(AValue, soBegin);
     58    Remove(StreamSize - AValue);
     59  end;
     60  Position := OldPosition;
     61end;
     62
     63function TGStream.GetSize: TGStreamIndex;
     64var
     65  OldPosition: Integer;
     66begin
     67  OldPosition := Position;
     68  Result := Seek(0, soEnd);
     69  Position := OldPosition;
     70end;
     71
     72constructor TGStream.Create;
     73begin
     74  inherited;
     75end;
    2376
    2477{$UNDEF IMPLEMENTATION}
Note: See TracChangeset for help on using the changeset viewer.