QuickSparseArray[{rp_QuickSparseArray[rp_?VectorQ, ci_?MatrixQ, vals_?VectorQ, dims_?VectorQ, background_ : 0}]0] :=
With[{data = {Automatic, dims, background, {1, {rp, ci}, vals}}},
SparseArray @@ data
];
ThreadCount[] := "ParallelThreadNumber" /. ("ParallelOptions" /. SystemOptions["ParallelOptions"]);
JobPointers[jobCount_Integer?Positive, threadCount_Integer?Positive] :=
Ceiling[Subdivide[0, jobCount, Min[threadCount, jobCount]]];
cFindHorizontalNeighbors =
Compile[{{rp, _Integer, 1}, {ci, _Integer, 2}, {idx, _Integer,
1}, {start, _Integer}, {end, _Integer}},
Block[{bag, col, nextcol, i, j},
bag = Internal`Bag[Most[{0}]];
Do[
Do[
col = Compile`GetElement[ci, k, 1];
nextcol = Compile`GetElement[ci, k + 1, 1];
If[nextcol == col + 1,
i = Compile`GetElement[idx, k];
j = Compile`GetElement[idx, k + 1];
Internal`StuffBag[bag, i];
Internal`StuffBag[bag, j];
Internal`StuffBag[bag, j];
Internal`StuffBag[bag, i];
]
, {k, Compile`GetElement[rp, row] + 1,
Compile`GetElement[rp, row + 1] - 1}]
, {row, start, end}]; Partition[Internal`BagPart[bag, All], 2]
],
CompilationTarget -> "C",
RuntimeAttributes -> {Listable},
Parallelization -> True,
RuntimeOptions -> "Speed"
];
In principalprinciple, one could do a similar thing for the diagonal lookup. I am just not in the modd for that...