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

📄 listtemplate.inc

📁 DelphiDoc is a program for automatic generation of documentation on a Delphi-Project. At the momen
💻 INC
📖 第 1 页 / 共 2 页
字号:

{$IFDEF ListTemplateItemMayBeFreed}

{Creates a new list.
~param FreeItems if the objects in the list should be automatically freed }
constructor TListTemplate.Create(FreeItems: Boolean =
 {$IFDEF ListTemplateItemFreedByDefault}
                                                      True
 {$ELSE}
                                                      False
 {$ENDIF}
                                                           );
begin
 inherited Create;                   //create the object

 FFreeItems := FreeItems;            //save if the objects should be freed
end;
{$ENDIF}


{$IFDEF ListTemplateItemMayBeFreed}
{Free all items, if needed. }
destructor TListTemplate.Destroy;
var        i            :Integer;  //counter through all items
begin
 if FFreeItems then                //if the items should be freed automatically
  for i := 0 to FCount - 1 do
   FContent[i].Free;                  //free each item

 inherited Destroy;                //free the list
end;
{$ENDIF}






{Returns the current size/capacity of the list.
~result the current size/capacity }
function TListTemplate.GetSize: Integer;
begin
 Result := Length(FContent);
end;


{Sets an item in the list to another value.
~param Index the index of the item in the list to set
~param Value the new value for the item in the list }
procedure TListTemplate.SetItems(Index: Integer;
                                 const Value: TListTemplateListItem);
begin
 Assert(Index < FCount);
 Assert(Index >= 0);
 FContent[Index] := Value;
end;


{Sets the size/capacity of the list, can also truncate the content.
~param Value the new size/capacity of the list }
procedure TListTemplate.SetSize(Value: Integer);
{$IFDEF ListTemplateItemMayBeFreed}
var       i            :Integer;
{$ENDIF}
begin
 Assert(Value >= 0);
 if Value < FCount then                  //list will be truncated?
  begin
{$IFDEF ListTemplateItemMayBeFreed}
   if FFreeItems then              //if the items should be freed automatically
    for i := Value to FCount - 1 do        //free all truncated objects
     FContent[i].Free;
{$ENDIF}
   FCount := Value;                        //set new number of items
  end;
 SetLength(FContent, Value);             //set the capacity
end;




{Adds an item to the list.
~param Item the item to add to the list }
procedure TListTemplate.Add(const Item: TListTemplateListItem);
var       Size         :Integer;         //the size/capacity of the list
begin
 Size := Length(FContent);               //get current size/capacity
 if FCount = Size then                   //need to grow it?
  begin
   if Size > 64 then
    Inc(Size, Size div 4)
   else
    if Size > 8 then
     Inc(Size, 16)
    else
     Inc(Size, 4);
   SetLength(FContent, Size);              //grow the list
  end;

 FContent[FCount] := Item;               //add the item to the list
 Inc(FCount);
end;

{Returns an item of the list.
~param Index the index in the list of the item to return
~result the item in the list at the index }
function TListTemplate.Get(Index: Integer): TListTemplateListItem;
begin
 Assert(Index < FCount);
 Assert(Index >= 0);
 Result := FContent[Index];
end;



{$IFDEF ListTemplateUseIndexOf}

{Returns the index of the elemtn in the list or -1.
~param Item the item to search in the list
~result the index of the item in the list or -1 if not found }
function TListTemplate.IndexOf(const Item: TListTemplateListItem): Integer;
begin
 Result := 0;                             //search the whole list for the item
 while (Result < FCount) and
{$IFDEF ListTemplateUseCompareFunction}
                             not CompareItems(FContent[Result], Item)
{$ELSE}
 {$IFDEF ListTemplateItemIsMethod}
                             (@FContent[Result] <> @Item)
 {$ELSE}
                             (FContent[Result] <> Item)
 {$ENDIF}
{$ENDIF}
                                                                      do
  inc(Result);
 if Result = FCount then                   //item not in list?
  Result := -1;
end;

{$ENDIF}



{Returns the last item of the list.
~result the last item of the list/stack }
function TListTemplate.Last: TListTemplateListItem;
begin
 assert(FCount > 0);
 Result := FContent[FCount - 1];       //get the last item
end;

