Change and document the internal adjustArray() protocol: we can't use NIL as a...

Change and document the internal adjustArray() protocol: we can't use NIL as a marker for "absent initial contents": It's valid for ZeroRankArray.
parent 50f82a43
......@@ -316,13 +316,30 @@ public abstract class AbstractArray extends LispObject
}
}
public abstract AbstractArray adjustArray(int[] dims,
/** Returns a newly allocated array or the current array with
* adjusted dimensions.
*
* @param dims
* @param initialElement @c null if none
* @param initialContents @c null if none
* @return @c this or a new array
* @throws org.armedbear.lisp.ConditionThrowable
*/
public abstract AbstractArray adjustArray(int[] dims,
LispObject initialElement,
LispObject initialContents)
throws ConditionThrowable;
throws ConditionThrowable;
public abstract AbstractArray adjustArray(int[] dims,
/**
*
* @param dims
* @param displacedTo
* @param displacement
* @return
* @throws org.armedbear.lisp.ConditionThrowable
*/
public abstract AbstractArray adjustArray(int[] dims,
AbstractArray displacedTo,
int displacement)
throws ConditionThrowable;
throws ConditionThrowable;
}
......@@ -263,7 +263,7 @@ public final class BasicVector_UnsignedByte16 extends AbstractVector
LispObject initialContents)
throws ConditionThrowable
{
if (initialContents != NIL) {
if (initialContents != null) {
LispObject[] newElements = new LispObject[newCapacity];
if (initialContents.listp()) {
LispObject list = initialContents;
......@@ -282,8 +282,9 @@ public final class BasicVector_UnsignedByte16 extends AbstractVector
LispObject[] newElements = new LispObject[newCapacity];
System.arraycopy(elements, 0, newElements, 0,
Math.min(capacity, newCapacity));
for (int i = capacity; i < newCapacity; i++)
newElements[i] = initialElement;
if (initialElement != null)
for (int i = capacity; i < newCapacity; i++)
newElements[i] = initialElement;
return new BasicVector_UnsignedByte16(newElements);
}
// No change.
......
......@@ -275,7 +275,7 @@ public final class BasicVector_UnsignedByte32 extends AbstractVector
LispObject initialContents)
throws ConditionThrowable
{
if (initialContents != NIL)
if (initialContents != null)
{
LispObject[] newElements = new LispObject[newCapacity];
if (initialContents.listp())
......@@ -301,8 +301,9 @@ public final class BasicVector_UnsignedByte32 extends AbstractVector
LispObject[] newElements = new LispObject[newCapacity];
System.arraycopy(elements, 0, newElements, 0,
Math.min(capacity, newCapacity));
for (int i = capacity; i < newCapacity; i++)
newElements[i] = initialElement;
if (initialElement != null)
for (int i = capacity; i < newCapacity; i++)
newElements[i] = initialElement;
return new BasicVector_UnsignedByte32(newElements);
}
// No change.
......
......@@ -273,7 +273,7 @@ public final class BasicVector_UnsignedByte8 extends AbstractVector
LispObject initialContents)
throws ConditionThrowable
{
if (initialContents != NIL)
if (initialContents != null)
{
LispObject[] newElements = new LispObject[newCapacity];
if (initialContents.listp())
......@@ -299,8 +299,9 @@ public final class BasicVector_UnsignedByte8 extends AbstractVector
LispObject[] newElements = new LispObject[newCapacity];
System.arraycopy(elements, 0, newElements, 0,
Math.min(capacity, newCapacity));
for (int i = capacity; i < newCapacity; i++)
newElements[i] = initialElement;
if (initialElement != null)
for (int i = capacity; i < newCapacity; i++)
newElements[i] = initialElement;
return new BasicVector_UnsignedByte8(newElements);
}
// No change.
......
......@@ -242,13 +242,14 @@ public final class ComplexArray extends AbstractArray
LispObject initialContents)
throws ConditionThrowable {
if (isAdjustable()) {
if (initialContents != NIL)
if (initialContents != null)
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);
if (initialElement != null)
tempArray.fill(initialElement);
SimpleArray_T.copyArray(this, tempArray);
this.data = tempArray.data;
......@@ -257,11 +258,12 @@ public final class ComplexArray extends AbstractArray
}
return this;
} else {
if (initialContents != NIL)
if (initialContents != null)
return new ComplexArray(dims, elementType, initialContents);
else {
ComplexArray newArray = new ComplexArray(dims, elementType);
newArray.fill(initialElement);
if (initialElement != null)
newArray.fill(initialElement);
return newArray;
}
}
......
......@@ -239,13 +239,14 @@ public final class ComplexArray_UnsignedByte32 extends AbstractArray
LispObject initialContents)
throws ConditionThrowable {
if (isAdjustable()) {
if (initialContents != NIL)
if (initialContents != null)
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);
tempArray.fill(initialElement);
if (initialElement != null)
tempArray.fill(initialElement);
SimpleArray_UnsignedByte32.copyArray(this, tempArray);
this.data = tempArray.data;
......@@ -254,11 +255,12 @@ public final class ComplexArray_UnsignedByte32 extends AbstractArray
}
return this;
} else {
if (initialContents != NIL)
if (initialContents != null)
return new ComplexArray_UnsignedByte32(dims, initialContents);
else {
ComplexArray_UnsignedByte32 newArray = new ComplexArray_UnsignedByte32(dims);
newArray.fill(initialElement);
if (initialElement != null)
newArray.fill(initialElement);
return newArray;
}
}
......
......@@ -241,13 +241,14 @@ public final class ComplexArray_UnsignedByte8 extends AbstractArray
LispObject initialContents)
throws ConditionThrowable {
if (isAdjustable()) {
if (initialContents != NIL)
if (initialContents != null)
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);
tempArray.fill(initialElement);
if (initialElement != null)
tempArray.fill(initialElement);
SimpleArray_UnsignedByte8.copyArray(this, tempArray);
this.data = tempArray.data;
......@@ -256,11 +257,12 @@ public final class ComplexArray_UnsignedByte8 extends AbstractArray
}
return this;
} else {
if (initialContents != NIL)
if (initialContents != null)
return new ComplexArray_UnsignedByte8(dims, initialContents);
else {
ComplexArray_UnsignedByte8 newArray = new ComplexArray_UnsignedByte8(dims);
newArray.fill(initialElement);
if (initialElement != null)
newArray.fill(initialElement);
return newArray;
}
}
......
......@@ -345,7 +345,7 @@ public final class ComplexBitVector extends AbstractBitVector
int size = newCapacity >>> 6;
if ((newCapacity & LONG_MASK) != 0)
++size;
if (initialContents != NIL) {
if (initialContents != null) {
bits = new long[size];
capacity = newCapacity;
if (initialContents.listp()) {
......@@ -364,7 +364,7 @@ public final class ComplexBitVector extends AbstractBitVector
System.arraycopy(bits, 0, newBits, 0,
Math.min(bits.length, newBits.length));
bits = newBits;
if (newCapacity > capacity) {
if (newCapacity > capacity && initialElement != null) {
int n = Fixnum.getValue(initialElement);
if (n == 1)
for (int i = capacity; i < newCapacity; i++)
......
......@@ -602,7 +602,7 @@ public final class ComplexString extends AbstractString
LispObject initialContents)
throws ConditionThrowable
{
if (initialContents != NIL)
if (initialContents != null)
{
// "If INITIAL-CONTENTS is supplied, it is treated as for MAKE-
// ARRAY. In this case none of the original contents of array
......@@ -658,7 +658,7 @@ public final class ComplexString extends AbstractString
Math.min(capacity, newCapacity));
chars = newElements;
}
if (initialElement != NIL && capacity < newCapacity)
if (initialElement != null && capacity < newCapacity)
{
// Initialize new elements.
final char c = LispCharacter.getValue(initialElement);
......
......@@ -370,7 +370,7 @@ public final class ComplexVector extends AbstractVector
LispObject initialContents)
throws ConditionThrowable
{
if (initialContents != NIL) {
if (initialContents != null) {
// "If INITIAL-CONTENTS is supplied, it is treated as for MAKE-
// ARRAY. In this case none of the original contents of array
// appears in the resulting array."
......@@ -401,8 +401,9 @@ public final class ComplexVector extends AbstractVector
elements = newElements;
}
// Initialize new elements (if any).
for (int i = capacity; i < newCapacity; i++)
elements[i] = initialElement;
if (initialElement != null)
for (int i = capacity; i < newCapacity; i++)
elements[i] = initialElement;
}
capacity = newCapacity;
array = null;
......
......@@ -372,7 +372,7 @@ public final class ComplexVector_UnsignedByte32 extends AbstractVector
LispObject initialContents)
throws ConditionThrowable
{
if (initialContents != NIL) {
if (initialContents != null) {
// "If INITIAL-CONTENTS is supplied, it is treated as for MAKE-
// ARRAY. In this case none of the original contents of array
// appears in the resulting array."
......@@ -403,7 +403,7 @@ public final class ComplexVector_UnsignedByte32 extends AbstractVector
elements = newElements;
}
// Initialize new elements (if aapplicable).
if (initialElement != NIL) {
if (initialElement != null) {
for (int i = capacity; i < newCapacity; i++)
elements[i] = initialElement;
}
......
......@@ -384,7 +384,7 @@ public final class ComplexVector_UnsignedByte8 extends AbstractVector
LispObject initialContents)
throws ConditionThrowable
{
if (initialContents != NIL) {
if (initialContents != null) {
// "If INITIAL-CONTENTS is supplied, it is treated as for MAKE-
// ARRAY. In this case none of the original contents of array
// appears in the resulting array."
......@@ -415,7 +415,7 @@ public final class ComplexVector_UnsignedByte8 extends AbstractVector
elements = newElements;
}
// Initialize new elements (if aapplicable).
if (initialElement != NIL) {
if (initialElement != null) {
byte b = coerceLispObjectToJavaByte(initialElement);
for (int i = capacity; i < newCapacity; i++)
elements[i] = b;
......
......@@ -327,14 +327,15 @@ public final class SimpleArray_T extends AbstractArray
LispObject initialContents)
throws ConditionThrowable
{
if (initialContents != NIL)
if (initialContents != null)
return new SimpleArray_T(dimv, elementType, initialContents);
for (int i = 0; i < dimv.length; i++)
{
if (dimv[i] != this.dimv[i])
{
SimpleArray_T newArray = new SimpleArray_T(dimv, elementType);
newArray.fill(initialElement);
if (initialElement != null)
newArray.fill(initialElement);
copyArray(this, newArray);
return newArray;
}
......
......@@ -303,13 +303,14 @@ public final class SimpleArray_UnsignedByte16 extends AbstractArray
LispObject initialContents)
throws ConditionThrowable
{
if (initialContents != NIL)
if (initialContents != null)
return new SimpleArray_UnsignedByte16(dimv, initialContents);
for (int i = 0; i < dimv.length; i++) {
if (dimv[i] != this.dimv[i]) {
SimpleArray_UnsignedByte16 newArray =
new SimpleArray_UnsignedByte16(dimv);
newArray.fill(initialElement);
if (initialElement != null)
newArray.fill(initialElement);
copyArray(this, newArray);
return newArray;
}
......
......@@ -293,13 +293,14 @@ public final class SimpleArray_UnsignedByte32 extends AbstractArray
LispObject initialContents)
throws ConditionThrowable
{
if (initialContents != NIL)
if (initialContents != null)
return new SimpleArray_UnsignedByte32(dimv, initialContents);
for (int i = 0; i < dimv.length; i++) {
if (dimv[i] != this.dimv[i]) {
SimpleArray_UnsignedByte32 newArray =
new SimpleArray_UnsignedByte32(dimv);
newArray.fill(initialElement);
if (initialElement != null)
newArray.fill(initialElement);
copyArray(this, newArray);
return newArray;
}
......
......@@ -290,13 +290,14 @@ public final class SimpleArray_UnsignedByte8 extends AbstractArray
LispObject initialContents)
throws ConditionThrowable
{
if (initialContents != NIL)
if (initialContents != null)
return new SimpleArray_UnsignedByte8(dimv, initialContents);
for (int i = 0; i < dimv.length; i++) {
if (dimv[i] != this.dimv[i]) {
SimpleArray_UnsignedByte8 newArray =
new SimpleArray_UnsignedByte8(dimv);
newArray.fill(initialElement);
if (initialElement != null)
newArray.fill(initialElement);
copyArray(this, newArray);
return newArray;
}
......
......@@ -199,7 +199,7 @@ public final class SimpleBitVector extends AbstractBitVector
LispObject initialContents)
throws ConditionThrowable
{
if (initialContents != NIL) {
if (initialContents != null) {
SimpleBitVector v = new SimpleBitVector(newCapacity);
if (initialContents.listp()) {
LispObject list = initialContents;
......@@ -223,7 +223,7 @@ public final class SimpleBitVector extends AbstractBitVector
else
v.clearBit(i);
}
if (initialElement != NIL && capacity < newCapacity) {
if (initialElement != null && capacity < newCapacity) {
int n = Fixnum.getValue(initialElement);
if (n == 1)
for (int i = capacity; i < newCapacity; i++)
......
......@@ -465,7 +465,7 @@ public final class SimpleString extends AbstractString
LispObject initialContents)
throws ConditionThrowable
{
if (initialContents != NIL) {
if (initialContents != null) {
char[] newChars = new char[newCapacity];
if (initialContents.listp()) {
LispObject list = initialContents;
......@@ -483,7 +483,7 @@ public final class SimpleString extends AbstractString
if (capacity != newCapacity) {
char[] newChars = new char[newCapacity];
System.arraycopy(chars, 0, newChars, 0, Math.min(newCapacity, capacity));
if (initialElement != NIL && capacity < newCapacity) {
if (initialElement != null && capacity < newCapacity) {
final char c = LispCharacter.getValue(initialElement);
for (int i = capacity; i < newCapacity; i++)
newChars[i] = c;
......
......@@ -336,7 +336,7 @@ public final class SimpleVector extends AbstractVector
LispObject initialContents)
throws ConditionThrowable
{
if (initialContents != NIL)
if (initialContents != null)
{
LispObject[] newData = new LispObject[newCapacity];
if (initialContents.listp())
......@@ -362,8 +362,9 @@ public final class SimpleVector extends AbstractVector
LispObject[] newData = new LispObject[newCapacity];
System.arraycopy(data, 0, newData, 0,
Math.min(capacity, newCapacity));
for (int i = capacity; i < newCapacity; i++)
newData[i] = initialElement;
if (initialElement != null)
for (int i = capacity; i < newCapacity; i++)
newData[i] = initialElement;
return new SimpleVector(newData);
}
// No change.
......
......@@ -169,15 +169,15 @@ public final class ZeroRankArray extends AbstractArray
LispObject initialContents)
throws ConditionThrowable {
if (isAdjustable()) {
if (initialContents != NIL)
// initial element doesn't matter:
// we're not creating new elements
if (initialContents != null)
data = initialContents;
else
data = initialElement;
return this;
} else {
return new ZeroRankArray(elementType,
initialContents != NIL ? initialContents : initialElement,
false);
initialContents != null ? initialContents :
initialElement != null ? initialElement : data, false);
}
}
......
......@@ -51,14 +51,14 @@ public final class adjust_array extends Primitive
AbstractArray array = checkArray(args[0]);
LispObject dimensions = args[1];
LispObject elementType = args[2];
LispObject initialElement = args[3];
LispObject initialElementProvided = args[4];
LispObject initialContents = args[5];
LispObject initialContentsProvided = args[6];
boolean initialElementProvided = args[4] != NIL;
boolean initialContentsProvided = args[6] != NIL;
LispObject initialElement = initialElementProvided ? args[3] : null;
LispObject initialContents = initialContentsProvided ? args[5] : null;
LispObject fillPointer = args[7];
LispObject displacedTo = args[8];
LispObject displacedIndexOffset = args[9];
if (initialElementProvided != NIL && initialContentsProvided != NIL) {
if (initialElementProvided && initialContentsProvided) {
return error(new LispError("ADJUST-ARRAY: cannot specify both initial element and initial contents."));
}
if (elementType != array.getElementType() &&
......@@ -67,10 +67,9 @@ public final class adjust_array extends Primitive
return error(new LispError("ADJUST-ARRAY: incompatible element type."));
}
if (array.getRank() == 0) {
return array.adjustArray(new int[0], NIL,
(initialContentsProvided != NIL) ? initialContents : initialElement);
return array.adjustArray(new int[0], initialElement, initialContents);
}
if (initialElementProvided == NIL && array.getElementType() == T)
if (!initialElementProvided && array.getElementType() == T)
initialElement = Fixnum.ZERO;
if (array.getRank() == 1) {
final int newSize;
......
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