Changeset 69 for Generics


Ignore:
Timestamp:
Oct 27, 2010, 9:28:58 PM (14 years ago)
Author:
george
Message:
  • Added: Demo application.
Location:
Generics/TemplateGenerics
Files:
2 added
13 edited

Legend:

Unmodified
Added
Removed
  • Generics/TemplateGenerics/List/GenericListImplementation.tpl

    r68 r69  
    1010{ TGList }
    1111
    12 function TGList.GetCapacity: TIndexType;
     12function TGList.GetCapacity: TListIndex;
    1313begin
    1414  Result := Length(FItems);
    1515end;
    1616
    17 procedure TGList.SetCapacity(const AValue: TIndexType);
     17procedure TGList.SetCapacity(const AValue: TListIndex);
    1818begin
    1919  SetLength(FItems, AValue);
    2020end;
    2121
    22 function TGList.Get(Index: TIndexType): TItemType;
     22function TGList.Get(Index: TListIndex): TListItem;
    2323begin
    2424  Result := FItems[Index];
    2525end;
    2626
    27 function TGList.GetCount: TIndexType;
     27function TGList.GetCount: TListIndex;
    2828begin
    2929  Result := FCount;
    3030end;
    3131
    32 procedure TGList.Put(Index: TIndexType; const AValue: TItemType);
     32procedure TGList.Put(Index: TListIndex; const AValue: TListItem);
    3333begin
    3434  FItems[Index] := AValue;
    3535end;
    3636
    37 procedure TGList.SetCount(const AValue: TIndexType);
     37procedure TGList.SetCount(const AValue: TListIndex);
    3838begin
    3939  SetLength(FItems, AValue);
     
    4141end;
    4242
    43 procedure TGList.QuickSort(L, R: TIndexType; Compare: TGListSortCompare);
    44 var
    45   I, J: TIndexType;
    46   P, Q: TItemType;
     43procedure TGList.QuickSort(L, R: TListIndex; Compare: TGListSortCompare);
     44var
     45  I, J: TListIndex;
     46  P, Q: TListItem;
    4747begin
    4848 repeat
     
    8484procedure TGList.Expand;
    8585var
    86   IncSize: TIndexType;
     86  IncSize: TListIndex;
    8787begin
    8888  if FCount = Capacity then begin
     
    9595end;
    9696
    97 function TGList.Extract(Item: TItemType): TItemType;
    98 var
    99   I: TIndexType;
     97function TGList.Extract(Item: TListItem): TListItem;
     98var
     99  I: TListIndex;
    100100begin
    101101  I := IndexOf(Item);
     
    107107end;
    108108
    109 function TGList.ExtractList(Item: TItemType; Count: TIndexType): TItemType;
     109function TGList.ExtractList(Item: TListItem; Count: TListIndex): TListItem;
    110110begin
    111111  raise Exception.Create(SNotImplemented);
    112112end;
    113113
    114 function TGList.First: TItemType;
     114function TGList.First: TListItem;
    115115begin
    116116  if FCount = 0 then
     
    120120end;
    121121
    122 function TGList.IndexOf(Item: TItemType): TIndexType;
     122function TGList.IndexOf(Item: TListItem): TListIndex;
    123123begin
    124124  Result := 0;
     
    128128end;
    129129
    130 procedure TGList.Insert(Index: TIndexType; Item: TItemType);
     130procedure TGList.Insert(Index: TListIndex; Item: TListItem);
    131131begin
    132132  if (Index < 0) or (Index > FCount ) then
     
    134134  if FCount = Capacity then Expand;
    135135  if Index < FCount then
    136     SystemMove(FItems[Index], FItems[Index + 1], (FCount - Index) * SizeOf(TItemType));
     136    SystemMove(FItems[Index], FItems[Index + 1], (FCount - Index) * SizeOf(TListItem));
    137137  FItems[Index] := Item;
    138138  FCount := FCount + 1;
    139139end;
    140140
    141 procedure TGList.InsertList(Index: TIndexType; List: TGList);
    142 var
    143   I: TIndexType;
     141procedure TGList.InsertList(Index: TListIndex; List: TGList);
     142var
     143  I: TListIndex;
    144144begin
    145145  I := 0;
     
    150150end;
    151151
    152 function TGList.Last: TItemType;
     152function TGList.Last: TListItem;
    153153begin
    154154  if FCount = 0 then
     
    158158end;
    159159
    160 procedure TGList.Move(CurIndex, NewIndex: TIndexType);
    161 var
    162   Temp: TItemType;
     160procedure TGList.Move(CurIndex, NewIndex: TListIndex);
     161var
     162  Temp: TListItem;
    163163begin
    164164  if ((CurIndex < 0) or (CurIndex > Count - 1)) then
     
    171171end;
    172172
    173 procedure TGList.MoveItems(CurIndex, NewIndex, Count: TIndexType);
     173procedure TGList.MoveItems(CurIndex, NewIndex, Count: TListIndex);
    174174begin
    175175  raise Exception.Create(SNotImplemented);
    176176end;
    177177
    178 function TGList.Remove(Item: TItemType): TIndexType;
     178procedure TGList.Swap(Index1, Index2: TListIndex);
     179var
     180  Temp: TListItem;
     181begin
     182  Temp := Items[Index1];
     183  Items[Index1] := Items[Index2];
     184  Items[Index2] := Temp;
     185end;
     186
     187function TGList.Remove(Item: TListItem): TListIndex;
    179188begin
    180189  Result := IndexOf(Item);
     
    183192end;
    184193
    185 function TGList.Equals(Obj: TObject): Boolean;
    186 var
    187   I: TIndexType;
     194(*function TGList.Equals(Obj: TObject): Boolean;
     195var
     196  I: TListIndex;
    188197begin
    189198  Result := Count = (Obj as TGList).Count;
     
    198207    end;
    199208  end;
    200 end;
     209end;*)
    201210
    202211procedure TGList.Reverse;
    203 begin
    204   raise Exception.Create(SNotImplemented);
     212var
     213  I: TListIndex;
     214begin
     215  I := 0;
     216  while I < (Count div 2) do begin
     217    Swap(I, Count - 1 - I);
     218    I := I + 1;
     219  end;
    205220end;
    206221
     
    211226end;
    212227
    213 procedure TGList.SetArray(Values: array of TItemType);
    214 var
    215   I: TIndexType;
     228procedure TGList.SetArray(Values: array of TListItem);
     229var
     230  I: TListIndex;
    216231begin
    217232  Clear;
     
    223238end;
    224239
    225 function TGList.Implode(Separator: string): string;
    226 var
    227   I: TIndexType;
     240function TGList.Implode(Separator: string; Converter: TGListStringConverter): string;
     241var
     242  I: TListIndex;
    228243begin
    229244  Result := '';
    230245  I := 0;
    231246  while I < Count do begin
    232     Result := Result + string(Items[I]);
     247    Result := Result + Converter(Items[I]);
    233248    if I < (Count - 1) then
    234249      Result := Result + Separator;
     
    237252end;
    238253
    239 function TGList.Add(Item: TItemType): TIndexType;
     254function TGList.Add(Item: TListItem): TListIndex;
    240255begin
    241256  if FCount = Capacity then
     
    248263procedure TGList.AddList(List: TGList);
    249264var
    250   I: TIndexType;
     265  I: TListIndex;
    251266begin
    252267  I := 0;
     
    263278end;
    264279
    265 procedure TGList.Delete(Index: TIndexType);
     280procedure TGList.Delete(Index: TListIndex);
    266281begin
    267282  if (Index < 0) or (Index >= FCount) then
    268283    raise EListError.CreateFmt(SListIndexError, [Index]);
    269284  FCount := FCount - 1;
    270   SystemMove(FItems[Index + 1], FItems[Index], (FCount - Index) * SizeOf(TItemType));
     285  SystemMove(FItems[Index + 1], FItems[Index], (FCount - Index) * SizeOf(TListItem));
    271286  // Shrink the list if appropriate
    272287  if (Capacity > 256) and (FCount < Capacity shr 2) then
     
    276291end;
    277292
    278 procedure TGList.DeleteItems(Index, Count: TIndexType);
    279 var
    280   I: TIndexType;
     293procedure TGList.DeleteItems(Index, Count: TListIndex);
     294var
     295  I: TListIndex;
    281296begin
    282297  I := 0;
     
    287302end;
    288303
    289 procedure TGList.Exchange(Index1, Index2: TIndexType);
    290 var
    291   Temp: TItemType;
     304procedure TGList.Exchange(Index1, Index2: TListIndex);
     305var
     306  Temp: TListItem;
    292307begin
    293308  if ((Index1 >= FCount) or (Index1 < 0)) then
  • Generics/TemplateGenerics/List/GenericListInterface.tpl

    r68 r69  
    44
    55type
    6   TGListSortCompare = function(const Item1, Item2: TItemType): Integer;
     6  TGListSortCompare = function(const Item1, Item2: TListItem): Integer of object;
     7  TGListStringConverter = function(Item: TListItem): string;
    78  //TGListNotification = (lnAdded, lnExtracted, lnDeleted);
    89
    9   // TGList<TIndexType, TItemType> = class
     10  // TGList<TListIndex, TListItem> = class
    1011  TGList = class
    1112  private
    12     FItems: array of TItemType;
    13     FCount: TIndexType;
    14     function Get(Index: TIndexType): TItemType;
    15     function GetCount: TIndexType;
    16     function GetCapacity: TIndexType;
    17     procedure SetCapacity(const AValue: TIndexType);
    18     procedure Put(Index: TIndexType; const AValue: TItemType);
    19     procedure SetCount(const AValue: TIndexType);
    20     procedure QuickSort(L, R : TIndexType; Compare: TGListSortCompare);
    21     property Capacity: TIndexType read GetCapacity write SetCapacity;
     13    FItems: array of TListItem;
     14    FCount: TListIndex;
     15    function Get(Index: TListIndex): TListItem;
     16    function GetCount: TListIndex;
     17    function GetCapacity: TListIndex;
     18    procedure SetCapacity(const AValue: TListIndex);
     19    procedure Put(Index: TListIndex; const AValue: TListItem);
     20    procedure SetCount(const AValue: TListIndex);
     21    procedure QuickSort(L, R : TListIndex; Compare: TGListSortCompare);
     22    property Capacity: TListIndex read GetCapacity write SetCapacity;
    2223  public
    2324    // All items
     
    2627    procedure Expand;
    2728    procedure Sort(Compare: TGListSortCompare);
    28     function Implode(Separator: string): string;
     29    function Implode(Separator: string; Converter: TGListStringConverter): string;
    2930    // Many items
    30     procedure MoveItems(CurIndex, NewIndex, Count: TIndexType);
     31    procedure MoveItems(CurIndex, NewIndex, Count: TListIndex);
     32    procedure Swap(Index1, Index2: TListIndex);
    3133    // One item
    32     function Add(Item: TItemType): TIndexType;
    33     procedure Delete(Index: TIndexType);
    34     procedure Exchange(Index1, Index2: TIndexType);
    35     function Extract(Item: TItemType): TItemType;
    36     function First: TItemType;
    37     function IndexOf(Item: TItemType): TIndexType;
    38     procedure Insert(Index: TIndexType; Item: TItemType);
    39     function Last: TItemType;
    40     procedure Move(CurIndex, NewIndex: TIndexType);
    41     function Remove(Item: TItemType): TIndexType;
    42     property Items[Index: TIndexType]: TItemType read Get write Put; default;
     34    function Add(Item: TListItem): TListIndex;
     35    procedure Delete(Index: TListIndex);
     36    procedure Exchange(Index1, Index2: TListIndex);
     37    function Extract(Item: TListItem): TListItem;
     38    function First: TListItem;
     39    function IndexOf(Item: TListItem): TListIndex;
     40    procedure Insert(Index: TListIndex; Item: TListItem);
     41    function Last: TListItem;
     42    procedure Move(CurIndex, NewIndex: TListIndex);
     43    function Remove(Item: TListItem): TListIndex;
     44    property Items[Index: TListIndex]: TListItem read Get write Put; default;
    4345    // List
    4446    procedure AddList(List: TGList);
    4547    procedure Assign(List: TGList);
    46     procedure DeleteItems(Index, Count: TIndexType);
    47     function Equals(Obj: TObject): Boolean; override;
    48     function ExtractList(Item: TItemType; Count: TIndexType): TItemType;
    49     procedure InsertList(Index: TIndexType; List: TGList);
     48    procedure DeleteItems(Index, Count: TListIndex);
     49    //function Equals(Obj: TObject): Boolean; override;
     50    function ExtractList(Item: TListItem; Count: TListIndex): TListItem;
     51    procedure InsertList(Index: TListIndex; List: TGList);
    5052    // Other
    51     property Count: TIndexType read GetCount write SetCount;
     53    property Count: TListIndex read GetCount write SetCount;
    5254    // Additional
    53     procedure SetArray(Values: array of TItemType);
     55    procedure SetArray(Values: array of TListItem);
    5456  end;
  • Generics/TemplateGenerics/List/IntegerList.pas

    r68 r69  
    99
    1010type
    11   TIndexType = Integer;
    12   TItemType = Integer;
     11  TListIndex = Integer;
     12  TListItem = Integer;
    1313{$INCLUDE 'GenericListInterface.tpl'}
    1414
    1515type
    16   TIntegerGList = class(TGList)
     16  TIntegerList = class(TGList)
    1717  end;
    1818
  • Generics/TemplateGenerics/List/ObjectList.pas

    r68 r69  
    99
    1010type
    11   TIndexType = Integer;
    12   TItemType = TObject;
     11  TListIndex = Integer;
     12  TListItem = TObject;
    1313{$INCLUDE 'GenericListInterface.tpl'}
    1414
    1515type
    16   TObjectGList = class(TGList)
     16
     17  { TObjectList }
     18
     19  TObjectList = class(TGList)
     20    //OwnObjects: Boolean;
     21    destructor Destroy; override;
    1722  end;
    1823
     
    2227
    2328
     29{ TObjectList }
     30
     31destructor TObjectList.Destroy;
     32begin
     33  inherited Destroy;
     34end;
     35
    2436end.
  • Generics/TemplateGenerics/List/PointerList.pas

    r68 r69  
    99
    1010type
    11   TIndexType = Integer;
    12   TItemType = Pointer;
     11  TListIndex = Integer;
     12  TListItem = Pointer;
    1313{$INCLUDE 'GenericListInterface.tpl'}
    1414
    1515type
    16   TPointerGList = class(TGList)
     16  TPointerList = class(TGList)
    1717  end;
    1818
  • Generics/TemplateGenerics/List/StringList.pas

    r68 r69  
    99
    1010type
    11   TIndexType = Integer;
    12   TItemType = string;
     11  TListIndex = Integer;
     12  TListItem = string;
    1313{$INCLUDE 'GenericListInterface.tpl'}
    1414
    1515type
    16   TStringGList = class(TGList)
     16  TStringList = class(TGList)
    1717  end;
    1818
  • Generics/TemplateGenerics/TemplateGenerics.lpk

    r68 r69  
    88      <PathDelim Value="\"/>
    99      <SearchPaths>
    10         <OtherUnitFiles Value="List;Tree"/>
     10        <OtherUnitFiles Value="List\;Tree\"/>
    1111        <UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
    1212      </SearchPaths>
     
    1515      </Other>
    1616    </CompilerOptions>
    17     <Files Count="12">
     17    <Files Count="13">
    1818      <Item1>
    1919        <Filename Value="List\StringList.pas"/>
     
    6464        <UnitName Value="PointerTree"/>
    6565      </Item12>
     66      <Item13>
     67        <Filename Value="List\DoubleList.pas"/>
     68        <UnitName Value="DoubleList"/>
     69      </Item13>
    6670    </Files>
    6771    <Type Value="RunAndDesignTime"/>
  • Generics/TemplateGenerics/TemplateGenerics.pas

    r68 r69  
    99uses
    1010    StringList, IntegerList, ObjectList, PointerList, StringTree, IntegerTree,
    11   ObjectTree, PointerTree, LazarusPackageIntf;
     11  ObjectTree, PointerTree, DoubleList, LazarusPackageIntf;
    1212
    1313implementation
  • Generics/TemplateGenerics/Tree/GenericTreeInterface.tpl

    r68 r69  
    1   // TGTreeNode<TIndexType, TItemType> = class
     1
     2
     3type
     4  TListIndex = TTreeIndex;
     5  TListItem = TTreeItem;
     6//{$INCLUDE 'GenericTreeInterface.tpl'}
     7
     8  // TGTreeNode<TTreeIndex, TTreeItem> = class
    29  TGTreeNode = class
    310
    411  end;
    512
    6   // TGTree<TIndexType, TItemType> = class
     13  // TGTree<TTreeIndex, TTreeItem> = class
    714  TGTree = class
    815    TopItem: TGTreeNode;
  • Generics/TemplateGenerics/Tree/IntegerTree.pas

    r68 r69  
    99
    1010type
    11   TIndexType = Integer;
    12   TItemType = Integer;
     11  TTreeIndex = Integer;
     12  TTreeItem = Integer;
    1313{$INCLUDE 'GenericTreeInterface.tpl'}
    1414
    1515type
    16   TIntegerGTree = class(TGTree)
     16  TIntegerTree = class(TGTree)
    1717  end;
    1818
    19   TIntegerGTreeNode = class(TGTreeNode)
     19  TIntegerTreeNode = class(TGTreeNode)
    2020  end;
    2121
  • Generics/TemplateGenerics/Tree/ObjectTree.pas

    r68 r69  
    99
    1010type
    11   TIndexType = Integer;
    12   TItemType = TObject;
     11  TTreeIndex = Integer;
     12  TTreeItem = TObject;
    1313{$INCLUDE 'GenericTreeInterface.tpl'}
    1414
     
    1717  end;
    1818
    19   TObjectGTreeNode = class(TGTreeNode)
     19  TObjectTreeNode = class(TGTreeNode)
    2020  end;
    2121
  • Generics/TemplateGenerics/Tree/PointerTree.pas

    r68 r69  
    99
    1010type
    11   TIndexType = Integer;
    12   TItemType = Pointer;
     11  TTreeIndex = Integer;
     12  TTreeItem = Pointer;
    1313{$INCLUDE 'GenericTreeInterface.tpl'}
    1414
     
    1717  end;
    1818
    19   TPointerGTreeNode = class(TGTreeNode)
     19  TPointerTreeNode = class(TGTreeNode)
    2020  end;
    2121
  • Generics/TemplateGenerics/Tree/StringTree.pas

    r68 r69  
    99
    1010type
    11   TIndexType = Integer;
    12   TItemType = string;
     11  TTreeIndex = Integer;
     12  TTreeItem = string;
    1313{$INCLUDE 'GenericTreeInterface.tpl'}
    1414
    1515type
    16   TStringGTree = class(TGTree)
     16  TStringTree = class(TGTree)
    1717  end;
    1818
    19   TStringGTreeNode = class(TGTreeNode)
     19  TStringTreeNode = class(TGTreeNode)
    2020  end;
    2121
Note: See TracChangeset for help on using the changeset viewer.