~ubuntu-branches/ubuntu/trusty/coccinelle/trusty-proposed

« back to all changes in this revision

Viewing changes to commons/seti.ml

  • Committer: Bazaar Package Importer
  • Author(s): Євгеній Мещеряков
  • Date: 2010-06-21 14:54:01 UTC
  • mfrom: (7.1.6 sid)
  • Revision ID: james.westby@ubuntu.com-20100621145401-virakyz93l2mwbk4
Tags: 0.2.2.deb-2
* Build depend on libpycaml-ocaml-dev and remove build dependency on
  python2.5-dev (closes: #567942)
* Urgency medium because of fix for an RC bug
* Updated patches:
  - system-pycaml.diff - do not add pycaml to ocamldep include path,
    otherwise it tries to compile pycaml.ml
* Use dh-ocaml to calculate dependencies

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 
6
6
(* todo: could take an incr/decr func in param, to make it generic
7
7
 * opti: remember the min/max (optimisation to have intersect biggest x -> x)
8
 
 * opti: avoid all those rev, and avoid the intervise 
 
8
 * opti: avoid all those rev, and avoid the intervise
9
9
 * (but yes the algo are then more complex :)
10
 
 * opti: balanced set intervalle 
 
10
 * opti: balanced set intervalle
11
11
*)
12
12
 
13
13
(*****************************************************************************)
14
14
type seti = elt list (* last elements is in first pos, ordered reverse *)
15
15
  and elt = Exact of int | Interv of int * int
16
16
 
17
 
(* invariant= ordered list, no incoherent interv (one elem or zero elem), 
 
17
(* invariant= ordered list, no incoherent interv (one elem or zero elem),
18
18
 * merged (intervalle are separated) *)
19
 
let invariant xs = 
20
 
  let rec aux min xs = 
21
 
    xs +> List.fold_left (fun min e -> 
22
 
      match e with 
23
 
      | Exact i -> 
 
19
let invariant xs =
 
20
  let rec aux min xs =
 
21
    xs +> List.fold_left (fun min e ->
 
22
      match e with
 
23
      | Exact i ->
24
24
          if i <= min then pr2 (sprintf "i = %d, min = %d" i min);
25
25
          (* todo: should be even stronger, shoud be i > min+1 *)
26
26
          assert (i > min);
27
27
          i
28
 
      | Interv (i,j) -> 
 
28
      | Interv (i,j) ->
29
29
          assert (i > min);
30
30
          assert (j > i);
31
31
          j
32
32
    ) min
33
33
  in
34
 
  ignore(aux min_int (List.rev xs)); 
 
34
  ignore(aux min_int (List.rev xs));
35
35
  ()
36
36
 
37
 
let string_of_seti xs = 
38
 
  "[" ^  
39
 
    join "," (xs +> List.rev +> map (function 
 
37
let string_of_seti xs =
 
38
  "[" ^
 
39
    join "," (xs +> List.rev +> map (function
40
40
    | (Exact i) -> string_of_int i
41
41
    | (Interv (i,j)) -> Printf.sprintf "%d - %d" i j)) ^
42
42
    "]"
46
46
 
47
47
let pack newi j = function
48
48
  | [] -> [Interv (newi,j)]
49
 
  | (Exact z)::xs -> 
 
49
  | (Exact z)::xs ->
50
50
      (Interv (newi, j))::(if newi =|= z then xs else (Exact z)::xs)
51
 
  | (Interv (i', j'))::xs -> 
52
 
      if newi =|= j' 
 
51
  | (Interv (i', j'))::xs ->
 
52
      if newi =|= j'
53
53
      then (Interv (i', j))::xs  (* merge *)
54
54
      else (Interv (newi, j))::(Interv (i', j'))::xs
55
 
        
 
55
 
56
56
 
57
57
(* the only possible merges are when x = i-1, otherwise, the job is done before *)
58
 
let rec (add2: int -> seti -> seti) = fun x -> function 
 
58
let rec (add2: int -> seti -> seti) = fun x -> function
59
59
  | [] -> [Exact x]
60
60
  | (Exact i)::xs when x > i+1 -> (Exact x)::(Exact i)::xs
61
61
  | (Interv (i,j)::xs) when x > j+1 -> (Exact x)::(Interv (i,j))::xs
62
62
  | (Interv (i,j)::xs) when x =|= j+1 -> (Interv (i,x))::xs
63
63
  | (Exact i)::xs when x =|= i+1 -> (Interv (i,x))::xs
64
 
      
 
64
 
65
65
  | (Exact i)::xs when i =|= x   -> (Exact i)::xs
66
66
  | (Interv (i,j)::xs) when x <= j && x >= i -> (Interv (i,j))::xs
67
 
  | other -> 
 
67
  | other ->
68
68
(*         let _ = log "Cache miss" in *)
69
69
      let _ = count2 () in
70
70
      (match other with
71
 
      |       (Exact i)::xs when x =|= i-1 -> pack x i xs 
 
71
      |       (Exact i)::xs when x =|= i-1 -> pack x i xs
72
72
      |       (Exact i)::xs when x < i-1 -> (Exact i)::add x xs
73
 
                
 
73
 
74
74
      |       (Interv (i,j)::xs) when x =|= i-1 -> pack x j xs
75
75
      |       (Interv (i,j)::xs) when x < i-1 -> (Interv (i,j))::add x xs
76
76
      |       _ -> raise Impossible
77
77
      )
78
 
and add x y = let _ = count5 () in add2 x y                                                         
79
 
 
80
 
            
 
78
and add x y = let _ = count5 () in add2 x y
 
79
 
 
80
 
81
81
let rec tolist2 = function
82
82
  | [] -> []
83
83
  | (Exact i)::xs -> i::tolist2 xs
84
 
  | (Interv (i,j))::xs -> enum i j @ tolist2 xs 
 
84
  | (Interv (i,j))::xs -> enum i j @ tolist2 xs
85
85
let rec tolist xs = List.rev (tolist2 xs)
86
86
 
87
87
let rec fromlist = function xs -> List.fold_left (fun a e -> add e a) empty xs
95
95
let exactize2 x y = if x =|= y then Exact x else Interv (x,y)
96
96
 
97
97
 
98
 
let rec (remove: int -> seti -> seti) = fun x xs -> 
 
98
let rec (remove: int -> seti -> seti) = fun x xs ->
99
99
  match xs with
100
100
  | [] -> [] (*  pb, not in  *)
101
 
  | (Exact z)::zs -> 
 
101
  | (Exact z)::zs ->
102
102
      (match x <=> z with
103
103
      | Equal -> zs
104
104
      | Sup -> xs  (*  pb, not in *)
105
105
      | Inf -> (Exact z)::remove x zs
106
 
      ) 
107
 
  | (Interv (i,j)::zs) -> 
 
106
      )
 
107
  | (Interv (i,j)::zs) ->
108
108
      if x > j then xs (*  pb not in *)
109
 
      else 
 
109
      else
110
110
        if x >= i && x <= j then
111
111
          (
112
112
            let _ = assert (j > i) in (* otherwise can lead to construct seti such as [7,6] when removing 6 from [6,6] *)
126
126
let _ = assert_equal (remove 4 [Interv (3, 4)])  [Exact (3);]
127
127
(* let _ = example (try (ignore(remove 6 [Interv (6, 6)] = []); false) with _ -> true)   *)
128
128
 
129
 
    
 
129
 
130
130
let rec mem e = function
131
 
  | [] -> false 
132
 
  | (Exact x)::xs -> 
 
131
  | [] -> false
 
132
  | (Exact x)::xs ->
133
133
      (match e <=> x with
134
134
      | Equal -> true
135
135
      | Sup -> false
136
136
      | Inf -> mem e xs
137
 
      ) 
138
 
  | (Interv (i,j)::xs) -> 
 
137
      )
 
138
  | (Interv (i,j)::xs) ->
139
139
      if e > j then false
140
 
      else 
 
140
      else
141
141
        if e >= i && e <= j then true
142
142
      else mem e xs
143
143
 
144
 
let iter f xs = xs +> List.iter 
 
144
let iter f xs = xs +> List.iter
145
145
  (function
146
146
  | Exact i -> f i
147
147
  | Interv (i, j) -> for k = i to j do f k done
148
148
  )
149
 
        
 
149
 
150
150
let is_empty xs = xs =*= []
151
151
let choose = function
152
152
  | [] -> failwith "not supposed to be called with empty set"
153
153
  | (Exact i)::xs -> i
154
154
  | (Interv (i,j))::xs -> i
155
 
      
 
155
 
156
156
let elements xs = tolist xs
157
157
let rec cardinal = function
158
158
  | [] -> 0
159
159
  | (Exact _)::xs -> 1+cardinal xs
160
160
  | (Interv (i,j)::xs) -> (j-i) +1 + cardinal xs
161
 
      
 
161
 
162
162
(*****************************************************************************)
163
163
(*  TODO: could return corresponding osetb ? *)
164
 
let rec inter xs ys = 
165
 
  let rec aux = fun xs ys -> 
 
164
let rec inter xs ys =
 
165
  let rec aux = fun xs ys ->
166
166
    match (xs, ys) with
167
167
    | (_, []) -> []
168
168
    | ([],_)  -> []
169
 
    | (x::xs, y::ys) -> 
 
169
    | (x::xs, y::ys) ->
170
170
        (match (x, y) with
171
 
        | (Interv (i1, j1), Interv (i2, j2)) -> 
 
171
        | (Interv (i1, j1), Interv (i2, j2)) ->
172
172
            (match i1 <=> i2 with
173
 
            | Equal -> 
 
173
            | Equal ->
174
174
                (match j1 <=> j2 with
175
175
                | Equal -> (Interv (i1,j1))::aux xs ys
176
176
                    (*  [  ] *)
182
182
                    (*  [    ] *)
183
183
                    (*  [ ] [       same *)
184
184
                )
185
 
            | Inf -> 
 
185
            | Inf ->
186
186
                if j1 < i2 then aux xs (y::ys) (* need order ? *)
187
187
                  (*  [    ] *)
188
188
                  (*         [ ] *)
189
 
                else 
 
189
                else
190
190
                  (match j1 <=> j2 with
191
191
                  | Equal -> (Interv (i2, j1))::aux xs ys
192
192
                      (*  [    ] *)
205
205
  in
206
206
  (* TODO avoid the rev rev, but aux good ? need order ?  *)
207
207
  List.rev_map exactize (aux (List.rev_map intervise xs) (List.rev_map intervise ys))
208
 
      
209
 
let union xs ys = 
210
 
  let rec aux = fun xs ys -> 
 
208
 
 
209
let union xs ys =
 
210
  let rec aux = fun xs ys ->
211
211
    match (xs, ys) with
212
212
    | (vs, []) -> vs
213
213
    | ([],vs)  -> vs
214
 
    | (x::xs, y::ys) -> 
 
214
    | (x::xs, y::ys) ->
215
215
        (match (x, y) with
216
 
        | (Interv (i1, j1), Interv (i2, j2)) -> 
 
216
        | (Interv (i1, j1), Interv (i2, j2)) ->
217
217
            (match i1 <=> i2 with
218
 
            | Equal -> 
 
218
            | Equal ->
219
219
                (match j1 <=> j2 with
220
220
                | Equal -> (Interv (i1,j1))::aux xs ys
221
221
                    (*  [  ] *)
227
227
                    (*  [    ] *)
228
228
                    (*  [ ] [       same *)
229
229
                )
230
 
            | Inf -> 
 
230
            | Inf ->
231
231
                if j1 < i2 then Interv (i1, j1):: aux xs (y::ys)
232
232
                  (*  [    ] *)
233
233
                  (*         [ ] *)
234
 
                else 
 
234
                else
235
235
                  (match j1 <=> j2 with
236
236
                  | Equal -> (Interv (i1, j1))::aux xs ys
237
237
                      (*  [    ] *)
255
255
 * not very strong, should return (Interv (1,4)) *)
256
256
(* let _ = Example (union [Interv (1, 4)] [Interv (1, 3)] = ([Exact 4; Interv (1,3)])) *)
257
257
 
258
 
let diff xs ys = 
259
 
  let rec aux = fun xs ys -> 
 
258
let diff xs ys =
 
259
  let rec aux = fun xs ys ->
260
260
    match (xs, ys) with
261
261
    | (vs, []) -> vs
262
262
    | ([],vs)  -> []
263
 
    | (x::xs, y::ys) -> 
 
263
    | (x::xs, y::ys) ->
264
264
        (match (x, y) with
265
 
        | (Interv (i1, j1), Interv (i2, j2)) -> 
 
265
        | (Interv (i1, j1), Interv (i2, j2)) ->
266
266
            (match i1 <=> i2 with
267
 
            | Equal -> 
 
267
            | Equal ->
268
268
                (match j1 <=> j2 with
269
269
                | Equal -> aux xs ys
270
270
                    (*  [  ] *)
276
276
                    (*  [    ] *)
277
277
                    (*  [ ]  *)
278
278
                )
279
 
            | Inf -> 
 
279
            | Inf ->
280
280
                if j1 < i2 then Interv (i1, j1):: aux xs (y::ys)
281
281
                  (*  [    ] *)
282
282
                  (*         [ ] *)
283
 
                else 
 
283
                else
284
284
                  (match j1 <=> j2 with
285
285
                  | Equal -> (Interv (i1, i2-1))::aux xs ys (* -1 cos exlude [ *)
286
286
                      (*  [    ] *)
292
292
                      (*  [       ] *)
293
293
                      (*     [ ]  *)
294
294
                  )
295
 
            | Sup -> 
 
295
            | Sup ->
296
296
                if j2 < i1 then aux (x::xs) ys
297
297
                  (*       [    ] *)
298
298
                  (*  [ ] *)
299
 
                else 
 
299
                else
300
300
                  (match j1 <=> j2 with
301
301
                  | Equal -> aux xs ys
302
302
                      (*         [    ] *)
317
317
 
318
318
 
319
319
(*     let _ = Example (diff [Interv (3,7)] [Interv (4,5)] = [Interv (6, 7); Exact 3]) *)
320
 
 
 
320
 
321
321
(*****************************************************************************)
322
322
let rec debug = function
323
323
  | [] -> ""
327
327
(*****************************************************************************)
328
328
(* if operation return wrong result, then may later have to patch them *)
329
329
let patch1 xs = List.map exactize xs
330
 
let patch2 xs = xs +> List.map (fun e -> 
 
330
let patch2 xs = xs +> List.map (fun e ->
331
331
  match e with
332
 
  | Interv (i,j) when i > j && i =|= j+1 -> 
 
332
  | Interv (i,j) when i > j && i =|= j+1 ->
333
333
      let _ = pr2 (sprintf "i = %d, j = %d" i j) in
334
334
      Exact i
335
335
  | e -> e
336
336
)
337
 
let patch3 xs = 
338
 
  let rec aux min xs = 
339
 
    xs +> List.fold_left (fun (min,acc) e -> 
340
 
      match e with 
341
 
      | Exact i -> 
342
 
          if i =|= min 
 
337
let patch3 xs =
 
338
  let rec aux min xs =
 
339
    xs +> List.fold_left (fun (min,acc) e ->
 
340
      match e with
 
341
      | Exact i ->
 
342
          if i =|= min
343
343
          then (min, acc)
344
344
          else (i, (Exact i)::acc)
345
 
      | Interv (i,j) -> 
 
345
      | Interv (i,j) ->
346
346
          (j, (Interv (i,j)::acc))
347
347
    ) (min, [])
348
348
  in