Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If they're guaranteed to be the same size - actually, the same dimensions - then I'm not seeing the importance of all this hashing and binary searches and other overhead. You can simply compare the two byte-by-byte in a loop, and if they don't match, add a "point" to your diff containing both the index and the value in A. To reverse the process, you don't need to look at every byte because you already have indexes.</p> <p>If the two arrays differ by, say, just 1 byte, then you'll end up with a diff structure that's 5 bytes in size (assuming you use an Int32 for the index), and takes exactly 1 iteration to mutate B back into A. In general the process is O(n) for the diff and O(<em>m</em>) for the revert, where <em>m</em> is the total number of points that actually changed. I'm not an expert on data structures but I doubt you'll be able to come up with something more efficient.</p> <p>So, something like this:</p> <pre><code>Diff GetDiff(byte[] a, byte[] b) { Diff diff = new Diff(); for (int i = 0; i &lt; a.Length; i++) { if (a[i] != b[i]) { diff.Points.Add(new DiffPoint(i, a[i])); } } return diff; } // Mutator method - turns "b" into the original "a" void ApplyDiff(byte[] b, Diff diff) { foreach (DiffPoint point in diff.Points) { b[point.Index] = point.Value; } } // Copy method - recreates "a" leaving "b" intact byte[] ApplyDiffCopy(byte[] b, Diff diff) { byte[] a = new byte[b.Length]; int startIndex = 0; foreach (DffPoint point in diff.Points) { for (int i = startIndex; i &lt; point.Index; i++) { a[i] = b[i]; } a[point.Index] = point.Value; startIndex = point.Index + 1; } for (int j = startIndex; j &lt; b.Length; j++) { a[j] = b[j]; } return a; } struct DiffPoint { public int Index; public byte Value; public DiffPoint(int index, byte value) : this() { this.Index = index; this.Value = value; } } class Diff { public Diff() { Points = new List&lt;DiffPoint&gt;(); } public List&lt;DiffPoint&gt; Points { get; private set; } } </code></pre> <p>There's a lot of looping in the <code>ApplyDiffCopy</code> but if you work it out you'll see that it actually only performs one operation per point. Of course, if you don't need a copy and just want to mutate B, then the first <code>ApplyDiff</code> method will be extremely fast if there aren't many actual differences.</p> <p>And obviously I haven't done much error-checking here. You would want to write your version a bit more defensively (verify array lengths, etc.)</p> <p>If I've correctly understood the assumptions here and the problem you're trying to solve, then the original <code>ApplyDiff</code> method is going to be the fastest way to restore the original image.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload