private void addCleanColIntoCols(CT_Cols cols, CT_Col newCol, TreeSet <CT_Col> trackedCols) { List <CT_Col> overlapping = getOverlappingCols(newCol, trackedCols); if (overlapping.Count == 0) { trackedCols.Add(CloneCol(cols, newCol)); return; } trackedCols.RemoveAll(overlapping); foreach (CT_Col existing in overlapping) { // We add up to three columns for each existing one: non-overlap // before, overlap, non-overlap after. long[] overlap = getOverlap(newCol, existing); CT_Col overlapCol = cloneCol(cols, existing, overlap); SetColumnAttributes(newCol, overlapCol); trackedCols.Add(overlapCol); CT_Col beforeCol = existing.min < newCol.min ? existing : newCol; long[] before = new long[] { Math.Min(existing.min, newCol.min), overlap[0] - 1 }; if (before[0] <= before[1]) { trackedCols.Add(cloneCol(cols, beforeCol, before)); } CT_Col afterCol = existing.max > newCol.max ? existing : newCol; long[] after = new long[] { overlap[1] + 1, Math.Max(existing.max, newCol.max) }; if (after[0] <= after[1]) { trackedCols.Add(cloneCol(cols, afterCol, after)); } } }