The implementation is basically the same as the Classes.TList implementation, just changed from Pointer to TMethod as data carrier.Originally Posted by chronozphere
Anyhow this is how it looks
[code=pascal]
constructor TPHXMethodList.Create;
begin
end;
destructor TPHXMethodList.Destroy;
begin
SetCapacity(0);
inherited Destroy;
end;
procedure TPHXMethodList.Grow;
var Delta: Integer;
begin
if FCapacity > 64 then
Delta := FCapacity div 4
else
if FCapacity > 8 then
Delta := 16
else
Delta := 4;
SetCapacity(Count + Delta);
end;
procedure TPHXMethodList.SetCapacity(const Value: Integer);
begin
if FCapacity <> Value then
begin
FCapacity := Value;
ReallocMem(FList, FCapacity * SizeOf(TMethod));
end;
end;
procedure TPHXMethodList.SetCount(const Value: Integer);
begin
FCount := Value;
if(FCount > FCapacity) then Grow;
end;
procedure TPHXMethodList.Clear;
begin
FCount:= 0;
end;
procedure TPHXMethodList.Add(const AMethod: TMethod);
begin
SetCount(Count + 1);
FList^[Count - 1].Data:= AMethod.Data;
FList^[Count - 1].Code:= AMethod.Code;
end;
procedure TPHXMethodList.Remove(const AMethod: TMethod);
var Index: integer;
begin
Index:= IndexOf(AMethod);
// The method isnt found, dont do anything more
if Index < 0 then Exit;
if Index < Count then
begin
System.Move(FList^[Index + 1], FList^[Index], (FCount - Index) * SizeOf(TMethod));
end;
Dec(FCount);
end;
function TPHXMethodList.IndexOf(const AMethod: TMethod): Integer;
var Index: integer;
begin
For Index:=0 to Count-1 do
begin
if (FList^[Index].Data = AMethod.Data) and (FList^[Index].Code = AMethod.Code) then
begin
Result:= Index;
Exit;
end;
end;
Result:= -1;
end;
function TPHXMethodList.GetItem(Index: integer): TMethod;
begin
Result:= FList^[Index];
end;
procedure TPHXMethodList.SetItem(Index: Integer; const AValue: TMethod);
begin
FList^[Index]:= AValue;
end;
[/code]
Bookmarks