Ignore:
Timestamp:
Oct 29, 2010, 7:49:29 AM (14 years ago)
Author:
george
Message:
  • Added: Partial generic set implementation.
  • Modified: TGStack, TGQueue and TGSet owns TGList instead of inherits.
  • Added: TList and TListPointer benchmark comparasion.
Location:
Generics/TemplateGenerics/Generic
Files:
2 added
5 edited

Legend:

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

    r74 r76  
    11uses
    22  RtlConsts;
    3 
    4 // Used instead of System.Move form because of error: Identifier "System" not found
    5 procedure SystemMove(const Source; var Dest; Count: SizeInt);
    6 begin
    7   Move(Source, Dest, Count);
    8 end;
    93
    104{ TGList }
     
    130124  if FCount = Capacity then Expand;
    131125  if Index < FCount then
    132     SystemMove(FItems[Index], FItems[Index + 1], (FCount - Index) * SizeOf(TListItem));
     126    System.Move(FItems[Index], FItems[Index + 1], (FCount - Index) * SizeOf(TListItem));
    133127  FItems[Index] := Item;
    134128  FCount := FCount + 1;
     
    163157    raise EListError.CreateFmt(SlistIndexError, [NewIndex]);
    164158  Temp := FItems[CurIndex];
    165   Delete(CurIndex);
    166   Insert(NewIndex, Temp);
     159  if NewIndex > CurIndex then begin
     160    System.Move(FItems[CurIndex + 1], FItems[CurIndex], (NewIndex - CurIndex) * SizeOf(TListItem));
     161  end else
     162  if NewIndex < CurIndex then begin
     163    System.Move(FItems[NewIndex], FItems[NewIndex + 1], (CurIndex - NewIndex) * SizeOf(TListItem));
     164  end;
     165  FItems[NewIndex] := Temp;
     166  //Delete(CurIndex);
     167  //Insert(NewIndex, Temp);
    167168end;
    168169
     
    289290    raise EListError.CreateFmt(SListIndexError, [Index]);
    290291  FCount := FCount - 1;
    291   SystemMove(FItems[Index + 1], FItems[Index], (FCount - Index) * SizeOf(TListItem));
     292  System.Move(FItems[Index + 1], FItems[Index], (FCount - Index) * SizeOf(TListItem));
    292293  // Shrink the list if appropriate
    293294  if (Capacity > 256) and (FCount < Capacity shr 2) then
  • Generics/TemplateGenerics/Generic/QueueImplementation.tpl

    r73 r76  
    55procedure TGQueue.Enqueue(Value: TQueueItem);
    66begin
    7   Add(Value);
     7  FList.Add(Value);
    88end;
    99
    1010function TGQueue.Peek: TQueueItem;
    1111begin
    12   Result := First;
     12  Result := FList.First;
     13end;
     14
     15constructor TGQueue.Create;
     16begin
     17  FList := TGList.Create;
     18end;
     19
     20destructor TGQueue.Destroy;
     21begin
     22  FList.Free;
     23  inherited Destroy;
    1324end;
    1425
    1526function TGQueue.Dequeue: TQueueItem;
    1627begin
    17   Result := Extract(First);
     28  Result := FList.Extract(FList.First);
    1829end;
    1930
  • Generics/TemplateGenerics/Generic/QueueInterface.tpl

    r73 r76  
    44  {$INCLUDE 'ListInterface.tpl'}
    55
    6   // TGQueue<TListIndex, TListItem> = class(TGList)
    7   TGQueue = class(TGList)
     6  // TGQueue<TSetIndex, TSetItem> = class(TGList)
     7  TGQueue = class
    88  private
     9    FList: TGList;
    910  public
    1011    procedure Enqueue(Value: TQueueItem);
    1112    function Dequeue: TQueueItem;
    1213    function Peek: TQueueItem;
     14    constructor Create;
     15    destructor Destroy; override;
     16    property List: TGList read FList;
    1317  end;
  • Generics/TemplateGenerics/Generic/StackImplementation.tpl

    r73 r76  
    55procedure TGStack.Push(Value: TStackItem);
    66begin
    7   Add(Value);
     7  FList.Add(Value);
    88end;
    99
    1010function TGStack.Pop: TStackItem;
    1111begin
    12   Result := Extract(Last);
     12  Result := FList.Extract(FList.Last);
    1313end;
    1414
     15constructor TGStack.Create;
     16begin
     17  FList := TGList.Create;
     18end;
     19
     20destructor TGStack.Destroy;
     21begin
     22  FList.Free;
     23  inherited Destroy;
     24end;
     25
  • Generics/TemplateGenerics/Generic/StackInterface.tpl

    r73 r76  
    44  {$INCLUDE 'ListInterface.tpl'}
    55
    6   // TGStack<TListIndex, TListItem> = class(TGList)
    7   TGStack = class(TGList)
     6  // TGStack<TStackIndex, TStackItem> = class(TGList)
     7  TGStack = class
    88  private
     9    FList: TGList;
    910  public
    1011    procedure Push(Value: TStackItem);
    1112    function Pop: TStackItem;
     13    constructor Create;
     14    destructor Destroy; override;
     15    property List: TGList read FList;
    1216  end;
Note: See TracChangeset for help on using the changeset viewer.