Commit 0bdb1eaa authored by Stefan Marr's avatar Stefan Marr

Backported array improvements

Signed-off-by: 's avatarStefan Marr <git@stefan-marr.de>
parent 896885d2
......@@ -337,15 +337,9 @@ class Universe(object):
return Array(self.nilObject, length)
def new_array_from_list(self, values):
# Allocate a new array with the same length as the list
result = self.new_array_with_length(len(values))
make_sure_not_resized(values)
return Array(self.nilObject, 0, values)
# Copy all elements from the list into the array
for i in range(len(values)):
result.set_indexable_field(i, values[i])
return result
def new_array_with_strings(self, strings):
# Allocate a new array with the same length as the string array
result = self.new_array_with_length(len(strings))
......
from som.vmobjects.abstract_object import AbstractObject
from .abstract_object import AbstractObject
from rpython.rlib.debug import make_sure_not_resized
class Array(AbstractObject):
_immutable_fields_ = ["_indexable_fields"]
def __init__(self, nilObject, number_of_indexable_fields):
def __init__(self, nilObject, number_of_indexable_fields, values = None):
AbstractObject.__init__(self)
# Private array of indexable fields
self._indexable_fields = [nilObject] * number_of_indexable_fields
if values is None:
self._indexable_fields = [nilObject] * number_of_indexable_fields
else:
self._indexable_fields = values
make_sure_not_resized(self._indexable_fields)
def get_indexable_field(self, index):
# Get the indexable field with the given index
......@@ -18,29 +24,26 @@ class Array(AbstractObject):
# Set the indexable field with the given index to the given value
self._indexable_fields[index] = value
def get_indexable_fields(self):
return self._indexable_fields
def get_number_of_indexable_fields(self):
# Get the number of indexable fields in this array
return len(self._indexable_fields)
def copy_and_extend_with(self, value, universe):
# Allocate a new array which has one indexable field more than this
# array
result = universe.new_array_with_length(
self.get_number_of_indexable_fields() + 1)
result = Array(universe.nilObject,
self.get_number_of_indexable_fields() + 1)
# Copy the indexable fields from this array to the new array
self._copy_indexable_fields_to(result)
# Insert the given object as the last indexable field in the new array
result.set_indexable_field(self.get_number_of_indexable_fields(), value)
# Return the new array
return result
def _copy_indexable_fields_to(self, destination):
# Copy all indexable fields from this array to the destination array
for i in range(self.get_number_of_indexable_fields()):
destination.set_indexable_field(i, self.get_indexable_field(i))
for i, value in enumerate(self._indexable_fields):
destination._indexable_fields[i] = value
def get_class(self, universe):
return universe.arrayClass
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment