src/Tools/Metis/src/RandomSet.sml
changeset 25430 372d6749f00e
parent 23510 4521fead5609
equal deleted inserted replaced
25429:9e14fbd43e6b 25430:372d6749f00e
    14 
    14 
    15 val K = Useful.K;
    15 val K = Useful.K;
    16 
    16 
    17 val snd = Useful.snd;
    17 val snd = Useful.snd;
    18 
    18 
    19 val randomInt = Useful.random;
    19 val randomInt = Portable.randomInt;
       
    20 
       
    21 val randomWord = Portable.randomWord;
    20 
    22 
    21 (* ------------------------------------------------------------------------- *)
    23 (* ------------------------------------------------------------------------- *)
    22 (* Random search trees.                                                      *)
    24 (* Random search trees.                                                      *)
    23 (* ------------------------------------------------------------------------- *)
    25 (* ------------------------------------------------------------------------- *)
       
    26 
       
    27 type priority = Word.word;
    24 
    28 
    25 datatype 'a tree =
    29 datatype 'a tree =
    26     E
    30     E
    27   | T of
    31   | T of
    28     {size : int,
    32     {size : int,
    29      priority : real,
    33      priority : priority,
    30      left : 'a tree,
    34      left : 'a tree,
    31      key : 'a,
    35      key : 'a,
    32      right : 'a tree};
    36      right : 'a tree};
    33     
    37     
    34 type 'a node =
    38 type 'a node =
    35      {size : int,
    39      {size : int,
    36       priority : real,
    40       priority : priority,
    37       left : 'a tree,
    41       left : 'a tree,
    38       key : 'a,
    42       key : 'a,
    39       right : 'a tree};
    43       right : 'a tree};
    40 
    44 
    41 datatype 'a set = Set of ('a * 'a -> order) * 'a tree;
    45 datatype 'a set = Set of ('a * 'a -> order) * 'a tree;
    43 (* ------------------------------------------------------------------------- *)
    47 (* ------------------------------------------------------------------------- *)
    44 (* Random priorities.                                                        *)
    48 (* Random priorities.                                                        *)
    45 (* ------------------------------------------------------------------------- *)
    49 (* ------------------------------------------------------------------------- *)
    46 
    50 
    47 local
    51 local
    48   val randomPriority =
    52   val randomPriority = randomWord;
    49       let
    53 
    50         val gen = Random.newgenseed 2.0
    54   val priorityOrder = Word.compare;
    51       in
       
    52         fn () => Random.random gen
       
    53       end;
       
    54 
       
    55   val priorityOrder = Real.compare;
       
    56 in
    55 in
    57   fun treeSingleton key =
    56   fun treeSingleton key =
    58       T {size = 1, priority = randomPriority (),
    57       T {size = 1, priority = randomPriority (),
    59          left = E, key = key, right = E};
    58          left = E, key = key, right = E};
    60 
    59 
   465 
   464 
   466 fun null s = size s = 0;
   465 fun null s = size s = 0;
   467 
   466 
   468 fun member x s = Option.isSome (peek s x);
   467 fun member x s = Option.isSome (peek s x);
   469 
   468 
   470 (* add must be primitive to get hold of the comparison function *)
       
   471 
       
   472 fun add s x = union s (singleton (comparison s) x);
   469 fun add s x = union s (singleton (comparison s) x);
   473 
   470 
   474 (*DEBUG
   471 (*DEBUG
   475 val add = fn s => fn x =>
   472 val add = fn s => fn x =>
   476     checkWellformed "RandomSet.add: result"
   473     checkWellformed "RandomSet.add: result"
   480 local
   477 local
   481   fun unionPairs ys [] = rev ys
   478   fun unionPairs ys [] = rev ys
   482     | unionPairs ys (xs as [_]) = List.revAppend (ys,xs)
   479     | unionPairs ys (xs as [_]) = List.revAppend (ys,xs)
   483     | unionPairs ys (x1 :: x2 :: xs) = unionPairs (union x1 x2 :: ys) xs;
   480     | unionPairs ys (x1 :: x2 :: xs) = unionPairs (union x1 x2 :: ys) xs;
   484 in
   481 in
   485   fun unionList [] = raise Error "Set.unionList: no sets"
   482   fun unionList [] = raise Error "RandomSet.unionList: no sets"
   486     | unionList [s] = s
   483     | unionList [s] = s
   487     | unionList l = unionList (unionPairs [] l);
   484     | unionList l = unionList (unionPairs [] l);
   488 end;
   485 end;
   489 
   486 
   490 local
   487 local
   491   fun intersectPairs ys [] = rev ys
   488   fun intersectPairs ys [] = rev ys
   492     | intersectPairs ys (xs as [_]) = List.revAppend (ys,xs)
   489     | intersectPairs ys (xs as [_]) = List.revAppend (ys,xs)
   493     | intersectPairs ys (x1 :: x2 :: xs) =
   490     | intersectPairs ys (x1 :: x2 :: xs) =
   494       intersectPairs (intersect x1 x2 :: ys) xs;
   491       intersectPairs (intersect x1 x2 :: ys) xs;
   495 in
   492 in
   496   fun intersectList [] = raise Error "Set.intersectList: no sets"
   493   fun intersectList [] = raise Error "RandomSet.intersectList: no sets"
   497     | intersectList [s] = s
   494     | intersectList [s] = s
   498     | intersectList l = intersectList (intersectPairs [] l);
   495     | intersectList l = intersectList (intersectPairs [] l);
   499 end;
   496 end;
   500 
   497 
   501 fun symmetricDifference s1 s2 = union (difference s1 s2) (difference s2 s1);
   498 fun symmetricDifference s1 s2 = union (difference s1 s2) (difference s2 s1);
   592 fun pick s =
   589 fun pick s =
   593     case findl (K true) s of
   590     case findl (K true) s of
   594       SOME p => p
   591       SOME p => p
   595     | NONE => raise Error "RandomSet.pick: empty";
   592     | NONE => raise Error "RandomSet.pick: empty";
   596 
   593 
   597 fun random s = case size s of 0 => raise Empty | n => nth s (randomInt n);
   594 fun random s =
       
   595     case size s of
       
   596       0 => raise Error "RandomSet.random: empty"
       
   597     | n => nth s (randomInt n);
   598 
   598 
   599 fun deletePick s = let val x = pick s in (x, delete s x) end;
   599 fun deletePick s = let val x = pick s in (x, delete s x) end;
   600 
   600 
   601 fun deleteRandom s = let val x = random s in (x, delete s x) end;
   601 fun deleteRandom s = let val x = random s in (x, delete s x) end;
   602 
   602