{Returns the last item of the list and removes it. The item will never be
 freed.
~result the last item of the list/stack }
function TListTemplate.Pop: TListTemplateListItem;
begin
 Assert(FCount > 0);
 Result := FContent[FCount - 1];       //get the last item
 Dec(FCount);                          //last item deleted out of the list
end;




{$IFDEF ListTemplateItemAssignFromSelf}

{Adds all items of the other list.
~param OtherList the list to add all items from }
procedure TListTemplate.AddAll(OtherList: TListTemplate);
var       i            :Integer;        //counter through the items to be added
begin
 //not enough room for all new items?
 if Length(FContent) - FCount < OtherList.FCount then
  Size := FCount + OtherList.FCount;      //create it

 for i := 0 to OtherList.FCount - 1 do  //add all the items
  begin
   FContent[FCount] := OtherList.FContent[i];
   Inc(FCount);
  end;
end;

{$ENDIF}






{Removes an item from the list.
~param Index the index of the item to delete from the list }
procedure TListTemplate.Delete(Index: Integer);
var       i            :Integer;        //counter through items
begin
 assert(Index < FCount);
 assert(Index >= 0);

{$IFDEF ListTemplateItemMayBeFreed}
 if FFreeItems then                     //object should be freed?
  FContent[Index].Free;                   //free it
{$ENDIF}

 for i := Index to FCount - 2 do        //close the gap of the deleted item
  FContent[i] := FContent[i + 1];
 dec(FCount);                           //one item deleted out of the list
end;

{Removes several items from the list. Only removes items, if EndIndex is
 greater or equal StartIndex.
~param StartIndex the index of the first item to delete from the list
~param EndIndex   the index of the last item to delete from the list }
procedure TListTemplate.DeleteRange(StartIndex, EndIndex: Integer);
var       DelCount     :Integer;        //number of items to delete
          i            :Integer;        //counter through items
begin
 assert(StartIndex < FCount);
 assert(StartIndex >= 0);
 assert(EndIndex < FCount);
 assert(EndIndex >= 0);

 if EndIndex >= StartIndex then         //some items to delete?
  begin
   //calculate number of items to be deleted
   DelCount := EndIndex - StartIndex + 1;
{$IFDEF ListTemplateItemMayBeFreed}
   if FFreeItems then              //if the items should be freed automatically
    for i := StartIndex to EndIndex do      //if objects should be freed
     FContent[i].Free;                        //free them
{$ENDIF}
   for i := StartIndex to FCount - DelCount - 1 do //close the gap of the
    FContent[i] := FContent[i + DelCount];          //deleted items

   dec(FCount, DelCount);                 //items deleted out of the list
  end;
end;



{$IFDEF ListTemplateUseIndexOf}

{Removes an item from the list.
~param Item the item to be removed
~result if the item was in the list and has been removed }
function TListTemplate.Remove(const Item: TListTemplateListItem): Boolean;
var      Index        :Integer;    //the index of the item in the list
begin
 Index := IndexOf(Item);           //search item in the list
 Result := Index <> -1;
 if Result then                    //item is in the list?
  Delete(Index);                     //remove it
end;


 {$IFDEF ListTemplateItemMayBeFreed}

{Removes an item from the list without freeing it.
~param Item the item to be removed
~result if the item was in the list and has been removed }
function TListTemplate.RemoveReference(const Item: TListTemplateListItem):
                                                                       Boolean;
var      Index        :Integer;      //the index of the item in the list
begin
 Index := IndexOf(Item);             //search item in the list
 Result := Index <> -1;
 if Result then                      //item is in the list?
  begin
   for Index := Index to FCount - 2 do //close the gap of the deleted item
    FContent[Index] := FContent[Index + 1];
   dec(FCount);                        //one item deleted out of the list
  end;
end;

 {$ENDIF}

{$ENDIF}



{Removes all items from the list. }
procedure TListTemplate.Clear;
begin
 Size := 0;                        //free the list
end;


{Returns whether the list is empty.
~result whether the list is empty }
function TListTemplate.IsEmpty: Boolean;
begin
 Result := FCount = 0;             //is empty when there are zero elements
end;





//just to test this program:

{$IFNDEF LINUX}

//{$include ..\..\GUI\onefunc.inc}

{$ENDIF  NOT LINUX }




 {$ENDIF  NOT ListTemplateThirdRun }
{$ENDIF  NOT ListTemplateSecondRun }


⌨️ 快捷键说明

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