1 21 22 package org.armedbear.lisp; 23 24 public final class adjust_array extends Primitive 28 { 29 public adjust_array() 30 { 31 super("%adjust-array", PACKAGE_SYS, false); 32 } 33 34 public LispObject execute(LispObject[] args) throws ConditionThrowable 35 { 36 if (args.length != 10) 37 return signal(new WrongNumberOfArgumentsException(this)); 38 AbstractArray array = checkArray(args[0]); 39 LispObject dimensions = args[1]; 40 LispObject elementType = args[2]; 41 LispObject initialElement = args[3]; 42 LispObject initialElementProvided = args[4]; 43 LispObject initialContents = args[5]; 44 LispObject initialContentsProvided = args[6]; 45 LispObject fillPointer = args[7]; 46 LispObject displacedTo = args[8]; 47 LispObject displacedIndexOffset = args[9]; 48 if (initialElementProvided != NIL && initialContents != NIL) { 49 return signal(new LispError("ADJUST-ARRAY: cannot specify both initial element and initial contents.")); 50 } 51 if (elementType != array.getElementType() && 52 getUpgradedArrayElementType(elementType) != array.getElementType()) 53 { 54 return signal(new LispError("ADJUST-ARRAY: incompatible element type.")); 55 } 56 if (array.getRank() == 0) { 57 if (initialContentsProvided != NIL) 58 array.setRowMajor(0, initialContents); 59 return array; 60 } 61 if (array.getRank() == 1) { 62 final int newSize; 63 if (dimensions instanceof Cons && dimensions.length() == 1) 64 newSize = Fixnum.getValue(dimensions.car()); 65 else 66 newSize = Fixnum.getValue(dimensions); 67 if (array instanceof AbstractVector) { 68 AbstractVector v = (AbstractVector) array; 69 AbstractVector v2; 70 if (displacedTo != NIL) { 71 final int displacement; 72 if (displacedIndexOffset == NIL) 73 displacement = 0; 74 else 75 displacement = Fixnum.getValue(displacedIndexOffset); 76 v2 = v.adjustVector(newSize, 77 checkArray(displacedTo), 78 displacement); 79 } else { 80 v2 = v.adjustVector(newSize, 81 initialElement, 82 initialContents); 83 } 84 if (fillPointer != NIL) 85 v2.setFillPointer(fillPointer); 86 return v2; 87 } 88 } 89 final int rank = dimensions.listp() ? dimensions.length() : 1; 91 int[] dimv = new int[rank]; 92 if (dimensions.listp()) { 93 for (int i = 0; i < rank; i++) { 94 LispObject dim = dimensions.car(); 95 dimv[i] = Fixnum.getValue(dim); 96 dimensions = dimensions.cdr(); 97 } 98 } else 99 dimv[0] = Fixnum.getValue(dimensions); 100 if (array instanceof SimpleArray) { 101 SimpleArray a = (SimpleArray) array; 102 if (displacedTo != NIL) { 103 final int displacement; 104 if (displacedIndexOffset == NIL) 105 displacement = 0; 106 else 107 displacement = Fixnum.getValue(displacedIndexOffset); 108 return a.adjustArray(dimv, 109 checkArray(displacedTo), 110 displacement); 111 } else { 112 return a.adjustArray(dimv, 113 initialElement, 114 initialContents); 115 } 116 } 117 return signal(new LispError("ADJUST-ARRAY: unsupported case.")); 118 } 119 120 private static final Primitive _ADJUST_ARRAY = new adjust_array(); 121 } 122 | Popular Tags |