1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Objective Caml *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
|
|
|
(* under the terms of the GNU Library General Public License. *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Array operations *)
|
|
|
|
|
|
|
|
external length : 'a array -> int = "%array_length"
|
1995-07-10 02:48:27 -07:00
|
|
|
external get: 'a array -> int -> 'a = "%array_safe_get"
|
|
|
|
external set: 'a array -> int -> 'a -> unit = "%array_safe_set"
|
1995-06-15 01:10:01 -07:00
|
|
|
external unsafe_get: 'a array -> int -> 'a = "%array_unsafe_get"
|
|
|
|
external unsafe_set: 'a array -> int -> 'a -> unit = "%array_unsafe_set"
|
1997-09-11 08:10:23 -07:00
|
|
|
external make: int -> 'a -> 'a array = "make_vect"
|
1996-04-22 04:15:41 -07:00
|
|
|
external create: int -> 'a -> 'a array = "make_vect"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1997-11-12 00:56:35 -08:00
|
|
|
let init l f =
|
|
|
|
if l = 0 then [||] else
|
|
|
|
let res = create l (f 0) in
|
|
|
|
for i = 1 to pred l do
|
|
|
|
unsafe_set res i (f i)
|
|
|
|
done;
|
|
|
|
res
|
|
|
|
|
1997-09-11 08:10:23 -07:00
|
|
|
let make_matrix sx sy init =
|
1996-04-22 04:15:41 -07:00
|
|
|
let res = create sx [||] in
|
1995-05-04 03:15:53 -07:00
|
|
|
for x = 0 to pred sx do
|
1996-04-22 04:15:41 -07:00
|
|
|
unsafe_set res x (create sy init)
|
1995-05-04 03:15:53 -07:00
|
|
|
done;
|
|
|
|
res
|
|
|
|
|
1997-09-11 08:10:23 -07:00
|
|
|
let create_matrix = make_matrix
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
let copy a =
|
|
|
|
let l = length a in
|
|
|
|
if l = 0 then [||] else begin
|
1997-11-12 00:56:35 -08:00
|
|
|
let res = create l (unsafe_get a 0) in
|
|
|
|
for i = 1 to pred l do
|
|
|
|
unsafe_set res i (unsafe_get a i)
|
1995-05-04 03:15:53 -07:00
|
|
|
done;
|
1997-11-12 00:56:35 -08:00
|
|
|
res
|
1995-05-04 03:15:53 -07:00
|
|
|
end
|
|
|
|
|
1995-06-05 06:42:38 -07:00
|
|
|
let append a1 a2 =
|
1995-05-04 03:15:53 -07:00
|
|
|
let l1 = length a1 and l2 = length a2 in
|
1998-04-27 02:55:50 -07:00
|
|
|
if l1 = 0 && l2 = 0 then [||] else begin
|
1996-04-22 04:15:41 -07:00
|
|
|
let r = create (l1 + l2) (unsafe_get (if l1 > 0 then a1 else a2) 0) in
|
1995-05-04 03:15:53 -07:00
|
|
|
for i = 0 to l1 - 1 do unsafe_set r i (unsafe_get a1 i) done;
|
1995-06-15 01:10:01 -07:00
|
|
|
for i = 0 to l2 - 1 do unsafe_set r (i + l1) (unsafe_get a2 i) done;
|
1995-05-04 03:15:53 -07:00
|
|
|
r
|
|
|
|
end
|
|
|
|
|
1995-06-05 06:42:38 -07:00
|
|
|
let concat_aux init al =
|
|
|
|
let size = List.fold_left (fun sz a -> sz + length a) 0 al in
|
1996-04-22 04:15:41 -07:00
|
|
|
let res = create size init in
|
1995-06-05 06:42:38 -07:00
|
|
|
let pos = ref 0 in
|
|
|
|
List.iter
|
|
|
|
(fun a ->
|
|
|
|
for i = 0 to length a - 1 do
|
|
|
|
unsafe_set res !pos (unsafe_get a i);
|
|
|
|
incr pos
|
|
|
|
done)
|
|
|
|
al;
|
|
|
|
res
|
|
|
|
|
|
|
|
let concat al =
|
|
|
|
let rec find_init = function
|
|
|
|
[] -> [||]
|
|
|
|
| a :: rem ->
|
|
|
|
if length a > 0 then concat_aux (unsafe_get a 0) al else find_init rem
|
|
|
|
in find_init al
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
let sub a ofs len =
|
|
|
|
if ofs < 0 or len < 0 or ofs + len > length a then invalid_arg "Array.sub"
|
|
|
|
else if len = 0 then [||]
|
|
|
|
else begin
|
1996-04-22 04:15:41 -07:00
|
|
|
let r = create len (unsafe_get a ofs) in
|
1995-05-04 03:15:53 -07:00
|
|
|
for i = 1 to len - 1 do unsafe_set r i (unsafe_get a (ofs + i)) done;
|
|
|
|
r
|
|
|
|
end
|
|
|
|
|
|
|
|
let fill a ofs len v =
|
|
|
|
if ofs < 0 or len < 0 or ofs + len > length a
|
|
|
|
then invalid_arg "Array.fill"
|
|
|
|
else for i = ofs to ofs + len - 1 do unsafe_set a i v done
|
|
|
|
|
|
|
|
let blit a1 ofs1 a2 ofs2 len =
|
|
|
|
if len < 0 or ofs1 < 0 or ofs1 + len > length a1
|
|
|
|
or ofs2 < 0 or ofs2 + len > length a2
|
|
|
|
then invalid_arg "Array.blit"
|
1996-05-22 09:21:16 -07:00
|
|
|
else if ofs1 < ofs2 then
|
|
|
|
(* Top-down copy *)
|
|
|
|
for i = len - 1 downto 0 do
|
|
|
|
unsafe_set a2 (ofs2 + i) (unsafe_get a1 (ofs1 + i))
|
|
|
|
done
|
1995-05-04 03:15:53 -07:00
|
|
|
else
|
1996-05-22 09:21:16 -07:00
|
|
|
(* Bottom-up copy *)
|
1995-05-04 03:15:53 -07:00
|
|
|
for i = 0 to len - 1 do
|
|
|
|
unsafe_set a2 (ofs2 + i) (unsafe_get a1 (ofs1 + i))
|
|
|
|
done
|
|
|
|
|
|
|
|
let iter f a =
|
|
|
|
for i = 0 to length a - 1 do f(unsafe_get a i) done
|
|
|
|
|
|
|
|
let map f a =
|
|
|
|
let l = length a in
|
|
|
|
if l = 0 then [||] else begin
|
1996-04-22 04:15:41 -07:00
|
|
|
let r = create l (f(unsafe_get a 0)) in
|
1995-05-04 03:15:53 -07:00
|
|
|
for i = 1 to l - 1 do
|
|
|
|
unsafe_set r i (f(unsafe_get a i))
|
|
|
|
done;
|
|
|
|
r
|
|
|
|
end
|
|
|
|
|
1997-10-24 08:54:07 -07:00
|
|
|
let iteri f a =
|
|
|
|
for i = 0 to length a - 1 do f i (unsafe_get a i) done
|
|
|
|
|
|
|
|
let mapi f a =
|
|
|
|
let l = length a in
|
|
|
|
if l = 0 then [||] else begin
|
|
|
|
let r = create l (f 0 (unsafe_get a 0)) in
|
|
|
|
for i = 1 to l - 1 do
|
|
|
|
unsafe_set r i (f i (unsafe_get a i))
|
|
|
|
done;
|
|
|
|
r
|
|
|
|
end
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
let to_list a =
|
1997-07-26 05:20:44 -07:00
|
|
|
let rec tolist i res =
|
|
|
|
if i < 0 then res else tolist (i - 1) (unsafe_get a i :: res) in
|
|
|
|
tolist (length a - 1) []
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let of_list = function
|
|
|
|
[] -> [||]
|
|
|
|
| hd::tl ->
|
1996-04-22 04:15:41 -07:00
|
|
|
let a = create (List.length tl + 1) hd in
|
1995-05-04 03:15:53 -07:00
|
|
|
let rec fill i = function
|
|
|
|
[] -> a
|
|
|
|
| hd::tl -> unsafe_set a i hd; fill (i+1) tl in
|
|
|
|
fill 1 tl
|
1997-10-24 08:54:07 -07:00
|
|
|
|
|
|
|
let fold_left f x a =
|
|
|
|
let r = ref x in
|
1997-11-06 09:28:16 -08:00
|
|
|
for i = 0 to length a - 1 do
|
1997-10-24 08:54:07 -07:00
|
|
|
r := f !r (unsafe_get a i)
|
|
|
|
done;
|
|
|
|
!r
|
|
|
|
|
|
|
|
let fold_right f a x =
|
|
|
|
let r = ref x in
|
1997-11-06 09:28:16 -08:00
|
|
|
for i = length a - 1 downto 0 do
|
1997-10-24 08:54:07 -07:00
|
|
|
r := f (unsafe_get a i) !r
|
|
|
|
done;
|
|
|
|
!r
|