2004-07-16 02:26:21 -04:00
|
|
|
#include "factor.h"
|
|
|
|
|
|
|
|
VECTOR* vector(FIXNUM capacity)
|
|
|
|
{
|
2004-08-05 16:49:55 -04:00
|
|
|
VECTOR* vector = allot_object(VECTOR_TYPE,sizeof(VECTOR));
|
2004-07-16 02:26:21 -04:00
|
|
|
vector->top = 0;
|
|
|
|
vector->array = array(capacity,F);
|
|
|
|
return vector;
|
|
|
|
}
|
|
|
|
|
|
|
|
void primitive_vector(void)
|
|
|
|
{
|
2004-08-12 17:36:36 -04:00
|
|
|
drepl(tag_object(vector(to_fixnum(dpeek()))));
|
2004-07-16 02:26:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void primitive_vector_length(void)
|
|
|
|
{
|
2004-08-12 17:36:36 -04:00
|
|
|
drepl(tag_fixnum(untag_vector(dpeek())->top));
|
2004-07-16 02:26:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void primitive_set_vector_length(void)
|
|
|
|
{
|
2004-08-12 17:36:36 -04:00
|
|
|
VECTOR* vector = untag_vector(dpop());
|
2004-07-29 17:18:41 -04:00
|
|
|
FIXNUM length = to_fixnum(dpop());
|
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 > vector->array->capacity)
|
2004-07-16 02:26:21 -04:00
|
|
|
vector->array = grow_array(vector->array,length,F);
|
|
|
|
}
|
|
|
|
|
|
|
|
void primitive_vector_nth(void)
|
|
|
|
{
|
2004-08-12 17:36:36 -04:00
|
|
|
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);
|
2004-08-12 17:36:36 -04:00
|
|
|
dpush(array_nth(vector->array,index));
|
2004-07-16 02:26:21 -04:00
|
|
|
}
|
|
|
|
|
2004-07-31 14:58:16 -04:00
|
|
|
void vector_ensure_capacity(VECTOR* vector, CELL index)
|
2004-07-16 02:26:21 -04:00
|
|
|
{
|
|
|
|
ARRAY* array = vector->array;
|
|
|
|
CELL capacity = array->capacity;
|
|
|
|
if(index >= capacity)
|
|
|
|
array = grow_array(array,index * 2 + 1,F);
|
|
|
|
vector->top = index + 1;
|
|
|
|
vector->array = array;
|
|
|
|
}
|
|
|
|
|
|
|
|
void primitive_set_vector_nth(void)
|
|
|
|
{
|
2004-08-12 17:36:36 -04:00
|
|
|
VECTOR* vector = untag_vector(dpop());
|
2004-07-29 17:18:41 -04:00
|
|
|
FIXNUM index = to_fixnum(dpop());
|
2004-07-16 02:26:21 -04:00
|
|
|
CELL value = dpop();
|
|
|
|
|
|
|
|
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(vector->array,index,value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void fixup_vector(VECTOR* vector)
|
|
|
|
{
|
2004-07-27 21:12:22 -04:00
|
|
|
vector->array = (ARRAY*)((CELL)vector->array
|
2004-08-31 20:31:16 -04:00
|
|
|
+ (active.base - relocation_base));
|
2004-07-16 02:26:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void collect_vector(VECTOR* vector)
|
|
|
|
{
|
|
|
|
vector->array = copy_array(vector->array);
|
|
|
|
}
|