home *** CD-ROM | disk | FTP | other *** search
/ Chip: Shareware for Win 95 / Chip-Shareware-Win95.bin / ostatni / powerj / java.z / BitSet.java < prev    next >
Encoding:
Java Source  |  1996-05-03  |  4.9 KB  |  208 lines

  1. /*
  2.  * @(#)BitSet.java    1.12 95/12/01  
  3.  *
  4.  * Copyright (c) 1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.util;
  21.  
  22. /**
  23.  * A set of bits. The set automatically grows as more bits are
  24.  * needed. 
  25.  *
  26.  * @version     1.12, 01 Dec 1995
  27.  * @author Arthur van Hoff
  28.  */
  29. public final class BitSet implements Cloneable {
  30.     final static int BITS = 6;
  31.     final static int MASK = (1<<BITS)-1;
  32.     long bits[];
  33.  
  34.     /**
  35.      * Creates an empty set.
  36.      */
  37.     public BitSet() {
  38.     this(1<<BITS);
  39.     }
  40.  
  41.     /**
  42.      * Creates an empty set with the specified size.
  43.      * @param nbits the size of the set
  44.      */
  45.     public BitSet(int nbits) {
  46.     bits = new long[(nbits + MASK)>>BITS];
  47.     }
  48.  
  49.     /**
  50.      * Grows the set to a larger number of bits.
  51.      * @param nbits the number of bits to increase the set by
  52.      */
  53.     private void grow(int nbits) {
  54.     long newbits[] = new long[Math.max(bits.length<<1, (nbits + MASK)>>BITS)];
  55.     System.arraycopy(bits, 0, newbits, 0, bits.length);
  56.     bits = newbits;
  57.     }
  58.  
  59.     /**
  60.      * Sets a bit.
  61.      * @param bit the bit to be set
  62.      */
  63.     public void set(int bit) {
  64.     int n = bit>>BITS;
  65.     if (n >= bits.length) {
  66.         grow(bit);
  67.     }
  68.     bits[n] |= (1L << (bit & MASK));
  69.     }
  70.  
  71.     /**
  72.      * Clears a bit.
  73.      * @param bit the bit to be cleared
  74.      */
  75.     public void clear(int bit) {
  76.     int n = bit>>BITS;
  77.     if (n >= bits.length) {
  78.         grow(bit);
  79.     }
  80.     bits[n] &= ~(1L << (bit & MASK));
  81.     }
  82.  
  83.     /**
  84.      * Gets a bit.
  85.      * @param bit the bit to be gotten
  86.      */
  87.     public boolean get(int bit) {
  88.     int n = bit>>BITS;
  89.     return (n < bits.length) ? ((bits[n] & (1L << (bit & MASK))) != 0) : false;
  90.     }
  91.  
  92.     /**
  93.      * Logically ANDs this bit set with the specified set of bits.
  94.      * @param set the bit set to be ANDed with
  95.      */
  96.     public void and(BitSet set) {
  97.     int n = Math.min(bits.length, set.bits.length);
  98.     for (int i = n ; i-- > 0 ; ) {
  99.         bits[i] &= set.bits[i];
  100.     }
  101.     for (; n < bits.length ; n++) {
  102.         bits[n] = 0;
  103.     }
  104.     }
  105.  
  106.     /**
  107.      * Logically ORs this bit set with the specified set of bits.
  108.      * @param set the bit set to be ORed with
  109.      */
  110.     public void or(BitSet set) {
  111.     for (int i = Math.min(bits.length, set.bits.length) ; i-- > 0 ;) {
  112.         bits[i] |= set.bits[i];
  113.     }
  114.     }
  115.  
  116.     /**
  117.      * Logically XORs this bit set with the specified set of bits.
  118.      * @param set the bit set to be XORed with
  119.      */
  120.     public void xor(BitSet set) {
  121.     for (int i = Math.min(bits.length, set.bits.length) ; i-- > 0 ;) {
  122.         bits[i] ^= set.bits[i];
  123.     }
  124.     }
  125.  
  126.     /**
  127.      * Gets the hashcode.
  128.      */
  129.     public int hashCode() {
  130.     long h = 1234;
  131.     for (int i = bits.length; --i >= 0; ) {
  132.         h ^= bits[i] * i;
  133.     }
  134.     return (int)((h >> 32) ^ h);
  135.     }
  136.  
  137.     /**
  138.      * Calculates and returns the set's size
  139.      */
  140.     public int size() {
  141.     return bits.length << BITS;
  142.     }
  143.  
  144.     /**
  145.      * Compares this object against the specified object.
  146.      * @param obj the object to commpare with
  147.      * @return true if the objects are the same; false otherwise.
  148.      */
  149.     public boolean equals(Object obj) {
  150.     if ((obj != null) && (obj instanceof BitSet)) {
  151.         BitSet set = (BitSet)obj;
  152.  
  153.         int n = Math.min(bits.length, set.bits.length);
  154.         for (int i = n ; i-- > 0 ;) {
  155.         if (bits[i] != set.bits[i]) {
  156.             return false;
  157.         }
  158.         }
  159.         if (bits.length > n) {
  160.         for (int i = bits.length ; i-- > n ;) {
  161.             if (bits[i] != 0) {
  162.             return false;
  163.             }
  164.         }
  165.         } else if (set.bits.length > n) {
  166.         for (int i = set.bits.length ; i-- > n ;) {
  167.             if (set.bits[i] != 0) {
  168.             return false;
  169.             }
  170.         }
  171.         }
  172.         return true;
  173.     }
  174.     return false;
  175.     }
  176.  
  177.     /**
  178.      * Clones the BitSet.
  179.      */
  180.     public Object clone() {
  181.     try { 
  182.         BitSet set = (BitSet)super.clone();
  183.         set.bits = new long[bits.length];
  184.         System.arraycopy(bits, 0, set.bits, 0, bits.length);
  185.         return set;
  186.     } catch (CloneNotSupportedException e) {
  187.         // this shouldn't happen, since we are Cloneable
  188.         throw new InternalError();
  189.     }
  190.     }
  191.  
  192.     /**
  193.      * Converts the BitSet to a String.
  194.      */
  195.     public String toString() {
  196.     String str = "";
  197.     for (int i = 0 ; i < (bits.length << BITS) ; i++) {
  198.         if (get(i)) {
  199.         if (str.length() > 0) {
  200.             str += ", ";
  201.         }
  202.         str = str + i;
  203.         }
  204.     }
  205.     return "{" + str + "}";
  206.     }
  207. }
  208.