简体   繁体   English

比较大字符串内容

[英]Compare Large String Contents

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. 我正在实现一个系统,其中要处理多种复杂的数据,并且我必须能够使用IComparable接口协定对给定其内部值的数据对象进行排序。

One of these object types handles short to very long (2*10^28) strings, materialized as TextReader. 这些对象类型之一处理短到非常长的(2 * 10 ^ 28)字符串,具体化为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: 由于它们被实现为TextReaders我可能会使用如下方法:

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM