Repository navigation
Expand file tree
/
Copy pathcf2cs.ml
More file actions
7528 lines (6867 loc) · 207 KB
/
Copy pathcf2cs.ml
File metadata and controls
7528 lines (6867 loc) · 207 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(*
For using the interpreter:
ocaml -I /home/hlp/local/lib/ocaml/site-lib/res res.cma unix.cma str.cma
*)
module MyStrat = struct
type t = float * float * int
let default = 1.5, 0.1, 3 (* was 16 in library *)
let grow (waste, _, min_size) resizer new_len =
resizer (max (truncate (float new_len *. waste)) min_size)
let shrink (waste, shrink_trig, min_size) resizer rlen new_len =
if rlen > min_size && truncate (float rlen *. shrink_trig) > new_len then
resizer (max (truncate (float new_len *. waste)) min_size)
end
open Format
module MySmallArray = Res.MakeArray(MyStrat)
module MyArray = Res.Array
module MyBits = Res.Bits
let init_hash_size = 0
(* let list_map = List.map *)
let list_map = List.rev_map
(*****************************************************)
(***************** Interface ********************)
(*****************************************************)
let mystdout = ref(stdout)
let print_char c = output_char !mystdout c
let print_string s = output_string !mystdout s
let print_int i = output_string !mystdout (string_of_int i)
let print_float f = output_string !mystdout (string_of_float f)
let print_endline s =
output_string !mystdout s; output_char !mystdout '\n'; flush !mystdout
let print_newline () = output_char !mystdout '\n'; flush !mystdout
let set_stdout c = mystdout := c
let reset_stdout () = mystdout := stdout
(*****************************************************)
(***************** Auxiliary ********************)
(*****************************************************)
let list_iter_pair f lst =
let rec aux_l l = function
[] -> ()
| l2::lst ->
f l l2;
aux_l l lst in
let rec aux = function
[] -> ()
| l::lst ->
aux_l l lst;
aux lst
in aux lst
(* including L,L *)
let list_iter_pair_same f lst =
let rec aux_l l = function
[] -> ()
| l2::lst ->
f l l2;
aux_l l lst in
let rec aux = function
[] -> ()
| l::lst ->
aux_l l (l::lst);
aux lst
in aux lst
let print_list_int lst =
print_string "{ ";
List.iter (fun x ->
print_int x;
print_string " ") lst;
print_endline "}"
let false_fun _ = false
let string_starts_with s pre =
let rec aux i =
if i = -1 then
true
else (s.[i] = pre.[i])
&& (aux (i-1)) in
let len_pre = (String.length pre)
in if len_pre > (String.length s) then
false
else
aux (len_pre-1)
let combs_hits_all llist =
let rec aux acc = function
[] -> acc
| a::lst ->
if a == [] then
aux acc lst
else
aux
(List.concat
(list_map
(fun ai ->
(list_map (fun li -> ai::li) acc))
a))
lst
in match llist with
[] -> []
| a::lst ->
aux (list_map (fun l->[l]) a)
lst
let combs_hits_one llist =
let rec aux = function
[] -> []
| []::lst ->
aux lst
| (init::_)::lst ->
init::(aux lst)
in [aux llist]
module Timed =
struct
let report_time_str msg t =
begin
print_string "************* Time elapsed in ";
print_string msg;
print_string ": ";
print_string t;
print_endline "\n";
end
let report_time msg {Unix.tms_utime = u } =
report_time_str
msg
(match Unix.times()
with {Unix.tms_utime = u2 } -> string_of_float (u2-.u))
let call msg f =
let t = Unix.times() in
let res =
try
f ()
with Not_found as a ->
print_endline ("Not_found exception raised while calling "^msg);
raise a
in begin
report_time msg t;
res
end
end
module OrderedString =
struct
type t = string
let compare = compare
end
module SetString = Set.Make(OrderedString)
module MapString = Map.Make(OrderedString)
module OrderedInt =
struct
type t = int
let compare = compare
end
module MapInt = Map.Make(OrderedInt)
module Verbose =
struct
let level0 = 10
let level1 = 20
let level2 = 30
let level = ref(level1)
end
(*****************************************************)
(***************** Types ********************)
(*****************************************************)
(* *)
module type AbstractAtom =
sig
type t
val compare: t -> t -> int
val equal: t -> t -> bool
val hash: t -> int
val to_text: t -> string
val nil: t
end
module type FACTORY =
sig
type element
type t
val reset : unit -> unit
val make : element -> t
val tbl_iter :
(element -> t -> unit) -> unit
val tbl_fold :
(element -> t -> 'a -> 'a) -> 'a -> 'a
val tbl_mem : element -> bool
val num_elements : unit -> int
val get : t -> element
val num : t -> int
val compare : t -> t -> int
val equal : t -> t -> bool
val hash : t -> int
val to_text : t -> string
val nil : t
end
let use_now_short_names = ref(false)
module Factory (AA:AbstractAtom) : (FACTORY with type element = AA.t) =
struct
type element = AA.t
type t = int * element
let init_i = 0
let i = ref init_i (* safe for reentrant is use reset() *)
module InternalHash = Hashtbl.Make(AA)
let new_tbl() = InternalHash.create init_hash_size
(* safe for reentrant is use reset() *)
let tbl = ref( new_tbl() )
let reset () =
begin
i := init_i;
tbl := new_tbl();
end
let make x =
try
InternalHash.find !tbl x
with Not_found ->
begin
incr i;
let res = !i, x
in
begin
InternalHash.add !tbl x res;
res
end
end
(* In general these function go over
all used atoms.
For Factory of KL/t, a function asking whether
a KL/t does exist, should use is_tagged,
and avoid make_atom.
It won't case a soundness problem though.
*)
let tbl_iter f =
InternalHash.iter f !tbl
let tbl_fold f i =
InternalHash.fold f !tbl i
let tbl_mem l =
InternalHash.mem !tbl l
let num_elements () = !i
let get ((_, a):t) = a
let num ((n, _):t) = n
let compare ((a,_):t) ((b,_):t) = compare a b
let equal ((a,_):t) ((b,_):t) = a == b
let hash ((a,_ ):t) = a
let base_name_l_1 = "bcghjklmqrtuvxyz"
let base_name_l_2 = "abcdefghijklmnopqsrtuvwxyz1234567890"
let append_one res src value =
let i = value mod String.length src
in (res ^ (String.sub src i 1),
value / String.length src)
let c value =
let rec c1 (acc,value) =
if value == 0 then
acc
else
c1 (append_one acc base_name_l_2 value)
in c1 (append_one "" base_name_l_1 value)
let to_text ((n,a):t) =
if !use_now_short_names then (* OJO: global var! BAD *)
c n
else
AA.to_text a
(* let to_text ((_,a):t) = AA.to_text a *)
(* let to_text (n,a) = (AA.to_text a)^"-"^(string_of_int n) *)
let (nil:t) = (0,AA.nil)
end
module Mylist =
struct
module T = MySmallArray
type 'a p = 'a * int
type 'a t = 'a T.t
let empty () = T.empty()
let clear (l: 'a t) = T.clear l
let length (l: 'a t) = T.length l
let is_empty (l: 'a t) = length l == 0
let get (l: 'a t) i = T.get l i
let set (l: 'a t) i = T.set l i
let iter f (l: 'a t) =
T.iter f l
let iteri f (l: 'a t) =
T.iteri f l
let add x (l: 'a t) =
T.add_one l x
let swap (l: 'a t) i j =
T.swap l i j
let partition f (l: 'a t) =
T.partition f l
let concat (ll: 'a t list) =
T.concat ll
let find f l =
try
let value = ref(get l 0) (* safe: reentrant *)
in
(try
T.iter
(fun it ->
if f it then
begin
value := it;
raise (Failure "");
end)
l;
raise Not_found
with
Failure "" -> !value)
with _ -> raise Not_found
let fold f init (l: 'a t) =
T.fold_left f init l
let append_to (l1: 'a t) (l2: 'a t) =
iter
(fun it2 -> add it2 l1)
l2
let map f (l: 'a t) =
let (res: 'b t) = T.emptyn (length l)
in iter
(fun it -> T.add_one res (f it))
l;
res
let map_flat f (l: 'a t) =
let (res: 'b t) = T.emptyn (length l)
in iter
(fun it ->
List.iter
(fun vi -> T.add_one res vi)
(f it))
l;
res
let map_same f (l: 'a t) =
T.iteri
(fun i it -> T.set l i (f it))
l
let map_from_list f l =
let (res: 'b t) = empty()
in List.iter
(fun it -> T.add_one res (f it))
l;
res
let copy l =
map (fun x -> x) l
(* use carefully. Change order of elements. You index can loose sense. *)
(* safe strategy for bunch of deletions: from higher index to lower *)
let remove_i i (l: 'a t) =
begin
if i != T.lix l then
T.set l i (T.get l (T.lix l));
T.remove_one l;
end
let rev (l: 'a t) =
let (res: 'b t) = T.emptyn (length l)
in for i = (length l)-1 downto 0 do
T.add_one res (T.get l i)
done;
res
(* keeps elt st (f elt) *)
let filter f (l: 'a t) =
(* Don't use T.filter_in_place because we
allow to change the order: more efficient *)
let index_to_del = ref([]) (* safe: reentrant *)
in begin
T.iteri
(fun i it -> if not(f it) then
index_to_del := i::!index_to_del)
l;
List.iter
(fun i -> remove_i i l)
!index_to_del;
(* It is not necessary to do:
(List.rev (List.sort compare !index_to_del))
as they come in that order *)
end
let filter_copy f (l: 'a t) =
let res = copy l
in T.filter f res
let filter_and_map f (l: 'a t) =
let index_to_del = ref([]) (* safe: reentrant *)
in begin
T.iteri
(fun i it ->
let (keep, value) = f it
in if keep then
set l i value
else
index_to_del := i::!index_to_del
)
l;
if false then
begin
print_string "Index_to_del: ";
List.iter
(fun i -> print_int i; print_string " ")
!index_to_del;
print_newline();
end;
List.iter
(fun i -> remove_i i l)
!index_to_del;
(* It is not necessary to do:
(List.rev (List.sort compare !index_to_del))
as they come in that order *)
end
let exists f (l: 'a t) =
T.exists f l
let for_all f (l: 'a t) =
T.for_all f l
let to_list l =
fold (fun acc it -> it::acc) [] l
let iter_pair_i f (lst: 'a t) =
let rec aux_l i j =
if j < 0 then ()
else
begin
f i j;
aux_l i (j-1)
end in
let rec aux j =
if j < 0 then ()
else
begin
aux_l j (j-1);
aux (j-1)
end
in aux ((length lst)-1)
let iter_pair f (lst: 'a t) =
iter_pair_i
(fun i j -> f (get lst i) (get lst j))
lst
let iter_pair_i2 f (lst: 'a t) (lst2: 'a t) =
let rec aux_l i j =
if j < 0 then ()
else
begin
f i j;
aux_l i (j-1)
end in
let rec aux i j =
if i < 0 then ()
else
begin
aux_l i j;
aux (i-1) j;
end
in aux ((length lst)-1) ((length lst2)-1)
let iter_pair2 f (lst: 'a t) (lst2: 'a t) =
iter_pair_i2
(fun i j -> f (get lst i) (get lst2 j))
lst
lst2
end
module type AbstractObj =
sig
type t
val compare: t -> t -> int
val equal: t -> t -> bool
val hash: t -> int
val num: t -> int (* Should be >= 0, because can use a repository indexed from 0 *)
val nil: t
end
(* Small footprint, maybe slow
Functional.
*)
module SmallSet(AO:AbstractObj) =
struct
module T = Set.Make(AO)
type t = T.t
let empty = T.empty
let fold = T.fold
let iter = T.iter
let add = T.add
let mem = T.mem
let remove = T.remove
let for_all = T.for_all
let exists = T.exists
let diff = T.diff
let cardinal = T.cardinal
let choose = T.choose
let filter f s =
let aux e ns =
if f e then
add e ns
else
ns
in T.fold aux s empty
let remove_list_of l s =
let f acc elt =
T.remove elt acc
in List.fold_left f s l
let add_list_to l s =
let f acc elt =
T.add elt acc
in List.fold_left f s l
let add_mylist_to l s =
let f acc elt =
T.add elt acc
in Mylist.fold f s l
let of_list ll =
let acc_list acc l =
T.add l acc
in List.fold_left acc_list T.empty ll
let of_mylist ll =
let acc_list acc l =
T.add l acc
in Mylist.fold acc_list T.empty ll
let list_of sl =
T.elements sl
let mylist_of sl =
let res = Mylist.empty ()
in iter (fun elt -> Mylist.add elt res) sl;
res
end
module IntAO =
struct
type t = int
let compare = compare
let equal x y = (x == y)
let hash x = x
let num x = if x > 0 then 2*x else (-2*x)+1
let nil = 0
end
module SmallSetInt = SmallSet(IntAO)
module SmallMap(AO:AbstractObj) =
struct
module T = Map.Make(AO)
type 'a t = 'a T.t
let empty = T.empty
let fold = T.fold
let iter = T.iter
let add = T.add
let mem = T.mem
let find = T.find
end
module type SPARSESET =
sig
type t
type element
val cardinal : t -> int
val length : t -> int (* quickier but inexact *)
val copy : t -> t
val empty : unit -> t
val iter : (element -> unit) -> t -> unit
val add : element -> t -> unit
val remove : element -> t -> unit
val mem : t -> element -> bool
val fold : (element -> 'a -> 'a) -> t -> 'a -> 'a
val clear : t -> unit
val fill: t -> int -> unit
val list_of : t -> element list
val mylist_of : t -> element Mylist.t
val of_mylist : element Mylist.t -> t
val add_list_to : element list -> t -> unit
val add_mylist_to : element Mylist.t -> t -> unit
val add_to : t -> t -> unit
val add_f_to : (element -> element) -> t -> t -> unit
val map : (element -> element) -> t -> t
val diff : t -> t -> t
val union : t -> t -> t
val inter : t -> t -> t
val subset : t -> t -> bool
val for_all : (element -> bool) -> t -> bool
end
module type GenSet =
sig
type t
type element
val empty : unit -> t
val add : element -> t -> unit
val fold : (element -> 'a -> 'a) -> t -> 'a -> 'a
val iter : (element -> unit) -> t -> unit
val mem : t -> element -> bool
end
module SetOperations(GSBig:GenSet)(GSSmall:GenSet with type element = GSBig.element) =
struct
let list_of sl =
let acc_set elt acc =
elt::acc
in GSBig.fold acc_set sl []
let mylist_of sl =
let res = Mylist.empty ()
in GSBig.iter (fun elt -> Mylist.add elt res) sl;
res
let of_mylist ll =
let res = GSBig.empty() in
Mylist.iter (fun elt -> GSBig.add elt res) ll;
res
let add_list_to l s =
List.iter (fun elt -> GSBig.add elt s) l
let add_mylist_to l s =
Mylist.iter (fun elt -> GSBig.add elt s) l
let add_to src dst =
GSSmall.iter (fun e -> GSBig.add e dst) src
let add_f_to f src dst =
GSSmall.iter (fun e -> GSBig.add (f e) dst) src
let map f s =
let res = GSBig.empty() in
GSBig.iter (fun elt -> GSBig.add (f elt) res) s;
res
let diff s1 s2 =
let res = GSSmall.empty() in
GSSmall.iter
(fun k ->
if not(GSBig.mem s2 k) then
GSSmall.add k res)
s1;
res
let union s1 s2 =
let res = GSBig.empty() in
add_to s1 res;
add_to s2 res;
res
let inter s1 s2 =
let res = GSSmall.empty ()
in GSSmall.iter
(fun k ->
if GSBig.mem s2 k then
GSSmall.add k res)
s1;
res
let subset s1 s2 =
try
GSSmall.iter
(fun k ->
if not(GSBig.mem s2 k) then
raise (Failure ""))
s1;
true
with Failure _ ->
false
let for_all f s =
try
GSSmall.iter
(fun k ->
if not(f k) then
raise (Failure ""))
s;
true
with Failure _ ->
false
end
(*
Not really small footprint, but fast...
Imperative.
Semantics interchangable with Array versions.
*)
module SetHash(AO:AbstractObj) : (SPARSESET with type element = AO.t) =
struct
module T = Hashtbl.Make(AO) (* with type 'a = unit *)
type t = unit T.t
type element = AO.t
let length (t:t) = T.length t
let cardinal = length
let copy (t:t) = T.copy t
let empty () = (T.create init_hash_size:t)
let iter f (t:t) = T.iter (fun v _ -> f v) t
let fill (t:t) n = ()
let add x (t:t) = T.replace t x ()
let remove x (t:t) = T.remove t x
let mem (t:t) e = T.mem t e
let fold f (t:t) init = T.fold (fun k () acc -> f k acc) t init
let clear (t:t) = T.clear t
module GenSparse =
struct
type element = AO.t
type t = unit T.t
let fold = fold
let add = add
let iter = iter
let empty = empty
let mem = mem
end
include SetOperations(GenSparse)(GenSparse)
end
module SetArray(AO:AbstractObj) : (SPARSESET with type element = AO.t) =
struct
module T = MyArray
type element = AO.t
type t = element T.t
let length (t:t) = T.length t
let copy (t:t) = T.copy t
let empty () = (T.empty():t)
let cardinal (t:t) =
let s = ref(0) (* safe: reentrant *)
in T.iter
(function v ->
if v != AO.nil then
incr s)
t;
!s
let iter f (t:t) =
T.iter
(function v ->
if v != AO.nil then
f v)
t
let fill (t:t) n =
(* fill_space is a function added to Res library to allow directed growing *)
T.fill_space t n AO.nil
let add x (t:t) =
assert(AO.num x >= 0);
fill t (AO.num x);
T.set t (AO.num x) x
let remove x (t:t) =
assert(AO.num x >= 0);
fill t (AO.num x);
T.set t (AO.num x) AO.nil
let mem (t:t) e =
assert(AO.num e >= 0);
fill t (AO.num e);
if (T.get t (AO.num e)) != AO.nil then
begin
assert (T.get t (AO.num e) == e);
true
end
else
false
let fold f (t:t) init =
T.fold_right
(fun k acc ->
if k == AO.nil then
acc
else
f k acc)
t init
let clear (t:t) = T.clear t
module GenArray =
struct
type element = AO.t
type t = element T.t
let fold = fold
let add = add
let iter = iter
let empty = empty
let mem = mem
end
include SetOperations(GenArray)(GenArray)
end
module SparseSet(AO:AbstractObj) = SetHash(AO)
module FastSet(AO:AbstractObj) = SetArray(AO)
module type SPARSEMAP =
sig
type 'a t
type element
val empty : unit -> 'a t
val iter : (element -> 'a -> unit) -> 'a t -> unit
val add : element -> 'a -> 'a t -> unit
val remove : element -> 'a t -> unit
val mem : 'a t -> element -> bool
val find : 'a t -> element -> 'a
val clear : 'a t -> unit
val fill: 'a t -> int -> unit
end
module MapHash(AO:AbstractObj) : (SPARSEMAP with type element = AO.t) =
struct
module T = Hashtbl.Make(AO)
type 'a t = 'a T.t
type element = AO.t
let empty () = T.create init_hash_size
let iter = T.iter
let fill t n = ()
let add x v t = T.replace t x v
let remove x t = T.remove t x
let mem = T.mem
let find = T.find
let clear = T.clear
end
module MapArray(AO:AbstractObj) : (SPARSEMAP with type element = AO.t) =
struct
module T = MyArray
module B = MyBits
type element = AO.t
type 'a t = { ar: (element * 'a) T.t; used: B.t }
let empty () = { ar = T.empty(); used = B.empty() }
let iter f { ar = ar; used = used } =
T.iteri
(fun i (e,v) ->
if B.get used i then
f e v)
ar
(*
Use "Obj.magic 0" as nil value for content.
Obj.magic fools the type system.
It is safe because those values are never accessed
unless the boolean array indicates it have a right value.
*)
let our_nil = (AO.nil, Obj.magic 0)
let fill_arrays ar used n =
T.fill_space ar n our_nil;
B.fill_space used n false
let fill { ar = ar; used = used } n =
fill_arrays ar used n
let add x v { ar = ar; used = used } =
let n = AO.num x in
begin
assert(n >= 0);
fill_arrays ar used n;
T.set ar n (x,v);
B.set used n true
end
let remove x { ar = ar; used = used } =
let n = AO.num x in
assert(n >= 0);
fill_arrays ar used n;
T.set ar n our_nil;
B.set used n false
let mem { ar = ar; used = used } e =
let n = AO.num e in
assert(n >= 0);
fill_arrays ar used n;
B.get used n
let find { ar = ar; used = used } e =
let n = AO.num e in
fill_arrays ar used n;
if B.get used n then
match T.get ar n with
_,v -> v
else
raise Not_found
let clear { ar = ar; used = used } =
T.clear ar;
B.clear used
end
module SparseMap(AO:AbstractObj) = MapHash(AO)
module FastMap(AO:AbstractObj) = MapArray(AO)
module OrderedList(AO:AbstractObj) =
struct
type t = AO.t list
let rec compare l1 l2 = match (l1,l2) with
[],[] -> 0
| [],_ -> -1
| _,[] -> 1
| (a1::l1),(a2::l2) ->
let r = AO.compare a1 a2
in if r != 0 then r
else compare l1 l2
let equal l1 l2 = (compare l1 l2) == 0
let hash_val = 5381
let hash v =
let rec aux acc = function
[] -> acc
| c::l ->
aux (((acc lsl 5) + acc) + (AO.hash c)) l
in aux hash_val v
end
(*
Assumes that list are ordered.
Very slow. Try to use instead a module in KAtom
*)
module SparseSetList(AO:AbstractObj) =
struct
module OrderedListAO = OrderedList(AO)
module T = Hashtbl.Make(OrderedListAO)
type t = unit T.t
let length (t:t) = T.length t
let empty () = (T.create init_hash_size:t)
let iter f (t:t) = T.iter (fun v _ -> f v) t
let add x (t:t) = T.replace t x ()
let remove x (t:t) = T.remove t x
let mem (t:t) e = T.mem t e
let fold f (t:t) init = T.fold (fun k () acc -> f k acc) t init
end
(* Probably very slow *)
module OrderedSet(AO:AbstractObj) =
struct
module SmallSetAO = SmallSet(AO)
type t = SmallSetAO.t
let compare = compare
end
(* Map of Set of AO *)
module MapSetAO(AO:AbstractObj) =
Map.Make(OrderedSet(AO))
(* Set of Set of AO *)
module SetSetAO(AO:AbstractObj) =
Set.Make(OrderedSet(AO))
module Literal (AA:AbstractAtom) =
struct
module A = Factory(AA)
module L =
struct
type t = P1 of A.t | N1 of A.t
let hash = function
P1 o -> A.num(o) lsl 1
| N1 o -> (A.num(o) lsl 1) lor 1
let equal x y = (hash x) == (hash y)
let compare x y = compare (hash x) (hash y)
let to_text = function
P1 a -> A.to_text a
| N1 a -> "-"^A.to_text a
let nil = P1 A.nil
let num = hash
end
module F = Factory(L)
type t = F.t
let hash = F.hash
let compare = F.compare
let equal = F.equal
let to_text = F.to_text
let nil = F.nil
let print_lit l =
match F.get l with
L.P1 a -> print_string " ("; print_string(A.to_text a); print_string ")"
| L.N1 a -> print_string " (not("; print_string(A.to_text a);
print_string "))"
let print_atom a =
begin
print_string "(";
print_string (A.to_text a);
print_string ")";
end
let make_atom =
function a -> A.make a
module SparseMapL = SparseMap(F)
module SparseMapA = SparseMap(A)