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