1 /*
   2  * Permission is hereby granted, free of charge, to any person obtaining a copy of
   3  * this software and associated documentation files (the "Software"), to deal in
   4  * the Software without restriction, including without limitation the rights to
   5  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
   6  * of the Software, and to permit persons to whom the Software is furnished to do
   7  * so, subject to the following conditions:
   8  *
   9  * The above copyright notice and this permission notice shall be included in all
  10  * copies or substantial portions of the Software.
  11  *
  12  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18  * SOFTWARE.
  19  */
  20 package jdk.nashorn.internal.runtime.regexp.joni;
  21 
  22 public final class BitSet {
  23     static final int BITS_PER_BYTE = 8;
  24     public static final int SINGLE_BYTE_SIZE = (1 << BITS_PER_BYTE);
  25     private static final int BITS_IN_ROOM = 4 * BITS_PER_BYTE;
  26     static final int BITSET_SIZE = (SINGLE_BYTE_SIZE / BITS_IN_ROOM);
  27     static final int ROOM_SHIFT = log2(BITS_IN_ROOM);
  28 
  29     final int[] bits = new int[BITSET_SIZE];
  30 
  31     private static final int BITS_TO_STRING_WRAP = 4;
  32     public String toString() {
  33         StringBuilder buffer = new StringBuilder();
  34         buffer.append("BitSet");
  35         for (int i=0; i<SINGLE_BYTE_SIZE; i++) {
  36             if ((i % (SINGLE_BYTE_SIZE / BITS_TO_STRING_WRAP)) == 0) buffer.append("\n  ");
  37             buffer.append(at(i) ? "1" : "0");
  38         }
  39         return buffer.toString();
  40     }
  41 
  42     public boolean at(int pos) {
  43         return (bits[pos >>> ROOM_SHIFT] & bit(pos)) != 0;
  44     }
  45 
  46     public void set(int pos) {
  47         bits[pos >>> ROOM_SHIFT] |= bit(pos);
  48     }
  49 
  50     public void clear(int pos) {
  51         bits[pos >>> ROOM_SHIFT] &= ~bit(pos);
  52     }
  53 
  54     public void invert(int pos) {
  55         bits[pos >>> ROOM_SHIFT] ^= bit(pos);
  56     }
  57 
  58     public void clear() {
  59         for (int i=0; i<BITSET_SIZE; i++) bits[i]=0;
  60     }
  61 
  62     public boolean isEmpty() {
  63         for (int i=0; i<BITSET_SIZE; i++) {
  64             if (bits[i] != 0) return false;
  65         }
  66         return true;
  67     }
  68 
  69     public void setRange(int from, int to) {
  70         for (int i=from; i<=to && i < SINGLE_BYTE_SIZE; i++) set(i);
  71     }
  72 
  73     public void setAll() {
  74         for (int i=0; i<BITSET_SIZE; i++) bits[i] = ~0;
  75     }
  76 
  77     public void invert() {
  78         for (int i=0; i<BITSET_SIZE; i++) bits[i] = ~bits[i];
  79     }
  80 
  81     public void invertTo(BitSet to) {
  82         for (int i=0; i<BITSET_SIZE; i++) to.bits[i] = ~bits[i];
  83     }
  84 
  85     public void and(BitSet other) {
  86         for (int i=0; i<BITSET_SIZE; i++) bits[i] &= other.bits[i];
  87     }
  88 
  89     public void or(BitSet other) {
  90         for (int i=0; i<BITSET_SIZE; i++) bits[i] |= other.bits[i];
  91     }
  92 
  93     public void copy(BitSet other) {
  94         for (int i=0; i<BITSET_SIZE; i++) bits[i] = other.bits[i];
  95     }
  96 
  97     public int numOn() {
  98         int num = 0;
  99         for (int i=0; i<SINGLE_BYTE_SIZE; i++) {
 100             if (at(i)) num++;
 101         }
 102         return num;
 103     }
 104 
 105     static int bit(int pos){
 106         return 1 << (pos % SINGLE_BYTE_SIZE);
 107     }
 108 
 109     private static int log2(int n){
 110         int log = 0;
 111         while ((n >>>= 1) != 0) log++;
 112         return log;
 113     }
 114 
 115 }