📄 classes.pas
字号:
end else Break;
end;
EOS := Tail^ = #0;
if (Head <> Tail) and (Head^ <> #0) then
begin
if Strings <> nil then
begin
SetString(Item, Head, Tail - Head);
Strings.Add(Item);
end;
Inc(Result);
end;
Inc(Tail);
until EOS;
finally
Strings.EndUpdate;
end;
end;
{ TList }
destructor TList.Destroy;
begin
Clear;
end;
function TList.Add(Item: Pointer): Integer;
begin
Result := FCount;
if Result = FCapacity then Grow;
FList^[Result] := Item;
Inc(FCount);
end;
procedure TList.Clear;
begin
SetCount(0);
SetCapacity(0);
end;
procedure TList.Delete(Index: Integer);
begin
if (Index < 0) or (Index >= FCount) then Error(SListIndexError, Index);
Dec(FCount);
if Index < FCount then
System.Move(FList^[Index + 1], FList^[Index],
(FCount - Index) * SizeOf(Pointer));
end;
class procedure TList.Error(const Msg: string; Data: Integer);
function ReturnAddr: Pointer;
asm
MOV EAX,[EBP+4]
end;
begin
raise EListError.CreateFmt(Msg, [Data]) at ReturnAddr;
end;
procedure TList.Exchange(Index1, Index2: Integer);
var
Item: Pointer;
begin
if (Index1 < 0) or (Index1 >= FCount) then Error(SListIndexError, Index1);
if (Index2 < 0) or (Index2 >= FCount) then Error(SListIndexError, Index2);
Item := FList^[Index1];
FList^[Index1] := FList^[Index2];
FList^[Index2] := Item;
end;
function TList.Expand: TList;
begin
if FCount = FCapacity then Grow;
Result := Self;
end;
function TList.First: Pointer;
begin
Result := Get(0);
end;
function TList.Get(Index: Integer): Pointer;
begin
if (Index < 0) or (Index >= FCount) then Error(SListIndexError, Index);
Result := FList^[Index];
end;
procedure TList.Grow;
var
Delta: Integer;
begin
if FCapacity > 64 then Delta := FCapacity div 4 else
if FCapacity > 8 then Delta := 16 else
Delta := 4;
SetCapacity(FCapacity + Delta);
end;
function TList.IndexOf(Item: Pointer): Integer;
begin
Result := 0;
while (Result < FCount) and (FList^[Result] <> Item) do Inc(Result);
if Result = FCount then Result := -1;
end;
procedure TList.Insert(Index: Integer; Item: Pointer);
begin
if (Index < 0) or (Index > FCount) then Error(SListIndexError, Index);
if FCount = FCapacity then Grow;
if Index < FCount then
System.Move(FList^[Index], FList^[Index + 1],
(FCount - Index) * SizeOf(Pointer));
FList^[Index] := Item;
Inc(FCount);
end;
function TList.Last: Pointer;
begin
Result := Get(FCount - 1);
end;
procedure TList.Move(CurIndex, NewIndex: Integer);
var
Item: Pointer;
begin
if CurIndex <> NewIndex then
begin
if (NewIndex < 0) or (NewIndex >= FCount) then Error(SListIndexError, NewIndex);
Item := Get(CurIndex);
Delete(CurIndex);
Insert(NewIndex, Item);
end;
end;
procedure TList.Put(Index: Integer; Item: Pointer);
begin
if (Index < 0) or (Index >= FCount) then Error(SListIndexError, Index);
FList^[Index] := Item;
end;
function TList.Remove(Item: Pointer): Integer;
begin
Result := IndexOf(Item);
if Result <> -1 then Delete(Result);
end;
procedure TList.Pack;
var
I: Integer;
begin
for I := FCount - 1 downto 0 do if Items[I] = nil then Delete(I);
end;
procedure TList.SetCapacity(NewCapacity: Integer);
begin
if (NewCapacity < FCount) or (NewCapacity > MaxListSize) then
Error(SListCapacityError, NewCapacity);
if NewCapacity <> FCapacity then
begin
ReallocMem(FList, NewCapacity * SizeOf(Pointer));
FCapacity := NewCapacity;
end;
end;
procedure TList.SetCount(NewCount: Integer);
begin
if (NewCount < 0) or (NewCount > MaxListSize) then
Error(SListCountError, NewCount);
if NewCount > FCapacity then SetCapacity(NewCount);
if NewCount > FCount then
FillChar(FList^[FCount], (NewCount - FCount) * SizeOf(Pointer), 0);
FCount := NewCount;
end;
procedure QuickSort(SortList: PPointerList; L, R: Integer;
SCompare: TListSortCompare);
var
I, J: Integer;
P, T: Pointer;
begin
repeat
I := L;
J := R;
P := SortList^[(L + R) shr 1];
repeat
while SCompare(SortList^[I], P) < 0 do Inc(I);
while SCompare(SortList^[J], P) > 0 do Dec(J);
if I <= J then
begin
T := SortList^[I];
SortList^[I] := SortList^[J];
SortList^[J] := T;
Inc(I);
Dec(J);
end;
until I > J;
if L < J then QuickSort(SortList, L, J, SCompare);
L := I;
until I >= R;
end;
procedure TList.Sort(Compare: TListSortCompare);
begin
if (FList <> nil) and (Count > 0) then
QuickSort(FList, 0, Count - 1, Compare);
end;
{ TThreadList }
constructor TThreadList.Create;
begin
inherited Create;
InitializeCriticalSection(FLock);
FList := TList.Create;
end;
destructor TThreadList.Destroy;
begin
LockList; // Make sure nobody else is inside the list.
try
FList.Free;
inherited Destroy;
finally
UnlockList;
DeleteCriticalSection(FLock);
end;
end;
procedure TThreadList.Add(Item: Pointer);
begin
LockList;
try
if FList.IndexOf(Item) = -1 then
FList.Add(Item);
finally
UnlockList;
end;
end;
procedure TThreadList.Clear;
begin
LockList;
try
FList.Clear;
finally
UnlockList;
end;
end;
function TThreadList.LockList: TList;
begin
EnterCriticalSection(FLock);
Result := FList;
end;
procedure TThreadList.Remove(Item: Pointer);
begin
LockList;
try
FList.Remove(Item);
finally
UnlockList;
end;
end;
procedure TThreadList.UnlockList;
begin
LeaveCriticalSection(FLock);
end;
{ TInterfaceList }
constructor TInterfaceList.Create;
begin
inherited Create;
FList := TThreadList.Create;
end;
destructor TInterfaceList.Destroy;
begin
Clear;
FList.Free;
inherited Destroy;
end;
procedure TInterfaceList.Clear;
var
I: Integer;
begin
if FList <> nil then
begin
with FList.LockList do
try
for I := 0 to Count - 1 do
IUnknown(List[I]) := nil;
Clear;
finally
Self.FList.UnlockList;
end;
end;
end;
procedure TInterfaceList.Delete(Index: Integer);
begin
with FList.LockList do
try
Self.Put(Index, nil);
Delete(Index);
finally
Self.FList.UnlockList;
end;
end;
function TInterfaceList.Expand: TInterfaceList;
begin
with FList.LockList do
try
Expand;
Result := Self;
finally
Self.FList.Unlocklist;
end;
end;
function TInterfaceList.First: IUnknown;
begin
Result := Get(0);
end;
function TInterfaceList.Get(Index: Integer): IUnknown;
begin
with FList.LockList do
try
Result := IUnknown(List[Index]);
finally
Self.FList.UnlockList;
end;
end;
function TInterfaceList.GetCapacity: Integer;
begin
with FList.LockList do
try
Result := Capacity;
finally
Self.FList.UnlockList;
end;
end;
function TInterfaceList.GetCount: Integer;
begin
with FList.LockList do
try
Result := Count;
finally
Self.FList.UnlockList;
end;
end;
function TInterfaceList.IndexOf(Item: IUnknown): Integer;
begin
with FList.LockList do
try
Result := IndexOf(Pointer(Item));
finally
Self.FList.UnlockList;
end;
end;
function TInterfaceList.Add(Item: IUnknown): Integer;
begin
with FList.LockList do
try
Result := Add(nil);
IUnknown(List[Result]) := Item;
finally
Self.FList.UnlockList;
end;
end;
procedure TInterfaceList.Insert(Index: Integer; Item: IUnknown);
begin
with FList.LockList do
try
Insert(Index, nil);
IUnknown(List[Index]) := Item;
finally
Self.FList.UnlockList;
end;
end;
function TInterfaceList.Last: IUnknown;
begin
with FList.LockList do
try
Result := Self.Get(Count - 1);
finally
Self.FList.UnlockList;
end;
end;
procedure TInterfaceList.Put(Index: Integer; Item: IUnknown);
begin
with FList.LockList do
try
if (Index < 0) or (Index >= Count) then Error(SListIndexError, Index);
IUnknown(List[Index]) := Item;
finally
Self.FList.UnlockList;
end;
end;
function TInterfaceList.Remove(Item: IUnknown): Integer;
begin
with FList.LockList do
try
Result := IndexOf(Pointer(Item));
if Result > -1 then
begin
IUnknown(List[Result]) := nil;
Delete(Resu
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -