⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 typedlist_template.pas

📁 Delphi, typed list generator code snippled, wonderfull delphi sample code
💻 PAS
📖 第 1 页 / 共 2 页
字号:
    {$ELSE}
      {$IFDEF GTL_USEPOINTERS} 
        pItem := <!DATATYPE>_Ptr( Storage[ Index ] );
        If Assigned( pItem ) Then 
          Dispose( pItem );
      {$ENDIF} 
    {$ENDIF}
  End; { <!LISTCLASSNAME>.FreeItem }

{+------------------------------------------------------------
 | Procedure <!LISTCLASSNAME>.Delete
 |
 | Parameters :
 |   Index: index of item to delete.
 | Call method: virtual, overridden
 | Visibility : public
 | Description:
 |   Deletes the item at index from the list, freeing memory for
 |   it if necessary.
 | Error Conditions: 
 |   A invalid index will raise a EListError exception!
 | Created: <!DATE> by GenTypedLists Version <!VERSION>
 +------------------------------------------------------------}
Procedure <!LISTCLASSNAME>.Delete(Index: Integer); 
  Begin
    FreeItem( Index );
    inherited Delete( Index );
  End; { <!LISTCLASSNAME>.Delete }

{+------------------------------------------------------------
 | Function  <!LISTCLASSNAME>.Detach
 |
 | Parameters : index of item to remove from the list
 | Returns    : the removed item
 | Call method: static
 | Visibility : public
 | Description:
 |   Returns the item at index and then deletes the entry from
 |   the list. If the list stores objects the object will of 
 |   course *not* be freed, the list relegates ownership of the
 |   data to the caller.
 | Error Conditions: 
 |   A invalid index will raise a EListError exception!
 | Created: <!DATE> by GenTypedLists Version <!VERSION>
 +------------------------------------------------------------}
Function  <!LISTCLASSNAME>.Detach(Index: Integer): <!DATATYPE>; 
  Begin
    Result := Items[ Index ];
    {$IFDEF GTL_USEPOINTERS} 
    Delete( index );
    {$ELSE}
    inherited Delete( index );
    {$ENDIF} 
  End; { <!LISTCLASSNAME>.Detach }

{+------------------------------------------------------------
 | Procedure <!LISTCLASSNAME>.Put
 |
 | Property   : Items ( write )
 | Call method: static
 | Visibility : private
 | Description:
 |   Frees memory for the item at index, if required, and stores
 |   the passed item in that slot of the list. If the data type is 
 |   larger than 4 bytes memory for the Item is allocated on the 
 |   heap, the item is copied into it and the pointer is stored
 |   in the list, otherwise the data is stored directly, typecast
 |   to a pointer.
 | Error Conditions: 
 |   A invalid index will raise a EListError exception!
 | Created: <!DATE> by GenTypedLists Version <!VERSION>
 +------------------------------------------------------------}
Procedure <!LISTCLASSNAME>.Put( index: Integer; Const Item: <!DATATYPE> );
  {$IFDEF GTL_USEPOINTERS} 
  Var
    pTemp: <!DATATYPE>_Ptr;
  {$ENDIF} 
  Begin
    FreeItem( index );
    {$IFDEF GTL_USEPOINTERS} 
    New( pTemp );
    Try
      pTemp^ := Item;
      Storage.Items[ index ]:= pTemp;
    Except
      Dispose( pTemp );
      raise
    End;
    {$ELSE} 
    Storage.Items[ Index ]:= Pointer( Item );
    {$ENDIF} 
  End; { <!LISTCLASSNAME>.Put }

{+------------------------------------------------------------
 | Function  <!LISTCLASSNAME>.Get
 |
 | Property   : Items ( read )
 | Call method: static
 | Visibility : private
 | Description:
 |   Returns the item at Index in the list.
 | Error Conditions: 
 |   A invalid index will raise a EListError exception!
 | Created: <!DATE> by GenTypedLists Version <!VERSION>
 +------------------------------------------------------------}
Function  <!LISTCLASSNAME>.Get( index: Integer ): <!DATATYPE>;
  Begin
    {$IFDEF GTL_USEPOINTERS} 
    Result := <!DATATYPE>_Ptr( Storage.Items[ index ] )^
    {$ELSE} 
    Result := <!DATATYPE>( Storage.Items[ index ] );
    {$ENDIF} 
  End; { <!LISTCLASSNAME>.Get }

