1 21 22 package org.armedbear.lisp; 23 24 public final class SimpleBitVector extends AbstractBitVector 28 { 29 public SimpleBitVector(int capacity) { 31 this.capacity = capacity; 32 int size = capacity >>> 6; 33 if ((capacity & LONG_MASK) != 0) 34 ++size; 35 bits = new long[size]; 36 } 37 38 public SimpleBitVector(String s) throws ConditionThrowable 39 { 40 this(s.length()); 41 for (int i = capacity; i-- > 0;) { 42 char c = s.charAt(i); 43 if (c == '0') 44 ; 45 else if (c == '1') 46 setBit(i); 47 else 48 Debug.assertTrue(false); 49 } 50 } 51 52 public LispObject typeOf() 53 { 54 return list2(Symbol.SIMPLE_BIT_VECTOR, new Fixnum(capacity)); 55 } 56 57 public LispClass classOf() 58 { 59 return BuiltInClass.SIMPLE_BIT_VECTOR; 60 } 61 62 public LispObject typep(LispObject type) throws ConditionThrowable 63 { 64 if (type == Symbol.SIMPLE_BIT_VECTOR) 65 return T; 66 if (type == Symbol.SIMPLE_ARRAY) 67 return T; 68 if (type == BuiltInClass.SIMPLE_BIT_VECTOR) 69 return T; 70 if (type == BuiltInClass.SIMPLE_ARRAY) 71 return T; 72 return super.typep(type); 73 } 74 75 public boolean hasFillPointer() 76 { 77 return false; 78 } 79 80 public boolean isAdjustable() 81 { 82 return false; 83 } 84 85 public boolean isSimpleVector() 86 { 87 return true; 88 } 89 90 public int length() 91 { 92 return capacity; 93 } 94 95 public LispObject elt(int index) throws ConditionThrowable 96 { 97 if (index < 0 || index >= length()) 98 badIndex(index, length()); 99 int offset = index >> 6; 100 return (bits[offset] & (1L << index)) != 0 ? Fixnum.ONE : Fixnum.ZERO; 101 } 102 103 public LispObject getRowMajor(int index) throws ConditionThrowable 104 { 105 if (index < 0 || index >= capacity) 106 badIndex(index, capacity); 107 int offset = index >> 6; 108 return (bits[offset] & (1L << index)) != 0 ? Fixnum.ONE : Fixnum.ZERO; 109 } 110 111 public void setRowMajor(int index, LispObject newValue) throws ConditionThrowable 112 { 113 if (index < 0 || index >= capacity) 114 badIndex(index, capacity); 115 final int offset = index >> 6; 116 try { 117 switch (((Fixnum)newValue).value) { 118 case 0: 119 bits[offset] &= ~(1L << index); 120 return; 121 case 1: 122 bits[offset] |= 1L << index; 123 return; 124 } 125 } 126 catch (ClassCastException e) { 127 } 129 signal(new TypeError(newValue, Symbol.BIT)); 130 } 131 132 protected int getBit(int index) 133 { 134 int offset = index >> 6; 135 return (bits[offset] & (1L << index)) != 0 ? 1 : 0; 136 } 137 138 protected void setBit(int index) 139 { 140 int offset = index >> 6; 141 bits[offset] |= 1L << index; 142 } 143 144 protected void clearBit(int index) 145 { 146 int offset = index >> 6; 147 bits[offset] &= ~(1L << index); 148 } 149 150 public void shrink(int n) throws ConditionThrowable 151 { 152 if (n < capacity) { 153 int size = n >>> 6; 154 if ((n & LONG_MASK) != 0) 155 ++size; 156 if (size < bits.length) { 157 long[] newbits = new long[size]; 158 System.arraycopy(bits, 0, newbits, 0, size); 159 bits = newbits; 160 } 161 capacity = n; 162 return; 163 } 164 if (n == capacity) 165 return; 166 signal(new LispError()); 167 } 168 169 public AbstractVector adjustVector(int newCapacity, 170 LispObject initialElement, 171 LispObject initialContents) 172 throws ConditionThrowable 173 { 174 if (initialContents != NIL) { 175 SimpleBitVector v = new SimpleBitVector(newCapacity); 176 if (initialContents.listp()) { 177 LispObject list = initialContents; 178 for (int i = 0; i < newCapacity; i++) { 179 v.setRowMajor(i, list.car()); 180 list = list.cdr(); 181 } 182 } else if (initialContents.vectorp()) { 183 for (int i = 0; i < newCapacity; i++) 184 v.setRowMajor(i, initialContents.elt(i)); 185 } else 186 signal(new TypeError(initialContents, Symbol.SEQUENCE)); 187 return v; 188 } 189 if (capacity != newCapacity) { 190 SimpleBitVector v = new SimpleBitVector(newCapacity); 191 final int limit = Math.min(capacity, newCapacity); 192 for (int i = limit; i-- > 0;) { 193 if (getBit(i) == 1) 194 v.setBit(i); 195 else 196 v.clearBit(i); 197 } 198 if (initialElement != NIL && capacity < newCapacity) { 199 int n = Fixnum.getValue(initialElement); 200 if (n == 1) 201 for (int i = capacity; i < newCapacity; i++) 202 v.setBit(i); 203 else 204 for (int i = capacity; i < newCapacity; i++) 205 v.clearBit(i); 206 } 207 return v; 208 } 209 return this; 211 } 212 213 public AbstractVector adjustVector(int newCapacity, 214 AbstractArray displacedTo, 215 int displacement) 216 throws ConditionThrowable 217 { 218 return new ComplexBitVector(newCapacity, displacedTo, displacement); 219 } 220 } 221 | Popular Tags |