Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
disjoint test in set.
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2008-01-24 (22:34)
From: Christophe Raffalli <christophe.raffalli@u...>
Subject: disjoint test in set.

Dear list member,

I think a function testing if two sets are disjoint is really simple, 
more efficient than building the intersection, and missing in the 
standard library ?
I may be wrong ?

Here it is if someone wants to copy it in :

    let rec disjoint t1 t2 =
      match (t1, t2) with
        (Empty, _) | (_, Empty) ->
      | (Node (l1, v1, r1, _), Node (l2, v2, r2, _)) ->
          let c = v1 v2 in
          if c = 0 then
          else if c < 0 then
            disjoint (Node (l1, v1, Empty, 0)) l2 && disjoint r1 t2
            disjoint l1 (Node (l2, v2, Empty, 0)) && disjoint t1 r2

+ the necessary lignes in signatures ...

Then, a small question:

Can we make disjoint (and subset) do no allocation in the heap (That is 
remove Node (l1, v1, Empty, 0) and the symmetric
from the code without loss of performance ? (checking v1 and l1 
separately is surely a bad idea) ...

Best Regards,