Skip to content

Instantly share code, notes, and snippets.

@bpatra
Last active December 5, 2020 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bpatra/003404081d81a3c24930 to your computer and use it in GitHub Desktop.
Save bpatra/003404081d81a3c24930 to your computer and use it in GitHub Desktop.
Build Union of even lines with a fast approach based on divide and conquer.
private static Range UnionDivideAndConquer(int[] excelRows, int start, int end)
{
if (excelRows.Length == 0) return null;
if (start == end)
{
return ExcelAddIn.ActiveSheet.Range["A" + excelRows[start] + ":" + "C" + excelRows[start]];
}
int middle = (start + end) / 2;
Range left = UnionDivideAndConquer(excelRows, start, middle);
Range right = UnionDivideAndConquer(excelRows, middle + 1, end);
return ExcelAddIn.Application.Union(left, right);
}
public BuildUnionFast(int count)
{
var rowIndices = Enumerable.Range(1, 2*count+1).Where(k => k % 2 == 1).ToArray();
Range range = UnionDivideAndConquer(rowIndices, 0, rowIndices.Length-1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment