1 17 18 19 package SOFA.SOFAnode.Util.DFSRChecker.state; 20 21 25 public class BitList implements Comparable { 26 30 public BitList(int size) { 31 buffer = new int[1]; 32 max = size; 33 length = 0; 34 } 35 36 41 public void set(int position, boolean bit) { 42 int idx = position >> 5; 43 int off = position & 0x1F; 44 45 while (idx >= buffer.length) { 46 int size = buffer.length * 2; 48 49 53 int[] newbuffer = new int[size]; 54 55 for (int i = 0; i < buffer.length; ++i) 56 newbuffer[i] = buffer[i]; 57 58 buffer = newbuffer; 59 } 60 61 if (bit) 62 buffer[idx] = buffer[idx] | (1 << (31 - off)); 63 else 64 buffer[idx] = buffer[idx] & ~(1 << (31 - off)); 65 66 if (position >= length) 67 length = position + 1; 68 } 69 70 75 public boolean get(int position) { 76 int idx = position >> 5; 77 int off = position & 0x1F; 78 79 return ((buffer[idx] & (1 << off)) == 0) ? true : false; 80 } 81 82 87 public int compareTo(Object o) { 88 if (!(o instanceof BitList)) 89 return -1; 90 91 BitList other = (BitList)o; 92 93 int min = this.buffer.length < other.buffer.length ? this.buffer.length : other.buffer.length; 94 95 for (int i = 0; i < min; ++i) { 96 if (this.buffer[i] < other.buffer[i]) 97 return -1; 98 if (this.buffer[i] > other.buffer[i]) 99 return 1; 100 } 101 102 if (this.length < other.length) 103 return -1; 104 105 if (this.length > other.length) 106 return 1; 107 108 return 0; 109 } 110 111 112 115 public boolean equals(Object o) { 116 if (!(o instanceof BitList)) 117 return false; 118 119 BitList other = (BitList)o; 120 121 if (this.length != other.length) 122 return false; 123 124 int min = this.buffer.length; 125 126 for (int i = 0; i < min; ++i) 127 if (this.buffer[i] != other.buffer[i]) 128 return false; 129 130 return true; 131 } 132 133 136 public int hashCode() { 137 int result = 0; 138 139 for (int i = 0; i < buffer.length; ++i) 140 result = result ^ (buffer[i] << i); 141 142 return result; 143 } 144 145 149 public void concat(BitList second) { 150 int thisidx = this.length >> 5; 151 int thisoff = this.length & 0x1F; 152 int secondleft = second.length; 153 154 155 int needed = ((this.length + second.length) >> 5) + ((((this.length + second.length) & 0x1F) != 0) ? 1 : 0); 156 157 158 while (needed >= buffer.length) { 159 int size = (buffer.length * 2) > needed ? (buffer.length * 2) : needed + 1; 161 165 int[] newbuffer = new int[size]; 166 167 for (int i = 0; i < buffer.length; ++i) 168 newbuffer[i] = buffer[i]; 169 170 buffer = newbuffer; 171 } 172 173 174 int i = 0; 175 176 while (secondleft > 0) { 177 this.buffer[thisidx + i] |= (second.buffer[i] >>> thisoff); 178 if ((secondleft - 32 + thisoff > 0) && (thisoff > 0)) 179 this.buffer[thisidx + i + 1] |= (second.buffer[i] << (32 - thisoff)); 180 181 ++i; 182 secondleft -= 32; 183 } 184 185 this.length += second.length; 186 } 187 188 191 public String toString() { 192 String s = new String (); 193 194 for (int i = 0; i < this.length; ++i) 195 s += ((buffer[i >> 5] & (1 << (31 - i))) != 0) ? "1" : "0"; 196 197 return s; 198 } 199 200 201 205 public int[] getBuffer() { 206 return buffer; 207 } 208 209 213 private int[] buffer; 214 215 218 private int max; 219 220 223 private int length; 224 225 } | Popular Tags |