PageRenderTime 72ms CodeModel.GetById 51ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/vm/arrays.cpp

http://github.com/abeaumont/factor
C++ | 117 lines | 90 code | 17 blank | 10 comment | 4 complexity | 7816cbbb5b2c9f2d8097cc9352e0f39f MD5 | raw file
  1#include "master.hpp"
  2
  3namespace factor
  4{
  5
  6/* Allocates memory */
  7array *factor_vm::allot_array(cell capacity, cell fill_)
  8{
  9	data_root<object> fill(fill_,this);
 10	array *new_array = allot_uninitialized_array<array>(capacity);
 11	memset_cell(new_array->data(),fill.value(),capacity * sizeof(cell));
 12	return new_array;
 13}
 14
 15/* Allocates memory */
 16void factor_vm::primitive_array()
 17{
 18	data_root<object> fill(ctx->pop(),this);
 19	cell capacity = unbox_array_size();
 20	array *new_array = allot_uninitialized_array<array>(capacity);
 21	memset_cell(new_array->data(),fill.value(),capacity * sizeof(cell));
 22	ctx->push(tag<array>(new_array));
 23}
 24
 25/* Allocates memory */
 26cell factor_vm::allot_array_1(cell obj_)
 27{
 28	data_root<object> obj(obj_,this);
 29	data_root<array> a(allot_uninitialized_array<array>(1),this);
 30	set_array_nth(a.untagged(),0,obj.value());
 31	return a.value();
 32}
 33
 34/* Allocates memory */
 35cell factor_vm::allot_array_2(cell v1_, cell v2_)
 36{
 37	data_root<object> v1(v1_,this);
 38	data_root<object> v2(v2_,this);
 39	data_root<array> a(allot_uninitialized_array<array>(2),this);
 40	set_array_nth(a.untagged(),0,v1.value());
 41	set_array_nth(a.untagged(),1,v2.value());
 42	return a.value();
 43}
 44
 45/* Allocates memory */
 46cell factor_vm::allot_array_4(cell v1_, cell v2_, cell v3_, cell v4_)
 47{
 48	data_root<object> v1(v1_,this);
 49	data_root<object> v2(v2_,this);
 50	data_root<object> v3(v3_,this);
 51	data_root<object> v4(v4_,this);
 52	data_root<array> a(allot_uninitialized_array<array>(4),this);
 53	set_array_nth(a.untagged(),0,v1.value());
 54	set_array_nth(a.untagged(),1,v2.value());
 55	set_array_nth(a.untagged(),2,v3.value());
 56	set_array_nth(a.untagged(),3,v4.value());
 57	return a.value();
 58}
 59
 60/* Allocates memory */
 61void factor_vm::primitive_resize_array()
 62{
 63	data_root<array> a(ctx->pop(),this);
 64	a.untag_check(this);
 65	cell capacity = unbox_array_size();
 66	ctx->push(tag<array>(reallot_array(a.untagged(),capacity)));
 67}
 68
 69/* Allocates memory */
 70cell factor_vm::std_vector_to_array(std::vector<cell> &elements)
 71{
 72	cell element_count = elements.size();
 73	data_roots.push_back(data_root_range(&elements[0],element_count));
 74
 75	tagged<array> objects(allot_uninitialized_array<array>(element_count));
 76	memcpy(objects->data(),&elements[0],element_count * sizeof(cell));
 77
 78	data_roots.pop_back();
 79
 80	return objects.value();
 81}
 82
 83/* Allocates memory */
 84void growable_array::add(cell elt_)
 85{
 86	factor_vm *parent = elements.parent;
 87	data_root<object> elt(elt_,parent);
 88	if(count == array_capacity(elements.untagged()))
 89		elements = parent->reallot_array(elements.untagged(),count * 2);
 90
 91	parent->set_array_nth(elements.untagged(),count++,elt.value());
 92}
 93
 94/* Allocates memory */
 95void growable_array::append(array *elts_)
 96{
 97	factor_vm *parent = elements.parent;
 98	data_root<array> elts(elts_,parent);
 99	cell capacity = array_capacity(elts.untagged());
100	if(count + capacity > array_capacity(elements.untagged()))
101	{
102		elements = parent->reallot_array(elements.untagged(),
103			(count + capacity) * 2);
104	}
105
106	for(cell index = 0; index < capacity; index++)
107		parent->set_array_nth(elements.untagged(),count++,array_nth(elts.untagged(),index));
108}
109
110/* Allocates memory */
111void growable_array::trim()
112{
113	factor_vm *parent = elements.parent;
114	elements = parent->reallot_array(elements.untagged(),count);
115}
116
117}