Advanced search
Log In | New Account   
Home My Page Project Tree Code Snippets Project Openings Batteries : Revised Standard Library
Summary Activity Forums Tracker Lists Tasks Docs News Files

Bugs: Browse | Download .csv

[#228] Bitset.diff and Bitset.differentiate

Date:
2009-06-19 06:17
Priority:
3
State:
Open
Submitted by:
Assigned to:
Nobody (None)
Summary:
Bitset.diff and Bitset.differentiate

Detailed description
The functions are incorrect.

    Bitset.diff a b

can calculate the wrong answer when the underlying string for a
is longer than the underlying string for b.

Here is the code:

1.     let diff a b =
2.       let maxlen = max a.len b.len in
3.       let buf = bcreate maxlen in
4.       bblit a.data 0 buf 0 a.len;
5.       let sl = min a.len b.len in
6.       let abuf = a.data
7.       and bbuf = b.data in
8.       for i = 0 to sl-1 do
9.         bset buf i ((bget abuf i) land (lnot (bget bbuf i)))
10.      done;
11.      { data = buf; len = maxlen }

If b is longer than a, the result, buf, gets created on line 3 at the
same size as b.  The contents of buf at this point are garbage,
bcreate does not clear them.  The bblit on line 4 copies a into buf,
leaving some garbage at the end.  The for loop on line 8 touches only
up to the length of a, so there is still garbage at the end.

A second issue is that differentiate is defined in terms of diff.
This is undesirable because it ends up bcreating a new buffer and
throwing away an old buffer; I expect from the type of differentiate
that it reuses its buffer.

A fix is to use this for differentiate:

  let differentiate a b =
    let sl = min a.len b.len in
    let abuf = a.data
    and bbuf = b.data in
    for i = 0 to sl-1 do
      bset abuf i ((bget abuf i) land (lnot (bget bbuf i)))
    done

Then define (diff a b) by copying a and using differentiate on the
result.

Followup

Message
Date: 2009-06-19 16:09
Sender: Trevor Jim

Hard to construct a test case because the garbage is nondeterministic.
E.g., it's possible that it works correctly depending on what
random garbage is in the buffer.
Date: 2009-06-19 07:52
Sender: David Teller

Diff should now be fixed. Differentiate will come next. Do you
have a short testcase we could add to the testsuite?

Related Tasks:

No Related Tasks

Attached Files:

No Files Currently Attached

Changes:

Field Old Value Date By
ResolutionNone2009-06-19 07:52yoric

Powered By GForge Collaborative Development Environment