sorting - 如何执行排序+索引同时删除重复项?

标签 sorting delphi optimization

我使用以下代码对项目和索引进行排序: valuesindex 数组中大约有 256*1024*1024 个条目。
未压缩的 values 数组大约占用 10GB,这就是为什么我想通过将有很多重复值分组在一起来压缩它。

我已经提出了以下概念验证代码,但 Compress 方法存在问题。每次找到重复项时,它都会在索引中执行搜索,这会花费 O(N2) 时间。

我需要保留一个索引,因为我需要能够访问数组中的元素,就好像没有进行重复数据删除一样。

这是使用简单的default属性数组属性完成的,从而模仿原始数组:

function TLookupTable.GetItems(Index: integer): TSlice;
begin   
  Result:= FData[FIndex[Index]];
end;

概念验证代码(速度很慢)如下。

TMyArray = class
  class procedure QuickSort<T,Idx>(var Values: array of T; var Index: array of Idx; const Comparer: IComparer<T>;
  L, R: Integer);
  class procedure Compress<T>(const Values: array of T; var Index: array of Integer; out CompressedValues: TArray<T>; const Comparer: IComparer<T>);
end;

class procedure TMyArray.QuickSort<T,Idx>(var Values: array of T; var Index: array of Idx; const Comparer: IComparer<T>;
  L, R: Integer);
var
  I, J: Integer;
  pivot, temp: T;
  TempIdx: Idx;
begin
  if (Length(Values) = 0) or ((R - L) <= 0) then
    Exit;
  repeat
    I := L;
    J := R;
    pivot := Values[L + (R - L) shr 1];
    repeat
      while Comparer.Compare(Values[I], pivot) < 0 do Inc(I);
      while Comparer.Compare(Values[J], pivot) > 0 do Dec(J);
      if I <= J then begin
        if I <> J then begin
          temp := Values[I];
          Values[I] := Values[J];
          Values[J] := temp;

          //Keep the index in sync
          tempIdx := Index[I];
          Index[I] := Index[J];
          Index[J] := tempIdx;
        end;
        Inc(I);
        Dec(J);
      end;
    until I > J;
    if L < J then QuickSort<T,Idx>(Values, Index, Comparer, L, J);
    L := I;
  until I >= R;
end;

class procedure TMyArray.Compress<T>(const Values: array of T; var Index: array of integer; out CompressedValues: TArray<T>; const Comparer: IComparer<T>);
var
  i,j: integer;
  Count: integer;
  Duplicate: integer;
begin
  Count:= 256*1024*1024;
  SetLength(CompressedValues, Count);
  CompressedValues[0]:= Values[0];
  Duplicate:= 0;
  for i := 1 to High(Values) do begin
    //Compress duplicate values
    if Comparer.Compare(Values[i], CompressedValues[Duplicate]) = 0 then begin
      //Search for the indexed item
      //Very time consuming: O(N*N)
      for j:= i to High(Index) do if Index[j] = i then begin
        Index[j]:= Duplicate;  //Fix up the index
        Break;
      end; {for j}
    end else begin
      Inc(Duplicate);
      if Duplicate >= Count  then begin
        Inc(Count, 1024*1024);
        SetLength(CompressedValues, Count);
      end;
      CompressedValues[Duplicate]:= Values[i];
    end; 
  end; {for i}
  SetLength(CompressedValues, Duplicate+1)
end;

如何加快压缩步骤,使其花费 O(N) 时间?

如果有一种方法可以同时保留索引、删除重复项和排序,那就太好了。我在下面的回答将排序和重复数据删除分为两个单独的阶段。

最佳答案

诀窍是保留原始数据数组,只对索引进行排序。 然后,我们可以利用原始数据顺序正确的事实,并使用它来构建新索引。

此外,这意味着我们不再需要自定义的 Sort 函数;它还移动了更少的数据。

像这样创建索引:

  FIndex: TArray<integer>;
  ....
  SetLength(FIndex, Length(FAllData));
  for i:= 0 to count-1 do begin
    FIndex[i]:= i;
  end;
  TArray.Sort<Integer>(FIndex, TDelegatedComparer<integer>.Construct(
  function(const Left, Right: Integer): Integer
  begin
    if FAllData[Left] > FAllData[Right] then Exit(1);
    if FAllData[Left] < FAllData[Right] then Exit(-1);
    Result:= 0;
  end));

像这样更改 TMyArray 类:

TMyArray = class
  class procedure Compress<T>(const Values: array of T; var Index: TArray<integer>; out CompressedValues: TArray<T>; const Comparer: IComparer<T>);
end;

class procedure TMyArray.Compress<T>(const Values: array of T; var Index: TArray<integer>; out CompressedValues: TArray<T>; const Comparer: IComparer<T>);
const
  Equal = 0;
var
  i,j: integer;
  Count: integer;
  Duplicate: integer;
  IndexEntry: integer;
  OutIndex: TArray<integer>;
begin

  Count:= 16*1024*1024;  //Start with something reasonable
  SetLength(CompressedValues, Count);
  SetLength(OutIndex, Length(Index));
  Duplicate:= 0;
  CompressedValues[0]:= Values[Index[0]];
  OutIndex[Index[0]]:= 0;
  for i:= 1 to High(Index) do begin
    if Comparer.Compare(Values[Index[i]], CompressedValues[Duplicate]) = Equal then begin
      OutIndex[Index[i]]:= Duplicate;
    end else begin
      Inc(Duplicate);
      //Grow as needed
      if (Duplicate >= Length(CompressedValues)) then SetLength(CompressedValues, Length(CompressedValues) + 1024*1024);
      CompressedValues[Duplicate]:= Values[Index[i]];
      OutIndex[Index[i]]:= Duplicate;
    end;
  end;
  Index:= OutIndex;
  SetLength(CompressedValues, Duplicate+1);
end;

关于sorting - 如何执行排序+索引同时删除重复项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52166370/

相关文章:

Delphi 编辑和备忘录中的文本偏移

c++ - 在 Windows 7 中管理应用程序音量

c++ - 多重双线性插值的更快方法?

javascript - 使用多个键和日期简单方法 JavaScript 对数组进行排序

c - 用指针对c中的数组进行排序

按列中的名称对 pandas DataFrame 中的数据进行排序

delphi - 如何防止 TTreeView 的自定义树节点数据丢失?

c++ - 高效的多项式结果

Oracle 中的 SQL 调优

algorithm - 在什么情况下,较慢的排序算法(冒泡排序、选择排序等)比快速排序等较快的算法更有用?