Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program RemoveDupes;
- (*
- Removes duplicates while keeping the order of the array
- Can be modified to fit other datatypes.
- *)
- procedure FastRemoveDupes(var Arr:TIntegerArray);
- var
- n,l,i,j,c:Int32;
- hash: UInt32;
- table: Array of TIntegerArray;
- Isset:Boolean;
- begin
- n := Length(Arr);
- SetLength(Table, Trunc(Pow(2, Floor(Logn(2,n)) + 1)));
- c := 0;
- for i:=0 to n-1 do
- begin
- Isset := False;
- hash := Arr[i] and High(Table);
- l := Length(Table[hash]);
- for j:=0 to l-1 do
- if (Table[hash][j] = Arr[i]) then
- begin
- Isset := True;
- Break;
- end;
- if not(Isset) then
- begin
- SetLength(Table[hash], l+1);
- Table[hash][l] := Arr[i];
- Arr[c] := Arr[i];
- Inc(c);
- end;
- end;
- SetLength(Arr, c);
- end;
- (*
- TBoxArray example version
- *)
- procedure FastRemoveDupes_TBoxArray(var Arr:TBoxArray);
- var
- n,l,i,j,c:Int32;
- hash,h1,h2: UInt32;
- table: Array of TBoxArray;
- Isset:Boolean;
- begin
- n := Length(Arr);
- if n <= 1 then Exit();
- SetLength(Table, Trunc(Pow(2, Floor(Logn(2,n)) + 1)));
- for i:=0 to n-1 do
- begin
- Isset := False;
- h1 := UInt32((Arr[i].y1 * $0f0f1f1f) xor Arr[i].x1);
- h2 := UInt32((Arr[i].y2 * $0f0f1f1f) xor Arr[i].x2);
- hash := ((h1 * $0f0f1f1f) xor h2) and High(Table);
- l := Length(Table[hash]);
- for j:=0 to l-1 do
- if (Table[hash][j].x1 = Arr[i].x1) and (Table[hash][j].y1 = Arr[i].y1) and
- (Table[hash][j].x2 = Arr[i].x2) and (Table[hash][j].y2 = Arr[i].y2) then
- begin
- Isset := True;
- Break;
- end;
- if not(Isset) then
- begin
- SetLength(Table[hash], l+1);
- Table[hash][l] := Arr[i];
- Arr[c] := Arr[i];
- Inc(c);
- end;
- end;
- SetLength(Arr, c);
- end;
- //----------------------------------------------------------------------------\\
- //---| Benchmark and test code |----------------------------------------------\\
- function RandomTIA(n:Int32; Low,Hi:Int32): TIntegerArray;
- var i:Int32;
- begin
- SetLength(Result, n);
- for i:=0 to n-1 do
- Result[i] := RandomRange(Low,Hi);
- end;
- var
- Arr, Out1, Out2:TIntegerArray;
- t:UInt32;
- begin
- WriteLn('---| Benchmark |-------------------------');
- Arr := RandomTIA(100000,0,$FFFFFF);
- Out1 := Copy(Arr);
- t := GetTimeRunning();
- FastRemoveDupes(Out1);
- WriteLn('| FastRemoveDupes (interpreted): ', GetTimeRunning() - t,'ms');
- Out2 := Copy(Arr);
- t := GetTimeRunning();
- ClearSameIntegers(Out2);
- WriteLn('| ClearSameIntegers (compiled): ', GetTimeRunning() - t,'ms');
- //verify same length
- WriteLn('| Same length: ', Length(Out2) = Length(Out1));
- //test
- WriteLn('---| Test |------------------------------');
- Arr := [1,5,7,4,7,8,3,6,1,8,6,2,3,5,8,9,6,3,2,3,7];
- WriteLn('| ',Arr);
- FastRemoveDupes(Arr);
- WriteLn('| ',Arr);
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement