Index: config/Makefile.in
===================================================================
RCS file: /cvsroot/mldonkey/mldonkey/config/Makefile.in,v
retrieving revision 1.128
diff -u -r1.128 Makefile.in
--- config/Makefile.in	7 Nov 2005 20:11:40 -0000	1.128
+++ config/Makefile.in	11 Nov 2005 15:22:12 -0000
@@ -215,7 +215,7 @@
   $(LIB)/gettext.ml4 $(LIB)/md5_c.c $(LIB)/$(SHA1_VERSION)_c.c \
   $(LIB)/tiger.c \
   $(LIB)/CryptoPP.cc \
-  $(LIB)/stubs_c.c  $(LIB)/queues.ml \
+  $(LIB)/stubs_c.c  $(LIB)/set2.ml $(LIB)/queues.ml \
   $(LIB)/charsetstubs.c $(LIB)/charset.ml
 
 NET_SRCS = \
Index: src/utils/lib/queues.ml
===================================================================
RCS file: /cvsroot/mldonkey/mldonkey/src/utils/lib/queues.ml,v
retrieving revision 1.2
diff -u -r1.2 queues.ml
--- src/utils/lib/queues.ml	16 Apr 2004 17:14:56 -0000	1.2
+++ src/utils/lib/queues.ml	11 Nov 2005 15:22:14 -0000
@@ -89,7 +89,7 @@
       val compare : t -> t -> int
     end) = struct
     
