1 16 17 package org.cojen.classfile; 18 19 24 final class BitList implements Cloneable { 25 private int[] mData; 27 28 31 public BitList(int capacity) { 32 mData = new int[(capacity + 31) >> 5]; 33 } 34 35 public boolean get(int index) { 36 return (mData[index >> 5] & (0x80000000 >>> index)) != 0; 37 } 38 39 42 public boolean set(int index) { 43 int i = index >> 5; 44 int v = mData[i]; 45 return v != (mData[i] = v | (0x80000000 >>> index)); 46 } 47 48 51 public boolean or(BitList list) { 52 boolean changes = ensureCapacity(list.capacity()); 53 for (int i=list.mData.length; --i >= 0; ) { 54 int v = mData[i]; 55 changes |= (v != (mData[i] = v | list.mData[i])); 56 } 57 return changes; 58 } 59 60 public boolean isAllClear() { 61 for (int i=mData.length; --i >= 0; ) { 62 if (mData[i] != 0) { 63 return false; 64 } 65 } 66 return true; 67 } 68 69 public boolean isAllSet() { 70 for (int i=mData.length; --i >= 0; ) { 71 if (mData[i] != 0xffffffff) { 72 return false; 73 } 74 } 75 return true; 76 } 77 78 82 public boolean intersects(BitList list) { 83 if (list != null) { 84 for (int i=Math.min(mData.length, list.mData.length); --i >= 0; ) { 85 int v1 = mData[i]; 86 int v2 = list.mData[i]; 87 if ((v1 | v2) != (v1 ^ v2)) { 88 return true; 89 } 90 } 91 } 92 return false; 93 } 94 95 public int hashCode() { 96 int hash = 0; 97 for (int i=mData.length; --i >= 0; ) { 98 hash = hash * 31 + mData[i]; 99 } 100 return hash; 101 } 102 103 public int capacity() { 104 return mData.length << 5; 105 } 106 107 public boolean equals(Object obj) { 108 if (obj instanceof BitList) { 109 return java.util.Arrays.equals(mData, ((BitList)obj).mData); 110 } 111 return false; 112 } 113 114 public BitList copy() { 115 return (BitList)clone(); 116 } 117 118 public Object clone() { 119 try { 120 return super.clone(); 121 } 122 catch (CloneNotSupportedException e) { 123 throw new InternalError (); 124 } 125 } 126 127 public String toString() { 128 StringBuffer buf = new StringBuffer (mData.length + 2); 129 buf.append('['); 130 for (int i=0; i<mData.length; i++) { 131 String binary = Integer.toBinaryString(mData[i]); 132 for (int j=binary.length(); j<32; j++) { 133 buf.append('0'); 134 } 135 buf.append(binary); 136 } 137 buf.append(']'); 138 return buf.toString(); 139 } 140 141 private boolean ensureCapacity(int capacity) { 142 int len = (capacity + 31) >> 5; 143 if (len > mData.length) { 144 int[] newData = new int[len]; 145 System.arraycopy(mData, 0, newData, 0, mData.length); 146 mData = newData; 147 return true; 148 } 149 return false; 150 } 151 } 152 | Popular Tags |