discover self IP via DC UserIP
[mldonkey.git] / src / utils / cdk / list2.ml
blob0dc7a92ee38574646d2eb185296a479f65476239
1 (* Copyright 2001, 2002 b8_bavard, b8_fee_carabine, INRIA *)
2 (*
3 This file is part of mldonkey.
5 mldonkey is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 mldonkey is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with mldonkey; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 let rec removeq_rec ele list tail =
21 match list with
22 [] -> List.rev tail
23 | e :: list ->
24 if e == ele then removeq_rec ele list tail
25 else
26 removeq_rec ele list (e :: tail)
28 let rec removeq ele list =
29 removeq_rec ele list []
31 let rec remove_rec ele list tail =
32 match list with
33 [] -> List.rev tail
34 | e :: list ->
35 if e = ele then remove_rec ele list tail
36 else
37 remove_rec ele list (e :: tail)
39 let remove ele list =
40 remove_rec ele list []
42 let rec remove_one_rec ele list tail =
43 match list with
44 [] -> List.rev tail
45 | e :: list ->
46 if e = ele then
47 List.rev_append tail list
48 else
49 remove_one_rec ele list (e :: tail)
51 let remove_one ele list =
52 remove_rec ele list []
54 let rec removeq_first ele list =
55 match list with
56 e :: tail when e == ele -> tail
57 | e :: tail -> e :: (removeq_first ele tail)
58 | _ -> []
60 let rec remove_first ele list =
61 match list with
62 e :: tail when e = ele -> remove_first ele tail
63 | e :: tail -> e :: (remove_first ele tail)
64 | _ -> []
66 let rec cut_rec n list r =
67 match n, list with
68 (0,_) | (_, []) -> List.rev r, list
69 | _, x :: tail ->
70 cut_rec (n-1) tail (x :: r)
72 let cut n list =
73 if n < 0 then failwith "List2.sub: invalid parameter";
74 cut_rec n list []
76 let tail_map f list =
77 List.rev (List.rev_map f list)
80 let rec assoc_inv x = function
81 [] -> raise Not_found
82 | (a,b)::l -> if b = x then a else assoc_inv x l
84 let safe_iter f list =
85 List.iter (fun v -> try f v with _ -> ()) list
87 let min list =
88 let rec iter m tail =
89 match tail with
90 [] -> m
91 | m' :: tail ->
92 iter (min m' m) tail
94 match list with
95 [] -> raise Not_found
96 | m :: tail -> iter m tail
98 let max list =
99 let rec iter m tail =
100 match tail with
101 [] -> m
102 | m' :: tail ->
103 iter (max m' m) tail
105 match list with
106 [] -> raise Not_found
107 | m :: tail -> iter m tail
109 let shuffle list =
110 let a = Array.of_list list in
111 let len = Array.length a in
112 for i = 0 to len-1 do
113 let p = Random.int (len-1) in
114 let tmp = a.(i) in
115 a.(i) <- a.(p);
116 a.(p) <- tmp;
117 done;
118 Array.to_list a
120 let filter_map f =
121 List.fold_left (fun acc x -> match f x with Some y -> y :: acc | None -> acc) []