forked from johnwhitington/camlpdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdfutil.ml
1315 lines (1047 loc) · 34.7 KB
/
pdfutil.ml
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
(* This module contains general-purpose functions used in many modules.
Typically a module will use the [open] keyword to bring these definitions up to
top level, so their names are considered reserved words in other modules.
All functions in this module are tail-recursive, unless otherwise noted. *)
let rec position_gen n e = function
[] -> None
| e'::t when e = e' -> Some n
| _::t -> position_gen (n + 1) e t
let position e l = position_gen 0 e l
let position_1 e l = position_gen 1 e l
(* Replace all instances of x with x' in s *)
let string_replace_all x x' s =
if x = "" then s else
let p = ref 0
and slen = String.length s
and xlen = String.length x in
let output = Buffer.create (slen * 2) in
while !p < slen do
try
if String.sub s !p xlen = x
then (Buffer.add_string output x'; p := !p + xlen)
else (Buffer.add_char output s.[!p]; incr p)
with
_ -> Buffer.add_char output s.[!p]; incr p
done;
Buffer.contents output
let string_replace_all_lazy x x' s =
if x = "" then s else
let p = ref 0
and slen = String.length s
and xlen = String.length x in
let output = Buffer.create (slen * 2) in
while !p < slen do
try
if String.sub s !p xlen = x
then (Buffer.add_string output (x' ()); p := !p + xlen)
else (Buffer.add_char output s.[!p]; incr p)
with
_ -> Buffer.add_char output s.[!p]; incr p
done;
Buffer.contents output
(* Print something and then flush standard output. *)
let flprint s =
print_string s; flush stdout
let fleprint s =
print_string s; flush stderr
(* Debug printing *)
let dp_print = ref false
let dpr s = if !dp_print then flprint s
(* [xxx] is a tail-recursive version of [List.xxx]. See [List] module for
details. *)
let sort = List.sort
let hd = List.hd
let tl = List.tl
let rev = List.rev
let iter = List.iter
let iter2 = List.iter2
let rec iter3 f a b c =
match a, b, c with
| [], [], [] -> ()
| ah::a', bh::b', ch::c' ->
f ah bh ch;
iter3 f a' b' c'
| _ -> raise (Invalid_argument "Pdfutil.iter3")
let append a b =
List.rev_append (rev a) b
let ( @ ) = append
let flatten lists =
let rec flatten out = function
| [] -> out
| l::ls -> flatten (append l out) ls
in
flatten [] (rev lists)
let rev_map = List.rev_map
let map f l =
rev (List.rev_map f l)
let map2 f a b =
rev (List.rev_map2 f a b)
let split l =
let rec split_inner (l1, l2) = function
| [] -> rev l1, rev l2
| (a, b)::t -> split_inner (a::l1, b::l2) t
in
split_inner ([], []) l
let split3 l =
let rec split3_inner (l1, l2, l3) = function
| [] -> rev l1, rev l2, rev l3
| (a, b, c)::t -> split3_inner (a::l1, b::l2, c::l3) t
in
split3_inner ([], [], []) l
let split5 l =
let rec split5_inner (l1, l2, l3, l4, l5) = function
| [] -> rev l1, rev l2, rev l3, rev l4, rev l5
| (a, b, c, d, e)::t -> split5_inner (a::l1, b::l2, c::l3, d::l4, e::l5) t
in
split5_inner ([], [], [], [], []) l
let split6 l =
let rec split6_inner (l1, l2, l3, l4, l5, l6) = function
| [] -> rev l1, rev l2, rev l3, rev l4, rev l5, rev l6
| (a, b, c, d, e, f)::t ->
split6_inner (a::l1, b::l2, c::l3, d::l4, e::l5, f::l6) t
in
split6_inner ([], [], [], [], [], []) l
let split8 l =
let rec split8_inner (l1, l2, l3, l4, l5, l6, l7, l8) = function
| [] -> rev l1, rev l2, rev l3, rev l4, rev l5, rev l6, rev l7, rev l8
| (a, b, c, d, e, f, g, h)::t ->
split8_inner (a::l1, b::l2, c::l3, d::l4, e::l5, f::l6, g::l7, h::l8) t
in
split8_inner ([], [], [], [], [], [], [], []) l
let combine a b =
let pairs = ref [] in
try
List.iter2 (fun x y -> pairs := (x, y)::!pairs) a b;
rev !pairs
with
Invalid_argument _ -> raise (Invalid_argument "Pdfutil.combine")
let combine3 a b c =
let pairs = ref [] in
try
iter3 (fun x y z -> pairs := (x, y, z)::!pairs) a b c;
rev !pairs
with
Invalid_argument _ -> raise (Invalid_argument "Pdfutil.combine3")
let fold_left f b l = List.fold_left f b l
let fold_right f l e =
List.fold_left (fun x y -> f y x) e (rev l)
let length = List.length
let rec rev_map3_inner f a b c outputs =
match a, b, c with
| [], [], [] -> outputs
| ha::ta, hb::tb, hc::tc ->
rev_map3_inner f ta tb tc (f ha hb hc::outputs)
| _ -> raise (Invalid_argument "Pdfutil.map3")
let rev_map3 f a b c =
rev_map3_inner f a b c []
let map3 f a b c =
rev (rev_map3 f a b c)
let rec rev_map4_inner f a b c d outputs =
match a, b, c, d with
| [], [], [], [] -> outputs
| ha::ta, hb::tb, hc::tc, hd::td ->
rev_map4_inner f ta tb tc td (f ha hb hc hd::outputs)
| _ -> raise (Invalid_argument "Pdfutil.map4")
let rev_map4 f a b c d =
rev_map4_inner f a b c d []
let map4 f a b c d =
rev (rev_map4 f a b c d)
let rec rev_map5_inner f a b c d e outputs =
match a, b, c, d, e with
| [], [], [], [], [] -> outputs
| ha::ta, hb::tb, hc::tc, hd::td, he::te ->
rev_map5_inner f ta tb tc td te (f ha hb hc hd he::outputs)
| _ -> raise (Invalid_argument "Pdfutil.map5")
let rev_map5 f a b c d e =
rev_map5_inner f a b c d e []
let map5 f a b c d e =
rev (rev_map5 f a b c d e)
let rec rev_map6_inner f a b c d e g outputs =
match a, b, c, d, e, g with
| [], [], [], [], [], [] -> outputs
| ha::ta, hb::tb, hc::tc, hd::td, he::te, hg::tg ->
rev_map6_inner f ta tb tc td te tg (f ha hb hc hd he hg::outputs)
| _ -> raise (Invalid_argument "Pdfutil.map6")
let rev_map6 f a b c d e g =
rev_map6_inner f a b c d e g []
let map6 f a b c d e g =
rev (rev_map6 f a b c d e g)
let sum = fold_left ( + ) 0
let fsum = fold_left ( +. ) 0.
(* Calculate the cumulative sum of a list given a base e.g [cumulative_sum 5
[1;2;3] = [6; 8; 11]] *)
let cumulative_sum b l =
let rec cumulative_sum prev bse = function
| [] -> rev prev
| h::t -> cumulative_sum ((bse + h)::prev) (bse + h) t
in
cumulative_sum [] b l
(* Split a list into a list of lists at every point where [p] is true *)
let rec split_around_inner p prev curr = function
| [] -> if curr = [] then (rev prev) else (rev (rev curr::prev))
| h::t ->
if p h
then split_around_inner p (rev curr::prev) [] t
else split_around_inner p prev (h::curr) t
let split_around p l =
split_around_inner p [] [] l
(* Count the number of elements matching a predicate. *)
let rec lcount_inner p c = function
| [] -> c
| h::t ->
if p h
then lcount_inner p (c + 1) t
else lcount_inner p c t
let lcount p l =
lcount_inner p 0 l
(* Find the position of the first element matching a predicate. The first
element is number one. *)
let rec index_inner n p = function
| [] -> dpr "b"; raise Not_found
| h::_ when p h -> n
| _::t -> index_inner (n + 1) p t
let index n p = index_inner 1 n p
(* Functions on Strings *)
let firstchar s =
try Some s.[0] with Invalid_argument _ -> dpr "3R"; None
let lastchar s =
try Some s.[String.length s - 1] with Invalid_argument _ -> dpr "3S"; None
(* Make a list of characters from a string, preserving order. *)
let explode s =
let l = ref [] in
for p = String.length s downto 1 do
l := String.unsafe_get s (p - 1)::!l
done;
!l
(* Make a string from a list of characters, preserving order. *)
let implode l =
let s = Bytes.create (length l) in
let rec list_loop x = function
[] -> ()
| i::t -> Bytes.unsafe_set s x i; list_loop (x + 1) t
in
list_loop 0 l;
Bytes.to_string s
(* String of character. *)
let string_of_char c =
String.make 1 c
(* Long-integer function abbreviations *)
let i32ofi = Int32.of_int
let i32toi = Int32.to_int
let i32tof = Int32.to_float
let i32add = Int32.add
let i32sub = Int32.sub
let i32mul = Int32.mul
let i32div = Int32.div
let sr32 = Int32.shift_right
let lsr32 = Int32.shift_right_logical
let lsl32 = Int32.shift_left
let lor32 = Int32.logor
let land32 = Int32.logand
let lnot32 = Int32.lognot
let lxor32 = Int32.logxor
let i32succ = Int32.succ
let i32pred = Int32.pred
let i32max = Stdlib.max
let i32min = Stdlib.min
let i64ofi = Int64.of_int
let i64toi = Int64.to_int
let i64tof = Int64.to_float
let i64add = Int64.add
let i64sub = Int64.sub
let i64mul = Int64.mul
let i64div = Int64.div
let sr64 = Int64.shift_right
let lsr64 = Int64.shift_right_logical
let lsl64 = Int64.shift_left
let land64 = Int64.logand
let lor64 = Int64.logor
let lnot64 = Int64.lognot
let lxor64 = Int64.logxor
let i64succ = Int64.succ
let i64pred = Int64.pred
let i64max = Stdlib.max
let i64min = Stdlib.min
let i32ofi64 = Int64.to_int32
let i64ofi32 = Int64.of_int32
(* Sign extension for integer of number of bits l. *)
let sign_extend l n =
let shift = (if Nativeint.size = 32 then 33 else Nativeint.size) - 1 - l in (* 33 for js_of_ocaml *)
(n lsl shift) asr shift
(* Set each element of array [a] to value [v]. *)
let set_array a v =
Array.fill a 0 (Array.length a) v
(* Evaluate [v ()], evaluate and ignore [f ()], return [v ()], in that order. *)
let do_return v f =
let r = v () in ignore (f ()); r
(* Call [f ()] some number of times. *)
let rec do_many f = function
| n when n < 0 -> raise (Invalid_argument "Pdfutil.do_many")
| 0 -> ()
| n -> f (); do_many f (n - 1)
(* Interleave an element among a list, so that [interleave 0 [1; 2; 3]]
yields [[1; 0; 2; 0; 3]]. An empty or singleton list is unchanged. *)
let interleave e l =
let rec interleave_inner result elt = function
| [] -> rev result
| [e] -> interleave_inner (e::result) elt []
| h::t -> interleave_inner (elt::h::result) elt t
in
interleave_inner [] e l
(* Interleave two same-length lists together, taking from the first list first.
*)
let interleave_lists a b =
let rec interleave_lists_inner r a b =
match a, b with
| [], [] -> rev r
| h::t, h'::t' -> interleave_lists_inner (h'::h::r) t t'
| _ -> raise (Invalid_argument "Pdfutil.interleave_lists")
in
interleave_lists_inner [] a b
(* Cons on list references *)
let ( =| ) r e =
r := e::!r
(* Append on list references *)
let ( =@ ) r l =
r := l @ !r
(* Functions on characters. *)
let isdigit = function
| x when x >= '0' && x <= '9' -> true
| _ -> false
(* Abbreviation. *)
let toint x = int_of_float x
(* Invert a predicate. *)
let notpred f =
function e -> not (f e)
(* Prefix equality *)
let eq = ( = )
let neq = ( <> )
(* Map on the individual (inner) elements of a list of lists *)
let map_lol f =
map (map f)
(* Raise [x] to the power [i]. *)
let rec pow i x =
match i with
| 0 -> 1
| 1 -> x
| i -> pow (i / 2) (x * x) * (if i mod 2 = 0 then 1 else x)
(* Dictionaries implemented as association lists *)
(* Look something up in a dictionary. *)
let rec lookup k' = function
| [] -> None
| (k, v)::t -> if k = k' then Some v else lookup k' t
(* Same, but no [option] type. *)
let rec lookup_failnull k' = function
| [] -> dpr "e"; raise Not_found
| (k, v)::t -> if k = k' then v else lookup_failnull k' t
(* Add something to a dictionary, replacing it if it's already there. *)
let add k' v d =
let rec add_inner r k' v = function
| [] -> (k', v)::r
| (k, _)::t when k = k' -> r @ ((k', v)::t)
| h::t -> add_inner (h::r) k' v t
in
add_inner [] k' v d
(* Replace something in a dictionary, failing if it doesn't exist. *)
let replace k' v l =
let rec replace_inner r k' v = function
| [] -> dpr "f"; raise Not_found
| (k, _)::t when k = k' -> List.rev_append r ((k', v)::t)
| h::t -> replace_inner (h::r) k' v t
in
replace_inner [] k' v l
(* Remove something from a dictionary. *)
let remove k' l =
let rec remove_inner r k' = function
| [] -> r
| (k, _)::t when k = k' -> List.rev_append r t
| h::t -> remove_inner (h::r) k' t
in
remove_inner [] k' l
(* Merge two dictionaries, prefering elements in the second in the case of
clashes. *)
let rec mergedict d = function
| [] -> d
| (k, v)::es -> mergedict (add k v d) es
(* An infix operator for the composition of functions. *)
let ( <| ) a b = a b
(* Opposite version of [@], the reverse append. *)
let ( @@ ) a b = b @ a
(* In order to return pairs of list from recursive functions without recourse
to accumulating arguments. *)
let conspair ((x, y), (xs, ys)) = x::xs, y::ys
(* The same with options determining whether or not each element is included in
the output list. *)
let conspairopt ((xo, yo), (xs, ys)) =
(match xo with None -> xs | Some x -> x::xs),
(match yo with None -> ys | Some y -> y::ys)
(* Make consecutive elements of an even-length list into a list of pairs. *)
let pairs_of_list l =
let rec pairs_of_list_inner r = function
| [] -> rev r
| [_] -> raise (Invalid_argument "Pdfutil.pairs_of_list")
| h::h'::t -> pairs_of_list_inner ((h, h')::r) t
in
pairs_of_list_inner [] l
(* Return a list identical to the input but with any item true under predicate
[p] replaced with [o]. *)
let replaceinlist p o l =
let rec replaceinlist_inner r p o = function
| [] -> rev r
| h::t ->
if p h
then replaceinlist_inner (o::r) p o t
else replaceinlist_inner (h::r) p o t
in
replaceinlist_inner [] p o l
(* Produce a list of overlapping pairs of elements in a list in order, producing
the empty list if on singleton input. *)
let pairs l =
let rec pairs_inner r = function
| [] | [_] -> rev r
| a::b::rest -> pairs_inner ((a, b)::r) (b::rest)
in
pairs_inner [] l
(* Predicate to test if [x] is a member of a list. *)
let mem = List.mem
(* The same, with reversed arguments. *)
let mem' l x = mem x l
(* Return the set of distinct elements in a list. Does not preserve order. *)
let setify_simple l =
let rec setify_inner r = function
| [] -> r
| h::t ->
if mem h t
then setify_inner r t
else setify_inner (h::r) t
in
setify_inner [] l
(* The same, preserving the order of the first occurance of each distinct
element in the input list. *)
let setify_preserving_order l =
setify_simple (rev l)
let rec sorted_setify prev = function
[] -> rev prev
| [x] -> rev (x::prev)
| a::b::t when a = b -> sorted_setify prev (b::t)
| h::t -> sorted_setify (h::prev) t
let setify l =
sorted_setify [] (List.sort compare l)
let setify_large l =
let h = Hashtbl.create (length l) in
iter (fun k -> Hashtbl.replace h k ()) l;
Hashtbl.fold (fun k _ acc -> k::acc) h []
(* Remove all elts of l' from l if l, l' sets. *)
let setminus l l' =
let rec setminus_inner r l l' =
match l with
| [] -> r
| h::t ->
if mem h l'
then setminus_inner r t l'
else setminus_inner (h::r) t l'
in
setminus_inner [] l l'
let setminus_preserving_order l l' =
rev (setminus l l')
(* Return a list of the heads of a list of lists. *)
let heads l =
let rec heads_inner r = function
| [] -> rev r
| h::t -> heads_inner (hd h::r) t
in
heads_inner [] l
(* Return a list of the tails of a list of lists, failing if any of them are
the empty list. *)
let tails l =
let rec tails_inner r = function
| [] -> rev r
| h::t -> tails_inner (tl h::r) t
in
tails_inner [] l
(* Take a list of lists of equal length, and turn into a list of lists, the
first containing all the first elements of the original lists, the second the
second, and so on. *)
let zipn l =
let rec zipn_inner r = function
| [] | []::_ -> rev r
| l -> zipn_inner (heads l::r) (tails l)
in
zipn_inner [] l
(* Remove the second, fourth etc elements from a list, saving the last element
(if of even length) e.g [drop_evens [1;2;3;4;5;6] is [1;3;5;6]]. *)
let drop_evens l =
let rec drop_evens_inner r = function
| h::_::h''::t -> drop_evens_inner (h::r) (h''::t)
| h::h'::[] -> rev (h'::h::r)
| [x] -> rev (x::r)
| _ -> rev r
in
drop_evens_inner [] l
(* Same, but don't save the last even one. *)
let really_drop_evens l =
let rec really_drop_evens_inner r = function
| [] -> rev r
| [h] -> really_drop_evens_inner (h::r) []
| h::_::more -> really_drop_evens_inner (h::r) more
in
really_drop_evens_inner [] l
(* Remove the first, third etc. The last odd element is not saved. e.g
[drop_odds [1;2;3;4;5;6;7] is [2;4;6]]. *)
let drop_odds l =
let rec drop_odds_inner r = function
| _::h'::t -> drop_odds_inner (h'::r) t
| _ -> rev r
in
drop_odds_inner [] l
(* tl but silent failure. *)
let tail_no_fail = function
| [] -> []
| _::t -> t
(* Couple the elements of a list [l] using function [f]. For instance,
[couple ( + ) [[1; 3; 5]]] $\Longrightarrow$ [[4; 8]]. The two elements
are applied to [f] in the order in which they appear in the input list. *)
let couple f l =
let rec couple_inner r f = function
| x::x'::xs -> couple_inner (f x x'::r) f (x'::xs)
| _ -> rev r
in
couple_inner [] f l
(* As above, but an extra function [g] is applied to any last (odd) element. *)
let couple_ext f g l =
let rec couple_ext_inner r f g = function
| x::x'::xs -> couple_ext_inner (f x x'::r) f g (x'::xs)
| x::[] -> couple_ext_inner (g x::r) f g []
| [] -> rev r
in
couple_ext_inner [] f g l
(* Apply [couple] repeatedly until only one element remains. Return that
element. *)
let rec couple_reduce f = function
| [] -> raise (Invalid_argument "Pdfutil.couple_reduce")
| [a] -> a
| l -> couple_reduce f (couple f l)
(* A similar function to [couple], but the coupling is non-overlapping. *)
let pair f l =
let rec pair_inner r f = function
| [] -> rev r
| [a] -> pair_inner (a::r) f []
| a::b::t -> pair_inner (f a b::r) f t
in
pair_inner [] f l
(* A version of [pair] which adds a unary function for the singleton, much
like [couple_ext]. *)
let pair_ext f g l =
let rec pair_ext_inner r f g = function
| [] -> rev r
| [a] -> pair_ext_inner (g a::r) f g []
| a::b::t -> pair_ext_inner (f a b::r) f g t
in
pair_ext_inner [] f g l
(* As [couple_reduce] is to [couple], so this is to [pair]. *)
let rec pair_reduce f = function
| [] -> raise (Invalid_argument "Pdfutil.pair_reduce")
| [a] -> a
| l -> pair_reduce f (pair f l)
(* [List.filter] has a confusing name, so we define [keep] and [lose] to avoid
error. *)
let keep = List.filter
let rec lose_inner prev p = function
| [] -> rev prev
| h::t ->
if p h
then lose_inner prev p t
else lose_inner (h::prev) p t
let lose p = lose_inner [] p
(* Make a list of length [n] with each element equal to [x]. *)
let many x n =
Array.to_list (Array.make n x)
(* A version where we need to apply unit each time, for instance when producing
a list of random numbers. Result is ordered. *)
let manyunique f n =
let rec manyunique_inner r f n =
if n = 0
then rev r
else manyunique_inner (f ()::r) f (n - 1)
in
manyunique_inner [] f n
(* Take [n] elements from the front of a list [l], returning them in order. *)
let take l n =
if n < 0 then raise (Invalid_argument "Pdfutil.take") else
let rec take_inner r l n =
if n = 0 then rev r else
match l with
| [] -> raise (Invalid_argument "Pdfutil.take")
| h::t -> take_inner (h::r) t (n - 1)
in
take_inner [] l n
let take' n l = take l n
(* Same, but order is reversed *)
let takewhile_reverse p l =
let rec takewhile_reverse_inner r p = function
| [] -> r
| h::t -> if p h then takewhile_reverse_inner (h::r) p t else r
in
takewhile_reverse_inner [] p l
(* Take from the list [l] while the predicate [p] is true. *)
let takewhile p l =
let rec takewhile_inner r p l =
match l with
| [] -> rev r
| h::t -> if p h then takewhile_inner (h::r) p t else rev r
in
takewhile_inner [] p l
(* Drop [n] elements from the front of a list, returning the remainder in
order. *)
let rec drop_inner n = function
| [] -> raise (Invalid_argument "Pdfutil.drop")
| _::t -> if n = 1 then t else drop_inner (n - 1) t
let drop l n =
if n < 0 then raise (Invalid_argument "Pdfutil.drop") else
if n = 0 then l else
drop_inner n l
let drop' n l = drop l n
let rec dropwhile p = function
| [] -> []
| h::t -> if p h then dropwhile p t else (h::t)
(* Split a list [l] into two parts, the first part containing [n] elements. *)
let cleave l n =
let rec cleave_inner l left n =
if n = 0 then rev left, l else
match l with
| [] -> raise (Invalid_argument "Pdfutil.cleave: not enough elements")
| _ -> cleave_inner (tl l) (hd l::left) (n - 1)
in
if n < 0
then raise (Invalid_argument "Pdfutil.cleave: negative argument")
else cleave_inner l [] n
(* Returns elements for which p is true, until one is not, paired with the
remaining list. The same as [takewhile p l], [dropwhile p l], but requiring
only one pass over the list. *)
let cleavewhile p l =
let rec cleavewhile_inner p l elts =
match l with
| [] -> rev elts, []
| e::es ->
if p e
then cleavewhile_inner p es (e::elts)
else rev elts, l
in
cleavewhile_inner p l []
(* The same, faster, but output lists are unordered. *)
let cleavewhile_unordered p l =
let rec cleavewhile_unordered_inner p l elts =
match l with
| [] -> elts, []
| e::es ->
if p e
then cleavewhile_unordered_inner p es (e::elts)
else elts, l
in
cleavewhile_unordered_inner p l []
(* Isolate a central section of a list, from the first element after the element
for which predicate [p] is true, to the element before [p'] is first true. *)
let isolate p p' l =
let _, during_and_after = cleavewhile (notpred p) l in
match during_and_after with
| [] -> []
| _::t -> fst (cleavewhile (notpred p') t)
(* Collate a list into a list of lists based upon a comparison function by which
it has already been sorted. e.g [collate [1; 2; 2; 3; 3]] calculates
[[[1]; [2;2]; [3;3]]]. *)
let collate cmp l =
let rec collate_inner prev = function
| [] -> rev prev
| h::t ->
let x, y = cleavewhile (fun a -> cmp h a = 0) (h::t) in
collate_inner (x::prev) y
in
collate_inner [] l
(* Split a list into some lists of length [n] (and possibly a final one of
length < [n]). *)
let splitinto n l =
let rec splitinto_inner a n l len =
match l with [] -> rev a | _ ->
if len < n then rev (l::a) else
let h, t = cleave l n in
splitinto_inner (h::a) n t (len - n)
in
splitinto_inner [] n l (length l)
(* Non-tail recursive version, for use when [n] is small and fixed. *)
let rec takeatmost n l =
match l with
| h::t when n > 0 -> h :: takeatmost (n - 1) t
| _ -> []
let rec dropatmost n l =
match l with
| _::t when n > 0 -> dropatmost (n - 1) t
| l -> l
let rec splitinto_small n l =
match l with
| [] -> []
| _ ->
let first = takeatmost n l in
first :: splitinto_small n (dropatmost n l)
(* Split a list [l] at the given points. Point 1 means after the first element.
*)
let rec splitat_inner prev l = function
| [] -> begin match l with [] -> rev prev | _ -> rev (l::prev) end
| h::t ->
let this, rest = cleave l h in
splitat_inner (this::prev) rest t
let splitat points l =
splitat_inner [] l (couple (fun a b -> b - a) (0::points))
(* Select the nth element in a list (first is element 1) *)
let select n l =
try hd (drop l (n - 1)) with
Invalid_argument _ (*"drop"*)
| Failure _ (*"hd"*) -> raise (Invalid_argument "Pdfutil.select")
(* Replace the nth element of a list (first is element 1) *)
let rec replace_number_inner prev n e = function
| [] -> rev prev
| l::ls ->
if n = 1
then replace_number_inner (e::prev) (n - 1) e ls
else replace_number_inner (l::prev) (n - 1) e ls
let replace_number n e l =
replace_number_inner [] n e l
(* Simple list utilities. *)
let isnull = function [] -> true | _ -> false
let notnull = function [] -> false | _ -> true
(* Find the last element of a list. *)
let rec last = function
| [] -> raise (Invalid_argument "Pdfutil.last")
| x::[] -> x
| _::xs -> last xs
(* Produce a list containing all but the last element of a list *)
let all_but_last = function
| [] | [_] -> []
| l -> rev (tl (rev l))
(* Find the first and last element of a list. If the list has one element, that
is returned twice. *)
let extremes = function
| [] -> raise (Invalid_argument "Pdfutil.extremes")
| x::[] -> x, x
| x::xs -> x, last xs
(* Return the first, middle and last elements of a list which has length at
least two. *)
let extremes_and_middle = function
| [] | [_] ->
raise (Invalid_argument "Pdfutil.extremes_and_middle")
| h::t ->
let m, l = cleave t (length t - 1) in
h, m, hd l
(* Set a boolean reference. *)
let set r =
r := true
(* Clear a boolean reference. *)
let clear r =
r := false
(* Change the value of a boolean reference. *)
let flip r =
r := not !r
(* Increment and decrement integer references [r] by an integer [n]. *)
let ( += ) r n =
r := !r + n
let ( -= ) r n =
r := !r - n
let ( /= ) r n =
r := !r / n
let ( *= ) r n =
r := !r * n
(* Similar functions on floating-point references. *)
let ( +.= ) r n =
r := !r +. n
let ( -.= ) r n =
r := !r -. n
let ( /.= ) r n =
r := !r /. n
let ( *.= ) r n =
r := !r *. n
(* Vectors in two dimensions. *)
type vector = float * float
(* Make a vector from a point [(x0, y0)] to a point [(x1, y1)]. *)
let mkvector (x0, y0) (x1, y1) = x1 -. x0, y1 -. y0
(* Invert a vector. *)
let invert (a, b) = -.a, -.b
(* Offset a point [(px, py)] by a vector [(x, y)]. *)
let offset_point (x, y) (px, py) = px +. x, py +. y
(* Find the vector pi / 2 anticlockwise from the given one. *)
let perpendicular (a, b) = -.b, a
(* Square a number *)
let sqr x = x *. x
(* Find the length of a vector. *)
let veclength (x, y) =
sqrt (sqr x +. sqr y)
(* Scale a vector to a length [l]. *)
let scalevectolength l (a, b) =
let currentlength = veclength (a, b) in
if currentlength = 0. then (a, b) else
let factor = l /. currentlength in
a *. factor, b *. factor
(* Make a unit vector from [s] to [e] *)
let mkunitvector s e =
scalevectolength 1. (mkvector s e)
(* Find the point equidistant between two others. *)
let between (x, y) (x', y') =
(x +. x') /. 2., (y +. y') /. 2.
(* The cartesian distance between two points. *)
let distance_between (px, py) (px', py') =
sqrt (sqr (px -. px') +. sqr (py' -. py))
(* The largest power of two by which [n] is exactly divisible. *)
let largest_pow2_divisible n =
let rec s test n =
if n mod test = 0 then s (test * 2) n
else test / 2