factor/native/vector.c

91 lines
1.9 KiB
C
Raw Normal View History

2004-07-16 02:26:21 -04:00
#include "factor.h"
F_VECTOR* vector(F_FIXNUM capacity)
2004-07-16 02:26:21 -04:00
{
F_VECTOR* vector = allot_object(VECTOR_TYPE,sizeof(F_VECTOR));
2004-07-16 02:26:21 -04:00
vector->top = 0;
vector->array = tag_object(array(capacity,F));
2004-07-16 02:26:21 -04:00
return vector;
}
void primitive_vector(void)
{
maybe_garbage_collection();
drepl(tag_object(vector(to_fixnum(dpeek()))));
2004-07-16 02:26:21 -04:00
}
void primitive_vector_length(void)
{
drepl(tag_fixnum(untag_vector(dpeek())->top));
2004-07-16 02:26:21 -04:00
}
void primitive_set_vector_length(void)
{
F_VECTOR* vector;
F_FIXNUM length;
F_ARRAY* array;
maybe_garbage_collection();
vector = untag_vector(dpop());
length = to_fixnum(dpop());
array = untag_array(vector->array);
2004-07-16 02:26:21 -04:00
if(length < 0)
2004-07-24 17:37:42 -04:00
range_error(tag_object(vector),length,vector->top);
2004-08-27 02:09:24 -04:00
vector->top = length;
if(length > array->capacity)
vector->array = tag_object(grow_array(array,length,F));
2004-07-16 02:26:21 -04:00
}
void primitive_vector_nth(void)
{
F_VECTOR* vector = untag_vector(dpop());
2004-07-29 17:18:41 -04:00
CELL index = to_fixnum(dpop());
2004-07-16 02:26:21 -04:00
if(index < 0 || index >= vector->top)
2004-07-24 17:37:42 -04:00
range_error(tag_object(vector),index,vector->top);
dpush(array_nth(untag_array(vector->array),index));
2004-07-16 02:26:21 -04:00
}
void vector_ensure_capacity(F_VECTOR* vector, CELL index)
2004-07-16 02:26:21 -04:00
{
F_ARRAY* array = untag_array(vector->array);
2004-07-16 02:26:21 -04:00
CELL capacity = array->capacity;
if(index >= capacity)
array = grow_array(array,index * 2 + 1,F);
vector->top = index + 1;
vector->array = tag_object(array);
2004-07-16 02:26:21 -04:00
}
void primitive_set_vector_nth(void)
{
F_VECTOR* vector;
F_FIXNUM index;
CELL value;
maybe_garbage_collection();
vector = untag_vector(dpop());
index = to_fixnum(dpop());
value = dpop();
2004-07-16 02:26:21 -04:00
if(index < 0)
2004-07-24 17:37:42 -04:00
range_error(tag_object(vector),index,vector->top);
2004-07-16 02:26:21 -04:00
else if(index >= vector->top)
vector_ensure_capacity(vector,index);
/* the following does not check bounds! */
set_array_nth(untag_array(vector->array),index,value);
2004-07-16 02:26:21 -04:00
}
void fixup_vector(F_VECTOR* vector)
2004-07-16 02:26:21 -04:00
{
fixup(&vector->array);
2004-07-16 02:26:21 -04:00
}
void collect_vector(F_VECTOR* vector)
2004-07-16 02:26:21 -04:00
{
copy_object(&vector->array);
2004-07-16 02:26:21 -04:00
}