Please take our Survey
logo       

Choosing A Webhost:
A web hosting service is a type of Internet hosting service that allows individuals and organizations to provide their own website accessible via the World Wide Web. Web hosts are companies that provide space on a server they own for use by their clients as well as providing Internet connectivity, typically in a data center. Web hosts can also provide data center space and connectivity to the Internet for servers they do not own to be located in their data center, called colocation. more...

[PATCH] BitSet.compare fix (was Re: a bug in BitSet?): msg#00076

lang.ocaml.lib.devel

Subject: [PATCH] BitSet.compare fix (was Re: a bug in BitSet?)

Here is a patch that should fix BitSet.compare bugs. I made a relatively extensive tester for it and couldn't find any problems.

The compare now works as if the bitvector was a BigInt. I am guessing that that has been the original intent as well.

Perhaps someone can take a look before I commit the changes?

ciao,
janne

Compare seems completely broken to me too, at least if one
expects it to work like you suggest (which is IMHO the
correct way).
It doesn't seem to properly account for the two bitsets
possibly being of different lengths, it just starts
comparing at the MSB even if the difference between the
lengths of the sets is more than a byte. It gets more
complicated because you can't even use |t1|>|t2| as an
indication that t1>t2, because there is AFAICT no
guarantee that the leading bit (or even just one bit in
the first byte of the representation!) is set.
Index: bitSet.ml
===================================================================
RCS file: /cvsroot/ocaml-lib/extlib-dev/bitSet.ml,v
retrieving revision 1.12
diff -u -r1.12 bitSet.ml
--- bitSet.ml 22 Dec 2004 07:59:46 -0000 1.12
+++ bitSet.ml 22 Dec 2004 21:52:46 -0000
@@ -109,40 +109,56 @@
else
false

-(* we can't use Pervasives.compare because bitsets might be of different
- sizes but are actually the same integer *)
+
+exception Break_int of int
+
+(* Find highest set element or raise Not_found *)
+let find_msb t =
+ (* Find highest set bit in a byte. Does not work with zero. *)
+ let byte_msb b =
+ assert (b <> 0);
+ let rec loop n =
+ if b land (1 lsl n) = 0 then
+ loop (n-1)
+ else n in
+ loop 7 in
+ let n = t.len - 1
+ and buf = t.data in
+ try
+ for i = n downto 0 do
+ let byte = bget buf i in
+ if byte <> 0 then raise (Break_int ((i lsl log_int_size)+(byte_msb
byte)))
+ done;
+ raise Not_found
+ with
+ Break_int n -> n
+ | _ -> raise Not_found
+
let compare t1 t2 =
- let size1 = t1.len and size2 = t2.len in
- let size = (if size1 < size2 then size1 else size2) in
- let rec loop2 n =
- if n >= size2 then
- 0
- else if bget t2.data n <> 0 then
- 1
- else
- loop2 (n+1)
- in
- let rec loop1 n =
- if n >= size1 then
- 0
- else if bget t1.data n <> 0 then
- -1
- else
- loop1 (n+1)
- in
- let rec loop n =
- if n = size then
- (if size1 > size2 then loop1 n else loop2 n)
- else
- let d = bget t2.data n - bget t1.data n in
- if d = 0 then
- loop (n+1)
- else if d < 0 then
- -1
- else
- 1
- in
- loop 0
+ let some_msb b = try Some (find_msb b) with Not_found -> None in
+ match (some_msb t1, some_msb t2) with
+ (None, Some _) -> -1 (* 0-y -> -1 *)
+ | (Some _, None) -> 1 (* x-0 -> 1 *)
+ | (None, None) -> 0 (* 0-0 -> 0 *)
+ | (Some a, Some b) -> (* x-y *)
+ if a < b then -1
+ else if a > b then 1
+ else
+ begin
+ (* MSBs differ, we need to scan arrays until we find a
+ difference *)
+ let ndx = a lsr log_int_size in
+ assert (ndx < t1.len && ndx < t2.len);
+ try
+ for i = ndx downto 0 do
+ let b1 = bget t1.data i
+ and b2 = bget t2.data i in
+ if b1 <> b2 then raise (Break_int (compare b1 b2))
+ done;
+ 0
+ with
+ Break_int res -> res
+ end

let equals t1 t2 =
compare t1 t2 = 0
<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

Recently Viewed:
version-control...    qnx.openqnx.dev...    redhat.rhn.user...    ietf.openpgp/20...    mail.mutt.user/...    web.microformat...    java.sync4j.use...    education.ezpro...    user-groups.blu...    solaris.manager...    org.fitug.debat...    technology.erps...    politics.activi...    linux.redhat.fe...    bug-tracking.ma...    xfce.user/2004-...    hams/2004-11/ms...    kde.users.pim/2...    culture.cooking...    freebsd.devel.x...    gnu.m4.adhoc/20...    ngpt.user/2002-...    apple.fink.deve...   
Home | advertise | OSDir is an inevitable website. super tiny logo

Free Magazines

Cisco News
Receive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business.
subscribe

Systems Management News, the newspaper for IT systems administration and data center managers! Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field.
subscribe

The Enterprise Newsweekly eWeek is the essential technology information source for builders of e-business.
subscribe

Oracle Magazine Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company.
subscribe

Total Telecom Total Telecom is "The Economist of the communications industry".
subscribe

Navigation