{+------------------------------------------------------------
 | Function <!LISTCLASSNAME>.LinearSearch
 |
 | Parameters :
 |   Item: item to search for
 |   Compare: compare method to use
 | Returns    : the index of the item, or -1, if the item is not
 |              found.
 | Call method: static
 | Visibility : public
 | Description:
 |   Performs a linear search over the list and stops at the first
 |   item that matches the passed one.
 | Error Conditions: none
 | Created: <!DATE> by GenTypedLists Version <!VERSION>
 +------------------------------------------------------------}
Function <!LISTCLASSNAME>.LinearSearch( Const Item: <!DATATYPE>; 
                       Compare: <!DATATYPE>SortCompare): Integer;
  Var
    i: Integer;
  Begin
    Assert( Assigned( Compare ));
    Result := -1;
    For i := 0 To LastIndex Do Begin 
      If Compare( Item, Items[ i ] ) = 0 Then Begin
        Result := i;
        Break;
      End; { If }
    End; { For }
  End; { <!LISTCLASSNAME>.LinearSearch }

{+------------------------------------------------------------
 | Function <!LISTCLASSNAME>.BinarySearch
 |
 | Parameters :
 |   Item: item to search for
 |   Compare: compare method to use
 | Returns    : the index of the item, or -1, if the item is not
 |              found.
 | Call method: static
 | Visibility : public
 | Description:
 |   Performs a binary search over the list and stops at the first
 |   item that matches the passed one. The list needs to be sorted
 |   for this kind of find to work and the same Compare function 
 |   needs to be used for sort and search! If the list contains duplicate 
 |   items binary search will find one of them but not necessarily 
 |   the first!
 |   The implementation has not been optimized for speed in any way!
 | Error Conditions: none
 | Created: <!DATE> by GenTypedLists Version <!VERSION>
 +------------------------------------------------------------}
Function <!LISTCLASSNAME>.BinarySearch( Const Item: <!DATATYPE>; 
                       Compare: <!DATATYPE>SortCompare): Integer;
  Var
    first, last, pivot, res: Integer;
  Begin
    Assert( Assigned( Compare ));
    Result := -1;
    If count = 0 Then Exit;

    first := 0;
    last  := LastIndex;
    Repeat 
      pivot := ( first + last ) div 2;
      res := Compare( Item, Items[ pivot ] );
      If res = 0 Then Begin 
        { Found the item, return its index and exit. }
        Result := pivot;
        Break;
      End { If } 
      Else If res > 0 Then Begin 
        { Item is larger than item at pivot }
        first := pivot + 1;
      End { If }
      Else Begin 
        { Item is smaller than item at pivot } 
        last := pivot - 1;
      End;
    Until last < first;
  End; { <!LISTCLASSNAME>.BinarySearch }

Procedure <!LISTCLASSNAME>.QuickSort(L, R: Integer; Compare: <!DATATYPE>SortCompare );
  Var
    I, J: Integer;
    P: <!DATATYPE>;
  Begin
    Repeat
      I := L;
      J := R;
      P := Items[(L + R) shr 1];
      Repeat
        While Compare(Items[I], P) < 0 Do Inc(I);
        While Compare(Items[J], P) > 0 Do Dec(J);
        If I <= J Then
        Begin
          Exchange( I, J );
          Inc(I);
          Dec(J);
        End;
      Until I > J;
      If L < J Then QuickSort(L, J, Compare);
      L := I;
    Until I >= R;
  End; { <!LISTCLASSNAME>.QuickSort }

{+------------------------------------------------------------
 | Procedure <!LISTCLASSNAME>.Sort
 |
 | Parameters : function to use to compare items
 | Call method: static
 | Visibility : public
 | Description:
 |   Performs a quicksort on the list. The sort code is modified
 |   from TList.Sort.
 | Error Conditions: none
 | Created: <!DATE> by GenTypedLists Version <!VERSION>
 +------------------------------------------------------------}
Procedure <!LISTCLASSNAME>.Sort(Compare: <!DATATYPE>SortCompare);
  Begin
    Assert( Assigned( Compare ));
    If Count > 1 Then 
      QuickSort(0, LastIndex, Compare);
  End; { <!LISTCLASSNAME>.Sort }

Initialization
End. { <!UNITNAME> }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -