📄 jclvectors.pas
字号:
end;
function TItr.NextIndex: Integer;
begin
Result := FCursor;
end;
function TItr.Previous: TObject;
begin
Dec(FCursor);
FLastRet := FCursor;
Result := FOwnList.Items[FCursor];
end;
function TItr.PreviousIndex: Integer;
begin
Result := FCursor - 1;
end;
procedure TItr.Remove;
begin
with FOwnList do
begin
FreeObject(FItems[FCursor]);
System.Move(FItems[FCursor + 1], FItems[FCursor],
(FSize - FCursor) * SizeOf(TObject));
end;
Dec(FOwnList.FCount);
Dec(FSize);
end;
procedure TItr.SetObject(AObject: TObject);
begin
{
if FLastRet = -1 then
raise EJclIllegalState.Create(SIllegalState);
}
FOwnList.Items[FCursor] := AObject;
end;
//=== { TJclIntfVector } =====================================================
constructor TJclIntfVector.Create(ACapacity: Integer = DefaultContainerCapacity);
begin
inherited Create;
FCount := 0;
if ACapacity < 0 then
FCapacity := 0
else
FCapacity := ACapacity;
SetLength(FItems, FCapacity);
end;
destructor TJclIntfVector.Destroy;
begin
Clear;
inherited Destroy;
end;
procedure TJclIntfVector.Insert(Index: Integer; AInterface: IInterface);
begin
if (Index < 0) or (Index > FCount) then
raise EJclOutOfBoundsError.CreateRes(@RsEOutOfBounds);
System.Move(FItems[Index], FItems[Index - 1],
(FCount - Index) * SizeOf(IInterface));
FCapacity := Length(FItems);
FItems[Index] := AInterface;
Inc(FCount);
end;
function TJclIntfVector.Add(AInterface: IInterface): Boolean;
begin
if FCount = FCapacity then
Grow;
FItems[FCount] := AInterface;
Inc(FCount);
Result := True;
end;
function TJclIntfVector.InsertAll(Index: Integer; ACollection: IJclIntfCollection): Boolean;
var
It: IJclIntfIterator;
Size: Integer;
begin
Result := False;
if (Index < 0) or (Index >= FCount) then
raise EJclOutOfBoundsError.CreateRes(@RsEOutOfBounds);
if ACollection = nil then
Exit;
Size := ACollection.Size;
System.Move(FItems[Index], FItems[Index + Size], Size * SizeOf(IInterface));
It := ACollection.First;
while It.HasNext do
begin
FItems[Index] := It.Next;
Inc(Index);
end;
Result := True;
end;
function TJclIntfVector.AddAll(ACollection: IJclIntfCollection): Boolean;
var
It: IJclIntfIterator;
begin
Result := False;
if ACollection = nil then
Exit;
It := ACollection.First;
while It.HasNext do
Add(It.Next);
Result := True;
end;
procedure TJclIntfVector.Clear;
var
I: Integer;
begin
for I := 0 to FCount - 1 do
FItems[I] := nil;
FCount := 0;
end;
function TJclIntfVector.Clone: IInterface;
var
NewList: IJclIntfList;
begin
NewList := TJclIntfVector.Create(FCapacity);
NewList.AddAll(Self);
Result := NewList;
end;
function TJclIntfVector.Contains(AInterface: IInterface): Boolean;
var
I: Integer;
begin
Result := False;
if AInterface = nil then
Exit;
for I := 0 to FCount - 1 do
if Items[I] = AInterface then
begin
Result := True;
Break;
end;
end;
function TJclIntfVector.ContainsAll(ACollection: IJclIntfCollection): Boolean;
var
It: IJclIntfIterator;
begin
Result := True;
if ACollection = nil then
Exit;
It := ACollection.First;
while Result and It.HasNext do
Result := Contains(It.Next);
end;
function TJclIntfVector.Equals(ACollection: IJclIntfCollection): Boolean;
var
I: Integer;
It: IJclIntfIterator;
begin
Result := False;
if ACollection = nil then
Exit;
if FCount <> ACollection.Size then
Exit;
It := ACollection.First;
for I := 0 to FCount - 1 do
if Items[I] <> It.Next then
Exit;
Result := True;
end;
function TJclIntfVector.GetObject(Index: Integer): IInterface;
begin
if (Index < 0) or (Index >= FCount) then
begin
Result := nil;
Exit;
end;
Result := Items[Index];
end;
procedure TJclIntfVector.Grow;
begin
if FCapacity > 64 then
FCapacity := FCapacity + FCapacity div 4
else
if FCapacity = 0 then
FCapacity := 64
else
FCapacity := FCapacity * 4;
SetLength(FItems, FCapacity);
end;
function TJclIntfVector.IndexOf(AInterface: IInterface): Integer;
var
I: Integer;
begin
Result := -1;
if AInterface = nil then
Exit;
for I := 0 to FCount - 1 do
if Items[I] = AInterface then
begin
Result := I;
Break;
end;
end;
function TJclIntfVector.First: IJclIntfIterator;
begin
Result := TIntfItr.Create(Self);
end;
function TJclIntfVector.IsEmpty: Boolean;
begin
Result := FCount = 0;
end;
function TJclIntfVector.Last: IJclIntfIterator;
var
NewIterator: TIntfItr;
begin
NewIterator := TIntfItr.Create(Self);
NewIterator.FCursor := NewIterator.FOwnList.FCount;
NewIterator.FSize := NewIterator.FOwnList.FCount;
Result := NewIterator;
end;
function TJclIntfVector.LastIndexOf(AInterface: IInterface): Integer;
var
I: Integer;
begin
Result := -1;
if AInterface = nil then
Exit;
for I := FCount - 1 downto 0 do
if Items[I] = AInterface then
begin
Result := I;
Break;
end;
end;
function TJclIntfVector.Remove(Index: Integer): IInterface;
begin
if (Index < 0) or (Index >= FCount) then
raise EJclOutOfBoundsError.CreateRes(@RsEOutOfBounds);
Result := FItems[Index];
FItems[Index] := nil;
System.Move(FItems[Index + 1], FItems[Index],
(FCount - Index) * SizeOf(IInterface));
Dec(FCount);
end;
function TJclIntfVector.Remove(AInterface: IInterface): Boolean;
var
I: Integer;
begin
Result := False;
if AInterface = nil then
Exit;
for I := FCount - 1 downto 0 do
if FItems[I] = AInterface then // Removes all AInterface
begin
FItems[I] := nil; // Force Release
System.Move(FItems[I + 1], FItems[I], (FCount - I) * SizeOf(IInterface));
Dec(FCount);
Result := True;
end;
end;
function TJclIntfVector.RemoveAll(ACollection: IJclIntfCollection): Boolean;
var
It: IJclIntfIterator;
begin
Result := False;
if ACollection = nil then
Exit;
It := ACollection.First;
while It.HasNext do
Remove(It.Next);
end;
function TJclIntfVector.RetainAll(ACollection: IJclIntfCollection): Boolean;
var
I: Integer;
begin
Result := False;
if ACollection = nil then
Exit;
for I := FCount - 1 downto 0 do
if not ACollection.Contains(Items[I]) then
Remove(I);
end;
procedure TJclIntfVector.SetObject(Index: Integer; AInterface: IInterface);
begin
if (Index < 0) or (Index >= FCount) then
raise EJclOutOfBoundsError.CreateRes(@RsEOutOfBounds);
FItems[Index] := AInterface;
end;
function TJclIntfVector.Size: Integer;
begin
Result := FCount;
end;
function TJclIntfVector.SubList(First, Count: Integer): IJclIntfList;
var
I: Integer;
Last: Integer;
begin
Last := First + Count - 1;
if Last >= FCount then
Last := FCount - 1;
Result := TJclIntfVector.Create(Count);
for I := First to Last do
Result.Add(Items[I]);
end;
procedure TJclIntfVector.AfterConstruction;
begin
end;
procedure TJclIntfVector.BeforeDestruction;
begin
end;
//=== { TJclStrVector } ======================================================
constructor TJclStrVector.Create(ACapacity: Integer = DefaultContainerCapacity);
begin
inherited Create;
FCount := 0;
if ACapacity < 0 then
FCapacity := 0
else
FCapacity := ACapacity;
SetLength(FItems, FCapacity);
end;
destructor TJclStrVector.Destroy;
begin
Clear;
inherited Destroy;
end;
procedure TJclStrVector.Insert(Index: Integer; const AString: string);
begin
if (Index < 0) or (Index > FCount) then
raise EJclOutOfBoundsError.CreateRes(@RsEOutOfBounds);
System.Move(FItems[Index], FItems[Index - 1], (FCount - Index) * SizeOf(string));
FCapacity := Length(FItems);
FItems[Index] := AString;
Inc(FCount);
end;
function TJclStrVector.Add(const AString: string): Boolean;
begin
if FCount = FCapacity then
Grow;
FItems[FCount] := AString;
Inc(FCount);
Result := True;
end;
function TJclStrVector.AddAll(ACollection: IJclStrCollection): Boolean;
var
It: IJclStrIterator;
begin
Result := False;
if ACollection = nil then
Exit;
It := ACollection.First;
while It.HasNext do
Add(It.Next);
Result := True;
end;
function TJclStrVector.InsertAll(Index: Integer; ACollection: IJclStrCollection): Boolean;
var
It: IJclStrIterator;
Size: Integer;
begin
Result := False;
if (Index < 0) or (Index >= FCount) then
raise EJclOutOfBoundsError.CreateRes(@RsEOutOfBounds);
if ACollection = nil then
Exit;
Size := ACollection.Size;
System.Move(FItems[Index], FItems[Index + Size], Size * SizeOf(string));
It := ACollection.First;
while It.HasNext do
begin
FItems[Index] := It.Next;
Inc(Index);
end;
Result := True;
end;
procedure TJclStrVector.AfterConstruction;
begin
end;
procedure TJclStrVector.BeforeDestruction;
begin
end;
procedure TJclStrVector.Clear;
var
I: Integer;
begin
for I := 0 to FCount - 1 do
FItems[I] := '';
FCount := 0;
end;
function TJclStrVector.Clone: TObject;
var
NewList: TJclStrVector;
begin
NewList := TJclStrVector.Create(FCapacity);
NewList.AddAll(Self);
Result := NewList;
end;
function TJclStrVector.Contains(const AString: string): Boolean;
var
I: Integer;
begin
Result := False;
if AString = '' then
Exit;
for I := 0 to FCount - 1 do
if Items[I] = AString then
begin
Result := True;
Break;
end;
end;
function TJclStrVector.ContainsAll(ACollection: IJclStrCollection): Boolean;
var
It: IJclStrIterator;
begin
Result := True;
if ACollection = nil then
Exit;
It := ACollection.First;
while Result and It.HasNext do
Result := Contains(It.Next);
end;
function TJclStrVector.Equals(ACollection: IJclStrCollection): Boolean;
var
I: Integer;
It: IJclStrIterator;
begin
Result := False;
if ACollection = nil then
Exit;
if FCount <> ACollection.Size then
Exit;
It := ACollection.First;
for I := 0 to FCount - 1 do
if Items[I] <> It.Next then
Exit;
Result := True;
end;
function TJclStrVector.First: IJclStrIterator;
begin
Result := TStrItr.Create(Self);
end;
function TJclStrVector.GetString(Index: Integer): string;
begin
if (Index < 0) or (Index >= FCount) then
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -