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,14 +337,8 @@ class Universe(object): ...@@ -337,14 +337,8 @@ class Universe(object):
return Array(self.nilObject, length) return Array(self.nilObject, length)
def new_array_from_list(self, values): def new_array_from_list(self, values):
# Allocate a new array with the same length as the list make_sure_not_resized(values)
result = self.new_array_with_length(len(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): def new_array_with_strings(self, strings):
# Allocate a new array with the same length as the string array # Allocate a new array with the same length as the string array
......
from som.vmobjects.abstract_object import AbstractObject from .abstract_object import AbstractObject
from rpython.rlib.debug import make_sure_not_resized
class Array(AbstractObject): class Array(AbstractObject):
_immutable_fields_ = ["_indexable_fields"] _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) AbstractObject.__init__(self)
# Private array of indexable fields # Private array of indexable fields
if values is None:
self._indexable_fields = [nilObject] * number_of_indexable_fields 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): def get_indexable_field(self, index):
# Get the indexable field with the given index # Get the indexable field with the given index
...@@ -18,29 +24,26 @@ class Array(AbstractObject): ...@@ -18,29 +24,26 @@ class Array(AbstractObject):
# Set the indexable field with the given index to the given value # Set the indexable field with the given index to the given value
self._indexable_fields[index] = value self._indexable_fields[index] = value
def get_indexable_fields(self):
return self._indexable_fields
def get_number_of_indexable_fields(self): def get_number_of_indexable_fields(self):
# Get the number of indexable fields in this array # Get the number of indexable fields in this array
return len(self._indexable_fields) return len(self._indexable_fields)
def copy_and_extend_with(self, value, universe): def copy_and_extend_with(self, value, universe):
# Allocate a new array which has one indexable field more than this result = Array(universe.nilObject,
# array
result = universe.new_array_with_length(
self.get_number_of_indexable_fields() + 1) self.get_number_of_indexable_fields() + 1)
# Copy the indexable fields from this array to the new array
self._copy_indexable_fields_to(result) self._copy_indexable_fields_to(result)
# Insert the given object as the last indexable field in the new array # Insert the given object as the last indexable field in the new array
result.set_indexable_field(self.get_number_of_indexable_fields(), value) result.set_indexable_field(self.get_number_of_indexable_fields(), value)
# Return the new array
return result return result
def _copy_indexable_fields_to(self, destination): def _copy_indexable_fields_to(self, destination):
# Copy all indexable fields from this array to the destination array for i, value in enumerate(self._indexable_fields):
for i in range(self.get_number_of_indexable_fields()): destination._indexable_fields[i] = value
destination.set_indexable_field(i, self.get_indexable_field(i))
def get_class(self, universe): def get_class(self, universe):
return universe.arrayClass 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