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 */
|
2001-12-07 05:41:02 -08:00
|
|
|
/* under the terms of the GNU Library General Public License, with */
|
|
|
|
/* the special exception on linking described in file ../LICENSE. */
|
1995-08-09 08:06:35 -07:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
/* Operations on arrays */
|
|
|
|
|
|
|
|
#include "alloc.h"
|
|
|
|
#include "fail.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "mlvalues.h"
|
|
|
|
|
1998-04-06 02:15:55 -07:00
|
|
|
#ifndef NATIVE_CODE
|
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
CAMLprim value caml_array_get_addr(value array, value index)
|
1995-06-15 01:09:30 -07:00
|
|
|
{
|
|
|
|
long idx = Long_val(index);
|
2004-01-02 11:23:29 -08:00
|
|
|
if (idx < 0 || idx >= Wosize_val(array)) caml_array_bound_error();
|
1995-06-15 01:09:30 -07:00
|
|
|
return Field(array, idx);
|
|
|
|
}
|
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
CAMLprim value caml_array_get_float(value array, value index)
|
1998-04-06 02:15:55 -07:00
|
|
|
{
|
|
|
|
long idx = Long_val(index);
|
|
|
|
double d;
|
|
|
|
value res;
|
|
|
|
|
2003-11-21 07:55:24 -08:00
|
|
|
if (idx < 0 || idx >= Wosize_val(array) / Double_wosize)
|
2004-01-02 11:23:29 -08:00
|
|
|
caml_array_bound_error();
|
1998-04-06 02:15:55 -07:00
|
|
|
d = Double_field(array, idx);
|
|
|
|
#define Setup_for_gc
|
|
|
|
#define Restore_after_gc
|
|
|
|
Alloc_small(res, Double_wosize, Double_tag);
|
|
|
|
#undef Setup_for_gc
|
|
|
|
#undef Restore_after_gc
|
|
|
|
Store_double_val(res, d);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
CAMLprim value caml_array_get(value array, value index)
|
1998-04-06 02:15:55 -07:00
|
|
|
{
|
|
|
|
if (Tag_val(array) == Double_array_tag)
|
2003-12-31 06:20:40 -08:00
|
|
|
return caml_array_get_float(array, index);
|
1998-04-06 02:15:55 -07:00
|
|
|
else
|
2003-12-31 06:20:40 -08:00
|
|
|
return caml_array_get_addr(array, index);
|
1998-04-06 02:15:55 -07:00
|
|
|
}
|
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
CAMLprim value caml_array_set_addr(value array, value index, value newval)
|
1995-06-15 01:09:30 -07:00
|
|
|
{
|
|
|
|
long idx = Long_val(index);
|
2004-01-02 11:23:29 -08:00
|
|
|
if (idx < 0 || idx >= Wosize_val(array)) caml_array_bound_error();
|
1995-06-15 01:09:30 -07:00
|
|
|
Modify(&Field(array, idx), newval);
|
|
|
|
return Val_unit;
|
|
|
|
}
|
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
CAMLprim value caml_array_set_float(value array, value index, value newval)
|
1998-04-06 02:15:55 -07:00
|
|
|
{
|
|
|
|
long idx = Long_val(index);
|
|
|
|
if (idx < 0 || idx >= Wosize_val(array) / Double_wosize)
|
2004-01-02 11:23:29 -08:00
|
|
|
caml_array_bound_error();
|
1998-04-06 02:15:55 -07:00
|
|
|
Store_double_field(array, idx, Double_val(newval));
|
|
|
|
return Val_unit;
|
|
|
|
}
|
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
CAMLprim value caml_array_set(value array, value index, value newval)
|
1998-04-06 02:15:55 -07:00
|
|
|
{
|
|
|
|
if (Tag_val(array) == Double_array_tag)
|
2003-12-31 06:20:40 -08:00
|
|
|
return caml_array_set_float(array, index, newval);
|
1998-04-06 02:15:55 -07:00
|
|
|
else
|
2003-12-31 06:20:40 -08:00
|
|
|
return caml_array_set_addr(array, index, newval);
|
1998-04-06 02:15:55 -07:00
|
|
|
}
|
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
CAMLprim value caml_array_unsafe_get_float(value array, value index)
|
1995-05-04 03:15:53 -07:00
|
|
|
{
|
1998-04-06 02:15:55 -07:00
|
|
|
double d;
|
1995-05-04 03:15:53 -07:00
|
|
|
value res;
|
|
|
|
|
1998-04-06 02:15:55 -07:00
|
|
|
d = Double_field(array, Long_val(index));
|
|
|
|
#define Setup_for_gc
|
|
|
|
#define Restore_after_gc
|
|
|
|
Alloc_small(res, Double_wosize, Double_tag);
|
|
|
|
#undef Setup_for_gc
|
|
|
|
#undef Restore_after_gc
|
|
|
|
Store_double_val(res, d);
|
|
|
|
return res;
|
|
|
|
}
|
1997-05-26 10:16:31 -07:00
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
CAMLprim value caml_array_unsafe_get(value array, value index)
|
1998-04-06 02:15:55 -07:00
|
|
|
{
|
|
|
|
if (Tag_val(array) == Double_array_tag)
|
2003-12-31 06:20:40 -08:00
|
|
|
return caml_array_unsafe_get_float(array, index);
|
1998-04-06 02:15:55 -07:00
|
|
|
else
|
|
|
|
return Field(array, Long_val(index));
|
|
|
|
}
|
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
CAMLprim value caml_array_unsafe_set_addr(value array, value index,value newval)
|
1998-04-06 02:15:55 -07:00
|
|
|
{
|
|
|
|
long idx = Long_val(index);
|
|
|
|
Modify(&Field(array, idx), newval);
|
|
|
|
return Val_unit;
|
|
|
|
}
|
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
CAMLprim value caml_array_unsafe_set_float(value array,value index,value newval)
|
1998-04-06 02:15:55 -07:00
|
|
|
{
|
|
|
|
Store_double_field(array, Long_val(index), Double_val(newval));
|
|
|
|
return Val_unit;
|
|
|
|
}
|
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
CAMLprim value caml_array_unsafe_set(value array, value index, value newval)
|
1998-04-06 02:15:55 -07:00
|
|
|
{
|
|
|
|
if (Tag_val(array) == Double_array_tag)
|
2003-12-31 06:20:40 -08:00
|
|
|
return caml_array_unsafe_set_float(array, index, newval);
|
1998-04-06 02:15:55 -07:00
|
|
|
else
|
2003-12-31 06:20:40 -08:00
|
|
|
return caml_array_unsafe_set_addr(array, index, newval);
|
1998-04-06 02:15:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
CAMLprim value caml_make_vect(value len, value init)
|
1998-04-06 02:15:55 -07:00
|
|
|
{
|
1999-11-29 11:03:05 -08:00
|
|
|
CAMLparam2 (len, init);
|
|
|
|
CAMLlocal1 (res);
|
1998-04-06 02:15:55 -07:00
|
|
|
mlsize_t size, wsize, i;
|
|
|
|
double d;
|
|
|
|
|
|
|
|
size = Long_val(len);
|
|
|
|
if (size == 0) {
|
|
|
|
res = Atom(0);
|
|
|
|
}
|
2001-08-21 03:00:11 -07:00
|
|
|
else if (Is_block(init)
|
|
|
|
&& (Is_atom(init) || Is_young(init) || Is_in_heap(init))
|
|
|
|
&& Tag_val(init) == Double_tag) {
|
1998-04-06 02:15:55 -07:00
|
|
|
d = Double_val(init);
|
|
|
|
wsize = size * Double_wosize;
|
2004-01-01 08:42:43 -08:00
|
|
|
if (wsize > Max_wosize) caml_invalid_argument("Array.make");
|
2003-12-29 14:15:02 -08:00
|
|
|
res = caml_alloc(wsize, Double_array_tag);
|
1998-04-06 02:15:55 -07:00
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
Store_double_field(res, i, d);
|
1997-05-26 10:16:31 -07:00
|
|
|
}
|
1998-04-06 02:15:55 -07:00
|
|
|
} else {
|
2004-01-01 08:42:43 -08:00
|
|
|
if (size > Max_wosize) caml_invalid_argument("Array.make");
|
1999-11-29 11:03:05 -08:00
|
|
|
if (size < Max_young_wosize) {
|
2003-12-29 14:15:02 -08:00
|
|
|
res = caml_alloc_small(size, 0);
|
1999-11-29 11:03:05 -08:00
|
|
|
for (i = 0; i < size; i++) Field(res, i) = init;
|
|
|
|
}
|
|
|
|
else if (Is_block(init) && Is_young(init)) {
|
2003-12-31 06:20:40 -08:00
|
|
|
caml_minor_collection();
|
|
|
|
res = caml_alloc_shr(size, 0);
|
1999-11-29 11:03:05 -08:00
|
|
|
for (i = 0; i < size; i++) Field(res, i) = init;
|
2003-12-31 06:20:40 -08:00
|
|
|
res = caml_check_urgent_gc (res);
|
1999-11-29 11:03:05 -08:00
|
|
|
}
|
|
|
|
else {
|
2003-12-31 06:20:40 -08:00
|
|
|
res = caml_alloc_shr(size, 0);
|
|
|
|
for (i = 0; i < size; i++) caml_initialize(&Field(res, i), init);
|
|
|
|
res = caml_check_urgent_gc (res);
|
1999-11-29 11:03:05 -08:00
|
|
|
}
|
1998-04-06 02:15:55 -07:00
|
|
|
}
|
1999-11-29 11:03:05 -08:00
|
|
|
CAMLreturn (res);
|
1995-05-04 03:15:53 -07:00
|
|
|
}
|
1998-04-06 02:15:55 -07:00
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
CAMLprim value caml_make_array(value init)
|
1998-04-06 02:15:55 -07:00
|
|
|
{
|
1999-11-29 11:03:05 -08:00
|
|
|
CAMLparam1 (init);
|
1998-04-06 02:15:55 -07:00
|
|
|
mlsize_t wsize, size, i;
|
1999-11-29 11:03:05 -08:00
|
|
|
CAMLlocal2 (v, res);
|
1998-04-06 02:15:55 -07:00
|
|
|
|
|
|
|
size = Wosize_val(init);
|
|
|
|
if (size == 0) {
|
1999-11-29 11:03:05 -08:00
|
|
|
CAMLreturn (init);
|
1998-04-06 02:15:55 -07:00
|
|
|
} else {
|
|
|
|
v = Field(init, 0);
|
2001-08-21 03:00:11 -07:00
|
|
|
if (Is_long(v)
|
|
|
|
|| (!Is_atom(v) && !Is_young(v) && !Is_in_heap(v))
|
|
|
|
|| Tag_val(v) != Double_tag) {
|
1999-11-29 11:03:05 -08:00
|
|
|
CAMLreturn (init);
|
1998-04-06 02:15:55 -07:00
|
|
|
} else {
|
|
|
|
Assert(size < Max_young_wosize);
|
|
|
|
wsize = size * Double_wosize;
|
2003-12-29 14:15:02 -08:00
|
|
|
res = caml_alloc_small(wsize, Double_array_tag);
|
1999-11-29 11:03:05 -08:00
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
Store_double_field(res, i, Double_val(Field(init, i)));
|
|
|
|
}
|
|
|
|
CAMLreturn (res);
|
1998-04-06 02:15:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|