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 */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* Automatique. Distributed only by permission. */
|
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
1995-07-27 10:47:15 -07:00
|
|
|
/* Operations on arrays */
|
|
|
|
|
|
|
|
#include "alloc.h"
|
|
|
|
#include "fail.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "mlvalues.h"
|
|
|
|
|
|
|
|
value make_vect(len, init)
|
|
|
|
value len, init;
|
|
|
|
{
|
|
|
|
value res;
|
|
|
|
mlsize_t size, wsize, i;
|
|
|
|
double d;
|
|
|
|
|
|
|
|
size = Long_val(len);
|
|
|
|
if (size == 0) {
|
|
|
|
res = Atom(0);
|
|
|
|
}
|
|
|
|
else if (Is_block(init) && Tag_val(init) == Double_tag) {
|
|
|
|
d = Double_val(init);
|
|
|
|
wsize = size * Double_wosize;
|
1995-07-28 05:21:26 -07:00
|
|
|
if (wsize > Max_wosize)
|
1995-07-27 10:47:15 -07:00
|
|
|
invalid_argument("Array.new");
|
|
|
|
if (wsize < Max_young_wosize)
|
|
|
|
res = alloc(wsize, Double_array_tag);
|
|
|
|
else
|
|
|
|
res = alloc_shr(wsize, Double_array_tag);
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
Store_double_field(res, i, d);
|
|
|
|
}
|
|
|
|
} else {
|
1995-07-28 05:21:26 -07:00
|
|
|
Push_roots(root, 1);
|
1995-07-27 10:47:15 -07:00
|
|
|
if (size > Max_wosize) {
|
|
|
|
Pop_roots();
|
|
|
|
invalid_argument("Array.new");
|
|
|
|
}
|
|
|
|
if (size < Max_young_wosize) {
|
|
|
|
root[0] = init;
|
|
|
|
res = alloc(size, 0);
|
|
|
|
init = root[0];
|
|
|
|
for (i = 0; i < size; i++) Field(res, i) = init;
|
|
|
|
}
|
|
|
|
else if (Is_block(init) && Is_young(init)) {
|
|
|
|
root[0] = init;
|
|
|
|
minor_collection();
|
|
|
|
res = alloc_shr(size, 0);
|
|
|
|
init = root[0];
|
|
|
|
for (i = 0; i < size; i++) Field(res, i) = init;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
root[0] = init;
|
|
|
|
res = alloc_shr(size, 0);
|
|
|
|
init = root[0];
|
|
|
|
for (i = 0; i < size; i++) initialize(&Field(res, i), init);
|
|
|
|
}
|
1995-07-28 05:21:26 -07:00
|
|
|
Pop_roots();
|
1995-07-27 10:47:15 -07:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
value make_array(init)
|
|
|
|
value init;
|
|
|
|
{
|
|
|
|
mlsize_t wsize, size, i;
|
1995-07-28 05:21:26 -07:00
|
|
|
value v, res;
|
1995-07-27 10:47:15 -07:00
|
|
|
|
|
|
|
size = Wosize_val(init);
|
1995-07-28 05:21:26 -07:00
|
|
|
if (size == 0) {
|
1995-07-27 10:47:15 -07:00
|
|
|
return init;
|
|
|
|
} else {
|
1995-07-28 05:21:26 -07:00
|
|
|
v = Field(init, 0);
|
|
|
|
if (Is_long(v) || Tag_val(v) != Double_tag) {
|
|
|
|
return init;
|
|
|
|
} else {
|
|
|
|
Push_roots(root, 1);
|
|
|
|
root[0] = init;
|
|
|
|
wsize = size * Double_wosize;
|
|
|
|
if (wsize > Max_wosize) {
|
|
|
|
Pop_roots();
|
|
|
|
invalid_argument("Array.new");
|
|
|
|
}
|
|
|
|
if (wsize < Max_young_wosize)
|
|
|
|
res = alloc(wsize, Double_array_tag);
|
|
|
|
else
|
|
|
|
res = alloc_shr(wsize, Double_array_tag);
|
|
|
|
init = root[0];
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
Store_double_field(res, i, Double_val(Field(init, i)));
|
|
|
|
}
|
|
|
|
return res;
|
1995-07-27 10:47:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1995-12-05 05:07:49 -08:00
|
|
|
void array_bound_error()
|
|
|
|
{
|
1995-12-05 05:32:15 -08:00
|
|
|
fatal_error("Fatal error: out-of-bound access in array or string\n");
|
1995-12-05 05:07:49 -08:00
|
|
|
}
|