File: stdcompat__array.ml.in

package info (click to toggle)
ocaml-stdcompat 14-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,152 kB
  • sloc: ml: 22,329; makefile: 211; sh: 120
file content (128 lines) | stat: -rw-r--r-- 3,030 bytes parent folder | download
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
include Array

@BEGIN_BEFORE_4_08_0@
type 'a t = 'a array
@END_BEFORE_4_08_0@

@BEGIN_BEFORE_4_02_0@
let create_float l = Array.make l 0.

let make_float = create_float
@END_BEFORE_4_02_0@
@BEGIN_FROM_4_02_0@
@BEGIN_BEFORE_4_03_0@
let create_float = Array.make_float
@END_BEFORE_4_03_0@
@END_FROM_4_02_0@

@BEGIN_BEFORE_4_11_0@
exception Iter

let for_all2 f array1 array2 =
  if length array1 <> length array2 then
    invalid_arg "Array.for_all2";
  try
    for i = 0 to length array1 - 1 do
      if not (f (unsafe_get array1 i) (unsafe_get array2 i)) then
        raise Iter
    done;
    true
  with Iter -> false

let exists2 f array1 array2 =
  if length array1 <> length array2 then
    invalid_arg "Array.exists2";
  try
    for i = 0 to length array1 - 1 do
      if f (unsafe_get array1 i) (unsafe_get array2 i) then
        raise Iter
    done;
    false
  with Iter -> true
@END_BEFORE_4_11_0@

@BEGIN_BEFORE_4_03_0@
let iter2 f array1 array2 =
  if length array1 <> length array2 then
    invalid_arg "Array.iter2";
  for i = 0 to length array1 - 1 do
    f (unsafe_get array1 i) (unsafe_get array2 i)
  done

let map2 f array1 array2 =
  if length array1 <> length array2 then
    invalid_arg "Array.map2";
  init (length array1)
    (fun i -> f (unsafe_get array1 i) (unsafe_get array2 i))

let for_all f array =
  try
    for i = 0 to length array - 1 do
      if not (f (unsafe_get array i)) then
        raise Iter
    done;
    true
  with Iter -> false

let exists f array =
  try
    for i = 0 to length array - 1 do
      if f (unsafe_get array i) then
        raise Iter
    done;
    false
  with Iter -> true

let mem item =
  exists (( = ) item)

let memq item =
  exists (( == ) item)
@END_BEFORE_4_03_0@

@BEGIN_BEFORE_4_06_0@
module Floatarray = struct
  let create = create_float

  let length : Stdcompat__root.floatarray -> int = length

  let get : Stdcompat__root.floatarray -> int -> float = get

  let set : Stdcompat__root.floatarray -> int -> float -> unit = set

  let unsafe_get : Stdcompat__root.floatarray -> int -> float = unsafe_get

  let unsafe_set : Stdcompat__root.floatarray -> int -> float -> unit
    = unsafe_set
end
@END_BEFORE_4_06_0@

@BEGIN_BEFORE_4_07_0@
let to_seq s = Stdcompat__tools.vec_to_seq length unsafe_get s

let to_seqi s = Stdcompat__tools.vec_to_seqi length unsafe_get s
@END_BEFORE_4_07_0@

@BEGIN_BEFORE_4_07_1@
(* Array.of_seq is redefined in OCaml 4.07.0 to circumvent a bug in the
   implementation of the standard library. See:
   - https://caml.inria.fr/mantis/view.php?id=7820
   - https://github.com/ocaml/ocaml/pull/1897 *)

let of_rev_list l =
  match l with
  | [] -> [||]
  | hd :: tl ->
      let len = List.length l in
      let result = make len hd in
      let rec fill i l =
        match l with
        | [] -> result
        | hd :: tl ->
            unsafe_set result i hd;
            fill (pred i) tl in
      fill (len - 2) tl

let of_seq g =
  of_rev_list (Stdcompat__seq.fold_left (fun accu x -> x :: accu) [] g)
@END_BEFORE_4_07_1@