private static void Merge( ISequentialStorageReader <T> left, ISequentialStorageReader <T> right, ISequentialStorageWriter <T> output, long leftLength, long rightLength, IComparer <T> comparer) { var leftIndex = 0L; var rightIndex = 0L; var l = left.Read(); var r = right.Read(); while (true) { if (comparer.Compare(l, r) < 0) { output.Write(l); leftIndex++; if (leftIndex == leftLength) { break; } l = left.Read(); } else { output.Write(r); rightIndex++; if (rightIndex == rightLength) { break; } r = right.Read(); } } if (leftIndex < leftLength) { output.Write(l); Copy(left, output, leftLength - leftIndex - 1); } if (rightIndex < rightLength) { output.Write(r); Copy(right, output, rightLength - rightIndex - 1); } }
private static void Copy(ISequentialStorageReader <T> from, ISequentialStorageWriter <T> to, long count) { for (var i = 0; i < count; i++) { to.Write(from.Read()); } }