I'm implementing a system in which I'm handling multiple complex kind of data, and I must be able to sort the data objects given their inner value, using the IComparable interface contract.
One of these object types handles short to very long (2*10^28) strings, materialized as TextReader.
Does anyone know an effective way to compute such difference on such long objects in an optimal way? It seems I can't find any solution to determine which, between two objects, is the greatest or equal, but only text diff algorithms.
Since they are materialized as TextReaders
I would probably use a method like this:
static int Compare(TextReader r1, TextReader r2)
{
int c1 = 0, c2 = 0;
// read one char at a time and compare them
// until we reach end of one of the strings
while((c1 = r1.Read()) != -1 &&
(c2 = r2.Read()) != -1)
{
var result = ((char)c1).CompareTo((char)c2);
if (result != 0) return result;
}
// if both are -1 then strings have the same length and
// consist of same chars so they are equal
if (c1 == -1 && c2 == -1)
return 0;
// if r1 is subset of r2 then r2 is greater
else if (c1 == -1)
return -1;
// otherwise r1 is greater
else
return 1;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.