1. /*
  2. * @(#)BitSet.java 1.40 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.util;
  8. import java.io.*;
  9. /**
  10. * This class implements a vector of bits that grows as needed. Each
  11. * component of the bit set has a <code>boolean</code> value. The
  12. * bits of a <code>BitSet</code> are indexed by nonnegative integers.
  13. * Individual indexed bits can be examined, set, or cleared. One
  14. * <code>BitSet</code> may be used to modify the contents of another
  15. * <code>BitSet</code> through logical AND, logical inclusive OR, and
  16. * logical exclusive OR operations.
  17. * <p>
  18. * By default, all bits in the set initially have the value
  19. * <code>false</code>.
  20. * <p>
  21. * Every bit set has a current size, which is the number of bits
  22. * of space currently in use by the bit set. Note that the size is
  23. * related to the implementation of a bit set, so it may change with
  24. * implementation. The length of a bit set relates to logical length
  25. * of a bit set and is defined independently of implementation.
  26. *
  27. * @author Arthur van Hoff
  28. * @author Michael McCloskey
  29. * @version 1.40, 02/10/07
  30. * @since JDK1.0
  31. */
  32. public class BitSet implements Cloneable, java.io.Serializable {
  33. /*
  34. * BitSets are packed into arrays of "units." Currently a unit is a long,
  35. * which consists of 64 bits, requiring 6 address bits. The choice of unit
  36. * is determined purely by performance concerns.
  37. */
  38. private final static int ADDRESS_BITS_PER_UNIT = 6;
  39. private final static int BITS_PER_UNIT = 1 << ADDRESS_BITS_PER_UNIT;
  40. private final static int BIT_INDEX_MASK = BITS_PER_UNIT - 1;
  41. /**
  42. * The bits in this BitSet. The ith bit is stored in bits[i/64] at
  43. * bit position i % 64 (where bit position 0 refers to the least
  44. * significant bit and 63 refers to the most significant bit).
  45. * INVARIANT: The words in bits[] above unitInUse-1 are zero.
  46. *
  47. * @serial
  48. */
  49. private long bits[]; // this should be called unit[]
  50. /**
  51. * The number of units in the logical size of this BitSet.
  52. * INVARIANT: unitsInUse is nonnegative.
  53. * INVARIANT: bits[unitsInUse-1] is nonzero unless unitsInUse is zero.
  54. */
  55. private transient int unitsInUse = 0;
  56. /* use serialVersionUID from JDK 1.0.2 for interoperability */
  57. private static final long serialVersionUID = 7997698588986878753L;
  58. /**
  59. * Given a bit index return unit index containing it.
  60. */
  61. private static int unitIndex(int bitIndex) {
  62. return bitIndex >> ADDRESS_BITS_PER_UNIT;
  63. }
  64. /**
  65. * Given a bit index, return a unit that masks that bit in its unit.
  66. */
  67. private static long bit(int bitIndex) {
  68. return 1L << (bitIndex & BIT_INDEX_MASK);
  69. }
  70. /**
  71. * Set the field unitsInUse with the logical size in units of the bit
  72. * set. WARNING:This function assumes that the number of units actually
  73. * in use is less than or equal to the current value of unitsInUse!
  74. */
  75. private void recalculateUnitsInUse() {
  76. /* Traverse the bitset until a used unit is found */
  77. int i;
  78. for (i = unitsInUse-1; i >= 0; i--)
  79. if(bits[i] != 0)
  80. break; //this unit is in use!
  81. unitsInUse = i+1; //the new logical size
  82. }
  83. /**
  84. * Creates a new bit set. All bits are initially <code>false</code>.
  85. */
  86. public BitSet() {
  87. this(BITS_PER_UNIT);
  88. }
  89. /**
  90. * Creates a bit set whose initial size is large enough to explicitly
  91. * represent bits with indices in the range <code>0</code> through
  92. * <code>nbits-1</code>. All bits are initially <code>false</code>.
  93. *
  94. * @param nbits the initial size of the bit set.
  95. * @exception NegativeArraySizeException if the specified initial size
  96. * is negative.
  97. */
  98. public BitSet(int nbits) {
  99. /* nbits can't be negative; size 0 is OK */
  100. if (nbits < 0)
  101. throw new NegativeArraySizeException(Integer.toString(nbits));
  102. bits = new long[(unitIndex(nbits-1) + 1)];
  103. }
  104. /**
  105. * Ensures that the BitSet can hold enough units.
  106. * @param unitsRequired the minimum acceptable number of units.
  107. */
  108. private void ensureCapacity(int unitsRequired) {
  109. if (bits.length < unitsRequired) {
  110. /* Allocate larger of doubled size or required size */
  111. int request = Math.max(2 * bits.length, unitsRequired);
  112. long newBits[] = new long[request];
  113. System.arraycopy(bits, 0, newBits, 0, unitsInUse);
  114. bits = newBits;
  115. }
  116. }
  117. /**
  118. * Returns the "logical size" of this <code>BitSet</code>: the index of
  119. * the highest set bit in the <code>BitSet</code> plus one. Returns zero
  120. * if the <code>BitSet</code> contains no set bits.
  121. *
  122. * @return the logical size of this <code>BitSet</code>.
  123. * @since JDK1.2
  124. */
  125. public int length() {
  126. if (unitsInUse == 0)
  127. return 0;
  128. int highestBit = (unitsInUse - 1) * 64;
  129. long highestUnit = bits[unitsInUse - 1];
  130. do {
  131. highestUnit = highestUnit >>> 1;
  132. highestBit++;
  133. } while(highestUnit > 0);
  134. return highestBit;
  135. }
  136. /**
  137. * Sets the bit specified by the index to <code>true</code>.
  138. *
  139. * @param bitIndex a bit index.
  140. * @exception IndexOutOfBoundsException if the specified index is negative.
  141. * @since JDK1.0
  142. */
  143. public void set(int bitIndex) {
  144. if (bitIndex < 0)
  145. throw new IndexOutOfBoundsException(Integer.toString(bitIndex));
  146. int unitIndex = unitIndex(bitIndex);
  147. int unitsRequired = unitIndex+1;
  148. if (unitsInUse < unitsRequired) {
  149. ensureCapacity(unitsRequired);
  150. bits[unitIndex] |= bit(bitIndex);
  151. unitsInUse = unitsRequired;
  152. } else {
  153. bits[unitIndex] |= bit(bitIndex);
  154. }
  155. }
  156. /**
  157. * Sets the bit specified by the index to <code>false</code>.
  158. *
  159. * @param bitIndex the index of the bit to be cleared.
  160. * @exception IndexOutOfBoundsException if the specified index is negative.
  161. * @since JDK1.0
  162. */
  163. public void clear(int bitIndex) {
  164. if (bitIndex < 0)
  165. throw new IndexOutOfBoundsException(Integer.toString(bitIndex));
  166. int unitIndex = unitIndex(bitIndex);
  167. if (unitIndex >= unitsInUse)
  168. return;
  169. bits[unitIndex] &= ~bit(bitIndex);
  170. if (bits[unitsInUse-1] == 0)
  171. recalculateUnitsInUse();
  172. }
  173. /**
  174. * Clears all of the bits in this <code>BitSet</code> whose corresponding
  175. * bit is set in the specified <code>BitSet</code>.
  176. *
  177. * @param s the <code>BitSet</code> with which to mask this
  178. * <code>BitSet</code>.
  179. * @since JDK1.2
  180. */
  181. public void andNot(BitSet set) {
  182. int unitsInCommon = Math.min(unitsInUse, set.unitsInUse);
  183. // perform logical (a & !b) on bits in common
  184. for (int i=0; i<unitsInCommon; i++) {
  185. bits[i] &= ~set.bits[i];
  186. }
  187. recalculateUnitsInUse();
  188. }
  189. /**
  190. * Returns the value of the bit with the specified index. The value
  191. * is <code>true</code> if the bit with the index <code>bitIndex</code>
  192. * is currently set in this <code>BitSet</code> otherwise, the result
  193. * is <code>false</code>.
  194. *
  195. * @param bitIndex the bit index.
  196. * @return the value of the bit with the specified index.
  197. * @exception IndexOutOfBoundsException if the specified index is negative.
  198. */
  199. public boolean get(int bitIndex) {
  200. if (bitIndex < 0)
  201. throw new IndexOutOfBoundsException(Integer.toString(bitIndex));
  202. boolean result = false;
  203. int unitIndex = unitIndex(bitIndex);
  204. if (unitIndex < unitsInUse)
  205. result = ((bits[unitIndex] & bit(bitIndex)) != 0);
  206. return result;
  207. }
  208. /**
  209. * Performs a logical <b>AND</b> of this target bit set with the
  210. * argument bit set. This bit set is modified so that each bit in it
  211. * has the value <code>true</code> if and only if it both initially
  212. * had the value <code>true</code> and the corresponding bit in the
  213. * bit set argument also had the value <code>true</code>.
  214. *
  215. * @param set a bit set.
  216. */
  217. public void and(BitSet set) {
  218. if (this == set)
  219. return;
  220. // perform logical AND on bits in common
  221. int oldUnitsInUse = unitsInUse;
  222. unitsInUse = Math.min(unitsInUse, set.unitsInUse);
  223. int i;
  224. for(i=0; i<unitsInUse; i++)
  225. bits[i] &= set.bits[i];
  226. // clear out units no longer used
  227. for( ; i < oldUnitsInUse; i++)
  228. bits[i] = 0;
  229. // Recalculate units in use if necessary
  230. if (unitsInUse > 0 && bits[unitsInUse - 1] == 0)
  231. recalculateUnitsInUse();
  232. }
  233. /**
  234. * Performs a logical <b>OR</b> of this bit set with the bit set
  235. * argument. This bit set is modified so that a bit in it has the
  236. * value <code>true</code> if and only if it either already had the
  237. * value <code>true</code> or the corresponding bit in the bit set
  238. * argument has the value <code>true</code>.
  239. *
  240. * @param set a bit set.
  241. */
  242. public void or(BitSet set) {
  243. if (this == set)
  244. return;
  245. ensureCapacity(set.unitsInUse);
  246. // perform logical OR on bits in common
  247. int unitsInCommon = Math.min(unitsInUse, set.unitsInUse);
  248. int i;
  249. for(i=0; i<unitsInCommon; i++)
  250. bits[i] |= set.bits[i];
  251. // copy any remaining bits
  252. for(; i<set.unitsInUse; i++)
  253. bits[i] = set.bits[i];
  254. if (unitsInUse < set.unitsInUse)
  255. unitsInUse = set.unitsInUse;
  256. }
  257. /**
  258. * Performs a logical <b>XOR</b> of this bit set with the bit set
  259. * argument. This bit set is modified so that a bit in it has the
  260. * value <code>true</code> if and only if one of the following
  261. * statements holds:
  262. * <ul>
  263. * <li>The bit initially has the value <code>true</code>, and the
  264. * corresponding bit in the argument has the value <code>false</code>.
  265. * <li>The bit initially has the value <code>false</code>, and the
  266. * corresponding bit in the argument has the value <code>true</code>.
  267. * </ul>
  268. *
  269. * @param set a bit set.
  270. */
  271. public void xor(BitSet set) {
  272. int unitsInCommon;
  273. if (unitsInUse >= set.unitsInUse) {
  274. unitsInCommon = set.unitsInUse;
  275. } else {
  276. unitsInCommon = unitsInUse;
  277. int newUnitsInUse = set.unitsInUse;
  278. ensureCapacity(newUnitsInUse);
  279. unitsInUse = newUnitsInUse;
  280. }
  281. // perform logical XOR on bits in common
  282. int i;
  283. for (i=0; i<unitsInCommon; i++)
  284. bits[i] ^= set.bits[i];
  285. // copy any remaining bits
  286. for ( ; i<set.unitsInUse; i++)
  287. bits[i] = set.bits[i];
  288. recalculateUnitsInUse();
  289. }
  290. /**
  291. * Returns a hash code value for this bit set. The has code
  292. * depends only on which bits have been set within this
  293. * <code>BitSet</code>. The algorithm used to compute it may
  294. * be described as follows.<p>
  295. * Suppose the bits in the <code>BitSet</code> were to be stored
  296. * in an array of <code>long</code> integers called, say,
  297. * <code>bits</code>, in such a manner that bit <code>k</code> is
  298. * set in the <code>BitSet</code> (for nonnegative values of
  299. * <code>k</code>) if and only if the expression
  300. * <pre>((k>>6) < bits.length) && ((bits[k>>6] & (1L << (bit & 0x3F))) != 0)</pre>
  301. * is true. Then the following definition of the <code>hashCode</code>
  302. * method would be a correct implementation of the actual algorithm:
  303. * <pre>
  304. * public synchronized int hashCode() {
  305. * long h = 1234;
  306. * for (int i = bits.length; --i >= 0; ) {
  307. * h ^= bits[i] * (i + 1);
  308. * }
  309. * return (int)((h >> 32) ^ h);
  310. * }</pre>
  311. * Note that the hash code values change if the set of bits is altered.
  312. * <p>Overrides the <code>hashCode</code> method of <code>Object</code>.
  313. *
  314. * @return a hash code value for this bit set.
  315. */
  316. public int hashCode() {
  317. long h = 1234;
  318. for (int i = bits.length; --i >= 0; )
  319. h ^= bits[i] * (i + 1);
  320. return (int)((h >> 32) ^ h);
  321. }
  322. /**
  323. * Returns the number of bits of space actually in use by this
  324. * <code>BitSet</code> to represent bit values.
  325. * The maximum element in the set is the size - 1st element.
  326. *
  327. * @return the number of bits currently in this bit set.
  328. */
  329. public int size() {
  330. return bits.length << ADDRESS_BITS_PER_UNIT;
  331. }
  332. /**
  333. * Compares this object against the specified object.
  334. * The result is <code>true</code> if and only if the argument is
  335. * not <code>null</code> and is a <code>Bitset</code> object that has
  336. * exactly the same set of bits set to <code>true</code> as this bit
  337. * set. That is, for every nonnegative <code>int</code> index <code>k</code>,
  338. * <pre>((BitSet)obj).get(k) == this.get(k)</pre>
  339. * must be true. The current sizes of the two bit sets are not compared.
  340. * <p>Overrides the <code>equals</code> method of <code>Object</code>.
  341. *
  342. * @param obj the object to compare with.
  343. * @return <code>true</code> if the objects are the same;
  344. * <code>false</code> otherwise.
  345. * @see java.util.BitSet#size()
  346. */
  347. public boolean equals(Object obj) {
  348. if (obj == null || !(obj instanceof BitSet))
  349. return false;
  350. if (this == obj)
  351. return true;
  352. BitSet set = (BitSet) obj;
  353. int minUnitsInUse = Math.min(unitsInUse, set.unitsInUse);
  354. // Check units in use by both BitSets
  355. for (int i = 0; i < minUnitsInUse; i++)
  356. if (bits[i] != set.bits[i])
  357. return false;
  358. // Check any units in use by only one BitSet (must be 0 in other)
  359. if (unitsInUse > minUnitsInUse) {
  360. for (int i = minUnitsInUse; i<unitsInUse; i++)
  361. if (bits[i] != 0)
  362. return false;
  363. } else {
  364. for (int i = minUnitsInUse; i<set.unitsInUse; i++)
  365. if (set.bits[i] != 0)
  366. return false;
  367. }
  368. return true;
  369. }
  370. /**
  371. * Cloning this <code>BitSet</code> produces a new <code>BitSet</code>
  372. * that is equal to it.
  373. * The clone of the bit set is another bit set that has exactly the
  374. * same bits set to <code>true</code> as this bit set and the same
  375. * current size.
  376. * <p>Overrides the <code>clone</code> method of <code>Object</code>.
  377. *
  378. * @return a clone of this bit set.
  379. * @see java.util.BitSet#size()
  380. */
  381. public Object clone() {
  382. BitSet result = null;
  383. try {
  384. result = (BitSet) super.clone();
  385. } catch (CloneNotSupportedException e) {
  386. throw new InternalError();
  387. }
  388. result.bits = new long[bits.length];
  389. System.arraycopy(bits, 0, result.bits, 0, unitsInUse);
  390. return result;
  391. }
  392. /**
  393. * This override of readObject makes sure unitsInUse is set properly
  394. * when deserializing a bitset
  395. *
  396. */
  397. private void readObject(java.io.ObjectInputStream in)
  398. throws IOException, ClassNotFoundException {
  399. in.defaultReadObject();
  400. //assume maximum length then find real length
  401. //because recalculateUnitsInUse assumes maintenance
  402. //or reduction in logical size
  403. unitsInUse = bits.length;
  404. recalculateUnitsInUse();
  405. }
  406. /**
  407. * Returns a string representation of this bit set. For every index
  408. * for which this <code>BitSet</code> contains a bit in the set
  409. * state, the decimal representation of that index is included in
  410. * the result. Such indeces aer listed in order from lowest to
  411. * highest, separated by ",$nbsp;" (a comma and a space) and
  412. * surrounded by braces, resulting in the usual mathematical
  413. * notation for a set of integers.<p>
  414. * Overrides the <code>toString</code> method of <code>Object</code>.
  415. * <p>Example:
  416. * <pre>
  417. * BitSet drPepper = new BitSet();</pre>
  418. * Now <code>drPepper.toString()</code> returns "<code>{}</code>".<p>
  419. * <pre>
  420. * drPepper.set(2);</pre>
  421. * Now <code>drPepper.toString()</code> returns "<code>{2}</code>".<p>
  422. * <pre>
  423. * drPepper.set(4);
  424. * drPepper.set(10);</pre>
  425. * Now <code>drPepper.toString()</code> returns "<code>{2, 4, 10}</code>".
  426. *
  427. * @return a string representation of this bit set.
  428. */
  429. public String toString() {
  430. int numBits = unitsInUse << ADDRESS_BITS_PER_UNIT;
  431. StringBuffer buffer = new StringBuffer(8*numBits + 2);
  432. String separator = "";
  433. buffer.append('{');
  434. for (int i = 0 ; i < numBits; i++) {
  435. if (get(i)) {
  436. buffer.append(separator);
  437. separator = ", ";
  438. buffer.append(i);
  439. }
  440. }
  441. buffer.append('}');
  442. return buffer.toString();
  443. }
  444. }