1 21 22 package org.armedbear.lisp; 23 24 public abstract class AbstractBitVector extends AbstractVector 25 { 26 protected static final int LONG_MASK = 0x3f; 27 28 protected int capacity; 29 30 protected long[] bits; 32 33 public LispObject typep(LispObject type) throws ConditionThrowable 34 { 35 if (type == Symbol.BIT_VECTOR) 36 return T; 37 if (type == BuiltInClass.BIT_VECTOR) 38 return T; 39 return super.typep(type); 40 } 41 42 public LispClass classOf() 43 { 44 return BuiltInClass.BIT_VECTOR; 45 } 46 47 public final LispObject BIT_VECTOR_P() 48 { 49 return T; 50 } 51 52 public final int capacity() 53 { 54 return capacity; 55 } 56 57 public final LispObject getElementType() 58 { 59 return Symbol.BIT; 60 } 61 62 public boolean equal(LispObject obj) throws ConditionThrowable 63 { 64 if (this == obj) 65 return true; 66 if (obj instanceof AbstractBitVector) { 67 AbstractBitVector v = (AbstractBitVector) obj; 68 if (length() != v.length()) 69 return false; 70 for (int i = length(); i-- > 0;) { 71 if (getBit(i) != v.getBit(i)) 72 return false; 73 } 74 return true; 75 } 76 return false; 77 } 78 79 public boolean equalp(LispObject obj) throws ConditionThrowable 80 { 81 if (this == obj) 82 return true; 83 if (obj instanceof AbstractBitVector) { 84 AbstractBitVector v = (AbstractBitVector) obj; 85 if (length() != v.length()) 86 return false; 87 for (int i = length(); i-- > 0;) { 88 if (getBit(i) != v.getBit(i)) 89 return false; 90 } 91 return true; 92 } 93 if (obj instanceof AbstractVector) 94 return ((AbstractVector)obj).equalp(this); 95 return false; 96 } 97 98 public void fill(LispObject obj) throws ConditionThrowable 99 { 100 try { 101 switch (((Fixnum)obj).value) { 102 case 0: 103 if (bits != null) { 104 for (int i = bits.length; i-- > 0;) 105 bits[i] = 0; 106 } else { 107 for (int i = capacity; i-- > 0;) 108 clearBit(i); 109 } 110 return; 111 case 1: 112 if (bits != null) { 113 for (int i = bits.length; i-- > 0;) 114 bits[i] = -1L; 115 } else { 116 for (int i = capacity; i-- > 0;) 117 setBit(i); 118 } 119 return; 120 } 121 } 122 catch (ClassCastException e) { 123 } 125 signal(new TypeError(obj, Symbol.BIT)); 126 } 127 128 public LispObject subseq(int start, int end) throws ConditionThrowable 129 { 130 SimpleBitVector v = new SimpleBitVector(end - start); 131 int i = start, j = 0; 132 try { 133 while (i < end) { 134 if (getBit(i++) == 0) 135 v.clearBit(j++); 136 else 137 v.setBit(j++); 138 } 139 return v; 140 } 141 catch (ArrayIndexOutOfBoundsException e) { 142 return signal(new TypeError("Array index out of bounds: " + i + ".")); 143 } 144 } 145 146 public int hashCode() 147 { 148 int hashCode = 1; 149 try { 150 final int limit = Math.min(length(), 64); 152 for (int i = 0; i < limit; i++) 153 hashCode = hashCode * 31 + getBit(i); 154 } 155 catch (ConditionThrowable t) { 156 Debug.trace(t); 158 } 159 return hashCode; 160 } 161 162 public String writeToString() throws ConditionThrowable 163 { 164 final int limit = length(); 165 StringBuffer sb = new StringBuffer (limit + 2); 166 sb.append("#*"); 167 for (int i = 0; i < limit; i++) 168 sb.append(getBit(i) == 1 ? '1' : '0'); 169 return sb.toString(); 170 } 171 172 public LispObject AREF(LispObject index) throws ConditionThrowable 174 { 175 try { 176 return getRowMajor(((Fixnum)index).value); 177 } 178 catch (ClassCastException e) { 179 return signal(new TypeError(index, Symbol.FIXNUM)); 180 } 181 } 182 183 public LispObject reverse() throws ConditionThrowable 184 { 185 int length = length(); 186 SimpleBitVector result = new SimpleBitVector(length); 187 int i, j; 188 for (i = 0, j = length - 1; i < length; i++, j--) { 189 if (getBit(j) == 1) 190 result.setBit(i); 191 else 192 result.clearBit(i); 193 } 194 return result; 195 } 196 197 protected abstract int getBit(int index) throws ConditionThrowable; 198 199 protected abstract void setBit(int index) throws ConditionThrowable; 200 201 protected abstract void clearBit(int index) throws ConditionThrowable; 202 } 203 | Popular Tags |