Skip to content
Permalink
Browse files

new IncludeInteger/IncludeInt64 functions

- with associated tests
  • Loading branch information...
Arnaud Bouchez
Arnaud Bouchez committed Apr 11, 2019
1 parent 15ed6a0 commit fd822ef3ad362529252ccb397e2cdd82979631e0
Showing with 138 additions and 3 deletions.
  1. +76 −2 SynCommons.pas
  2. +61 −0 SynSelfTests.pas
  3. +1 −1 SynopseCommit.inc
@@ -4310,6 +4310,12 @@ procedure DeleteInteger(var Values: TIntegerDynArray; var ValuesCount: Integer;
procedure ExcludeInteger(var Values, Excluded: TIntegerDynArray;
ExcludedSortSize: Integer=32);

/// ensure some 32-bit integer from Values[] will only contain Included[]
// - Included is declared as var, since it will be sorted in-place during process
// if it contains more than IncludedSortSize items (i.e. if the sort is worth it)
procedure IncludeInteger(var Values, Included: TIntegerDynArray;
IncludedSortSize: Integer=32);

/// sort and remove any 32-bit duplicated integer from Values[]
procedure DeduplicateInteger(var Values: TIntegerDynArray); overload;

@@ -4338,6 +4344,12 @@ procedure DeleteInt64(var Values: TInt64DynArray; var ValuesCount: Integer; Inde
procedure ExcludeInt64(var Values, Excluded: TInt64DynArray;
ExcludedSortSize: Integer=32);

/// ensure some 64-bit integer from Values[] will only contain Included[]
// - Included is declared as var, since it will be sorted in-place during process
// if it contains more than IncludedSortSize items (i.e. if the sort is worth it)
procedure IncludeInt64(var Values, Included: TInt64DynArray;
IncludedSortSize: Integer=32);

/// sort and remove any 64-bit duplicated integer from Values[]
procedure DeduplicateInt64(var Values: TInt64DynArray); overload;

end;

procedure ExcludeInteger(var Values, Excluded: TIntegerDynArray; ExcludedSortSize: integer);
var i,v,x,n: integer;
var i,v,x,n: PtrInt;
begin
if (Values=nil) or (Excluded=nil) then
exit; // nothing to exclude
SetLength(Values,n);
end;

procedure IncludeInteger(var Values, Included: TIntegerDynArray;
IncludedSortSize: Integer);
var i,v,x,n: PtrInt;
begin
if (Values=nil) or (Included=nil) then begin
Values := nil;
exit;
end;
v := length(Values);
n := 0;
x := Length(Included);
if (x>IncludedSortSize) or (v>IncludedSortSize) then begin // sort if worth it
dec(x);
QuickSortInteger(pointer(Included),0,x);
for i := 0 to v-1 do
if FastFindIntegerSorted(pointer(Included),x,Values[i])>=0 then begin
if n<>i then
Values[n] := Values[i];
inc(n);
end;
end else
for i := 0 to v-1 do
if IntegerScanExists(pointer(Included),x,Values[i]) then begin
if n<>i then
Values[n] := Values[i];
inc(n);
end;
if n<>v then
SetLength(Values,n);
end;

procedure ExcludeInt64(var Values, Excluded: TInt64DynArray; ExcludedSortSize: Integer);
var i,v,x,n: integer;
var i,v,x,n: PtrInt;
begin
if (Values=nil) or (Excluded=nil) then
exit; // nothing to exclude
SetLength(Values,n);
end;

procedure IncludeInt64(var Values, Included: TInt64DynArray;
IncludedSortSize: integer);
var i,v,x,n: PtrInt;
begin
if (Values=nil) or (Included=nil) then begin
Values := nil;
exit;
end;
v := length(Values);
n := 0;
x := Length(Included);
if (x>IncludedSortSize) or (v>IncludedSortSize) then begin // sort if worth it
dec(x);
QuickSortInt64(pointer(Included),0,x);
for i := 0 to v-1 do
if FastFindInt64Sorted(pointer(Included),x,Values[i])>=0 then begin
if n<>i then
Values[n] := Values[i];
inc(n);
end;
end else
for i := 0 to v-1 do
if Int64ScanExists(pointer(Included),x,Values[i]) then begin
if n<>i then
Values[n] := Values[i];
inc(n);
end;
if n<>v then
SetLength(Values,n);
end;

procedure DeduplicateInteger(var Values: TIntegerDynArray);
begin
DeduplicateInteger(Values, length(Values));
@@ -3562,6 +3562,63 @@ procedure TTestLowLevelCommon.Integers;
Check(fAdd = added, 'added');
Check(fDel = deleted, 'deleted');
end;
procedure includes(const values, includes, excludes, included, excluded: RawUTF8);
procedure includes32;
var v, i, e: TIntegerDynArray;
begin
CSVToIntegerDynArray(Pointer(values),v);
CSVToIntegerDynArray(Pointer(excludes),e);
ExcludeInteger(v, e, 32); // no sort
Check(IntegerDynArrayToCSV(v) = excluded);
v := nil;
e := nil;
CSVToIntegerDynArray(Pointer(values),v);
CSVToIntegerDynArray(Pointer(excludes),e);
ExcludeInteger(v, e, 2); // sort
Check(IntegerDynArrayToCSV(v) = excluded);
v := nil;
e := nil;
CSVToIntegerDynArray(Pointer(values),v);
CSVToIntegerDynArray(Pointer(includes),i);
IncludeInteger(v, i, 32); // no sort
Check(IntegerDynArrayToCSV(v) = included);
v := nil;
e := nil;
CSVToIntegerDynArray(Pointer(values),v);
CSVToIntegerDynArray(Pointer(includes),i);
IncludeInteger(v, i, 2); // sort
Check(IntegerDynArrayToCSV(v) = included);
end;
procedure includes64;
var v, i, e: TInt64DynArray;
begin
CSVToInt64DynArray(Pointer(values),v);
CSVToInt64DynArray(Pointer(excludes),e);
ExcludeInt64(v, e, 32); // no sort
Check(Int64DynArrayToCSV(v) = excluded);
v := nil;
e := nil;
CSVToInt64DynArray(Pointer(values),v);
CSVToInt64DynArray(Pointer(excludes),e);
ExcludeInt64(v, e, 2); // sort
Check(Int64DynArrayToCSV(v) = excluded);
v := nil;
e := nil;
CSVToInt64DynArray(Pointer(values),v);
CSVToInt64DynArray(Pointer(includes),i);
IncludeInt64(v, i, 32); // no sort
Check(Int64DynArrayToCSV(v) = included);
v := nil;
e := nil;
CSVToInt64DynArray(Pointer(values),v);
CSVToInt64DynArray(Pointer(includes),i);
IncludeInt64(v, i, 2); // sort
Check(Int64DynArrayToCSV(v) = included);
end;
begin
Includes32;
Includes64;
end;
var i32: TIntegerDynArray;
i64: TInt64DynArray;
i,n: integer;
@@ -3626,6 +3683,10 @@ procedure TTestLowLevelCommon.Integers;
changes('1,2,3,4','5,6','5,6','1,2,3,4');
changes('1,2,4','1,3,5,6','3,5,6','2,4');
changes('1,2,4','3,5,6','3,5,6','1,2,4');
includes('1,2,3', '2', '2', '2', '1,3');
includes('1,2,3', '2,3', '2,3', '2,3', '1');
includes('1,2,3', '1,2,3', '1,2,3', '1,2,3', '');
includes('1,2,3', '3,1,2', '3,1,2', '1,2,3', '');
check(i64=nil);
DeduplicateInt64(i64);
check(i64=nil);
@@ -1 +1 @@
'1.18.5162'
'1.18.5164'

0 comments on commit fd822ef

Please sign in to comment.
You can’t perform that action at this time.