Fix ticket #28: Expressly adjustable array not adjustable.

Renames adjustVector() to adjustArray() and implements adjustArray() on arrays.

Note: This is not the nicest solution; it removes 'private' modifiers, however,
  given the size of the commit, it seems like an issue to be resolved in a later
  cleanup of the Array vs Vector code.
parent 07bd8611
......@@ -86,6 +86,16 @@ public abstract class AbstractArray extends LispObject
return -1; // Not reached.
}
public void setFillPointer(LispObject fillPointer) throws ConditionThrowable
{
setFillPointer(fillPointer.intValue());
}
public void setFillPointer(int fillPointer) throws ConditionThrowable
{
noFillPointer();
}
public boolean isAdjustable()
{
return true;
......@@ -211,7 +221,7 @@ public abstract class AbstractArray extends LispObject
sb.append('(');
if (this instanceof SimpleArray_T)
sb.append("SIMPLE-");
sb.append("ARRAY T (");
sb.append("ARRAY " + getElementType().writeToString() + " (");
for (int i = 0; i < dimv.length; i++) {
sb.append(dimv[i]);
if (i < dimv.length - 1)
......@@ -305,4 +315,14 @@ public abstract class AbstractArray extends LispObject
return 0;
}
}
public abstract AbstractArray adjustArray(int[] dims,
LispObject initialElement,
LispObject initialContents)
throws ConditionThrowable;
public abstract AbstractArray adjustArray(int[] dims,
AbstractArray displacedTo,
int displacement)
throws ConditionThrowable;
}
......@@ -297,13 +297,27 @@ public abstract class AbstractVector extends AbstractArray
}
}
public abstract AbstractVector adjustVector(int size,
public abstract AbstractArray adjustArray(int size,
LispObject initialElement,
LispObject initialContents)
throws ConditionThrowable;
public abstract AbstractVector adjustVector(int size,
public abstract AbstractArray adjustArray(int size,
AbstractArray displacedTo,
int displacement)
throws ConditionThrowable;
public AbstractArray adjustArray(int[] dims,
LispObject initialElement,
LispObject initialContents)
throws ConditionThrowable {
return adjustArray(dims[0], initialElement, initialContents);
}
public AbstractArray adjustArray(int[] dims,
AbstractArray displacedTo,
int displacement)
throws ConditionThrowable {
return adjustArray(dims[0], displacedTo, displacement);
}
}
......@@ -258,7 +258,7 @@ public final class BasicVector_UnsignedByte16 extends AbstractVector
}
@Override
public AbstractVector adjustVector(int newCapacity,
public AbstractVector adjustArray(int newCapacity,
LispObject initialElement,
LispObject initialContents)
throws ConditionThrowable
......@@ -291,7 +291,7 @@ public final class BasicVector_UnsignedByte16 extends AbstractVector
}
@Override
public AbstractVector adjustVector(int newCapacity,
public AbstractVector adjustArray(int newCapacity,
AbstractArray displacedTo,
int displacement)
{
......
......@@ -270,7 +270,7 @@ public final class BasicVector_UnsignedByte32 extends AbstractVector
}
@Override
public AbstractVector adjustVector(int newCapacity,
public AbstractVector adjustArray(int newCapacity,
LispObject initialElement,
LispObject initialContents)
throws ConditionThrowable
......@@ -310,7 +310,7 @@ public final class BasicVector_UnsignedByte32 extends AbstractVector
}
@Override
public AbstractVector adjustVector(int newCapacity,
public AbstractVector adjustArray(int newCapacity,
AbstractArray displacedTo,
int displacement)
{
......
......@@ -268,7 +268,7 @@ public final class BasicVector_UnsignedByte8 extends AbstractVector
}
@Override
public AbstractVector adjustVector(int newCapacity,
public AbstractVector adjustArray(int newCapacity,
LispObject initialElement,
LispObject initialContents)
throws ConditionThrowable
......@@ -308,7 +308,7 @@ public final class BasicVector_UnsignedByte8 extends AbstractVector
}
@Override
public AbstractVector adjustVector(int newCapacity,
public AbstractVector adjustArray(int newCapacity,
AbstractArray displacedTo,
int displacement)
{
......
......@@ -37,7 +37,7 @@ public final class ComplexArray extends AbstractArray
{
private final int[] dimv;
private final LispObject elementType;
private final int totalSize;
private int totalSize;
// For non-displaced arrays.
private LispObject[] data;
......@@ -235,4 +235,57 @@ public final class ComplexArray extends AbstractArray
{
return writeToString(dimv);
}
@Override
public AbstractArray adjustArray(int[] dims,
LispObject initialElement,
LispObject initialContents)
throws ConditionThrowable {
if (isAdjustable()) {
if (initialContents != NIL)
setInitialContents(0, dims, initialContents, 0);
else {
//### FIXME Take the easy way out: we don't want to reorganize
// all of the array code yet
SimpleArray_T tempArray = new SimpleArray_T(dims, elementType);
tempArray.fill(initialElement);
SimpleArray_T.copyArray(this, tempArray);
this.data = tempArray.data;
for (int i = 0; i < dims.length; i++)
dimv[i] = dims[i];
}
return this;
} else {
if (initialContents != NIL)
return new ComplexArray(dims, elementType, initialContents);
else {
ComplexArray newArray = new ComplexArray(dims, elementType);
newArray.fill(initialElement);
return newArray;
}
}
}
@Override
public AbstractArray adjustArray(int[] dims,
AbstractArray displacedTo,
int displacement)
throws ConditionThrowable {
if (isAdjustable()) {
for (int i = 0; i < dims.length; i++)
dimv[i] = dims[i];
this.data = null;
this.array = displacedTo;
this.displacement = displacement;
this.totalSize = computeTotalSize(dims);
return this;
} else {
ComplexArray a = new ComplexArray(dims, displacedTo, displacement);
return a;
}
}
}
......@@ -36,7 +36,7 @@ package org.armedbear.lisp;
public final class ComplexArray_UnsignedByte32 extends AbstractArray
{
private final int[] dimv;
private final int totalSize;
private int totalSize;
// For non-displaced arrays.
// FIXME We should really use an array of unboxed values!
......@@ -231,4 +231,58 @@ public final class ComplexArray_UnsignedByte32 extends AbstractArray
{
return writeToString(dimv);
}
@Override
public AbstractArray adjustArray(int[] dims,
LispObject initialElement,
LispObject initialContents)
throws ConditionThrowable {
if (isAdjustable()) {
if (initialContents != NIL)
setInitialContents(0, dims, initialContents, 0);
else {
//### FIXME Take the easy way out: we don't want to reorganize
// all of the array code yet
SimpleArray_UnsignedByte32 tempArray = new SimpleArray_UnsignedByte32(dims, getElementType());
tempArray.fill(initialElement);
SimpleArray_UnsignedByte32.copyArray(this, tempArray);
this.data = tempArray.data;
for (int i = 0; i < dims.length; i++)
dimv[i] = dims[i];
}
return this;
} else {
if (initialContents != NIL)
return new ComplexArray_UnsignedByte32(dims, initialContents);
else {
ComplexArray_UnsignedByte32 newArray = new ComplexArray_UnsignedByte32(dims);
newArray.fill(initialElement);
return newArray;
}
}
}
@Override
public AbstractArray adjustArray(int[] dims,
AbstractArray displacedTo,
int displacement)
throws ConditionThrowable {
if (isAdjustable()) {
for (int i = 0; i < dims.length; i++)
dimv[i] = dims[i];
this.data = null;
this.array = displacedTo;
this.displacement = displacement;
this.totalSize = computeTotalSize(dims);
return this;
} else {
ComplexArray_UnsignedByte32 a = new ComplexArray_UnsignedByte32(dims, displacedTo, displacement);
return a;
}
}
}
......@@ -36,7 +36,7 @@ package org.armedbear.lisp;
public final class ComplexArray_UnsignedByte8 extends AbstractArray
{
private final int[] dimv;
private final int totalSize;
private int totalSize;
// For non-displaced arrays.
private byte[] data;
......@@ -233,4 +233,58 @@ public final class ComplexArray_UnsignedByte8 extends AbstractArray
}
return writeToString(dimv);
}
@Override
public AbstractArray adjustArray(int[] dims,
LispObject initialElement,
LispObject initialContents)
throws ConditionThrowable {
if (isAdjustable()) {
if (initialContents != NIL)
setInitialContents(0, dims, initialContents, 0);
else {
//### FIXME Take the easy way out: we don't want to reorganize
// all of the array code yet
SimpleArray_UnsignedByte8 tempArray = new SimpleArray_UnsignedByte8(dims, getElementType());
tempArray.fill(initialElement);
SimpleArray_UnsignedByte8.copyArray(this, tempArray);
this.data = tempArray.data;
for (int i = 0; i < dims.length; i++)
dimv[i] = dims[i];
}
return this;
} else {
if (initialContents != NIL)
return new ComplexArray_UnsignedByte8(dims, initialContents);
else {
ComplexArray_UnsignedByte8 newArray = new ComplexArray_UnsignedByte8(dims);
newArray.fill(initialElement);
return newArray;
}
}
}
@Override
public AbstractArray adjustArray(int[] dims,
AbstractArray displacedTo,
int displacement)
throws ConditionThrowable {
if (isAdjustable()) {
for (int i = 0; i < dims.length; i++)
dimv[i] = dims[i];
this.data = null;
this.array = displacedTo;
this.displacement = displacement;
this.totalSize = computeTotalSize(dims);
return this;
} else {
ComplexArray_UnsignedByte8 a = new ComplexArray_UnsignedByte8(dims, displacedTo, displacement);
return a;
}
}
}
......@@ -319,7 +319,7 @@ public final class ComplexBitVector extends AbstractBitVector
}
@Override
public AbstractVector adjustVector(int newCapacity,
public AbstractVector adjustArray(int newCapacity,
LispObject initialElement,
LispObject initialContents)
throws ConditionThrowable
......@@ -380,7 +380,7 @@ public final class ComplexBitVector extends AbstractBitVector
}
@Override
public AbstractVector adjustVector(int size, AbstractArray displacedTo,
public AbstractVector adjustArray(int size, AbstractArray displacedTo,
int displacement)
throws ConditionThrowable
{
......
......@@ -597,7 +597,7 @@ public final class ComplexString extends AbstractString
}
@Override
public AbstractVector adjustVector(int newCapacity,
public AbstractVector adjustArray(int newCapacity,
LispObject initialElement,
LispObject initialContents)
throws ConditionThrowable
......@@ -674,7 +674,7 @@ public final class ComplexString extends AbstractString
}
@Override
public AbstractVector adjustVector(int newCapacity,
public AbstractVector adjustArray(int newCapacity,
AbstractArray displacedTo,
int displacement)
throws ConditionThrowable
......
......@@ -365,7 +365,7 @@ public final class ComplexVector extends AbstractVector
}
@Override
public AbstractVector adjustVector(int newCapacity,
public AbstractVector adjustArray(int newCapacity,
LispObject initialElement,
LispObject initialContents)
throws ConditionThrowable
......@@ -412,7 +412,7 @@ public final class ComplexVector extends AbstractVector
}
@Override
public AbstractVector adjustVector(int newCapacity,
public AbstractVector adjustArray(int newCapacity,
AbstractArray displacedTo,
int displacement)
throws ConditionThrowable
......
......@@ -367,7 +367,7 @@ public final class ComplexVector_UnsignedByte32 extends AbstractVector
}
@Override
public AbstractVector adjustVector(int newCapacity,
public AbstractVector adjustArray(int newCapacity,
LispObject initialElement,
LispObject initialContents)
throws ConditionThrowable
......@@ -416,7 +416,7 @@ public final class ComplexVector_UnsignedByte32 extends AbstractVector
}
@Override
public AbstractVector adjustVector(int newCapacity,
public AbstractVector adjustArray(int newCapacity,
AbstractArray displacedTo,
int displacement)
throws ConditionThrowable
......
......@@ -379,7 +379,7 @@ public final class ComplexVector_UnsignedByte8 extends AbstractVector
}
@Override
public AbstractVector adjustVector(int newCapacity,
public AbstractVector adjustArray(int newCapacity,
LispObject initialElement,
LispObject initialContents)
throws ConditionThrowable
......@@ -429,7 +429,7 @@ public final class ComplexVector_UnsignedByte8 extends AbstractVector
}
@Override
public AbstractVector adjustVector(int newCapacity,
public AbstractVector adjustArray(int newCapacity,
AbstractArray displacedTo,
int displacement)
throws ConditionThrowable
......
......@@ -247,7 +247,7 @@ public final class NilVector extends AbstractString
}
@Override
public AbstractVector adjustVector(int newCapacity,
public AbstractVector adjustArray(int newCapacity,
LispObject initialElement,
LispObject initialContents)
throws ConditionThrowable
......@@ -258,7 +258,7 @@ public final class NilVector extends AbstractString
}
@Override
public AbstractVector adjustVector(int size, AbstractArray displacedTo,
public AbstractVector adjustArray(int size, AbstractArray displacedTo,
int displacement)
throws ConditionThrowable
{
......
......@@ -38,7 +38,7 @@ public final class SimpleArray_T extends AbstractArray
private final int[] dimv;
private final LispObject elementType;
private final int totalSize;
private final LispObject[] data;
final LispObject[] data;
public SimpleArray_T(int[] dimv, LispObject elementType)
{
......@@ -89,6 +89,14 @@ public final class SimpleArray_T extends AbstractArray
setInitialContents(0, dimv, initialContents, 0);
}
public SimpleArray_T(final int[] dimv, final LispObject[] initialData,
final LispObject elementType) {
this.dimv = dimv;
this.elementType = elementType;
this.data = initialData;
this.totalSize = computeTotalSize(dimv);
}
private int setInitialContents(int axis, int[] dims, LispObject contents,
int index)
throws ConditionThrowable
......@@ -314,6 +322,7 @@ public final class SimpleArray_T extends AbstractArray
return writeToString(dimv);
}
@Override
public AbstractArray adjustArray(int[] dimv, LispObject initialElement,
LispObject initialContents)
throws ConditionThrowable
......@@ -330,12 +339,13 @@ public final class SimpleArray_T extends AbstractArray
return newArray;
}
}
// New dimensions are identical to old dimensions.
return this;
// New dimensions are identical to old dimensions, yet
// we're not mutable, so, we need to return a new array
return new SimpleArray_T(dimv, data.clone(), elementType);
}
// Copy a1 to a2 for index tuples that are valid for both arrays.
private static void copyArray(AbstractArray a1, AbstractArray a2)
static void copyArray(AbstractArray a1, AbstractArray a2)
throws ConditionThrowable
{
Debug.assertTrue(a1.getRank() == a2.getRank());
......@@ -366,6 +376,7 @@ public final class SimpleArray_T extends AbstractArray
}
}
@Override
public AbstractArray adjustArray(int[] dimv, AbstractArray displacedTo,
int displacement)
{
......
......@@ -39,7 +39,7 @@ public final class SimpleArray_UnsignedByte32 extends AbstractArray
private final int totalSize;
// FIXME We should really use an array of unboxed values!
private final LispObject[] data;
final LispObject[] data;
public SimpleArray_UnsignedByte32(int[] dimv)
{
......@@ -309,7 +309,7 @@ public final class SimpleArray_UnsignedByte32 extends AbstractArray
}
// Copy a1 to a2 for index tuples that are valid for both arrays.
private static void copyArray(AbstractArray a1, AbstractArray a2)
static void copyArray(AbstractArray a1, AbstractArray a2)
throws ConditionThrowable
{
Debug.assertTrue(a1.getRank() == a2.getRank());
......
......@@ -37,7 +37,7 @@ public final class SimpleArray_UnsignedByte8 extends AbstractArray
{
private final int[] dimv;
private final int totalSize;
private final byte[] data;
final byte[] data;
public SimpleArray_UnsignedByte8(int[] dimv)
{
......@@ -306,7 +306,7 @@ public final class SimpleArray_UnsignedByte8 extends AbstractArray
}
// Copy a1 to a2 for index tuples that are valid for both arrays.
private static void copyArray(AbstractArray a1, AbstractArray a2)
static void copyArray(AbstractArray a1, AbstractArray a2)
throws ConditionThrowable
{
Debug.assertTrue(a1.getRank() == a2.getRank());
......
......@@ -194,7 +194,7 @@ public final class SimpleBitVector extends AbstractBitVector
}
@Override
public AbstractVector adjustVector(int newCapacity,
public AbstractVector adjustArray(int newCapacity,
LispObject initialElement,
LispObject initialContents)
throws ConditionThrowable
......@@ -239,7 +239,7 @@ public final class SimpleBitVector extends AbstractBitVector
}
@Override
public AbstractVector adjustVector(int newCapacity,
public AbstractVector adjustArray(int newCapacity,
AbstractArray displacedTo,
int displacement)
throws ConditionThrowable
......
......@@ -460,7 +460,7 @@ public final class SimpleString extends AbstractString
}
@Override
public AbstractVector adjustVector(int newCapacity,
public AbstractVector adjustArray(int newCapacity,
LispObject initialElement,
LispObject initialContents)
throws ConditionThrowable
......@@ -495,7 +495,7 @@ public final class SimpleString extends AbstractString
}
@Override
public AbstractVector adjustVector(int newCapacity,
public AbstractVector adjustArray(int newCapacity,
AbstractArray displacedTo,
int displacement)
throws ConditionThrowable
......
......@@ -331,7 +331,7 @@ public final class SimpleVector extends AbstractVector
}
@Override
public AbstractVector adjustVector(int newCapacity,
public AbstractVector adjustArray(int newCapacity,
LispObject initialElement,
LispObject initialContents)
throws ConditionThrowable
......@@ -371,7 +371,7 @@ public final class SimpleVector extends AbstractVector
}
@Override
public AbstractVector adjustVector(int newCapacity,
public AbstractVector adjustArray(int newCapacity,
AbstractArray displacedTo,
int displacement)
{
......
......@@ -162,4 +162,31 @@ public final class ZeroRankArray extends AbstractArray
sb.append(" NIL");
return unreadableString(sb.toString());
}
@Override
public AbstractArray adjustArray(int[] dims,
LispObject initialElement,
LispObject initialContents)
throws ConditionThrowable {
if (isAdjustable()) {
if (initialContents != NIL)
data = initialContents;
else
data = initialElement;
return this;
} else {
return new ZeroRankArray(elementType,
initialContents != NIL ? initialContents : initialElement,
false);
}
}
@Override
public AbstractArray adjustArray(int[] dims,
AbstractArray displacedTo,
int displacement)
throws ConditionThrowable {
error(new TypeError("Displacement not supported for array of rank 0."));
return null;
}
}
......@@ -67,9 +67,8 @@ public final class adjust_array extends Primitive
return error(new LispError("ADJUST-ARRAY: incompatible element type."));
}
if (array.getRank() == 0) {
if (initialContentsProvided != NIL)
array.aset(0, initialContents);
return array;
return array.adjustArray(new int[0], NIL,
(initialContentsProvided != NIL) ? initialContents : initialElement);
}
if (initialElementProvided == NIL && array.getElementType() == T)
initialElement = Fixnum.ZERO;
......@@ -81,18 +80,18 @@ public final class adjust_array extends Primitive
newSize = Fixnum.getValue(dimensions);
if (array instanceof AbstractVector) {
AbstractVector v = (AbstractVector) array;
AbstractVector v2;
AbstractArray v2;
if (displacedTo != NIL) {
final int displacement;
if (displacedIndexOffset == NIL)
displacement = 0;
else
displacement = Fixnum.getValue(displacedIndexOffset);
v2 = v.adjustVector(newSize,
v2 = v.adjustArray(newSize,
checkArray(displacedTo),
displacement);
} else {
v2 = v.adjustVector(newSize,
v2 = v.adjustArray(newSize,
initialElement,
initialContents);
}
......@@ -112,24 +111,21 @@ public final class adjust_array extends Primitive
}
} else
dimv[0] = Fixnum.getValue(dimensions);
if (array instanceof SimpleArray_T) {
SimpleArray_T a = (SimpleArray_T) array;
if (displacedTo != NIL) {
final int displacement;
if (displacedIndexOffset == NIL)
displacement = 0;
else
displacement = Fixnum.getValue(displacedIndexOffset);
return a.adjustArray(dimv,
if (displacedTo != NIL) {
final int displacement;
if (displacedIndexOffset == NIL)
displacement = 0;
else
displacement = Fixnum.getValue(displacedIndexOffset);
return array.adjustArray(dimv,
checkArray(displacedTo),
displacement);
} else {
return a.adjustArray(dimv,
} else {
return array.adjustArray(dimv,
initialElement,
initialContents);
}
}
return error(new LispError("ADJUST-ARRAY: unsupported case."));
}
private static final Primitive _ADJUST_ARRAY = new adjust_array();
......
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