forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbsb_helper.ml
4521 lines (3693 loc) · 126 KB
/
bsb_helper.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
module Bsb_dir_index : sig
#1 "bsb_dir_index.mli"
(* Copyright (C) 2017 Authors of BuckleScript
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
(** Used to index [.bsbuildcache] may not be needed if we flatten dev
into a single group
*)
type t = private int
val lib_dir_index : t
val is_lib_dir : t -> bool
val get_dev_index : unit -> t
val of_int : int -> t
val get_current_number_of_dev_groups : unit -> int
val string_of_bsb_dev_include : t -> string
(** TODO: Need reset
when generating each ninja file to provide stronger guarantee.
Here we get a weak guarantee because only dev group is
inside the toplevel project
*)
val reset : unit -> unit
end = struct
#1 "bsb_dir_index.ml"
(* Copyright (C) 2017 Authors of BuckleScript
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
type t = int
(**
0 : lib
1 : dev 1
2 : dev 2
*)
external of_int : int -> t = "%identity"
let lib_dir_index = 0
let is_lib_dir x = x = lib_dir_index
let dir_index = ref 0
let get_dev_index ( ) =
incr dir_index ; !dir_index
let get_current_number_of_dev_groups =
(fun () -> !dir_index )
(** bsb generate pre-defined variables [bsc_group_i_includes]
for each rule, there is variable [bsc_extra_excludes]
[g_dev_incls] are for app test etc
it will be like
{[
g_dev_incls = ${bsc_group_1_includes}
]}
where [bsc_group_1_includes] will be pre-calcuated
*)
let bsc_group_1_includes = "bsc_group_1_includes"
let bsc_group_2_includes = "bsc_group_2_includes"
let bsc_group_3_includes = "bsc_group_3_includes"
let bsc_group_4_includes = "bsc_group_4_includes"
let string_of_bsb_dev_include i =
match i with
| 1 -> bsc_group_1_includes
| 2 -> bsc_group_2_includes
| 3 -> bsc_group_3_includes
| 4 -> bsc_group_4_includes
| _ ->
"bsc_group_" ^ string_of_int i ^ "_includes"
let reset () = dir_index := 0
end
module Ext_buffer : sig
#1 "ext_buffer.mli"
(***********************************************************************)
(* *)
(* OCaml *)
(* *)
(* Pierre Weis and Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
(* *)
(* Copyright 1999 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. *)
(* *)
(***********************************************************************)
(** Extensible buffers.
This module implements buffers that automatically expand
as necessary. It provides accumulative concatenation of strings
in quasi-linear time (instead of quadratic time when strings are
concatenated pairwise).
*)
(* BuckleScript customization: customized for efficient digest *)
type t
(** The abstract type of buffers. *)
val create : int -> t
(** [create n] returns a fresh buffer, initially empty.
The [n] parameter is the initial size of the internal byte sequence
that holds the buffer contents. That byte sequence is automatically
reallocated when more than [n] characters are stored in the buffer,
but shrinks back to [n] characters when [reset] is called.
For best performance, [n] should be of the same order of magnitude
as the number of characters that are expected to be stored in
the buffer (for instance, 80 for a buffer that holds one output
line). Nothing bad will happen if the buffer grows beyond that
limit, however. In doubt, take [n = 16] for instance.
If [n] is not between 1 and {!Sys.max_string_length}, it will
be clipped to that interval. *)
val contents : t -> string
(** Return a copy of the current contents of the buffer.
The buffer itself is unchanged. *)
val length : t -> int
(** Return the number of characters currently contained in the buffer. *)
val is_empty : t -> bool
val clear : t -> unit
(** Empty the buffer. *)
val add_char : t -> char -> unit
(** [add_char b c] appends the character [c] at the end of the buffer [b]. *)
val add_string : t -> string -> unit
(** [add_string b s] appends the string [s] at the end of the buffer [b]. *)
val add_bytes : t -> bytes -> unit
(** [add_string b s] appends the string [s] at the end of the buffer [b].
@since 4.02 *)
val add_substring : t -> string -> int -> int -> unit
(** [add_substring b s ofs len] takes [len] characters from offset
[ofs] in string [s] and appends them at the end of the buffer [b]. *)
val add_subbytes : t -> bytes -> int -> int -> unit
(** [add_substring b s ofs len] takes [len] characters from offset
[ofs] in byte sequence [s] and appends them at the end of the buffer [b].
@since 4.02 *)
val add_buffer : t -> t -> unit
(** [add_buffer b1 b2] appends the current contents of buffer [b2]
at the end of buffer [b1]. [b2] is not modified. *)
val add_channel : t -> in_channel -> int -> unit
(** [add_channel b ic n] reads exactly [n] character from the
input channel [ic] and stores them at the end of buffer [b].
Raise [End_of_file] if the channel contains fewer than [n]
characters. *)
val output_buffer : out_channel -> t -> unit
(** [output_buffer oc b] writes the current contents of buffer [b]
on the output channel [oc]. *)
val digest : t -> Digest.t
val not_equal :
t ->
string ->
bool
val add_int_1 :
t -> int -> unit
val add_int_2 :
t -> int -> unit
val add_int_3 :
t -> int -> unit
val add_int_4 :
t -> int -> unit
val add_string_char :
t ->
string ->
char ->
unit
val add_char_string :
t ->
char ->
string ->
unit
end = struct
#1 "ext_buffer.ml"
(**************************************************************************)
(* *)
(* OCaml *)
(* *)
(* Pierre Weis and Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
(* *)
(* Copyright 1999 Institut National de Recherche en Informatique et *)
(* en Automatique. *)
(* *)
(* All rights reserved. This file is distributed under the terms of *)
(* the GNU Lesser General Public License version 2.1, with the *)
(* special exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)
(* Extensible buffers *)
type t =
{mutable buffer : bytes;
mutable position : int;
mutable length : int;
initial_buffer : bytes}
let create n =
let n = if n < 1 then 1 else n in
let n = if n > Sys.max_string_length then Sys.max_string_length else n in
let s = Bytes.create n in
{buffer = s; position = 0; length = n; initial_buffer = s}
let contents b = Bytes.sub_string b.buffer 0 b.position
let to_bytes b = Bytes.sub b.buffer 0 b.position
let sub b ofs len =
if ofs < 0 || len < 0 || ofs > b.position - len
then invalid_arg "Ext_buffer.sub"
else Bytes.sub_string b.buffer ofs len
let blit src srcoff dst dstoff len =
if len < 0 || srcoff < 0 || srcoff > src.position - len
|| dstoff < 0 || dstoff > (Bytes.length dst) - len
then invalid_arg "Ext_buffer.blit"
else
Bytes.unsafe_blit src.buffer srcoff dst dstoff len
let length b = b.position
let is_empty b = b.position = 0
let clear b = b.position <- 0
let reset b =
b.position <- 0; b.buffer <- b.initial_buffer;
b.length <- Bytes.length b.buffer
let resize b more =
let len = b.length in
let new_len = ref len in
while b.position + more > !new_len do new_len := 2 * !new_len done;
if !new_len > Sys.max_string_length then begin
if b.position + more <= Sys.max_string_length
then new_len := Sys.max_string_length
else failwith "Ext_buffer.add: cannot grow buffer"
end;
let new_buffer = Bytes.create !new_len in
(* PR#6148: let's keep using [blit] rather than [unsafe_blit] in
this tricky function that is slow anyway. *)
Bytes.blit b.buffer 0 new_buffer 0 b.position;
b.buffer <- new_buffer;
b.length <- !new_len
let add_char b c =
let pos = b.position in
if pos >= b.length then resize b 1;
Bytes.unsafe_set b.buffer pos c;
b.position <- pos + 1
let add_substring b s offset len =
if offset < 0 || len < 0 || offset > String.length s - len
then invalid_arg "Ext_buffer.add_substring/add_subbytes";
let new_position = b.position + len in
if new_position > b.length then resize b len;
Bytes.blit_string s offset b.buffer b.position len;
b.position <- new_position
let add_subbytes b s offset len =
add_substring b (Bytes.unsafe_to_string s) offset len
let add_string b s =
let len = String.length s in
let new_position = b.position + len in
if new_position > b.length then resize b len;
Bytes.blit_string s 0 b.buffer b.position len;
b.position <- new_position
(* TODO: micro-optimzie *)
let add_string_char b s c =
let s_len = String.length s in
let len = s_len + 1 in
let new_position = b.position + len in
if new_position > b.length then resize b len;
let b_buffer = b.buffer in
Bytes.blit_string s 0 b_buffer b.position s_len;
Bytes.unsafe_set b_buffer (new_position - 1) c;
b.position <- new_position
let add_char_string b c s =
let s_len = String.length s in
let len = s_len + 1 in
let new_position = b.position + len in
if new_position > b.length then resize b len;
let b_buffer = b.buffer in
let b_position = b.position in
Bytes.unsafe_set b_buffer b_position c ;
Bytes.blit_string s 0 b_buffer (b_position + 1) s_len;
b.position <- new_position
let add_bytes b s = add_string b (Bytes.unsafe_to_string s)
let add_buffer b bs =
add_subbytes b bs.buffer 0 bs.position
let add_channel b ic len =
if len < 0
|| len > Sys.max_string_length
then (* PR#5004 *)
invalid_arg "Ext_buffer.add_channel";
if b.position + len > b.length then resize b len;
really_input ic b.buffer b.position len;
b.position <- b.position + len
let output_buffer oc b =
output oc b.buffer 0 b.position
external unsafe_string: bytes -> int -> int -> Digest.t = "caml_md5_string"
let digest b =
unsafe_string
b.buffer 0 b.position
let rec not_equal_aux (b : bytes) (s : string) i len =
if i >= len then false
else
(Bytes.unsafe_get b i
<>
String.unsafe_get s i )
|| not_equal_aux b s (i + 1) len
(** avoid a large copy *)
let not_equal (b : t) (s : string) =
let b_len = b.position in
let s_len = String.length s in
b_len <> s_len
|| not_equal_aux b.buffer s 0 s_len
(**
It could be one byte, two bytes, three bytes and four bytes
TODO: inline for better performance
*)
let add_int_1 (b : t ) (x : int ) =
let c = (Char.unsafe_chr (x land 0xff)) in
let pos = b.position in
if pos >= b.length then resize b 1;
Bytes.unsafe_set b.buffer pos c;
b.position <- pos + 1
let add_int_2 (b : t ) (x : int ) =
let c1 = (Char.unsafe_chr (x land 0xff)) in
let c2 = (Char.unsafe_chr (x lsr 8 land 0xff)) in
let pos = b.position in
if pos + 1 >= b.length then resize b 2;
let b_buffer = b.buffer in
Bytes.unsafe_set b_buffer pos c1;
Bytes.unsafe_set b_buffer (pos + 1) c2;
b.position <- pos + 2
let add_int_3 (b : t ) (x : int ) =
let c1 = (Char.unsafe_chr (x land 0xff)) in
let c2 = (Char.unsafe_chr (x lsr 8 land 0xff)) in
let c3 = (Char.unsafe_chr (x lsr 16 land 0xff)) in
let pos = b.position in
if pos + 2 >= b.length then resize b 3;
let b_buffer = b.buffer in
Bytes.unsafe_set b_buffer pos c1;
Bytes.unsafe_set b_buffer (pos + 1) c2;
Bytes.unsafe_set b_buffer (pos + 2) c3;
b.position <- pos + 3
let add_int_4 (b : t ) (x : int ) =
let c1 = (Char.unsafe_chr (x land 0xff)) in
let c2 = (Char.unsafe_chr (x lsr 8 land 0xff)) in
let c3 = (Char.unsafe_chr (x lsr 16 land 0xff)) in
let c4 = (Char.unsafe_chr (x lsr 24 land 0xff)) in
let pos = b.position in
if pos + 3 >= b.length then resize b 4;
let b_buffer = b.buffer in
Bytes.unsafe_set b_buffer pos c1;
Bytes.unsafe_set b_buffer (pos + 1) c2;
Bytes.unsafe_set b_buffer (pos + 2) c3;
Bytes.unsafe_set b_buffer (pos + 3) c4;
b.position <- pos + 4
end
module Ext_list : sig
#1 "ext_list.mli"
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
val map :
'a list ->
('a -> 'b) ->
'b list
val has_string :
string list ->
string ->
bool
val map_split_opt :
'a list ->
('a -> 'b option * 'c option) ->
'b list * 'c list
val mapi :
'a list ->
(int -> 'a -> 'b) ->
'b list
val map_snd : ('a * 'b) list -> ('b -> 'c) -> ('a * 'c) list
(** [map_last f xs ]
will pass [true] to [f] for the last element,
[false] otherwise.
For empty list, it returns empty
*)
val map_last :
'a list ->
(bool -> 'a -> 'b) -> 'b list
(** [last l]
return the last element
raise if the list is empty
*)
val last : 'a list -> 'a
val append :
'a list ->
'a list ->
'a list
val append_one :
'a list ->
'a ->
'a list
val map_append :
'b list ->
'a list ->
('b -> 'a) ->
'a list
val fold_right :
'a list ->
'b ->
('a -> 'b -> 'b) ->
'b
val fold_right2 :
'a list ->
'b list ->
'c ->
('a -> 'b -> 'c -> 'c) -> 'c
val map2 :
'a list ->
'b list ->
('a -> 'b -> 'c) ->
'c list
val fold_left_with_offset :
'a list ->
'acc ->
int ->
('a -> 'acc -> int -> 'acc) ->
'acc
(** @unused *)
val filter_map :
'a list ->
('a -> 'b option) ->
'b list
(** [exclude p l] is the opposite of [filter p l] *)
val exclude :
'a list ->
('a -> bool) ->
'a list
(** [excludes p l]
return a tuple [excluded,newl]
where [exluded] is true indicates that at least one
element is removed,[newl] is the new list where all [p x] for [x] is false
*)
val exclude_with_val :
'a list ->
('a -> bool) ->
'a list option
val same_length : 'a list -> 'b list -> bool
val init : int -> (int -> 'a) -> 'a list
(** [split_at n l]
will split [l] into two lists [a,b], [a] will be of length [n],
otherwise, it will raise
*)
val split_at :
'a list ->
int ->
'a list * 'a list
(** [split_at_last l]
It is equivalent to [split_at (List.length l - 1) l ]
*)
val split_at_last : 'a list -> 'a list * 'a
val filter_mapi :
'a list ->
('a -> int -> 'b option) ->
'b list
val filter_map2 :
'a list ->
'b list ->
('a -> 'b -> 'c option) ->
'c list
val length_compare : 'a list -> int -> [`Gt | `Eq | `Lt ]
val length_ge : 'a list -> int -> bool
(**
{[length xs = length ys + n ]}
input n should be positive
TODO: input checking
*)
val length_larger_than_n :
'a list ->
'a list ->
int ->
bool
(**
[rev_map_append f l1 l2]
[map f l1] and reverse it to append [l2]
This weird semantics is due to it is the most efficient operation
we can do
*)
val rev_map_append :
'a list ->
'b list ->
('a -> 'b) ->
'b list
val flat_map :
'a list ->
('a -> 'b list) ->
'b list
val flat_map_append :
'a list ->
'b list ->
('a -> 'b list) ->
'b list
(**
[stable_group eq lst]
Example:
Input:
{[
stable_group (=) [1;2;3;4;3]
]}
Output:
{[
[[1];[2];[4];[3;3]]
]}
TODO: this is O(n^2) behavior
which could be improved later
*)
val stable_group :
'a list ->
('a -> 'a -> bool) ->
'a list list
(** [drop n list]
raise when [n] is negative
raise when list's length is less than [n]
*)
val drop :
'a list ->
int ->
'a list
val find_first :
'a list ->
('a -> bool) ->
'a option
(** [find_first_not p lst ]
if all elements in [lst] pass, return [None]
otherwise return the first element [e] as [Some e] which
fails the predicate
*)
val find_first_not :
'a list ->
('a -> bool) ->
'a option
(** [find_opt f l] returns [None] if all return [None],
otherwise returns the first one.
*)
val find_opt :
'a list ->
('a -> 'b option) ->
'b option
val rev_iter :
'a list ->
('a -> unit) ->
unit
val iter:
'a list ->
('a -> unit) ->
unit
val for_all:
'a list ->
('a -> bool) ->
bool
val for_all_snd:
('a * 'b) list ->
('b -> bool) ->
bool
(** [for_all2_no_exn p xs ys]
return [true] if all satisfied,
[false] otherwise or length not equal
*)
val for_all2_no_exn :
'a list ->
'b list ->
('a -> 'b -> bool) ->
bool
(** [f] is applied follow the list order *)
val split_map :
'a list ->
('a -> 'b * 'c) ->
'b list * 'c list
(** [fn] is applied from left to right *)
val reduce_from_left :
'a list ->
('a -> 'a -> 'a) ->
'a
val sort_via_array :
'a list ->
('a -> 'a -> int) ->
'a list
(** [assoc_by_string default key lst]
if [key] is found in the list return that val,
other unbox the [default],
otherwise [assert false ]
*)
val assoc_by_string :
(string * 'a) list ->
string ->
'a option ->
'a
val assoc_by_int :
(int * 'a) list ->
int ->
'a option ->
'a
val nth_opt : 'a list -> int -> 'a option
val iter_snd : ('a * 'b) list -> ('b -> unit) -> unit
val iter_fst : ('a * 'b) list -> ('a -> unit) -> unit
val exists : 'a list -> ('a -> bool) -> bool
val exists_fst :
('a * 'b) list ->
('a -> bool) ->
bool
val exists_snd :
('a * 'b) list ->
('b -> bool) ->
bool
val concat_append:
'a list list ->
'a list ->
'a list
val fold_left2:
'a list ->
'b list ->
'c ->
('a -> 'b -> 'c -> 'c)
-> 'c
val fold_left:
'a list ->
'b ->
('b -> 'a -> 'b) ->
'b
val singleton_exn:
'a list -> 'a
val mem_string :
string list ->
string ->
bool
end = struct
#1 "ext_list.ml"
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
let rec map l f =
match l with
| [] ->
[]
| [x1] ->
let y1 = f x1 in
[y1]
| [x1; x2] ->
let y1 = f x1 in
let y2 = f x2 in
[y1; y2]
| [x1; x2; x3] ->
let y1 = f x1 in
let y2 = f x2 in
let y3 = f x3 in
[y1; y2; y3]
| [x1; x2; x3; x4] ->
let y1 = f x1 in
let y2 = f x2 in
let y3 = f x3 in
let y4 = f x4 in
[y1; y2; y3; y4]
| x1::x2::x3::x4::x5::tail ->
let y1 = f x1 in
let y2 = f x2 in
let y3 = f x3 in
let y4 = f x4 in
let y5 = f x5 in
y1::y2::y3::y4::y5::(map tail f)
let rec has_string l f =
match l with
| [] ->
false
| [x1] ->
x1 = f
| [x1; x2] ->
x1 = f || x2 = f
| [x1; x2; x3] ->
x1 = f || x2 = f || x3 = f
| x1 :: x2 :: x3 :: x4 ->
x1 = f || x2 = f || x3 = f || has_string x4 f
let rec map_split_opt
(xs : 'a list) (f : 'a -> 'b option * 'c option)
: 'b list * 'c list =
match xs with
| [] -> [], []
| x::xs ->
let c,d = f x in
let cs,ds = map_split_opt xs f in
(match c with Some c -> c::cs | None -> cs),
(match d with Some d -> d::ds | None -> ds)
let rec map_snd l f =
match l with
| [] ->
[]
| [ v1,x1 ] ->
let y1 = f x1 in
[v1,y1]
| [v1, x1; v2, x2] ->
let y1 = f x1 in
let y2 = f x2 in
[v1, y1; v2, y2]
| [ v1, x1; v2, x2; v3, x3] ->
let y1 = f x1 in
let y2 = f x2 in
let y3 = f x3 in
[v1, y1; v2, y2; v3, y3]
| [ v1, x1; v2, x2; v3, x3; v4, x4] ->
let y1 = f x1 in
let y2 = f x2 in
let y3 = f x3 in
let y4 = f x4 in
[v1, y1; v2, y2; v3, y3; v4, y4]
| (v1, x1) ::(v2, x2) :: (v3, x3)::(v4, x4) :: (v5, x5) ::tail ->
let y1 = f x1 in
let y2 = f x2 in
let y3 = f x3 in
let y4 = f x4 in
let y5 = f x5 in
(v1, y1)::(v2, y2) :: (v3, y3) :: (v4, y4) :: (v5, y5) :: (map_snd tail f)
let rec map_last l f=
match l with
| [] ->
[]
| [x1] ->
let y1 = f true x1 in
[y1]
| [x1; x2] ->
let y1 = f false x1 in
let y2 = f true x2 in
[y1; y2]
| [x1; x2; x3] ->
let y1 = f false x1 in
let y2 = f false x2 in
let y3 = f true x3 in
[y1; y2; y3]
| [x1; x2; x3; x4] ->
let y1 = f false x1 in
let y2 = f false x2 in
let y3 = f false x3 in
let y4 = f true x4 in
[y1; y2; y3; y4]
| x1::x2::x3::x4::tail ->
(* make sure that tail is not empty *)
let y1 = f false x1 in
let y2 = f false x2 in
let y3 = f false x3 in
let y4 = f false x4 in
y1::y2::y3::y4::(map_last tail f)
let rec mapi_aux lst i f =
match lst with
[] -> []
| a::l ->
let r = f i a in r :: mapi_aux l (i + 1) f
let mapi lst f = mapi_aux lst 0 f
let rec last xs =
match xs with
| [x] -> x
| _ :: tl -> last tl
| [] -> invalid_arg "Ext_list.last"
let rec append_aux l1 l2 =
match l1 with
| [] -> l2
| [a0] -> a0::l2
| [a0;a1] -> a0::a1::l2
| [a0;a1;a2] -> a0::a1::a2::l2
| [a0;a1;a2;a3] -> a0::a1::a2::a3::l2
| [a0;a1;a2;a3;a4] -> a0::a1::a2::a3::a4::l2
| a0::a1::a2::a3::a4::rest -> a0::a1::a2::a3::a4::append_aux rest l2
let append l1 l2 =
match l2 with
| [] -> l1
| _ -> append_aux l1 l2