Changeset 5955
 Timestamp:
 02/25/04 03:08:42 (17 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/j/src/org/armedbear/lisp/ComplexBitVector.java
r5951 r5955 3 3 * 4 4 * Copyright (C) 20032004 Peter Graves 5 * $Id: ComplexBitVector.java,v 1. 1 20040225 01:50:40piso Exp $5 * $Id: ComplexBitVector.java,v 1.2 20040225 03:08:42 piso Exp $ 6 6 * 7 7 * This program is free software; you can redistribute it and/or … … 26 26 private int capacity; 27 27 private int fillPointer = 1; // 1 indicates no fill pointer. 28 private boolean isDisplaced; 29 30 // For nondisplaced bitvectors. 28 31 private long[] bits; 32 33 // For displaced bitvectors. 34 private AbstractArray array; 35 private int displacement; 29 36 30 37 public ComplexBitVector(int capacity) throws ConditionThrowable … … 35 42 ++size; 36 43 bits = new long[size]; 44 } 45 46 public ComplexBitVector(int capacity, AbstractArray array, int displacement) 47 { 48 this.capacity = capacity; 49 this.array = array; 50 this.displacement = displacement; 51 isDisplaced = true; 37 52 } 38 53 … … 80 95 } 81 96 97 public LispObject arrayDisplacement() 98 { 99 if (array != null) 100 return LispThread.currentThread().setValues(array, new Fixnum(displacement)); 101 return super.arrayDisplacement(); 102 } 103 82 104 public int capacity() 83 105 { … … 94 116 if (index < 0  index >= length()) 95 117 badIndex(index, length()); 96 int offset = index >> 6; 97 return (bits[offset] & (1L << index)) != 0 ? Fixnum.ONE : Fixnum.ZERO; 118 return get(index); 98 119 } 99 120 100 121 public LispObject get(int index) throws ConditionThrowable 101 122 { 102 if (index >= capacity) 103 badIndex(index, capacity); 104 int offset = index >> 6; 105 return (bits[offset] & (1L << index)) != 0 ? Fixnum.ONE : Fixnum.ZERO; 106 } 107 108 protected int getBit(int index) 109 { 110 int offset = index >> 6; 111 return (bits[offset] & (1L << index)) != 0 ? 1 : 0; 123 if (bits != null) { 124 if (index >= capacity) 125 badIndex(index, capacity); 126 int offset = index >> 6; 127 return (bits[offset] & (1L << index)) != 0 ? Fixnum.ONE : Fixnum.ZERO; 128 } else 129 return array.getRowMajor(index + displacement); 130 } 131 132 protected int getBit(int index) throws ConditionThrowable 133 { 134 if (bits != null) { 135 int offset = index >> 6; 136 return (bits[offset] & (1L << index)) != 0 ? 1 : 0; 137 } else 138 return Fixnum.getValue(array.getRowMajor(index + displacement)); 112 139 } 113 140 … … 132 159 } 133 160 134 protected void setBit(int index) 135 { 136 int offset = index >> 6; 137 bits[offset] = 1L << index; 138 } 139 140 protected void clearBit(int index) 141 { 142 int offset = index >> 6; 143 bits[offset] &= ~(1L << index); 161 protected void setBit(int index) throws ConditionThrowable 162 { 163 if (bits != null) { 164 int offset = index >> 6; 165 bits[offset] = 1L << index; 166 } else 167 array.setRowMajor(index + displacement, Fixnum.ONE); 168 } 169 170 protected void clearBit(int index) throws ConditionThrowable 171 { 172 if (bits != null) { 173 int offset = index >> 6; 174 bits[offset] &= ~(1L << index); 175 } else 176 array.setRowMajor(index + displacement, Fixnum.ZERO); 144 177 } 145 178 146 179 public void fill(LispObject obj) throws ConditionThrowable 147 180 { 148 try { 149 int n = Fixnum.getValue(obj); 150 if (n == 1) { 151 for (int i = bits.length; i > 0;) 152 bits[i] = 1L; 181 if (bits != null) { 182 try { 183 int n = Fixnum.getValue(obj); 184 if (n == 1) { 185 for (int i = bits.length; i > 0;) 186 bits[i] = 1L; 187 return; 188 } 189 if (n == 0) { 190 for (int i = bits.length; i > 0;) 191 bits[i] = 0; 192 return; 193 } 194 // None of the above... 195 } 196 catch (ConditionThrowable t) {} 197 signal(new TypeError(obj, Symbol.BIT)); 198 } else { 199 try { 200 int n = Fixnum.getValue(obj); 201 if (n == 1) { 202 for (int i = capacity; i > 0;) 203 setBit(i); 204 return; 205 } 206 if (n == 0) { 207 for (int i = capacity; i > 0;) 208 clearBit(i); 209 return; 210 } 211 // None of the above... 212 } 213 catch (ConditionThrowable t) {} 214 signal(new TypeError(obj, Symbol.BIT)); 215 } 216 } 217 218 public void shrink(int n) throws ConditionThrowable 219 { 220 if (bits != null) { 221 if (n < capacity) { 222 int size = n >>> 6; 223 if ((n & LONG_MASK) != 0) 224 ++size; 225 if (size < bits.length) { 226 long[] newbits = new long[size]; 227 System.arraycopy(bits, 0, newbits, 0, size); 228 bits = newbits; 229 } 230 capacity = n; 153 231 return; 154 232 } 155 if (n == 0) { 156 for (int i = bits.length; i > 0;) 157 bits[i] = 0; 233 if (n == capacity) 158 234 return; 159 } 160 // None of the above... 161 } 162 catch (ConditionThrowable t) {} 163 signal(new TypeError(obj, "bit")); 164 } 165 166 public void shrink(int n) throws ConditionThrowable 167 { 168 if (n < capacity) { 169 int size = n >>> 6; 170 if ((n & LONG_MASK) != 0) 171 ++size; 172 if (size < bits.length) { 173 long[] newbits = new long[size]; 174 System.arraycopy(bits, 0, newbits, 0, size); 175 bits = newbits; 176 } 177 capacity = n; 178 return; 179 } 180 if (n == capacity) 181 return; 235 } 182 236 signal(new LispError()); 183 237 } … … 222 276 } 223 277 224 private final void ensureCapacity(int minCapacity) 225 { 226 if (capacity < minCapacity) { 227 int size = minCapacity >>> 6; 228 if ((minCapacity & LONG_MASK) != 0) 229 ++size; 230 long[] newBits = new long[size]; 231 System.arraycopy(bits, 0, newBits, 0, bits.length); 232 bits = newBits; 233 capacity = minCapacity; 278 private final void ensureCapacity(int minCapacity) throws ConditionThrowable 279 { 280 if (bits != null) { 281 if (capacity < minCapacity) { 282 int size = minCapacity >>> 6; 283 if ((minCapacity & LONG_MASK) != 0) 284 ++size; 285 long[] newBits = new long[size]; 286 System.arraycopy(bits, 0, newBits, 0, bits.length); 287 bits = newBits; 288 capacity = minCapacity; 289 } 290 } else { 291 Debug.assertTrue(array != null); 292 if (array.getTotalSize()  displacement < minCapacity) { 293 // Copy array. 294 int size = minCapacity >>> 6; 295 if ((minCapacity & LONG_MASK) != 0) 296 ++size; 297 bits = new long[size]; 298 for (int i = 0; i < capacity; i++) 299 bits[i] = Fixnum.getValue(array.getRowMajor(displacement + i)); 300 capacity = minCapacity; 301 array = null; 302 displacement = 0; 303 isDisplaced = false; 304 } 234 305 } 235 306 }
Note: See TracChangeset
for help on using the changeset viewer.