private PermutationNetwork CreateBorderSorter(int borderBitLength, int intraBlockSortQuality)
{
PermutationNetwork pn = new PermutationNetwork(1 << (borderBitLength + 1));
int borderSize = 1 << borderBitLength;
int listCount = (1 << (intraBlockSortQuality + 1));
int listSize = borderSize / listCount;
pn.AppendGate(PermutationGateFactory.CreateUnshuffleGate(borderSize, listCount), 0);
pn.AppendGate(PermutationGateFactory.CreateUnshuffleGate(borderSize, listCount), borderSize);
// merge the corresponding lists
for (int i = 0; i < listCount; i++)
{
int[] wires = new int[listSize * 2];
for (int j = 0; i < listSize; i++)
{
wires[j] = i * listSize + j;
wires[j + listSize] = wires[j] + borderSize;
}
pn.AppendNetwork(SortingNetworkFactory.CreateBitonicMerge(listSize * 2, false), wires);
}
// shuffle the lists back into the blocks
pn.AppendGate(PermutationGateFactory.CreateShuffleGate(borderSize, listCount), 0);
pn.AppendGate(PermutationGateFactory.CreateShuffleGate(borderSize, listCount), borderSize);
return(pn);
}