📄 jclvectors.pas
字号:
begin
Result := '';
Exit;
end;
Result := FItems[Index];
end;
procedure TJclStrVector.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 TJclStrVector.IndexOf(const AString: string): Integer;
var
I: Integer;
begin
Result := -1;
if AString = '' then
Exit;
for I := 0 to FCount - 1 do
if Items[I] = AString then
begin
Result := I;
Exit;
end;
end;
function TJclStrVector.IsEmpty: Boolean;
begin
Result := FCount = 0;
end;
function TJclStrVector.Last: IJclStrIterator;
var
NewIterator: TStrItr;
begin
NewIterator := TStrItr.Create(Self);
NewIterator.FCursor := NewIterator.FOwnList.FCount;
NewIterator.FSize := NewIterator.FOwnList.FCount;
Result := NewIterator;
end;
function TJclStrVector.LastIndexOf(const AString: string): Integer;
var
I: Integer;
begin
Result := -1;
if AString = '' then
Exit;
for I := FCount - 1 downto 0 do
if Items[I] = AString then
begin
Result := I;
Break;
end;
end;
function TJclStrVector.Remove(const AString: string): Boolean;
var
I: Integer;
begin
Result := False;
if AString = '' then
Exit;
for I := FCount - 1 downto 0 do
if FItems[I] = AString then // Removes all AString
begin
FItems[I] := ''; // Force Release
System.Move(FItems[I + 1], FItems[I], (FCount - I) * SizeOf(string));
Dec(FCount);
Result := True;
end;
end;
function TJclStrVector.Remove(Index: Integer): string;
begin
if (Index < 0) or (Index >= FCount) then
raise EJclOutOfBoundsError.CreateRes(@RsEOutOfBounds);
Result := FItems[Index];
FItems[Index] := '';
System.Move(FItems[Index + 1], FItems[Index], (FCount - Index) * SizeOf(string));
Dec(FCount);
end;
function TJclStrVector.RemoveAll(ACollection: IJclStrCollection): Boolean;
var
It: IJclStrIterator;
begin
Result := False;
if ACollection = nil then
Exit;
It := ACollection.First;
while It.HasNext do
Remove(It.Next);
end;
function TJclStrVector.RetainAll(ACollection: IJclStrCollection): 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 TJclStrVector.SetString(Index: Integer; const AString: string);
begin
if (Index < 0) or (Index >= FCount) then
raise EJclOutOfBoundsError.CreateRes(@RsEOutOfBounds);
FItems[Index] := AString;
end;
function TJclStrVector.Size: Integer;
begin
Result := FCount;
end;
function TJclStrVector.SubList(First, Count: Integer): IJclStrList;
var
I: Integer;
Last: Integer;
begin
Last := First + Count - 1;
if Last >= FCount then
Last := FCount - 1;
Result := TJclStrVector.Create(Count);
for I := First to Last do
Result.Add(Items[I]);
end;
//=== { TJclVector } =========================================================
constructor TJclVector.Create(ACapacity: Integer = DefaultContainerCapacity;
AOwnsObjects: Boolean = True);
begin
inherited Create;
FCount := 0;
FOwnsObjects := AOwnsObjects;
if ACapacity < 0 then
FCapacity := 0
else
FCapacity := ACapacity;
SetLength(FItems, FCapacity);
end;
destructor TJclVector.Destroy;
begin
Clear;
inherited Destroy;
end;
procedure TJclVector.Insert(Index: Integer; AObject: TObject);
begin
if (Index < 0) or (Index > FCount) then
raise EJclOutOfBoundsError.CreateRes(@RsEOutOfBounds);
System.Move(FItems[Index], FItems[Index - 1],
(FCount - Index) * SizeOf(TObject));
FCapacity := Length(FItems);
FItems[Index] := AObject;
Inc(FCount);
end;
function TJclVector.Add(AObject: TObject): Boolean;
begin
if FCount = FCapacity then
Grow;
FItems[FCount] := AObject;
Inc(FCount);
Result := True;
end;
function TJclVector.InsertAll(Index: Integer; ACollection: IJclCollection): Boolean;
var
It: IJclIterator;
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 TJclVector.AddAll(ACollection: IJclCollection): Boolean;
var
It: IJclIterator;
begin
Result := False;
if ACollection = nil then
Exit;
It := ACollection.First;
while It.HasNext do
Add(It.Next);
Result := True;
end;
procedure TJclVector.Clear;
var
I: Integer;
begin
for I := 0 to FCount - 1 do
FreeObject(FItems[I]);
FCount := 0;
end;
function TJclVector.Clone: TObject;
var
NewList: TJclVector;
begin
NewList := TJclVector.Create(FCapacity, False); // Only one can have FOwnsObject = True
NewList.AddAll(Self);
Result := NewList;
end;
function TJclVector.Contains(AObject: TObject): Boolean;
var
I: Integer;
begin
Result := False;
if AObject = nil then
Exit;
for I := 0 to FCount - 1 do
if Items[I] = AObject then
begin
Result := True;
Break;
end;
end;
function TJclVector.ContainsAll(ACollection: IJclCollection): Boolean;
var
It: IJclIterator;
begin
Result := True;
if ACollection = nil then
Exit;
It := ACollection.First;
while Result and It.HasNext do
Result := Contains(It.Next);
end;
function TJclVector.Equals(ACollection: IJclCollection): Boolean;
var
I: Integer;
It: IJclIterator;
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;
procedure TJclVector.FreeObject(var AObject: TObject);
begin
if FOwnsObjects then
begin
AObject.Free;
AObject := nil;
end;
end;
function TJclVector.GetObject(Index: Integer): TObject;
begin
if (Index < 0) or (Index >= FCount) then
begin
Result := nil;
Exit;
end;
Result := Items[Index];
end;
procedure TJclVector.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 TJclVector.IndexOf(AObject: TObject): Integer;
var
I: Integer;
begin
Result := -1;
if AObject = nil then
Exit;
for I := 0 to FCount - 1 do
if Items[I] = AObject then
begin
Result := I;
Break;
end;
end;
function TJclVector.First: IJclIterator;
begin
Result := TItr.Create(Self);
end;
function TJclVector.IsEmpty: Boolean;
begin
Result := FCount = 0;
end;
function TJclVector.Last: IJclIterator;
var
NewIterator: TItr;
begin
NewIterator := TItr.Create(Self);
NewIterator.FCursor := NewIterator.FOwnList.FCount;
NewIterator.FSize := NewIterator.FOwnList.FCount;
Result := NewIterator;
end;
function TJclVector.LastIndexOf(AObject: TObject): Integer;
var
I: Integer;
begin
Result := -1;
if AObject = nil then
Exit;
for I := FCount - 1 downto 0 do
if Items[I] = AObject then
begin
Result := I;
Break;
end;
end;
function TJclVector.Remove(AObject: TObject): Boolean;
var
I: Integer;
begin
Result := False;
if AObject = nil then
Exit;
for I := FCount - 1 downto 0 do
if FItems[I] = AObject then // Removes all AObject
begin
FreeObject(FItems[I]);
System.Move(FItems[I + 1], FItems[I], (FCount - I) * SizeOf(TObject));
Dec(FCount);
Result := True;
end;
end;
function TJclVector.Remove(Index: Integer): TObject;
begin
if (Index < 0) or (Index >= FCount) then
raise EJclOutOfBoundsError.CreateRes(@RsEOutOfBounds);
Result := FItems[Index];
FreeObject(FItems[Index]);
System.Move(FItems[Index + 1], FItems[Index], (FCount - Index) * SizeOf(TObject));
Dec(FCount);
end;
function TJclVector.RemoveAll(ACollection: IJclCollection): Boolean;
var
It: IJclIterator;
begin
Result := False;
if ACollection = nil then
Exit;
It := ACollection.First;
while It.HasNext do
Remove(It.Next);
end;
function TJclVector.RetainAll(ACollection: IJclCollection): Boolean;
var
I: Integer;
begin
Result := False;
if ACollection = nil then
Exit;
for I := FCount - 1 to 0 do
if not ACollection.Contains(Items[I]) then
Remove(I);
end;
procedure TJclVector.SetObject(Index: Integer; AObject: TObject);
begin
if (Index < 0) or (Index >= FCount) then
raise EJclOutOfBoundsError.CreateRes(@RsEOutOfBounds);
FItems[Index] := AObject;
end;
function TJclVector.Size: Integer;
begin
Result := FCount;
end;
function TJclVector.SubList(First, Count: Integer): IJclList;
var
I: Integer;
Last: Integer;
begin
Last := First + Count - 1;
if Last >= FCount then
Last := FCount - 1;
Result := TJclVector.Create(Count, FOwnsObjects);
for I := First to Last do
Result.Add(Items[I]);
end;
procedure TJclVector.AfterConstruction;
begin
end;
procedure TJclVector.BeforeDestruction;
begin
end;
// History:
// $Log: JclVectors.pas,v $
// Revision 1.8 2005/03/08 08:33:18 marquardt
// overhaul of exceptions and resourcestrings, minor style cleaning
//
// Revision 1.7 2005/03/03 08:02:57 marquardt
// various style cleanings, bugfixes and improvements
//
// Revision 1.6 2005/03/02 17:51:24 rrossmair
// - removed DCLAppendDelimited from JclAlgorithms, changed uses clauses accordingly
//
// Revision 1.5 2005/03/02 09:59:30 dade2004
// Added
// -TJclStrCollection in JclContainerIntf
// Every common methods for IJclStrCollection are implemented here
//
// -Every class that implement IJclStrCollection now derive from TJclStrCollection instead of TJclAbstractContainer
// -Every abstract method in TJclStrCollection has been marked as "override" in descendent classes
//
// DCLAppendDelimited has been removed from JclAlgorothms, his body has been fixed for a bug and put into
// relative method in TJclStrCollection
//
// Revision 1.4 2005/02/27 11:36:20 marquardt
// fixed and secured Capacity/Grow mechanism, raise exceptions with efficient CreateResRec
//
// Revision 1.3 2005/02/27 07:27:47 marquardt
// changed interface names from I to IJcl, moved resourcestrings to JclResource.pas
//
// Revision 1.2 2005/02/24 07:36:24 marquardt
// resolved the compiler warnings, style cleanup, removed code from JclContainerIntf.pas
//
// Revision 1.1 2005/02/24 03:57:10 rrossmair
// - donated DCL code, initial check-in
//
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -