use of com.oracle.truffle.api.object.Property in project graal by oracle.
the class PropertyMapTest method testPropertyMap.
@Test
public void testPropertyMap() {
PropertyMap map = PropertyMap.empty();
Map<Object, Property> referenceMap = new LinkedHashMap<>();
Random rnd = new Random();
final int size = 1000;
int[] randomSequence = rnd.ints().limit(size).toArray();
int[] shuffledSequence = randomSequence.clone();
shuffle(shuffledSequence, rnd);
Layout layout = Layout.newLayout().build();
// fill the map
for (int i = 0; i < size; i++) {
int id = randomSequence[i];
String key = String.valueOf(id);
Property value = Property.create(key, newLocation(layout, id), 0);
map = (PropertyMap) map.copyAndPut(key, value);
referenceMap.put(key, value);
assertEqualsOrdered(referenceMap, map);
}
// put the same values again, should not modify the map
PropertyMap initial = map;
for (int i = 0; i < size; i++) {
int id = randomSequence[i];
String key = String.valueOf(id);
Property value = Property.create(key, newLocation(layout, id), 0);
map = (PropertyMap) map.copyAndPut(key, value);
assertSame(initial, map);
}
assertEqualsOrdered(referenceMap, map);
// update existing values
for (int i = 0; i < size; i++) {
int id = randomSequence[i];
String key = String.valueOf(id);
Property value = Property.create(key, newLocation(layout, (double) id), 0);
map = (PropertyMap) map.copyAndPut(key, value);
referenceMap.put(key, value);
}
assertEqualsOrdered(referenceMap, map);
for (int i = size - 1; i >= 0; i--) {
int id = randomSequence[i];
String key = String.valueOf(id);
Property value = Property.create(key, newLocation(layout, (double) id), 0);
map = (PropertyMap) map.copyAndPut(key, value);
referenceMap.put(key, value);
}
assertEqualsOrdered(referenceMap, map);
// update existing values, in random order
for (int i = 0; i < size; i++) {
int id = shuffledSequence[i];
String key = String.valueOf(id);
Property value = Property.create(key, newLocation(layout, (long) id), 0);
map = (PropertyMap) map.copyAndPut(key, value);
referenceMap.put(key, value);
}
assertEqualsOrdered(referenceMap, map);
// remove keys
for (int i = size - 10; i < size; i++) {
int id = randomSequence[i];
String key = String.valueOf(id);
map = (PropertyMap) map.copyAndRemove(key);
referenceMap.remove(key);
assertEqualsOrdered(referenceMap, map);
}
for (int i = 10; i >= 0; i--) {
int id = randomSequence[i];
String key = String.valueOf(id);
map = (PropertyMap) map.copyAndRemove(key);
referenceMap.remove(key);
assertEqualsOrdered(referenceMap, map);
}
for (int i = 0; i < size; i++) {
int id = shuffledSequence[i];
String key = String.valueOf(id);
map = (PropertyMap) map.copyAndRemove(key);
referenceMap.remove(key);
assertEqualsOrdered(referenceMap, map);
}
}
use of com.oracle.truffle.api.object.Property in project graal by oracle.
the class LayoutStrategy method generalizeProperty.
/**
* @since 0.17 or earlier
*/
protected ShapeImpl generalizeProperty(Property oldProperty, Object value, ShapeImpl currentShape, ShapeImpl nextShape) {
Location oldLocation = oldProperty.getLocation();
Location newLocation = currentShape.allocator().locationForValueUpcast(value, oldLocation);
Property newProperty = oldProperty.relocate(newLocation);
ShapeImpl newShape = nextShape.replaceProperty(oldProperty, newProperty);
return newShape;
}
use of com.oracle.truffle.api.object.Property in project graal by oracle.
the class LayoutStrategy method propertySetWithShapeFallback.
/**
* @since 0.17 or earlier
*/
protected void propertySetWithShapeFallback(Property property, DynamicObject store, Object value, ShapeImpl currentShape, ShapeImpl nextShape) {
ShapeImpl oldShape = currentShape;
ShapeImpl newNextShape = generalizeProperty(property, value, oldShape, nextShape);
Property newProperty = newNextShape.getProperty(property.getKey());
newProperty.setSafe(store, value, oldShape, newNextShape);
}
use of com.oracle.truffle.api.object.Property in project graal by oracle.
the class LayoutStrategy method defineProperty.
/**
* @since 0.17 or earlier
*/
protected ShapeImpl defineProperty(ShapeImpl oldShape, Object key, Object value, int flags, LocationFactory locationFactory, Property existing) {
if (existing == null) {
Property property = Property.create(key, locationFactory.createLocation(oldShape, value), flags);
ShapeImpl newShape = oldShape.addProperty(property);
return newShape;
} else {
if (existing.getFlags() == flags) {
if (existing.getLocation().canSet(value)) {
return oldShape;
} else {
return definePropertyGeneralize(oldShape, existing, value, locationFactory);
}
} else {
return definePropertyChangeFlags(oldShape, existing, value, flags);
}
}
}
use of com.oracle.truffle.api.object.Property in project graal by oracle.
the class LayoutStrategy method definePropertyChangeFlags.
/**
* @since 0.17 or earlier
*/
protected ShapeImpl definePropertyChangeFlags(ShapeImpl oldShape, Property oldProperty, Object value, int flags) {
Location oldLocation = oldProperty.getLocation();
Location newLocation;
if (oldLocation.canSet(value)) {
newLocation = oldLocation;
} else {
newLocation = oldShape.allocator().locationForValueUpcast(value, oldLocation);
}
Property newProperty = Property.create(oldProperty.getKey(), newLocation, flags);
ShapeImpl newShape = oldShape.replaceProperty(oldProperty, newProperty);
return newShape;
}
Aggregations