VM: Refactor arrays.cpp/hpp to Factor style
parent
a437576dc9
commit
76375afd1c
|
@ -1,11 +1,9 @@
|
||||||
#include "master.hpp"
|
#include "master.hpp"
|
||||||
|
|
||||||
namespace factor
|
namespace factor {
|
||||||
{
|
|
||||||
|
|
||||||
/* Allocates memory */
|
/* Allocates memory */
|
||||||
array *factor_vm::allot_array(cell capacity, cell fill_)
|
array* factor_vm::allot_array(cell capacity, cell fill_) {
|
||||||
{
|
|
||||||
data_root<object> fill(fill_, this);
|
data_root<object> fill(fill_, this);
|
||||||
array* new_array = allot_uninitialized_array<array>(capacity);
|
array* new_array = allot_uninitialized_array<array>(capacity);
|
||||||
memset_cell(new_array->data(), fill.value(), capacity * sizeof(cell));
|
memset_cell(new_array->data(), fill.value(), capacity * sizeof(cell));
|
||||||
|
@ -13,8 +11,7 @@ array *factor_vm::allot_array(cell capacity, cell fill_)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Allocates memory */
|
/* Allocates memory */
|
||||||
void factor_vm::primitive_array()
|
void factor_vm::primitive_array() {
|
||||||
{
|
|
||||||
data_root<object> fill(ctx->pop(), this);
|
data_root<object> fill(ctx->pop(), this);
|
||||||
cell capacity = unbox_array_size();
|
cell capacity = unbox_array_size();
|
||||||
array* new_array = allot_uninitialized_array<array>(capacity);
|
array* new_array = allot_uninitialized_array<array>(capacity);
|
||||||
|
@ -23,8 +20,7 @@ void factor_vm::primitive_array()
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Allocates memory */
|
/* Allocates memory */
|
||||||
cell factor_vm::allot_array_1(cell obj_)
|
cell factor_vm::allot_array_1(cell obj_) {
|
||||||
{
|
|
||||||
data_root<object> obj(obj_, this);
|
data_root<object> obj(obj_, this);
|
||||||
data_root<array> a(allot_uninitialized_array<array>(1), this);
|
data_root<array> a(allot_uninitialized_array<array>(1), this);
|
||||||
set_array_nth(a.untagged(), 0, obj.value());
|
set_array_nth(a.untagged(), 0, obj.value());
|
||||||
|
@ -32,8 +28,7 @@ cell factor_vm::allot_array_1(cell obj_)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Allocates memory */
|
/* Allocates memory */
|
||||||
cell factor_vm::allot_array_2(cell v1_, cell v2_)
|
cell factor_vm::allot_array_2(cell v1_, cell v2_) {
|
||||||
{
|
|
||||||
data_root<object> v1(v1_, this);
|
data_root<object> v1(v1_, this);
|
||||||
data_root<object> v2(v2_, this);
|
data_root<object> v2(v2_, this);
|
||||||
data_root<array> a(allot_uninitialized_array<array>(2), this);
|
data_root<array> a(allot_uninitialized_array<array>(2), this);
|
||||||
|
@ -43,8 +38,7 @@ cell factor_vm::allot_array_2(cell v1_, cell v2_)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Allocates memory */
|
/* Allocates memory */
|
||||||
cell factor_vm::allot_array_4(cell v1_, cell v2_, cell v3_, cell v4_)
|
cell factor_vm::allot_array_4(cell v1_, cell v2_, cell v3_, cell v4_) {
|
||||||
{
|
|
||||||
data_root<object> v1(v1_, this);
|
data_root<object> v1(v1_, this);
|
||||||
data_root<object> v2(v2_, this);
|
data_root<object> v2(v2_, this);
|
||||||
data_root<object> v3(v3_, this);
|
data_root<object> v3(v3_, this);
|
||||||
|
@ -58,8 +52,7 @@ cell factor_vm::allot_array_4(cell v1_, cell v2_, cell v3_, cell v4_)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Allocates memory */
|
/* Allocates memory */
|
||||||
void factor_vm::primitive_resize_array()
|
void factor_vm::primitive_resize_array() {
|
||||||
{
|
|
||||||
data_root<array> a(ctx->pop(), this);
|
data_root<array> a(ctx->pop(), this);
|
||||||
a.untag_check(this);
|
a.untag_check(this);
|
||||||
cell capacity = unbox_array_size();
|
cell capacity = unbox_array_size();
|
||||||
|
@ -67,8 +60,7 @@ void factor_vm::primitive_resize_array()
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Allocates memory */
|
/* Allocates memory */
|
||||||
cell factor_vm::std_vector_to_array(std::vector<cell> &elements)
|
cell factor_vm::std_vector_to_array(std::vector<cell>& elements) {
|
||||||
{
|
|
||||||
cell element_count = elements.size();
|
cell element_count = elements.size();
|
||||||
data_roots.push_back(data_root_range(&elements[0], element_count));
|
data_roots.push_back(data_root_range(&elements[0], element_count));
|
||||||
|
|
||||||
|
@ -81,8 +73,7 @@ cell factor_vm::std_vector_to_array(std::vector<cell> &elements)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Allocates memory */
|
/* Allocates memory */
|
||||||
void growable_array::add(cell elt_)
|
void growable_array::add(cell elt_) {
|
||||||
{
|
|
||||||
factor_vm* parent = elements.parent;
|
factor_vm* parent = elements.parent;
|
||||||
data_root<object> elt(elt_, parent);
|
data_root<object> elt(elt_, parent);
|
||||||
if (count == array_capacity(elements.untagged()))
|
if (count == array_capacity(elements.untagged()))
|
||||||
|
@ -92,24 +83,22 @@ void growable_array::add(cell elt_)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Allocates memory */
|
/* Allocates memory */
|
||||||
void growable_array::append(array *elts_)
|
void growable_array::append(array* elts_) {
|
||||||
{
|
|
||||||
factor_vm* parent = elements.parent;
|
factor_vm* parent = elements.parent;
|
||||||
data_root<array> elts(elts_, parent);
|
data_root<array> elts(elts_, parent);
|
||||||
cell capacity = array_capacity(elts.untagged());
|
cell capacity = array_capacity(elts.untagged());
|
||||||
if(count + capacity > array_capacity(elements.untagged()))
|
if (count + capacity > array_capacity(elements.untagged())) {
|
||||||
{
|
elements =
|
||||||
elements = parent->reallot_array(elements.untagged(),
|
parent->reallot_array(elements.untagged(), (count + capacity) * 2);
|
||||||
(count + capacity) * 2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (cell index = 0; index < capacity; index++)
|
for (cell index = 0; index < capacity; index++)
|
||||||
parent->set_array_nth(elements.untagged(),count++,array_nth(elts.untagged(),index));
|
parent->set_array_nth(elements.untagged(), count++,
|
||||||
|
array_nth(elts.untagged(), index));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Allocates memory */
|
/* Allocates memory */
|
||||||
void growable_array::trim()
|
void growable_array::trim() {
|
||||||
{
|
|
||||||
factor_vm* parent = elements.parent;
|
factor_vm* parent = elements.parent;
|
||||||
elements = parent->reallot_array(elements.untagged(), count);
|
elements = parent->reallot_array(elements.untagged(), count);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
namespace factor
|
namespace factor {
|
||||||
{
|
|
||||||
|
|
||||||
inline cell array_nth(array *array, cell slot)
|
inline cell array_nth(array* array, cell slot) {
|
||||||
{
|
|
||||||
#ifdef FACTOR_DEBUG
|
#ifdef FACTOR_DEBUG
|
||||||
FACTOR_ASSERT(slot < array_capacity(array));
|
FACTOR_ASSERT(slot < array_capacity(array));
|
||||||
FACTOR_ASSERT(array->type() == ARRAY_TYPE);
|
FACTOR_ASSERT(array->type() == ARRAY_TYPE);
|
||||||
|
@ -10,8 +8,7 @@ inline cell array_nth(array *array, cell slot)
|
||||||
return array->data()[slot];
|
return array->data()[slot];
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void factor_vm::set_array_nth(array *array, cell slot, cell value)
|
inline void factor_vm::set_array_nth(array* array, cell slot, cell value) {
|
||||||
{
|
|
||||||
#ifdef FACTOR_DEBUG
|
#ifdef FACTOR_DEBUG
|
||||||
FACTOR_ASSERT(slot < array_capacity(array));
|
FACTOR_ASSERT(slot < array_capacity(array));
|
||||||
FACTOR_ASSERT(array->type() == ARRAY_TYPE);
|
FACTOR_ASSERT(array->type() == ARRAY_TYPE);
|
||||||
|
@ -25,8 +22,9 @@ struct growable_array {
|
||||||
cell count;
|
cell count;
|
||||||
data_root<array> elements;
|
data_root<array> elements;
|
||||||
|
|
||||||
explicit growable_array(factor_vm *parent, cell capacity = 10) :
|
explicit growable_array(factor_vm* parent, cell capacity = 10)
|
||||||
count(0), elements(parent->allot_array(capacity,false_object),parent) {}
|
: count(0),
|
||||||
|
elements(parent->allot_array(capacity, false_object), parent) {}
|
||||||
|
|
||||||
void add(cell elt);
|
void add(cell elt);
|
||||||
void append(array* elts);
|
void append(array* elts);
|
||||||
|
|
Loading…
Reference in New Issue