mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-12-06 15:47:41 +01:00
* Patch from Denis Volodarsky to add enumerator to vector (bug ID #22689)
git-svn-id: trunk@23341 -
This commit is contained in:
parent
aeba9af003
commit
7f10df3daa
@ -17,33 +17,57 @@ unit gvector;
|
|||||||
interface
|
interface
|
||||||
|
|
||||||
type
|
type
|
||||||
generic TVector<T>=class
|
|
||||||
|
{ TVector }
|
||||||
|
|
||||||
|
generic TVector<T> = class
|
||||||
private
|
private
|
||||||
type
|
type
|
||||||
PT=^ T;
|
PT = ^ T;
|
||||||
TArr=array of T;
|
TArr = array of T;
|
||||||
var
|
var
|
||||||
FCapacity:SizeUInt;
|
FCapacity:SizeUInt;
|
||||||
FDataSize:SizeUInt;
|
FDataSize:SizeUInt;
|
||||||
FData:TArr;
|
FData:TArr;
|
||||||
|
|
||||||
procedure SetValue(Position:SizeUInt; Value:T);inline;
|
procedure SetValue(Position: SizeUInt; const Value: T); inline;
|
||||||
function GetValue(Position:SizeUInt):T;inline;
|
function GetValue(Position: SizeUInt):T; inline;
|
||||||
function GetMutable(Position:SizeUInt):PT;inline;
|
function GetMutable(Position: SizeUInt):PT; inline;
|
||||||
procedure IncreaseCapacity;inline;
|
procedure IncreaseCapacity; inline;
|
||||||
|
|
||||||
|
const
|
||||||
|
// todo: move these constants to implementation when
|
||||||
|
// mantis #0021310 will be fixed.
|
||||||
|
SVectorPositionOutOfRange = 'Vector position out of range';
|
||||||
|
SAccessingElementOfEmptyVector = 'Accessing element of empty vector';
|
||||||
|
|
||||||
|
type
|
||||||
|
TVectorEnumerator = class
|
||||||
|
private
|
||||||
|
FVector: TVector;
|
||||||
|
FPosition: Integer;
|
||||||
|
public
|
||||||
|
constructor Create(AVector: TVector);
|
||||||
|
function GetCurrent: T; inline;
|
||||||
|
function MoveNext: Boolean; inline;
|
||||||
|
property Current: T read GetCurrent;
|
||||||
|
end;
|
||||||
|
|
||||||
public
|
public
|
||||||
constructor Create;
|
constructor Create;
|
||||||
function Size:SizeUInt;inline;
|
function Size: SizeUInt; inline;
|
||||||
procedure PushBack(Value:T);inline;
|
procedure PushBack(const Value: T); inline;
|
||||||
procedure PopBack;inline;
|
procedure PopBack; inline;
|
||||||
function IsEmpty:boolean;inline;
|
function IsEmpty: boolean; inline;
|
||||||
procedure Insert(Position:SizeUInt; Value:T);inline;
|
procedure Insert(Position: SizeUInt; const Value: T); inline;
|
||||||
procedure Erase(Position:SizeUInt);inline;
|
procedure Erase(Position: SizeUInt); inline;
|
||||||
procedure Clear;inline;
|
procedure Clear; inline;
|
||||||
function Front:T;inline;
|
function Front: T; inline;
|
||||||
function Back:T;inline;
|
function Back: T; inline;
|
||||||
procedure Reserve(Num:SizeUInt);inline;
|
procedure Reserve(Num: SizeUInt); inline;
|
||||||
procedure Resize(Num:SizeUInt);inline;
|
procedure Resize(Num: SizeUInt); inline;
|
||||||
|
|
||||||
|
function GetEnumerator: TVectorEnumerator;
|
||||||
|
|
||||||
property Items[i : SizeUInt]: T read getValue write setValue; default;
|
property Items[i : SizeUInt]: T read getValue write setValue; default;
|
||||||
property Mutable[i : SizeUInt]: PT read getMutable;
|
property Mutable[i : SizeUInt]: PT read getMutable;
|
||||||
@ -51,39 +75,61 @@ end;
|
|||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
|
{ TVector.TVectorEnumerator }
|
||||||
|
|
||||||
|
constructor TVector.TVectorEnumerator.Create(AVector: TVector);
|
||||||
|
begin
|
||||||
|
FVector := AVector;
|
||||||
|
FPosition := -1;
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TVector.TVectorEnumerator.GetCurrent: T;
|
||||||
|
begin
|
||||||
|
Result := FVector[FPosition];
|
||||||
|
end;
|
||||||
|
|
||||||
|
function TVector.TVectorEnumerator.MoveNext: Boolean;
|
||||||
|
begin
|
||||||
|
Result := FPosition < FVector.Size - 1;
|
||||||
|
if Result then
|
||||||
|
inc(FPosition);
|
||||||
|
end;
|
||||||
|
|
||||||
|
{ TVector }
|
||||||
|
|
||||||
constructor TVector.Create();
|
constructor TVector.Create();
|
||||||
begin
|
begin
|
||||||
FCapacity:=0;
|
FCapacity:=0;
|
||||||
FDataSize:=0;
|
FDataSize:=0;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TVector.SetValue(Position:SizeUInt; Value:T);inline;
|
procedure TVector.SetValue(Position: SizeUInt; const Value: T);
|
||||||
begin
|
begin
|
||||||
Assert(position < size, 'Vector position out of range');
|
Assert(position < size, SVectorPositionOutOfRange);
|
||||||
FData[Position]:=Value;
|
FData[Position]:=Value;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TVector.GetValue(Position:SizeUInt):T;inline;
|
function TVector.GetValue(Position:SizeUInt):T;inline;
|
||||||
begin
|
begin
|
||||||
Assert(position < size, 'Vector position out of range');
|
Assert(position < size, SVectorPositionOutOfRange);
|
||||||
GetValue:=FData[Position];
|
GetValue:=FData[Position];
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TVector.GetMutable(Position:SizeUInt):PT;inline;
|
function TVector.GetMutable(Position:SizeUInt):PT;inline;
|
||||||
begin
|
begin
|
||||||
Assert(position < size, 'Vector position out of range');
|
Assert(position < size, SVectorPositionOutOfRange);
|
||||||
GetMutable:=@FData[Position];
|
GetMutable:=@FData[Position];
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TVector.Front():T;inline;
|
function TVector.Front():T;inline;
|
||||||
begin
|
begin
|
||||||
Assert(size > 0, 'Accessing element of empty vector');
|
Assert(size > 0, SAccessingElementOfEmptyVector);
|
||||||
Front:=FData[0];
|
Front:=FData[0];
|
||||||
end;
|
end;
|
||||||
|
|
||||||
function TVector.Back():T;inline;
|
function TVector.Back():T;inline;
|
||||||
begin
|
begin
|
||||||
Assert(size > 0, 'Accessing element of empty vector');
|
Assert(size > 0, SAccessingElementOfEmptyVector);
|
||||||
Back:=FData[FDataSize-1];
|
Back:=FData[FDataSize-1];
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@ -100,7 +146,7 @@ begin
|
|||||||
IsEmpty:=false;
|
IsEmpty:=false;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TVector.PushBack(Value:T);inline;
|
procedure TVector.PushBack(const Value: T);
|
||||||
begin
|
begin
|
||||||
if FDataSize=FCapacity then
|
if FDataSize=FCapacity then
|
||||||
IncreaseCapacity;
|
IncreaseCapacity;
|
||||||
@ -117,13 +163,18 @@ begin
|
|||||||
SetLength(FData, FCapacity);
|
SetLength(FData, FCapacity);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
function TVector.GetEnumerator: TVectorEnumerator;
|
||||||
|
begin
|
||||||
|
Result := TVectorEnumerator.Create(self);
|
||||||
|
end;
|
||||||
|
|
||||||
procedure TVector.PopBack();inline;
|
procedure TVector.PopBack();inline;
|
||||||
begin
|
begin
|
||||||
if FDataSize>0 then
|
if FDataSize>0 then
|
||||||
FDataSize:=FDataSize-1;
|
FDataSize:=FDataSize-1;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure TVector.Insert(Position:SizeUInt; Value: T);inline;
|
procedure TVector.Insert(Position: SizeUInt; const Value: T);
|
||||||
var i:SizeUInt;
|
var i:SizeUInt;
|
||||||
begin
|
begin
|
||||||
pushBack(Value);
|
pushBack(Value);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user