-    module InsidesSet = Set.Make (
+    module InsidesSet = Set2.Make (
         struct
           type t = int * M.t
           let compare (t1,s1) (t2,s2) = 
@@ -142,7 +142,7 @@
 
       (*
     let max_first compare =
-      let module InsideSet = Set.Make(struct
+      let module InsideSet = Set2.Make(struct
             type t = source
             let compare = compare
           end) in
@@ -161,7 +161,7 @@
         }
 
     let min_first compare =
-      let module InsideSet = Set.Make(struct
+      let module InsideSet = Set2.Make(struct
             type t = source
             let compare = compare
           end) in
--- /dev/null	2005-11-10 20:30:21.089055160 +0100
+++ src/utils/lib/set2.mli	2005-11-11 16:02:18.000000000 +0100
@@ -0,0 +1,155 @@
+(***********************************************************************)
+(*                                                                     *)
+(*                           Objective Caml                            *)
+(*                                                                     *)
+(*            Xavier Leroy, projet Cristal, INRIA Rocquencourt         *)
+(*                                                                     *)
+(*  Copyright 1996 Institut National de Recherche en Informatique et   *)
+(*  en Automatique.  All rights reserved.  This file is distributed    *)
+(*  under the terms of the GNU Library General Public License, with    *)
+(*  the special exception on linking described in file ../LICENSE.     *)
+(*                                                                     *)
+(***********************************************************************)
+
+(* $Id: set2.mli,v 1.32 2004/04/23 10:01:54 xleroy Exp $ *)
+
+(** Sets over ordered types.
+
+   This module implements the set data structure, given a total ordering
+   function over the set elements. All operations over sets
+   are purely applicative (no side-effects).
+   The implementation uses balanced binary trees, and is therefore
+   reasonably efficient: insertion and membership take time
+   logarithmic in the size of the set, for instance. 
+
+   Modified to have a fast cardinal operation (cached lazy value).
+*)
+
+module type OrderedType = 
+  sig
+    type t
+      (** The type of the set elements. *)
+    val compare : t -> t -> int
+      (** A total ordering function over the set elements.
+          This is a two-argument function [f] such that
+          [f e1 e2] is zero if the elements [e1] and [e2] are equal,
+          [f e1 e2] is strictly negative if [e1] is smaller than [e2],
+          and [f e1 e2] is strictly positive if [e1] is greater than [e2].
+          Example: a suitable ordering function is the generic structural
+          comparison function {!Pervasives.compare}. *)
+  end
+(** Input signature of the functor {!Set.Make}. *)
+
+module type S =
+  sig
+    type elt
+    (** The type of the set elements. *)
+
+    type t
+    (** The type of sets. *)
+
+    val empty: t
+    (** The empty set. *)
+
+    val is_empty: t -> bool
+    (** Test whether a set is empty or not. *)
+
+    val mem: elt -> t -> bool
+    (** [mem x s] tests whether [x] belongs to the set [s]. *)
+
+    val add: elt -> t -> t
+    (** [add x s] returns a set containing all elements of [s],
+       plus [x]. If [x] was already in [s], [s] is returned unchanged. *)
+
+    val singleton: elt -> t
+    (** [singleton x] returns the one-element set containing only [x]. *)
+
+    val remove: elt -> t -> t
+    (** [remove x s] returns a set containing all elements of [s],
+       except [x]. If [x] was not in [s], [s] is returned unchanged. *)
+
+    val union: t -> t -> t
+    (** Set union. *)
+
+    val inter: t -> t -> t
+    (** Set intersection. *)
+
+    (** Set difference. *)
+    val diff: t -> t -> t
+
+    val compare: t -> t -> int
+    (** Total ordering between sets. Can be used as the ordering function
+       for doing sets of sets. *)
+
+    val equal: t -> t -> bool
+    (** [equal s1 s2] tests whether the sets [s1] and [s2] are
+       equal, that is, contain equal elements. *)
+
+    val subset: t -> t -> bool
+    (** [subset s1 s2] tests whether the set [s1] is a subset of
+       the set [s2]. *)
+
+    val iter: (elt -> unit) -> t -> unit
+    (** [iter f s] applies [f] in turn to all elements of [s].
+       The elements of [s] are presented to [f] in increasing order
+       with respect to the ordering over the type of the elements. *)
+
+    val fold: (elt -> 'a -> 'a) -> t -> 'a -> 'a
+    (** [fold f s a] computes [(f xN ... (f x2 (f x1 a))...)],
+       where [x1 ... xN] are the elements of [s], in increasing order. *)
+
+    val for_all: (elt -> bool) -> t -> bool
+    (** [for_all p s] checks if all elements of the set
+       satisfy the predicate [p]. *)
+
+    val exists: (elt -> bool) -> t -> bool
+    (** [exists p s] checks if at least one element of
+       the set satisfies the predicate [p]. *)
+        
+    val filter: (elt -> bool) -> t -> t
+    (** [filter p s] returns the set of all elements in [s]
+       that satisfy predicate [p]. *)
+
+    val partition: (elt -> bool) -> t -> t * t
+    (** [partition p s] returns a pair of sets [(s1, s2)], where
+       [s1] is the set of all the elements of [s] that satisfy the
+       predicate [p], and [s2] is the set of all the elements of
+       [s] that do not satisfy [p]. *)
+
+    val cardinal: t -> int
+    (** Return the number of elements of a set. *)
+
+    val elements: t -> elt list
+    (** Return the list of all elements of the given set.
+       The returned list is sorted in increasing order with respect
+       to the ordering [Ord.compare], where [Ord] is the argument
+       given to {!Set.Make}. *)
+
+    val min_elt: t -> elt
+    (** Return the smallest element of the given set
+       (with respect to the [Ord.compare] ordering), or raise
+       [Not_found] if the set is empty. *)
+
+    val max_elt: t -> elt
+    (** Same as {!Set.S.min_elt}, but returns the largest element of the
+       given set. *)
+
+    val choose: t -> elt
+    (** Return one element of the given set, or raise [Not_found] if
+       the set is empty. Which element is chosen is unspecified,
+       but equal elements will be chosen for equal sets. *)
+
+    val split: elt -> t -> t * bool * t
+    (** [split x s] returns a triple [(l, present, r)], where
+          [l] is the set of elements of [s] that are
+          strictly less than [x];
+          [r] is the set of elements of [s] that are
+          strictly greater than [x];
+          [present] is [false] if [s] contains no element equal to [x],
+          or [true] if [s] contains an element equal to [x]. *)
+  end
+(** Output signature of the functor {!Set.Make}. *)
+
+module Make (Ord : OrderedType) : S with type elt = Ord.t
+(** Functor building an implementation of the set structure
+   given a totally ordered type. *)
--- /dev/null	2005-11-10 20:30:21.089055160 +0100
+++ src/utils/lib/set2.ml	2005-11-11 16:20:31.000000000 +0100
@@ -0,0 +1,416 @@
+(***********************************************************************)
+(*                                                                     *)
+(*                           Objective Caml                            *)
+(*                                                                     *)
+(*            Xavier Leroy, projet Cristal, INRIA Rocquencourt         *)
+(*                                                                     *)
+(*  Copyright 1996 Institut National de Recherche en Informatique et   *)
+(*  en Automatique.  All rights reserved.  This file is distributed    *)
+(*  under the terms of the GNU Library General Public License, with    *)
+(*  the special exception on linking described in file ../LICENSE.     *)
+(*                                                                     *)
+(***********************************************************************)
+
+(* $Id: set2.ml,v 1.18.4.1 2004/11/03 21:19:49 doligez Exp $ *)
+
+(* Sets over ordered types *)
+
+module type OrderedType =
+  sig
+    type t
+    val compare: t -> t -> int
+  end
+
+module type S =
+  sig
+    type elt
+    type t
+    val empty: t
+    val is_empty: t -> bool
+    val mem: elt -> t -> bool
+    val add: elt -> t -> t
+    val singleton: elt -> t
+    val remove: elt -> t -> t
+    val union: t -> t -> t
+    val inter: t -> t -> t
+    val diff: t -> t -> t
+    val compare: t -> t -> int
+    val equal: t -> t -> bool
+    val subset: t -> t -> bool
+    val iter: (elt -> unit) -> t -> unit
+    val fold: (elt -> 'a -> 'a) -> t -> 'a -> 'a
+    val for_all: (elt -> bool) -> t -> bool
+    val exists: (elt -> bool) -> t -> bool
+    val filter: (elt -> bool) -> t -> t
+    val partition: (elt -> bool) -> t -> t * t
+    val cardinal: t -> int
+    val elements: t -> elt list
+    val min_elt: t -> elt
+    val max_elt: t -> elt
+    val choose: t -> elt
+    val split: elt -> t -> t * bool * t
+  end
+
+module Make(Ord: OrderedType) =
+  struct
+    type elt = Ord.t
+    type btree = Empty | Node of btree * elt * btree * int
+    type t = {
+	tree : btree;
+	card : int Lazy.t
+      }
+
+    (* Sets are represented by balanced binary trees (the heights of the
+       children differ by at most 2 *)
+
+    let height = function
+        Empty -> 0
+      | Node(_, _, _, h) -> h
+
+    let rec cardinal_tree = function
+        Empty -> 0
+      | Node(l, v, r, _) -> cardinal_tree l + 1 + cardinal_tree r
+
+    let t_of_tree t =
+      { tree = t;
+	card = lazy (cardinal_tree t); }
+
+    let cardinal t =
+      Lazy.force t.card
+
+    (* Creates a new node with left son l, value v and right son r.
+       We must have all elements of l < v < all elements of r.
+       l and r must be balanced and | height l - height r | <= 2.
+       Inline expansion of height for better speed. *)
+
+    let create l v r =
+      let hl = match l with Empty -> 0 | Node(_,_,_,h) -> h in
+      let hr = match r with Empty -> 0 | Node(_,_,_,h) -> h in
+      Node(l, v, r, (if hl >= hr then hl + 1 else hr + 1))
+
+    (* Same as create, but performs one step of rebalancing if necessary.
+       Assumes l and r balanced and | height l - height r | <= 3.
+       Inline expansion of create for better speed in the most frequent case
+       where no rebalancing is required. *)
+
+    let bal l v r =
+      let hl = match l with Empty -> 0 | Node(_,_,_,h) -> h in
+      let hr = match r with Empty -> 0 | Node(_,_,_,h) -> h in
+      if hl > hr + 2 then begin
+        match l with
+          Empty -> invalid_arg "Set2.bal"
+        | Node(ll, lv, lr, _) ->
+            if height ll >= height lr then
+              create ll lv (create lr v r)
+            else begin
+              match lr with
+                Empty -> invalid_arg "Set2.bal"
+              | Node(lrl, lrv, lrr, _)->
+                  create (create ll lv lrl) lrv (create lrr v r)
+            end
+      end else if hr > hl + 2 then begin
+        match r with
+          Empty -> invalid_arg "Set2.bal"
+        | Node(rl, rv, rr, _) ->
+            if height rr >= height rl then
+              create (create l v rl) rv rr
+            else begin
+              match rl with
+                Empty -> invalid_arg "Set2.bal"
+              | Node(rll, rlv, rlr, _) ->
+                  create (create l v rll) rlv (create rlr rv rr)
+            end
+      end else
+        Node(l, v, r, (if hl >= hr then hl + 1 else hr + 1))
+
+    (* Insertion of one element *)
+
+    let rec add_tree x = function
+        Empty -> Node(Empty, x, Empty, 1)
+      | Node(l, v, r, _) as t ->
+          let c = Ord.compare x v in
+          if c = 0 then t else
+          if c < 0 then bal (add_tree x l) v r else bal l v (add_tree x r)
+
+    let add x t =
+      let tree = add_tree x t.tree in
+      {
+       tree = tree;
+       card = if Lazy.lazy_is_val t.card then
+	 Lazy.lazy_from_val ((Lazy.force_val t.card) + 1)
+       else
+	 lazy (cardinal_tree tree)
+     }
+
+    (* Same as create and bal, but no assumptions are made on the
+       relative heights of l and r. *)
+
+    let rec join l v r =
+      match (l, r) with
+        (Empty, _) -> add_tree v r
+      | (_, Empty) -> add_tree v l
+      | (Node(ll, lv, lr, lh), Node(rl, rv, rr, rh)) ->
+          if lh > rh + 2 then bal ll lv (join lr v r) else
+          if rh > lh + 2 then bal (join l v rl) rv rr else
+          create l v r
+
+    (* Smallest and greatest element of a set *)
+
+    let rec min_elt_tree = function
+        Empty -> raise Not_found
+      | Node(Empty, v, r, _) -> v
+      | Node(l, v, r, _) -> min_elt_tree l
+	    
+    let min_elt t =
+      min_elt_tree t.tree
+
+    let rec max_elt_tree = function
+        Empty -> raise Not_found
+      | Node(l, v, Empty, _) -> v
+      | Node(l, v, r, _) -> max_elt_tree r
+
+    let max_elt t =
+      max_elt_tree t.tree
+
+    (* Remove the smallest element of the given set *)
+
+    let rec remove_min_elt = function
+        Empty -> invalid_arg "Set2.remove_min_elt"
+      | Node(Empty, v, r, _) -> r
+      | Node(l, v, r, _) -> bal (remove_min_elt l) v r
+
+    (* Merge two trees l and r into one.
+       All elements of l must precede the elements of r.
+       Assume | height l - height r | <= 2. *)
+
+    let merge t1 t2 =
+      match (t1, t2) with
+        (Empty, t) -> t
+      | (t, Empty) -> t
+      | (_, _) -> bal t1 (min_elt_tree t2) (remove_min_elt t2)
+
+    (* Merge two trees l and r into one.
+       All elements of l must precede the elements of r.
+       No assumption on the heights of l and r. *)
+
+    let concat t1 t2 =
+      match (t1, t2) with
+        (Empty, t) -> t
+      | (t, Empty) -> t
+      | (_, _) -> join t1 (min_elt_tree t2) (remove_min_elt t2)
+
+    (* Splitting.  split x s returns a triple (l, present, r) where
+        - l is the set of elements of s that are < x
+        - r is the set of elements of s that are > x
+        - present is false if s contains no element equal to x,
+          or true if s contains an element equal to x. *)
+
+    let rec split_tree x = function
+        Empty ->
+          (Empty, false, Empty)
+      | Node(l, v, r, _) ->
+          let c = Ord.compare x v in
+          if c = 0 then (l, true, r)
+          else if c < 0 then
+            let (ll, pres, rl) = split_tree x l in (ll, pres, join rl v r)
+          else
+            let (lr, pres, rr) = split_tree x r in (join l v lr, pres, rr)
+
+    let split e t =
+      let (ll, pres, rl) = split_tree e t.tree in
+      (t_of_tree ll, pres, t_of_tree rl)
+
+    (* Implementation of the set operations *)
+
+    let empty = { tree = Empty; card = Lazy.lazy_from_val 0; }
+
+    let is_empty_tree = function Empty -> true | _ -> false
+
+    let is_empty t = is_empty_tree t.tree
+
+    let rec mem_tree x = function
+        Empty -> false
+      | Node(l, v, r, _) ->
+          let c = Ord.compare x v in
+          c = 0 || mem_tree x (if c < 0 then l else r)
+	    
+    let mem x t = mem_tree x t.tree
+
+    let singleton x = 
+      { tree = Node(Empty, x, Empty, 1);
+	card = Lazy.lazy_from_val 1; }
+
+    let rec remove_tree x = function
+        Empty -> (Empty, false)
+      | Node(l, v, r, _) ->
+          let c = Ord.compare x v in
+          if c = 0 then (merge l r, true) else
+	  if c < 0 then
+	    let tree, found = remove_tree x l in
+	    (bal tree v r, found) 
+	  else 
+	    let tree, found = remove_tree x r in
+	    (bal l v tree, found)
+
+    let remove x t =
+      let tree, found = remove_tree x t.tree in
+      if found then
+	{ tree = tree;
+	  card = if Lazy.lazy_is_val t.card then
+	    Lazy.lazy_from_val ((Lazy.force_val t.card) - 1)
+	  else
+	    lazy (cardinal_tree tree) }
+      else t
+
+    let rec union_tree s1 s2 =
+      match (s1, s2) with
+        (Empty, t2) -> t2
+      | (t1, Empty) -> t1
+      | (Node(l1, v1, r1, h1), Node(l2, v2, r2, h2)) ->
+          if h1 >= h2 then
+            if h2 = 1 then add_tree v2 s1 else begin
+              let (l2, _, r2) = split_tree v1 s2 in
+              join (union_tree l1 l2) v1 (union_tree r1 r2)
+            end
+          else
+            if h1 = 1 then add_tree v1 s2 else begin
+              let (l1, _, r1) = split_tree v2 s1 in
+              join (union_tree l1 l2) v2 (union_tree r1 r2)
+            end
+
+    let union s1 s2 =
+      t_of_tree (union_tree s1.tree s2.tree)
+
+    let rec inter_tree s1 s2 =
+      match (s1, s2) with
+        (Empty, t2) -> Empty
+      | (t1, Empty) -> Empty
+      | (Node(l1, v1, r1, _), t2) ->
+          match split_tree v1 t2 with
+            (l2, false, r2) ->
+              concat (inter_tree l1 l2) (inter_tree r1 r2)
+          | (l2, true, r2) ->
+              join (inter_tree l1 l2) v1 (inter_tree r1 r2)
+
+    let inter s1 s2 =
+      t_of_tree (inter_tree s1.tree s2.tree)
+
+    let rec diff_tree s1 s2 =
+      match (s1, s2) with
+        (Empty, t2) -> Empty
+      | (t1, Empty) -> t1
+      | (Node(l1, v1, r1, _), t2) ->
+          match split_tree v1 t2 with
+            (l2, false, r2) ->
+              join (diff_tree l1 l2) v1 (diff_tree r1 r2)
+          | (l2, true, r2) ->
+              concat (diff_tree l1 l2) (diff_tree r1 r2)
+
+    let diff s1 s2 =
+      t_of_tree (diff_tree s1.tree s2.tree)
+
+    type enumeration = End | More of elt * btree * enumeration
+
+    let rec cons_enum s e =
+      match s with
+        Empty -> e
+      | Node(l, v, r, _) -> cons_enum l (More(v, r, e))
+
+    let rec compare_aux e1 e2 =
+        match (e1, e2) with
+        (End, End) -> 0
+      | (End, _)  -> -1
+      | (_, End) -> 1
+      | (More(v1, r1, e1), More(v2, r2, e2)) ->
+          let c = Ord.compare v1 v2 in
+          if c <> 0
+          then c
+          else compare_aux (cons_enum r1 e1) (cons_enum r2 e2)
+
+    let compare_tree s1 s2 =
+      compare_aux (cons_enum s1 End) (cons_enum s2 End)
+
+    let compare s1 s2 =
+      compare_tree s1.tree s2.tree
+
+    let equal s1 s2 =
+      compare s1 s2 = 0
+
+    let rec subset_tree s1 s2 =
+      match (s1, s2) with
+        Empty, _ ->
+          true
+      | _, Empty ->
+          false
+      | Node (l1, v1, r1, _), (Node (l2, v2, r2, _) as t2) ->
+          let c = Ord.compare v1 v2 in
+          if c = 0 then
+            subset_tree l1 l2 && subset_tree r1 r2
+          else if c < 0 then
+            subset_tree (Node (l1, v1, Empty, 0)) l2 && subset_tree r1 t2
+          else
+            subset_tree (Node (Empty, v1, r1, 0)) r2 && subset_tree l1 t2
+
+    let subset s1 s2 =
+      subset_tree s1.tree s2.tree
+
+    let rec iter_tree f = function
+        Empty -> ()
+      | Node(l, v, r, _) -> iter_tree f l; f v; iter_tree f r
+
+    let iter f t =
+      iter_tree f t.tree
+
+    let rec fold_tree f s accu =
+      match s with
+        Empty -> accu
+      | Node(l, v, r, _) -> fold_tree f r (f v (fold_tree f l accu))
+
+    let fold f s accu =
+      fold_tree f s.tree accu
+
+    let rec for_all_tree p = function
+        Empty -> true
+      | Node(l, v, r, _) -> p v && for_all_tree p l && for_all_tree p r
+
+    let for_all p t =
+      for_all_tree p t.tree
+
+    let rec exists_tree p = function
+        Empty -> false
+      | Node(l, v, r, _) -> p v || exists_tree p l || exists_tree p r
+
+    let exists p t =
+      exists_tree p t.tree
+
+    let filter_tree p s =
+      let rec filt accu = function
+        | Empty -> accu
+        | Node(l, v, r, _) ->
+            filt (filt (if p v then add_tree v accu else accu) l) r in
+      filt Empty s
+
+    let filter p s =
+      t_of_tree (filter_tree p s.tree)
+
+    let partition_tree p s =
+      let rec part (t, f as accu) = function
+        | Empty -> accu
+        | Node(l, v, r, _) ->
+            part (part (if p v then (add_tree v t, f) else (t, add_tree v f)) l) r in
+      part (Empty, Empty) s
+
+    let partition p s =
+      let tree1, tree2 = partition_tree p s.tree in
+      (t_of_tree tree1, t_of_tree tree2)
+
+    let rec elements_aux accu = function
+        Empty -> accu
+      | Node(l, v, r, _) -> elements_aux (v :: elements_aux accu r) l
+
+    let elements s =
+      elements_aux [] s.tree
+
+    let choose = min_elt 
+
+  end
