Skip to content

Commit

Permalink
TScriptDynamicStringArray removed and TScriptDynamicNativeStringArray…
Browse files Browse the repository at this point in the history
… now in use
  • Loading branch information
EricGrange committed Feb 26, 2021
1 parent dcb7393 commit 3dba39b
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 130 deletions.
4 changes: 1 addition & 3 deletions Source/dwsArrayIndexOfExprs.pas
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,12 @@ function TDynamicArrayIndexOfFuncPtrExpr.DoEval(exec : TdwsExecution; const base
var
fromIndex : Integer;
v : Variant;
dyn : TScriptDynamicValueArray;
begin
if FFromIndexExpr<>nil then
fromIndex:=FFromIndexExpr.EvalAsInteger(exec)
else fromIndex:=0;
FItemExpr.EvalAsVariant(exec, v);
dyn := (base.GetSelf as TScriptDynamicValueArray);
Result := dyn.IndexOfFuncPtr(v, fromIndex)
Result := base.IndexOfFuncPtr(v, fromIndex)
end;

// ------------------
Expand Down
176 changes: 79 additions & 97 deletions Source/dwsDynamicArrays.pas
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ TScriptDynamicArray = class abstract (TDataContext, IScriptDynArray)//(TInter
function IndexOfInteger(item : Int64; fromIndex : Integer) : Integer;
function IndexOfFloat(item : Double; fromIndex : Integer) : Integer;
function IndexOfString(const item : String; fromIndex : Integer) : Integer;
function IndexOfFuncPtr(const item : Variant; fromIndex : Integer) : Integer;

procedure Copy(src : TScriptDynamicArray; index, count : Integer);
procedure Concat(const src : IScriptDynArray; index, size : Integer);
Expand Down Expand Up @@ -116,8 +117,6 @@ TScriptDynamicValueArray = class (TScriptDynamicArray)

procedure Swap(i1, i2 : Integer); override;

function IndexOfFuncPtr(const item : Variant; fromIndex : Integer) : Integer;

function CompareString(i1, i2 : Integer) : Integer;
function CompareInteger(i1, i2 : Integer) : Integer;
function CompareFloat(i1, i2 : Integer) : Integer;
Expand All @@ -138,6 +137,8 @@ TScriptDynamicNativeArray = class abstract (TInterfacedSelfObject)
public
constructor Create(elemTyp : TTypeSymbol);

function ToString : String; override;

function BoundsCheckPassed(index : Integer) : Boolean;

property ElementTyp : TTypeSymbol read FElementTyp;
Expand All @@ -164,6 +165,7 @@ TScriptDynamicNativeIntegerArray = class (TScriptDynamicNativeArray, IScriptD
function IndexOfInteger(item : Int64; fromIndex : Integer) : Integer;
function IndexOfFloat(item : Double; fromIndex : Integer) : Integer;
function IndexOfString(const item : String; fromIndex : Integer) : Integer;
function IndexOfFuncPtr(const item : Variant; fromIndex : Integer) : Integer;

procedure WriteData(const src : TData; srcAddr, size : Integer);
procedure ReplaceData(const v : TData);
Expand Down Expand Up @@ -223,6 +225,7 @@ TScriptDynamicNativeFloatArray = class (TScriptDynamicNativeArray, IScriptDyn
function IndexOfInteger(item : Int64; fromIndex : Integer) : Integer;
function IndexOfFloat(item : Double; fromIndex : Integer) : Integer;
function IndexOfString(const item : String; fromIndex : Integer) : Integer;
function IndexOfFuncPtr(const item : Variant; fromIndex : Integer) : Integer;

procedure WriteData(const src : TData; srcAddr, size : Integer);
procedure ReplaceData(const v : TData);
Expand Down Expand Up @@ -282,6 +285,7 @@ TScriptDynamicNativeStringArray = class (TScriptDynamicNativeArray, IScriptDy
function IndexOfInteger(item : Int64; fromIndex : Integer) : Integer;
function IndexOfFloat(item : Double; fromIndex : Integer) : Integer;
function IndexOfString(const item : String; fromIndex : Integer) : Integer;
function IndexOfFuncPtr(const item : Variant; fromIndex : Integer) : Integer;

procedure WriteData(const src : TData; srcAddr, size : Integer);
procedure ReplaceData(const v : TData);
Expand Down Expand Up @@ -321,14 +325,6 @@ TScriptDynamicNativeStringArray = class (TScriptDynamicNativeArray, IScriptDy
procedure WriteToJSON(writer : TdwsJSONWriter);
end;

TScriptDynamicStringArray = class (TScriptDynamicValueArray)
public
procedure Add(const s : String);
procedure AddStrings(sl : TStrings);
function VarType(addr : Integer) : TVarType; override;
procedure NaturalSort; override;
end;

TScriptDynamicBooleanArray = class (TScriptDynamicValueArray)
public
end;
Expand Down Expand Up @@ -389,7 +385,7 @@ function CreateNewDynamicArray(elemTyp : TTypeSymbol) : IScriptDynArray;
if size = 1 then begin
elemTypClass := elemTyp.UnAliasedType.ClassType;
if elemTypClass = TBaseStringSymbol then
Result := TScriptDynamicStringArray.Create(elemTyp)
Result := TScriptDynamicNativeStringArray.Create(elemTyp)
else if elemTypClass = TBaseFloatSymbol then
Result := TScriptDynamicNativeFloatArray.Create(elemTyp)
else if elemTypClass = TBaseIntegerSymbol then
Expand Down Expand Up @@ -665,6 +661,29 @@ function TScriptDynamicArray.IndexOfString(const item : String; fromIndex : Inte
Result := inherited IndexOfValue(item, fromIndex, FArrayLength-1);
end;

// IndexOfFuncPtr
//
function TScriptDynamicArray.IndexOfFuncPtr(const item : Variant; fromIndex : Integer) : Integer;
var
i : Integer;
itemFunc : IFuncPointer;
p : PVarData;
begin
itemFunc := IFuncPointer(IUnknown(item));
if itemFunc = nil then begin
for i := fromIndex to ArrayLength-1 do begin
p := PVarData(@DirectData[i]);
if (p.VType=varUnknown) and (p.VUnknown=nil) then
Exit(i);
end;
end else begin
for i:=fromIndex to ArrayLength-1 do
if itemFunc.SameFunc(DirectData[i]) then
Exit(i);
end;
Result:=-1;
end;

// Copy
//
procedure TScriptDynamicArray.Copy(src : TScriptDynamicArray; index, count : Integer);
Expand Down Expand Up @@ -796,29 +815,6 @@ procedure TScriptDynamicValueArray.Swap(i1, i2 : Integer);
elem2^.VInt64:=buf.VInt64;
end;

// IndexOfFuncPtr
//
function TScriptDynamicValueArray.IndexOfFuncPtr(const item : Variant; fromIndex : Integer) : Integer;
var
i : Integer;
itemFunc : IFuncPointer;
p : PVarData;
begin
itemFunc := IFuncPointer(IUnknown(item));
if itemFunc = nil then begin
for i := fromIndex to ArrayLength-1 do begin
p := PVarData(@DirectData[i]);
if (p.VType=varUnknown) and (p.VUnknown=nil) then
Exit(i);
end;
end else begin
for i:=fromIndex to ArrayLength-1 do
if itemFunc.SameFunc(DirectData[i]) then
Exit(i);
end;
Result:=-1;
end;

// CompareString
//
function TScriptDynamicValueArray.CompareString(i1, i2 : Integer) : Integer;
Expand Down Expand Up @@ -872,49 +868,6 @@ function TScriptDynamicValueArray.CompareFloat(i1, i2 : Integer) : Integer;
else Result:=Ord(v1.VDouble>v2.VDouble);
end;

// ------------------
// ------------------ TScriptDynamicStringArray ------------------
// ------------------

// Add
//
procedure TScriptDynamicStringArray.Add(const s : String);
begin
ArrayLength:=ArrayLength+1;
if s<>'' then
AsString[ArrayLength-1]:=s;
end;

// AddStrings
//
procedure TScriptDynamicStringArray.AddStrings(sl : TStrings);
var
i, n : Integer;
begin
n := ArrayLength;
ArrayLength := n+sl.Count;
for i := 0 to sl.Count-1 do
AsString[n+i] := sl[i];
end;

// VarType
//
function TScriptDynamicStringArray.VarType(addr : Integer) : TVarType;
begin
Result := varUString;
end;

// NaturalSort
//
procedure TScriptDynamicStringArray.NaturalSort;
var
qs : TQuickSort;
begin
qs.CompareMethod := Self.CompareString;
qs.SwapMethod := Self.Swap;
qs.Sort(0, FArrayLength-1);
end;

// ------------------
// ------------------ TScriptDynamicDataArray ------------------
// ------------------
Expand Down Expand Up @@ -951,6 +904,13 @@ constructor TScriptDynamicNativeArray.Create(elemTyp : TTypeSymbol);
Assert(elemTyp.Size = 1);
end;

// ToString
//
function TScriptDynamicNativeArray.ToString : String;
begin
Result := 'array of ' + ElementTyp.Name;
end;

// BoundsCheckPassed
//
function TScriptDynamicNativeArray.BoundsCheckPassed(index : Integer) : Boolean;
Expand Down Expand Up @@ -1105,6 +1065,13 @@ function TScriptDynamicNativeIntegerArray.IndexOfString(const item : String; fro
Result := IndexOfInteger(StrToInt64(item), fromIndex);
end;

// IndexOfFuncPtr
//
function TScriptDynamicNativeIntegerArray.IndexOfFuncPtr(const item : Variant; fromIndex : Integer) : Integer;
begin
Result := -1;
end;

// WriteData
//
procedure TScriptDynamicNativeIntegerArray.WriteData(const src : TData; srcAddr, size : Integer);
Expand Down Expand Up @@ -1136,7 +1103,7 @@ procedure TScriptDynamicNativeIntegerArray.Concat(const src : IScriptDynArray; i
Assert(srcSelf.ClassType = TScriptDynamicNativeIntegerArray);
Assert(index >= 0);

srcDyn := TScriptDynamicNativeIntegerArray(src.GetSelf);
srcDyn := TScriptDynamicNativeIntegerArray(srcSelf);
if size > srcDyn.ArrayLength - index then
size := srcDyn.ArrayLength - index;
if size > 0 then begin
Expand Down Expand Up @@ -1454,6 +1421,13 @@ function TScriptDynamicNativeFloatArray.IndexOfString(const item : String; fromI
Result := IndexOfFloat(StrToFloat(item), fromIndex);
end;

// IndexOfFuncPtr
//
function TScriptDynamicNativeFloatArray.IndexOfFuncPtr(const item : Variant; fromIndex : Integer) : Integer;
begin
Result := -1;
end;

// WriteData
//
procedure TScriptDynamicNativeFloatArray.WriteData(const src : TData; srcAddr, size : Integer);
Expand All @@ -1478,14 +1452,14 @@ procedure TScriptDynamicNativeFloatArray.ReplaceData(const v : TData);
procedure TScriptDynamicNativeFloatArray.Concat(const src : IScriptDynArray; index, size : Integer);
var
srcSelf : TObject;
srcDyn : TScriptDynamicNativeIntegerArray;
srcDyn : TScriptDynamicNativeFloatArray;
n : Integer;
begin
srcSelf := src.GetSelf;
Assert(srcSelf.ClassType = TScriptDynamicNativeIntegerArray);
Assert(srcSelf.ClassType = TScriptDynamicNativeFloatArray);
Assert(index >= 0);

srcDyn := TScriptDynamicNativeIntegerArray(src.GetSelf);
srcDyn := TScriptDynamicNativeFloatArray(srcSelf);
if size > srcDyn.ArrayLength - index then
size := srcDyn.ArrayLength - index;
if size > 0 then begin
Expand Down Expand Up @@ -1746,8 +1720,8 @@ procedure TScriptDynamicNativeStringArray.MoveItem(source, destination : Integer

buf := PPointer(@FData[source])^;
if source < destination then
System.Move(FData[source+1], FData[source], SizeOf(Double)*(destination-source))
else System.Move(FData[destination], FData[destination+1], SizeOf(Double)*(source-destination));
System.Move(FData[source+1], FData[source], SizeOf(Pointer)*(destination-source))
else System.Move(FData[destination], FData[destination+1], SizeOf(Pointer)*(source-destination));
PPointer(@FData[destination])^ := buf;
end;

Expand All @@ -1757,9 +1731,9 @@ procedure TScriptDynamicNativeStringArray.Swap(index1, index2 : Integer);
var
buf : Pointer;
begin
buf := PPointer(FData[index1])^;
PPointer(FData[index1])^ := PPointer(FData[index2])^;
PPointer(FData[index2])^ := buf;
buf := PPointer(@FData[index1])^;
PPointer(@FData[index1])^ := PPointer(@FData[index2])^;
PPointer(@FData[index2])^ := buf;
end;

// IndexOfValue
Expand Down Expand Up @@ -1798,6 +1772,13 @@ function TScriptDynamicNativeStringArray.IndexOfString(const item : String; from
Result := -1;
end;

// IndexOfFuncPtr
//
function TScriptDynamicNativeStringArray.IndexOfFuncPtr(const item : Variant; fromIndex : Integer) : Integer;
begin
Result := -1;
end;

// WriteData
//
procedure TScriptDynamicNativeStringArray.WriteData(const src : TData; srcAddr, size : Integer);
Expand All @@ -1822,30 +1803,31 @@ procedure TScriptDynamicNativeStringArray.ReplaceData(const v : TData);
procedure TScriptDynamicNativeStringArray.Concat(const src : IScriptDynArray; index, size : Integer);
var
srcSelf : TObject;
srcDyn : TScriptDynamicNativeIntegerArray;
n : Integer;
srcDyn : TScriptDynamicNativeStringArray;
n, i : Integer;
begin
srcSelf := src.GetSelf;
Assert(srcSelf.ClassType = TScriptDynamicNativeIntegerArray);
Assert(srcSelf.ClassType = TScriptDynamicNativeStringArray);
Assert(index >= 0);

srcDyn := TScriptDynamicNativeIntegerArray(src.GetSelf);
srcDyn := TScriptDynamicNativeStringArray(srcSelf);
if size > srcDyn.ArrayLength - index then
size := srcDyn.ArrayLength - index;
if size > 0 then begin
n := FArrayLength;
FArrayLength := n + size;
SetLength(FData, FArrayLength);
System.Move(srcDyn.FData[index], FData[n], size*SizeOf(Double));
for i := 0 to size-1 do
FData[n + i] := srcDyn.FData[index + i];
end;
end;

// Reverse
//
procedure TScriptDynamicNativeStringArray.Reverse;
var
pLow, pHigh : PDouble;
t : Double;
pLow, pHigh : PPointer;
t : Pointer;
begin
if FArrayLength <= 1 then Exit;

Expand Down Expand Up @@ -1885,7 +1867,8 @@ procedure TScriptDynamicNativeStringArray.AddStrings(sl : TStrings);
i, n : Integer;
begin
n := FArrayLength;
SetLength(FData, n + sl.Count);
FArrayLength := n + sl.Count;
SetLength(FData, FArrayLength);
for i := 0 to sl.Count-1 do
FData[i+n] := sl[i];
end;
Expand All @@ -1894,9 +1877,8 @@ procedure TScriptDynamicNativeStringArray.AddStrings(sl : TStrings);
//
function TScriptDynamicNativeStringArray.AsPDouble(var nbElements, stride : Integer) : PDouble;
begin
Result := Pointer(FData);
nbElements := FArrayLength;
stride := SizeOf(Double);
Assert(False);
Result := nil;
end;

// GetAsFloat
Expand Down
6 changes: 3 additions & 3 deletions Source/dwsFileFunctions.pas
Original file line number Diff line number Diff line change
Expand Up @@ -824,13 +824,13 @@ procedure TEnumerateDirFunc.DoEvalAsVariant(const args : TExprBaseListExec; var
procedure TEnumerateSubDirsFunc.DoEvalAsVariant(const args : TExprBaseListExec; var result : Variant);
var
sl : TStringList;
newArray : TScriptDynamicStringArray;
newArray : IScriptDynArray;
begin
sl := TStringList.Create;
try
CollectSubDirs(args.AsFileName[0], sl);
newArray := CreateNewDynamicArray((args.Exec as TdwsProgramExecution).CompilerContext.TypString) as TScriptDynamicStringArray;
Result := IScriptDynArray(newArray);
newArray := CreateNewDynamicArray((args.Exec as TdwsProgramExecution).CompilerContext.TypString);
Result := newArray;
newArray.AddStrings(sl);
finally
sl.Free;
Expand Down
Loading

0 comments on commit 3dba39b

Please sign in to comment.