BitSet.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. /* BitSet.java -- A vector of bits.
  2. Copyright (C) 1998, 1999, 2000, 2001, 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.util;
  32. import gnu.java.lang.CPStringBuilder;
  33. import java.io.Serializable;
  34. /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
  35. * hashCode algorithm taken from JDK 1.2 docs.
  36. */
  37. /**
  38. * This class can be thought of in two ways. You can see it as a
  39. * vector of bits or as a set of non-negative integers. The name
  40. * <code>BitSet</code> is a bit misleading.
  41. *
  42. * It is implemented by a bit vector, but its equally possible to see
  43. * it as set of non-negative integer; each integer in the set is
  44. * represented by a set bit at the corresponding index. The size of
  45. * this structure is determined by the highest integer in the set.
  46. *
  47. * You can union, intersect and build (symmetric) remainders, by
  48. * invoking the logical operations and, or, andNot, resp. xor.
  49. *
  50. * This implementation is NOT synchronized against concurrent access from
  51. * multiple threads. Specifically, if one thread is reading from a bitset
  52. * while another thread is simultaneously modifying it, the results are
  53. * undefined.
  54. *
  55. * @author Jochen Hoenicke
  56. * @author Tom Tromey (tromey@cygnus.com)
  57. * @author Eric Blake (ebb9@email.byu.edu)
  58. * @status updated to 1.4
  59. */
  60. public class BitSet implements Cloneable, Serializable
  61. {
  62. /**
  63. * Compatible with JDK 1.0.
  64. */
  65. private static final long serialVersionUID = 7997698588986878753L;
  66. /**
  67. * A common mask.
  68. */
  69. private static final int LONG_MASK = 0x3f;
  70. /**
  71. * The actual bits.
  72. * @serial the i'th bit is in bits[i/64] at position i%64 (where position
  73. * 0 is the least significant).
  74. */
  75. private long[] bits;
  76. /**
  77. * Create a new empty bit set. All bits are initially false.
  78. */
  79. public BitSet()
  80. {
  81. this(64);
  82. }
  83. /**
  84. * Create a new empty bit set, with a given size. This
  85. * constructor reserves enough space to represent the integers
  86. * from <code>0</code> to <code>nbits-1</code>.
  87. *
  88. * @param nbits the initial size of the bit set
  89. * @throws NegativeArraySizeException if nbits &lt; 0
  90. */
  91. public BitSet(int nbits)
  92. {
  93. if (nbits < 0)
  94. throw new NegativeArraySizeException();
  95. int length = nbits >>> 6;
  96. if ((nbits & LONG_MASK) != 0)
  97. ++length;
  98. bits = new long[length];
  99. }
  100. /**
  101. * Performs the logical AND operation on this bit set and the
  102. * given <code>set</code>. This means it builds the intersection
  103. * of the two sets. The result is stored into this bit set.
  104. *
  105. * @param bs the second bit set
  106. * @throws NullPointerException if bs is null
  107. */
  108. public void and(BitSet bs)
  109. {
  110. int max = Math.min(bits.length, bs.bits.length);
  111. int i;
  112. for (i = 0; i < max; ++i)
  113. bits[i] &= bs.bits[i];
  114. while (i < bits.length)
  115. bits[i++] = 0;
  116. }
  117. /**
  118. * Performs the logical AND operation on this bit set and the
  119. * complement of the given <code>bs</code>. This means it
  120. * selects every element in the first set, that isn't in the
  121. * second set. The result is stored into this bit set and is
  122. * effectively the set difference of the two.
  123. *
  124. * @param bs the second bit set
  125. * @throws NullPointerException if bs is null
  126. * @since 1.2
  127. */
  128. public void andNot(BitSet bs)
  129. {
  130. int i = Math.min(bits.length, bs.bits.length);
  131. while (--i >= 0)
  132. bits[i] &= ~bs.bits[i];
  133. }
  134. /**
  135. * Returns the number of bits set to true.
  136. *
  137. * @return the number of true bits
  138. * @since 1.4
  139. */
  140. public int cardinality()
  141. {
  142. int card = 0;
  143. for (int i = bits.length - 1; i >= 0; i--)
  144. {
  145. long a = bits[i];
  146. // Take care of common cases.
  147. if (a == 0)
  148. continue;
  149. if (a == -1)
  150. {
  151. card += 64;
  152. continue;
  153. }
  154. // Successively collapse alternating bit groups into a sum.
  155. a = ((a >> 1) & 0x5555555555555555L) + (a & 0x5555555555555555L);
  156. a = ((a >> 2) & 0x3333333333333333L) + (a & 0x3333333333333333L);
  157. int b = (int) ((a >>> 32) + a);
  158. b = ((b >> 4) & 0x0f0f0f0f) + (b & 0x0f0f0f0f);
  159. b = ((b >> 8) & 0x00ff00ff) + (b & 0x00ff00ff);
  160. card += ((b >> 16) & 0x0000ffff) + (b & 0x0000ffff);
  161. }
  162. return card;
  163. }
  164. /**
  165. * Sets all bits in the set to false.
  166. *
  167. * @since 1.4
  168. */
  169. public void clear()
  170. {
  171. Arrays.fill(bits, 0);
  172. }
  173. /**
  174. * Removes the integer <code>pos</code> from this set. That is
  175. * the corresponding bit is cleared. If the index is not in the set,
  176. * this method does nothing.
  177. *
  178. * @param pos a non-negative integer
  179. * @throws IndexOutOfBoundsException if pos &lt; 0
  180. */
  181. public void clear(int pos)
  182. {
  183. int offset = pos >> 6;
  184. ensure(offset);
  185. // ArrayIndexOutOfBoundsException subclasses IndexOutOfBoundsException,
  186. // so we'll just let that be our exception.
  187. bits[offset] &= ~(1L << pos);
  188. }
  189. /**
  190. * Sets the bits between from (inclusive) and to (exclusive) to false.
  191. *
  192. * @param from the start range (inclusive)
  193. * @param to the end range (exclusive)
  194. * @throws IndexOutOfBoundsException if from &lt; 0 || to &lt; 0 ||
  195. * from &gt; to
  196. * @since 1.4
  197. */
  198. public void clear(int from, int to)
  199. {
  200. if (from < 0 || from > to)
  201. throw new IndexOutOfBoundsException();
  202. if (from == to)
  203. return;
  204. int lo_offset = from >>> 6;
  205. int hi_offset = to >>> 6;
  206. ensure(hi_offset);
  207. if (lo_offset == hi_offset)
  208. {
  209. bits[hi_offset] &= ((1L << from) - 1) | (-1L << to);
  210. return;
  211. }
  212. bits[lo_offset] &= (1L << from) - 1;
  213. bits[hi_offset] &= -1L << to;
  214. for (int i = lo_offset + 1; i < hi_offset; i++)
  215. bits[i] = 0;
  216. }
  217. /**
  218. * Create a clone of this bit set, that is an instance of the same
  219. * class and contains the same elements. But it doesn't change when
  220. * this bit set changes.
  221. *
  222. * @return the clone of this object.
  223. */
  224. public Object clone()
  225. {
  226. try
  227. {
  228. BitSet bs = (BitSet) super.clone();
  229. bs.bits = (long[]) bits.clone();
  230. return bs;
  231. }
  232. catch (CloneNotSupportedException e)
  233. {
  234. // Impossible to get here.
  235. return null;
  236. }
  237. }
  238. /**
  239. * Returns true if the <code>obj</code> is a bit set that contains
  240. * exactly the same elements as this bit set, otherwise false.
  241. *
  242. * @param obj the object to compare to
  243. * @return true if obj equals this bit set
  244. */
  245. public boolean equals(Object obj)
  246. {
  247. if (!(obj instanceof BitSet))
  248. return false;
  249. BitSet bs = (BitSet) obj;
  250. int max = Math.min(bits.length, bs.bits.length);
  251. int i;
  252. for (i = 0; i < max; ++i)
  253. if (bits[i] != bs.bits[i])
  254. return false;
  255. // If one is larger, check to make sure all extra bits are 0.
  256. for (int j = i; j < bits.length; ++j)
  257. if (bits[j] != 0)
  258. return false;
  259. for (int j = i; j < bs.bits.length; ++j)
  260. if (bs.bits[j] != 0)
  261. return false;
  262. return true;
  263. }
  264. /**
  265. * Sets the bit at the index to the opposite value.
  266. *
  267. * @param index the index of the bit
  268. * @throws IndexOutOfBoundsException if index is negative
  269. * @since 1.4
  270. */
  271. public void flip(int index)
  272. {
  273. int offset = index >> 6;
  274. ensure(offset);
  275. // ArrayIndexOutOfBoundsException subclasses IndexOutOfBoundsException,
  276. // so we'll just let that be our exception.
  277. bits[offset] ^= 1L << index;
  278. }
  279. /**
  280. * Sets a range of bits to the opposite value.
  281. *
  282. * @param from the low index (inclusive)
  283. * @param to the high index (exclusive)
  284. * @throws IndexOutOfBoundsException if from &gt; to || from &lt; 0 ||
  285. * to &lt; 0
  286. * @since 1.4
  287. */
  288. public void flip(int from, int to)
  289. {
  290. if (from < 0 || from > to)
  291. throw new IndexOutOfBoundsException();
  292. if (from == to)
  293. return;
  294. int lo_offset = from >>> 6;
  295. int hi_offset = to >>> 6;
  296. ensure(hi_offset);
  297. if (lo_offset == hi_offset)
  298. {
  299. bits[hi_offset] ^= (-1L << from) & ((1L << to) - 1);
  300. return;
  301. }
  302. bits[lo_offset] ^= -1L << from;
  303. bits[hi_offset] ^= (1L << to) - 1;
  304. for (int i = lo_offset + 1; i < hi_offset; i++)
  305. bits[i] ^= -1;
  306. }
  307. /**
  308. * Returns true if the integer <code>bitIndex</code> is in this bit
  309. * set, otherwise false.
  310. *
  311. * @param pos a non-negative integer
  312. * @return the value of the bit at the specified position
  313. * @throws IndexOutOfBoundsException if the pos is negative
  314. */
  315. public boolean get(int pos)
  316. {
  317. int offset = pos >> 6;
  318. if (offset >= bits.length)
  319. return false;
  320. // ArrayIndexOutOfBoundsException subclasses IndexOutOfBoundsException,
  321. // so we'll just let that be our exception.
  322. return (bits[offset] & (1L << pos)) != 0;
  323. }
  324. /**
  325. * Returns a new <code>BitSet</code> composed of a range of bits from
  326. * this one.
  327. *
  328. * @param from the low index (inclusive)
  329. * @param to the high index (exclusive)
  330. * @throws IndexOutOfBoundsException if from &gt; to || from &lt; 0 ||
  331. * to &lt; 0
  332. * @since 1.4
  333. */
  334. public BitSet get(int from, int to)
  335. {
  336. if (from < 0 || from > to)
  337. throw new IndexOutOfBoundsException();
  338. BitSet bs = new BitSet(to - from);
  339. int lo_offset = from >>> 6;
  340. if (lo_offset >= bits.length || to == from)
  341. return bs;
  342. int lo_bit = from & LONG_MASK;
  343. int hi_offset = to >>> 6;
  344. if (lo_bit == 0)
  345. {
  346. int len = Math.min(hi_offset - lo_offset + 1, bits.length - lo_offset);
  347. System.arraycopy(bits, lo_offset, bs.bits, 0, len);
  348. if (hi_offset < bits.length)
  349. bs.bits[hi_offset - lo_offset] &= (1L << to) - 1;
  350. return bs;
  351. }
  352. int len = Math.min(hi_offset, bits.length - 1);
  353. int reverse = 64 - lo_bit;
  354. int i;
  355. for (i = 0; lo_offset < len; lo_offset++, i++)
  356. bs.bits[i] = ((bits[lo_offset] >>> lo_bit)
  357. | (bits[lo_offset + 1] << reverse));
  358. if ((to & LONG_MASK) > lo_bit)
  359. bs.bits[i++] = bits[lo_offset] >>> lo_bit;
  360. if (hi_offset < bits.length)
  361. bs.bits[i - 1] &= (1L << (to - from)) - 1;
  362. return bs;
  363. }
  364. /**
  365. * Returns a hash code value for this bit set. The hash code of
  366. * two bit sets containing the same integers is identical. The algorithm
  367. * used to compute it is as follows:
  368. *
  369. * Suppose the bits in the BitSet were to be stored in an array of
  370. * long integers called <code>bits</code>, in such a manner that
  371. * bit <code>k</code> is set in the BitSet (for non-negative values
  372. * of <code>k</code>) if and only if
  373. *
  374. * <code>((k/64) &lt; bits.length)
  375. * && ((bits[k/64] & (1L &lt;&lt; (bit % 64))) != 0)
  376. * </code>
  377. *
  378. * Then the following definition of the hashCode method
  379. * would be a correct implementation of the actual algorithm:
  380. *
  381. *
  382. <pre>public int hashCode()
  383. {
  384. long h = 1234;
  385. for (int i = bits.length-1; i &gt;= 0; i--)
  386. {
  387. h ^= bits[i] * (i + 1);
  388. }
  389. return (int)((h >> 32) ^ h);
  390. }</pre>
  391. *
  392. * Note that the hash code values changes, if the set is changed.
  393. *
  394. * @return the hash code value for this bit set.
  395. */
  396. public int hashCode()
  397. {
  398. long h = 1234;
  399. for (int i = bits.length; i > 0; )
  400. h ^= i * bits[--i];
  401. return (int) ((h >> 32) ^ h);
  402. }
  403. /**
  404. * Returns true if the specified BitSet and this one share at least one
  405. * common true bit.
  406. *
  407. * @param set the set to check for intersection
  408. * @return true if the sets intersect
  409. * @throws NullPointerException if set is null
  410. * @since 1.4
  411. */
  412. public boolean intersects(BitSet set)
  413. {
  414. int i = Math.min(bits.length, set.bits.length);
  415. while (--i >= 0)
  416. if ((bits[i] & set.bits[i]) != 0)
  417. return true;
  418. return false;
  419. }
  420. /**
  421. * Returns true if this set contains no true bits.
  422. *
  423. * @return true if all bits are false
  424. * @since 1.4
  425. */
  426. public boolean isEmpty()
  427. {
  428. for (int i = bits.length - 1; i >= 0; i--)
  429. if (bits[i] != 0)
  430. return false;
  431. return true;
  432. }
  433. /**
  434. * Returns the logical number of bits actually used by this bit
  435. * set. It returns the index of the highest set bit plus one.
  436. * Note that this method doesn't return the number of set bits.
  437. *
  438. * @return the index of the highest set bit plus one.
  439. */
  440. public int length()
  441. {
  442. // Set i to highest index that contains a non-zero value.
  443. int i;
  444. for (i = bits.length - 1; i >= 0 && bits[i] == 0; --i)
  445. ;
  446. // if i < 0 all bits are cleared.
  447. if (i < 0)
  448. return 0;
  449. // Now determine the exact length.
  450. long b = bits[i];
  451. int len = (i + 1) * 64;
  452. // b >= 0 checks if the highest bit is zero.
  453. while (b >= 0)
  454. {
  455. --len;
  456. b <<= 1;
  457. }
  458. return len;
  459. }
  460. /**
  461. * Returns the index of the next false bit, from the specified bit
  462. * (inclusive).
  463. *
  464. * @param from the start location
  465. * @return the first false bit
  466. * @throws IndexOutOfBoundsException if from is negative
  467. * @since 1.4
  468. */
  469. public int nextClearBit(int from)
  470. {
  471. int offset = from >> 6;
  472. long mask = 1L << from;
  473. while (offset < bits.length)
  474. {
  475. // ArrayIndexOutOfBoundsException subclasses IndexOutOfBoundsException,
  476. // so we'll just let that be our exception.
  477. long h = bits[offset];
  478. do
  479. {
  480. if ((h & mask) == 0)
  481. return from;
  482. mask <<= 1;
  483. from++;
  484. }
  485. while (mask != 0);
  486. mask = 1;
  487. offset++;
  488. }
  489. return from;
  490. }
  491. /**
  492. * Returns the index of the next true bit, from the specified bit
  493. * (inclusive). If there is none, -1 is returned. You can iterate over
  494. * all true bits with this loop:<br>
  495. *
  496. <pre>for (int i = bs.nextSetBit(0); i &gt;= 0; i = bs.nextSetBit(i + 1))
  497. {
  498. // operate on i here
  499. }</pre>
  500. *
  501. * @param from the start location
  502. * @return the first true bit, or -1
  503. * @throws IndexOutOfBoundsException if from is negative
  504. * @since 1.4
  505. */
  506. public int nextSetBit(int from)
  507. {
  508. int offset = from >> 6;
  509. long mask = 1L << from;
  510. while (offset < bits.length)
  511. {
  512. // ArrayIndexOutOfBoundsException subclasses IndexOutOfBoundsException,
  513. // so we'll just let that be our exception.
  514. long h = bits[offset];
  515. do
  516. {
  517. if ((h & mask) != 0)
  518. return from;
  519. mask <<= 1;
  520. from++;
  521. }
  522. while (mask != 0);
  523. mask = 1;
  524. offset++;
  525. }
  526. return -1;
  527. }
  528. /**
  529. * Performs the logical OR operation on this bit set and the
  530. * given <code>set</code>. This means it builds the union
  531. * of the two sets. The result is stored into this bit set, which
  532. * grows as necessary.
  533. *
  534. * @param bs the second bit set
  535. * @throws NullPointerException if bs is null
  536. */
  537. public void or(BitSet bs)
  538. {
  539. ensure(bs.bits.length - 1);
  540. for (int i = bs.bits.length - 1; i >= 0; i--)
  541. bits[i] |= bs.bits[i];
  542. }
  543. /**
  544. * Add the integer <code>bitIndex</code> to this set. That is
  545. * the corresponding bit is set to true. If the index was already in
  546. * the set, this method does nothing. The size of this structure
  547. * is automatically increased as necessary.
  548. *
  549. * @param pos a non-negative integer.
  550. * @throws IndexOutOfBoundsException if pos is negative
  551. */
  552. public void set(int pos)
  553. {
  554. int offset = pos >> 6;
  555. ensure(offset);
  556. // ArrayIndexOutOfBoundsException subclasses IndexOutOfBoundsException,
  557. // so we'll just let that be our exception.
  558. bits[offset] |= 1L << pos;
  559. }
  560. /**
  561. * Sets the bit at the given index to the specified value. The size of
  562. * this structure is automatically increased as necessary.
  563. *
  564. * @param index the position to set
  565. * @param value the value to set it to
  566. * @throws IndexOutOfBoundsException if index is negative
  567. * @since 1.4
  568. */
  569. public void set(int index, boolean value)
  570. {
  571. if (value)
  572. set(index);
  573. else
  574. clear(index);
  575. }
  576. /**
  577. * Sets the bits between from (inclusive) and to (exclusive) to true.
  578. *
  579. * @param from the start range (inclusive)
  580. * @param to the end range (exclusive)
  581. * @throws IndexOutOfBoundsException if from &lt; 0 || from &gt; to ||
  582. * to &lt; 0
  583. * @since 1.4
  584. */
  585. public void set(int from, int to)
  586. {
  587. if (from < 0 || from > to)
  588. throw new IndexOutOfBoundsException();
  589. if (from == to)
  590. return;
  591. int lo_offset = from >>> 6;
  592. int hi_offset = to >>> 6;
  593. ensure(hi_offset);
  594. if (lo_offset == hi_offset)
  595. {
  596. bits[hi_offset] |= (-1L << from) & ((1L << to) - 1);
  597. return;
  598. }
  599. bits[lo_offset] |= -1L << from;
  600. bits[hi_offset] |= (1L << to) - 1;
  601. for (int i = lo_offset + 1; i < hi_offset; i++)
  602. bits[i] = -1;
  603. }
  604. /**
  605. * Sets the bits between from (inclusive) and to (exclusive) to the
  606. * specified value.
  607. *
  608. * @param from the start range (inclusive)
  609. * @param to the end range (exclusive)
  610. * @param value the value to set it to
  611. * @throws IndexOutOfBoundsException if from &lt; 0 || from &gt; to ||
  612. * to &lt; 0
  613. * @since 1.4
  614. */
  615. public void set(int from, int to, boolean value)
  616. {
  617. if (value)
  618. set(from, to);
  619. else
  620. clear(from, to);
  621. }
  622. /**
  623. * Returns the number of bits actually used by this bit set. Note
  624. * that this method doesn't return the number of set bits, and that
  625. * future requests for larger bits will make this automatically grow.
  626. *
  627. * @return the number of bits currently used.
  628. */
  629. public int size()
  630. {
  631. return bits.length * 64;
  632. }
  633. /**
  634. * Returns the string representation of this bit set. This
  635. * consists of a comma separated list of the integers in this set
  636. * surrounded by curly braces. There is a space after each comma.
  637. * A sample string is thus "{1, 3, 53}".
  638. * @return the string representation.
  639. */
  640. public String toString()
  641. {
  642. CPStringBuilder r = new CPStringBuilder("{");
  643. boolean first = true;
  644. for (int i = 0; i < bits.length; ++i)
  645. {
  646. long bit = 1;
  647. long word = bits[i];
  648. if (word == 0)
  649. continue;
  650. for (int j = 0; j < 64; ++j)
  651. {
  652. if ((word & bit) != 0)
  653. {
  654. if (! first)
  655. r.append(", ");
  656. r.append(64 * i + j);
  657. first = false;
  658. }
  659. bit <<= 1;
  660. }
  661. }
  662. return r.append("}").toString();
  663. }
  664. /**
  665. * Performs the logical XOR operation on this bit set and the
  666. * given <code>set</code>. This means it builds the symmetric
  667. * remainder of the two sets (the elements that are in one set,
  668. * but not in the other). The result is stored into this bit set,
  669. * which grows as necessary.
  670. *
  671. * @param bs the second bit set
  672. * @throws NullPointerException if bs is null
  673. */
  674. public void xor(BitSet bs)
  675. {
  676. ensure(bs.bits.length - 1);
  677. for (int i = bs.bits.length - 1; i >= 0; i--)
  678. bits[i] ^= bs.bits[i];
  679. }
  680. /**
  681. * Make sure the vector is big enough.
  682. *
  683. * @param lastElt the size needed for the bits array
  684. */
  685. private void ensure(int lastElt)
  686. {
  687. if (lastElt >= bits.length)
  688. {
  689. long[] nd = new long[lastElt + 1];
  690. System.arraycopy(bits, 0, nd, 0, bits.length);
  691. bits = nd;
  692. }
  693. }
  694. // This is used by EnumSet for efficiency.
  695. final boolean containsAll(BitSet other)
  696. {
  697. for (int i = other.bits.length - 1; i >= 0; i--)
  698. {
  699. if ((bits[i] & other.bits[i]) != other.bits[i])
  700. return false;
  701. }
  702. return true;
  703. }
  704. }