jdk/src/share/classes/java/util/BitSet.java
author sherman
Tue, 30 Aug 2011 11:53:11 -0700
changeset 10419 12c063b39232
parent 5506 202f599c92aa
child 14342 8435a30053c1
permissions -rw-r--r--
7084245: Update usages of InternalError to use exception chaining Summary: to use new InternalError constructor with cause chainning Reviewed-by: alanb, ksrini, xuelei, neugens Contributed-by: sebastian.sickelmann@gmx.de
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1995, 2007, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.ByteOrder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.LongBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * This class implements a vector of bits that grows as needed. Each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * component of the bit set has a {@code boolean} value. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * bits of a {@code BitSet} are indexed by nonnegative integers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Individual indexed bits can be examined, set, or cleared. One
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * {@code BitSet} may be used to modify the contents of another
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * {@code BitSet} through logical AND, logical inclusive OR, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * logical exclusive OR operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p>By default, all bits in the set initially have the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p>Every bit set has a current size, which is the number of bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * of space currently in use by the bit set. Note that the size is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * related to the implementation of a bit set, so it may change with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * implementation. The length of a bit set relates to logical length
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * of a bit set and is defined independently of implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>Unless otherwise noted, passing a null parameter to any of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * methods in a {@code BitSet} will result in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * {@code NullPointerException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <p>A {@code BitSet} is not safe for multithreaded use without
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * external synchronization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @author  Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @author  Michael McCloskey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @author  Martin Buchholz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @since   JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
public class BitSet implements Cloneable, java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * BitSets are packed into arrays of "words."  Currently a word is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * a long, which consists of 64 bits, requiring 6 address bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * The choice of word size is determined purely by performance concerns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private final static int ADDRESS_BITS_PER_WORD = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private final static int BITS_PER_WORD = 1 << ADDRESS_BITS_PER_WORD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private final static int BIT_INDEX_MASK = BITS_PER_WORD - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /* Used to shift left or right for a partial word mask */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static final long WORD_MASK = 0xffffffffffffffffL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @serialField bits long[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * The bits in this BitSet.  The ith bit is stored in bits[i/64] at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * bit position i % 64 (where bit position 0 refers to the least
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * significant bit and 63 refers to the most significant bit).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private static final ObjectStreamField[] serialPersistentFields = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        new ObjectStreamField("bits", long[].class),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * The internal field corresponding to the serialField "bits".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private long[] words;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * The number of words in the logical size of this BitSet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private transient int wordsInUse = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Whether the size of "words" is user-specified.  If so, we assume
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * the user knows what he's doing and try harder to preserve it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private transient boolean sizeIsSticky = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /* use serialVersionUID from JDK 1.0.2 for interoperability */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private static final long serialVersionUID = 7997698588986878753L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Given a bit index, return word index containing it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private static int wordIndex(int bitIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return bitIndex >> ADDRESS_BITS_PER_WORD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Every public method must preserve these invariants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private void checkInvariants() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        assert(wordsInUse == 0 || words[wordsInUse - 1] != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        assert(wordsInUse >= 0 && wordsInUse <= words.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        assert(wordsInUse == words.length || words[wordsInUse] == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Sets the field wordsInUse to the logical size in words of the bit set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * WARNING:This method assumes that the number of words actually in use is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * less than or equal to the current value of wordsInUse!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    private void recalculateWordsInUse() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        // Traverse the bitset until a used word is found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        for (i = wordsInUse-1; i >= 0; i--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            if (words[i] != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        wordsInUse = i+1; // The new logical size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Creates a new bit set. All bits are initially {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public BitSet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        initWords(BITS_PER_WORD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        sizeIsSticky = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Creates a bit set whose initial size is large enough to explicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * represent bits with indices in the range {@code 0} through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * {@code nbits-1}. All bits are initially {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param  nbits the initial size of the bit set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @throws NegativeArraySizeException if the specified initial size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *         is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public BitSet(int nbits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        // nbits can't be negative; size 0 is OK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (nbits < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            throw new NegativeArraySizeException("nbits < 0: " + nbits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        initWords(nbits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        sizeIsSticky = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    private void initWords(int nbits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        words = new long[wordIndex(nbits-1) + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Creates a bit set using words as the internal representation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * The last word (if there is one) must be non-zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    private BitSet(long[] words) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        this.words = words;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        this.wordsInUse = words.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Returns a new bit set containing all the bits in the given long array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * <p>More precisely,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * <br>{@code BitSet.valueOf(longs).get(n) == ((longs[n/64] & (1L<<(n%64))) != 0)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * <br>for all {@code n < 64 * longs.length}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * <p>This method is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * {@code BitSet.valueOf(LongBuffer.wrap(longs))}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @param longs a long array containing a little-endian representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *        of a sequence of bits to be used as the initial bits of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *        new bit set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public static BitSet valueOf(long[] longs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        for (n = longs.length; n > 0 && longs[n - 1] == 0; n--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        return new BitSet(Arrays.copyOf(longs, n));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Returns a new bit set containing all the bits in the given long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * buffer between its position and limit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * <p>More precisely,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * <br>{@code BitSet.valueOf(lb).get(n) == ((lb.get(lb.position()+n/64) & (1L<<(n%64))) != 0)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * <br>for all {@code n < 64 * lb.remaining()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * <p>The long buffer is not modified by this method, and no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * reference to the buffer is retained by the bit set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @param lb a long buffer containing a little-endian representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *        of a sequence of bits between its position and limit, to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *        used as the initial bits of the new bit set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    public static BitSet valueOf(LongBuffer lb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        lb = lb.slice();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        for (n = lb.remaining(); n > 0 && lb.get(n - 1) == 0; n--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        long[] words = new long[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        lb.get(words);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        return new BitSet(words);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * Returns a new bit set containing all the bits in the given byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * <p>More precisely,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * <br>{@code BitSet.valueOf(bytes).get(n) == ((bytes[n/8] & (1<<(n%8))) != 0)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * <br>for all {@code n <  8 * bytes.length}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * <p>This method is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * {@code BitSet.valueOf(ByteBuffer.wrap(bytes))}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @param bytes a byte array containing a little-endian
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *        representation of a sequence of bits to be used as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *        initial bits of the new bit set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    public static BitSet valueOf(byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        return BitSet.valueOf(ByteBuffer.wrap(bytes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * Returns a new bit set containing all the bits in the given byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * buffer between its position and limit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * <p>More precisely,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * <br>{@code BitSet.valueOf(bb).get(n) == ((bb.get(bb.position()+n/8) & (1<<(n%8))) != 0)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * <br>for all {@code n < 8 * bb.remaining()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * <p>The byte buffer is not modified by this method, and no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * reference to the buffer is retained by the bit set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @param bb a byte buffer containing a little-endian representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *        of a sequence of bits between its position and limit, to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *        used as the initial bits of the new bit set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    public static BitSet valueOf(ByteBuffer bb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        bb = bb.slice().order(ByteOrder.LITTLE_ENDIAN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        for (n = bb.remaining(); n > 0 && bb.get(n - 1) == 0; n--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        long[] words = new long[(n + 7) / 8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        bb.limit(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        while (bb.remaining() >= 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            words[i++] = bb.getLong();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        for (int remaining = bb.remaining(), j = 0; j < remaining; j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            words[i] |= (bb.get() & 0xffL) << (8 * j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        return new BitSet(words);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * Returns a new byte array containing all the bits in this bit set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * <p>More precisely, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * <br>{@code byte[] bytes = s.toByteArray();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * <br>then {@code bytes.length == (s.length()+7)/8} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * <br>{@code s.get(n) == ((bytes[n/8] & (1<<(n%8))) != 0)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * <br>for all {@code n < 8 * bytes.length}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @return a byte array containing a little-endian representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *         of all the bits in this bit set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    public byte[] toByteArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        int n = wordsInUse;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        if (n == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            return new byte[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        int len = 8 * (n-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        for (long x = words[n - 1]; x != 0; x >>>= 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            len++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        byte[] bytes = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        ByteBuffer bb = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        for (int i = 0; i < n - 1; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            bb.putLong(words[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        for (long x = words[n - 1]; x != 0; x >>>= 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            bb.put((byte) (x & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        return bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * Returns a new long array containing all the bits in this bit set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * <p>More precisely, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * <br>{@code long[] longs = s.toLongArray();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * <br>then {@code longs.length == (s.length()+63)/64} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * <br>{@code s.get(n) == ((longs[n/64] & (1L<<(n%64))) != 0)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * <br>for all {@code n < 64 * longs.length}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @return a long array containing a little-endian representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *         of all the bits in this bit set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    public long[] toLongArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        return Arrays.copyOf(words, wordsInUse);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * Ensures that the BitSet can hold enough words.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @param wordsRequired the minimum acceptable number of words.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    private void ensureCapacity(int wordsRequired) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        if (words.length < wordsRequired) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            // Allocate larger of doubled size or required size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            int request = Math.max(2 * words.length, wordsRequired);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            words = Arrays.copyOf(words, request);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            sizeIsSticky = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * Ensures that the BitSet can accommodate a given wordIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * temporarily violating the invariants.  The caller must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * restore the invariants before returning to the user,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * possibly using recalculateWordsInUse().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @param wordIndex the index to be accommodated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    private void expandTo(int wordIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        int wordsRequired = wordIndex+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        if (wordsInUse < wordsRequired) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            ensureCapacity(wordsRequired);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            wordsInUse = wordsRequired;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * Checks that fromIndex ... toIndex is a valid range of bit indices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    private static void checkRange(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        if (fromIndex < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            throw new IndexOutOfBoundsException("fromIndex < 0: " + fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        if (toIndex < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            throw new IndexOutOfBoundsException("toIndex < 0: " + toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        if (fromIndex > toIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            throw new IndexOutOfBoundsException("fromIndex: " + fromIndex +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                                                " > toIndex: " + toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * Sets the bit at the specified index to the complement of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * current value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @param  bitIndex the index of the bit to flip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @throws IndexOutOfBoundsException if the specified index is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    public void flip(int bitIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        if (bitIndex < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            throw new IndexOutOfBoundsException("bitIndex < 0: " + bitIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        int wordIndex = wordIndex(bitIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        expandTo(wordIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        words[wordIndex] ^= (1L << bitIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        recalculateWordsInUse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * Sets each bit from the specified {@code fromIndex} (inclusive) to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * specified {@code toIndex} (exclusive) to the complement of its current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * @param  fromIndex index of the first bit to flip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @param  toIndex index after the last bit to flip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * @throws IndexOutOfBoundsException if {@code fromIndex} is negative,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     *         or {@code toIndex} is negative, or {@code fromIndex} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *         larger than {@code toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    public void flip(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        checkRange(fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        if (fromIndex == toIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        int startWordIndex = wordIndex(fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        int endWordIndex   = wordIndex(toIndex - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        expandTo(endWordIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        long firstWordMask = WORD_MASK << fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        long lastWordMask  = WORD_MASK >>> -toIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        if (startWordIndex == endWordIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            // Case 1: One word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            words[startWordIndex] ^= (firstWordMask & lastWordMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            // Case 2: Multiple words
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            // Handle first word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            words[startWordIndex] ^= firstWordMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            // Handle intermediate words, if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            for (int i = startWordIndex+1; i < endWordIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                words[i] ^= WORD_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            // Handle last word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            words[endWordIndex] ^= lastWordMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        recalculateWordsInUse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * Sets the bit at the specified index to {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * @param  bitIndex a bit index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * @throws IndexOutOfBoundsException if the specified index is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * @since  JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    public void set(int bitIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        if (bitIndex < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            throw new IndexOutOfBoundsException("bitIndex < 0: " + bitIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        int wordIndex = wordIndex(bitIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        expandTo(wordIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        words[wordIndex] |= (1L << bitIndex); // Restores invariants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * Sets the bit at the specified index to the specified value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * @param  bitIndex a bit index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @param  value a boolean value to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * @throws IndexOutOfBoundsException if the specified index is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    public void set(int bitIndex, boolean value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        if (value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            set(bitIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            clear(bitIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * Sets the bits from the specified {@code fromIndex} (inclusive) to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * specified {@code toIndex} (exclusive) to {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * @param  fromIndex index of the first bit to be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * @param  toIndex index after the last bit to be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * @throws IndexOutOfBoundsException if {@code fromIndex} is negative,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *         or {@code toIndex} is negative, or {@code fromIndex} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     *         larger than {@code toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    public void set(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        checkRange(fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        if (fromIndex == toIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        // Increase capacity if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        int startWordIndex = wordIndex(fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        int endWordIndex   = wordIndex(toIndex - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        expandTo(endWordIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        long firstWordMask = WORD_MASK << fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        long lastWordMask  = WORD_MASK >>> -toIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        if (startWordIndex == endWordIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            // Case 1: One word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            words[startWordIndex] |= (firstWordMask & lastWordMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            // Case 2: Multiple words
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            // Handle first word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            words[startWordIndex] |= firstWordMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            // Handle intermediate words, if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            for (int i = startWordIndex+1; i < endWordIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                words[i] = WORD_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            // Handle last word (restores invariants)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            words[endWordIndex] |= lastWordMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * Sets the bits from the specified {@code fromIndex} (inclusive) to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * specified {@code toIndex} (exclusive) to the specified value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * @param  fromIndex index of the first bit to be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * @param  toIndex index after the last bit to be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * @param  value value to set the selected bits to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * @throws IndexOutOfBoundsException if {@code fromIndex} is negative,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     *         or {@code toIndex} is negative, or {@code fromIndex} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     *         larger than {@code toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    public void set(int fromIndex, int toIndex, boolean value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        if (value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            set(fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            clear(fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * Sets the bit specified by the index to {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * @param  bitIndex the index of the bit to be cleared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * @throws IndexOutOfBoundsException if the specified index is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * @since  JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    public void clear(int bitIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        if (bitIndex < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            throw new IndexOutOfBoundsException("bitIndex < 0: " + bitIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        int wordIndex = wordIndex(bitIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        if (wordIndex >= wordsInUse)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        words[wordIndex] &= ~(1L << bitIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        recalculateWordsInUse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * Sets the bits from the specified {@code fromIndex} (inclusive) to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * specified {@code toIndex} (exclusive) to {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * @param  fromIndex index of the first bit to be cleared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * @param  toIndex index after the last bit to be cleared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * @throws IndexOutOfBoundsException if {@code fromIndex} is negative,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     *         or {@code toIndex} is negative, or {@code fromIndex} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     *         larger than {@code toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    public void clear(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        checkRange(fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        if (fromIndex == toIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        int startWordIndex = wordIndex(fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        if (startWordIndex >= wordsInUse)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        int endWordIndex = wordIndex(toIndex - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        if (endWordIndex >= wordsInUse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            toIndex = length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            endWordIndex = wordsInUse - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        long firstWordMask = WORD_MASK << fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        long lastWordMask  = WORD_MASK >>> -toIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        if (startWordIndex == endWordIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            // Case 1: One word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            words[startWordIndex] &= ~(firstWordMask & lastWordMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            // Case 2: Multiple words
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            // Handle first word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            words[startWordIndex] &= ~firstWordMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            // Handle intermediate words, if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            for (int i = startWordIndex+1; i < endWordIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                words[i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            // Handle last word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            words[endWordIndex] &= ~lastWordMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        recalculateWordsInUse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * Sets all of the bits in this BitSet to {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        while (wordsInUse > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            words[--wordsInUse] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * Returns the value of the bit with the specified index. The value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * is {@code true} if the bit with the index {@code bitIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * is currently set in this {@code BitSet}; otherwise, the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * is {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * @param  bitIndex   the bit index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * @return the value of the bit with the specified index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * @throws IndexOutOfBoundsException if the specified index is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    public boolean get(int bitIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        if (bitIndex < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            throw new IndexOutOfBoundsException("bitIndex < 0: " + bitIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        int wordIndex = wordIndex(bitIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        return (wordIndex < wordsInUse)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            && ((words[wordIndex] & (1L << bitIndex)) != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * Returns a new {@code BitSet} composed of bits from this {@code BitSet}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * from {@code fromIndex} (inclusive) to {@code toIndex} (exclusive).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * @param  fromIndex index of the first bit to include
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * @param  toIndex index after the last bit to include
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * @return a new {@code BitSet} from a range of this {@code BitSet}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * @throws IndexOutOfBoundsException if {@code fromIndex} is negative,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     *         or {@code toIndex} is negative, or {@code fromIndex} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     *         larger than {@code toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    public BitSet get(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        checkRange(fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        int len = length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        // If no set bits in range return empty bitset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        if (len <= fromIndex || fromIndex == toIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            return new BitSet(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        // An optimization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        if (toIndex > len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            toIndex = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        BitSet result = new BitSet(toIndex - fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        int targetWords = wordIndex(toIndex - fromIndex - 1) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        int sourceIndex = wordIndex(fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        boolean wordAligned = ((fromIndex & BIT_INDEX_MASK) == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        // Process all words but the last word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        for (int i = 0; i < targetWords - 1; i++, sourceIndex++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            result.words[i] = wordAligned ? words[sourceIndex] :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                (words[sourceIndex] >>> fromIndex) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                (words[sourceIndex+1] << -fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        // Process the last word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        long lastWordMask = WORD_MASK >>> -toIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        result.words[targetWords - 1] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            ((toIndex-1) & BIT_INDEX_MASK) < (fromIndex & BIT_INDEX_MASK)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            ? /* straddles source words */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            ((words[sourceIndex] >>> fromIndex) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
             (words[sourceIndex+1] & lastWordMask) << -fromIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            ((words[sourceIndex] & lastWordMask) >>> fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        // Set wordsInUse correctly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        result.wordsInUse = targetWords;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        result.recalculateWordsInUse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        result.checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * Returns the index of the first bit that is set to {@code true}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * that occurs on or after the specified starting index. If no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * bit exists then {@code -1} is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * <p>To iterate over the {@code true} bits in a {@code BitSet},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * use the following loop:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     *     // operate on index i here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * @param  fromIndex the index to start checking from (inclusive)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * @return the index of the next set bit, or {@code -1} if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     *         is no such bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * @throws IndexOutOfBoundsException if the specified index is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    public int nextSetBit(int fromIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        if (fromIndex < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            throw new IndexOutOfBoundsException("fromIndex < 0: " + fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        int u = wordIndex(fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        if (u >= wordsInUse)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        long word = words[u] & (WORD_MASK << fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            if (word != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                return (u * BITS_PER_WORD) + Long.numberOfTrailingZeros(word);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            if (++u == wordsInUse)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            word = words[u];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * Returns the index of the first bit that is set to {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * that occurs on or after the specified starting index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * @param  fromIndex the index to start checking from (inclusive)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * @return the index of the next clear bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * @throws IndexOutOfBoundsException if the specified index is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    public int nextClearBit(int fromIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        // Neither spec nor implementation handle bitsets of maximal length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        // See 4816253.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        if (fromIndex < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            throw new IndexOutOfBoundsException("fromIndex < 0: " + fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        int u = wordIndex(fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        if (u >= wordsInUse)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            return fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        long word = ~words[u] & (WORD_MASK << fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            if (word != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                return (u * BITS_PER_WORD) + Long.numberOfTrailingZeros(word);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            if (++u == wordsInUse)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                return wordsInUse * BITS_PER_WORD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            word = ~words[u];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * Returns the index of the nearest bit that is set to {@code true}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * that occurs on or before the specified starting index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * If no such bit exists, or if {@code -1} is given as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * starting index, then {@code -1} is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * <p>To iterate over the {@code true} bits in a {@code BitSet},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * use the following loop:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * for (int i = bs.length(); (i = bs.previousSetBit(i-1)) >= 0; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     *     // operate on index i here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * @param  fromIndex the index to start checking from (inclusive)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * @return the index of the previous set bit, or {@code -1} if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     *         is no such bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * @throws IndexOutOfBoundsException if the specified index is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     *         than {@code -1}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * @since  1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    public int previousSetBit(int fromIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        if (fromIndex < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            if (fromIndex == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            throw new IndexOutOfBoundsException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                "fromIndex < -1: " + fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        int u = wordIndex(fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        if (u >= wordsInUse)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            return length() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        long word = words[u] & (WORD_MASK >>> -(fromIndex+1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            if (word != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                return (u+1) * BITS_PER_WORD - 1 - Long.numberOfLeadingZeros(word);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            if (u-- == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            word = words[u];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * Returns the index of the nearest bit that is set to {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * that occurs on or before the specified starting index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * If no such bit exists, or if {@code -1} is given as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * starting index, then {@code -1} is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * @param  fromIndex the index to start checking from (inclusive)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * @return the index of the previous clear bit, or {@code -1} if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     *         is no such bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * @throws IndexOutOfBoundsException if the specified index is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     *         than {@code -1}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * @since  1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    public int previousClearBit(int fromIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        if (fromIndex < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            if (fromIndex == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            throw new IndexOutOfBoundsException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                "fromIndex < -1: " + fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        int u = wordIndex(fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        if (u >= wordsInUse)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            return fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        long word = ~words[u] & (WORD_MASK >>> -(fromIndex+1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            if (word != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                return (u+1) * BITS_PER_WORD -1 - Long.numberOfLeadingZeros(word);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            if (u-- == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            word = ~words[u];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * Returns the "logical size" of this {@code BitSet}: the index of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * the highest set bit in the {@code BitSet} plus one. Returns zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * if the {@code BitSet} contains no set bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * @return the logical size of this {@code BitSet}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    public int length() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        if (wordsInUse == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        return BITS_PER_WORD * (wordsInUse - 1) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            (BITS_PER_WORD - Long.numberOfLeadingZeros(words[wordsInUse - 1]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * Returns true if this {@code BitSet} contains no bits that are set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * to {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * @return boolean indicating whether this {@code BitSet} is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        return wordsInUse == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * Returns true if the specified {@code BitSet} has any bits set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * {@code true} that are also set to {@code true} in this {@code BitSet}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * @param  set {@code BitSet} to intersect with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * @return boolean indicating whether this {@code BitSet} intersects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     *         the specified {@code BitSet}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    public boolean intersects(BitSet set) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        for (int i = Math.min(wordsInUse, set.wordsInUse) - 1; i >= 0; i--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            if ((words[i] & set.words[i]) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * Returns the number of bits set to {@code true} in this {@code BitSet}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * @return the number of bits set to {@code true} in this {@code BitSet}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    public int cardinality() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        int sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        for (int i = 0; i < wordsInUse; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            sum += Long.bitCount(words[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        return sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * Performs a logical <b>AND</b> of this target bit set with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * argument bit set. This bit set is modified so that each bit in it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * has the value {@code true} if and only if it both initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * had the value {@code true} and the corresponding bit in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * bit set argument also had the value {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * @param set a bit set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    public void and(BitSet set) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        if (this == set)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        while (wordsInUse > set.wordsInUse)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            words[--wordsInUse] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        // Perform logical AND on words in common
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        for (int i = 0; i < wordsInUse; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            words[i] &= set.words[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        recalculateWordsInUse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * Performs a logical <b>OR</b> of this bit set with the bit set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * argument. This bit set is modified so that a bit in it has the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * value {@code true} if and only if it either already had the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * value {@code true} or the corresponding bit in the bit set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * argument has the value {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * @param set a bit set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    public void or(BitSet set) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        if (this == set)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        int wordsInCommon = Math.min(wordsInUse, set.wordsInUse);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        if (wordsInUse < set.wordsInUse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            ensureCapacity(set.wordsInUse);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            wordsInUse = set.wordsInUse;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        // Perform logical OR on words in common
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        for (int i = 0; i < wordsInCommon; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
            words[i] |= set.words[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        // Copy any remaining words
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        if (wordsInCommon < set.wordsInUse)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            System.arraycopy(set.words, wordsInCommon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                             words, wordsInCommon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                             wordsInUse - wordsInCommon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        // recalculateWordsInUse() is unnecessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * Performs a logical <b>XOR</b> of this bit set with the bit set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * argument. This bit set is modified so that a bit in it has the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * value {@code true} if and only if one of the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * statements holds:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * <li>The bit initially has the value {@code true}, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     *     corresponding bit in the argument has the value {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * <li>The bit initially has the value {@code false}, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     *     corresponding bit in the argument has the value {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * @param  set a bit set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    public void xor(BitSet set) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        int wordsInCommon = Math.min(wordsInUse, set.wordsInUse);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        if (wordsInUse < set.wordsInUse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            ensureCapacity(set.wordsInUse);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            wordsInUse = set.wordsInUse;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        // Perform logical XOR on words in common
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        for (int i = 0; i < wordsInCommon; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            words[i] ^= set.words[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        // Copy any remaining words
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        if (wordsInCommon < set.wordsInUse)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            System.arraycopy(set.words, wordsInCommon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                             words, wordsInCommon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                             set.wordsInUse - wordsInCommon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        recalculateWordsInUse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     * Clears all of the bits in this {@code BitSet} whose corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     * bit is set in the specified {@code BitSet}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * @param  set the {@code BitSet} with which to mask this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     *         {@code BitSet}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
    public void andNot(BitSet set) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        // Perform logical (a & !b) on words in common
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        for (int i = Math.min(wordsInUse, set.wordsInUse) - 1; i >= 0; i--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            words[i] &= ~set.words[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        recalculateWordsInUse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * Returns the hash code value for this bit set. The hash code depends
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * only on which bits are set within this {@code BitSet}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * <p>The hash code is defined to be the result of the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * calculation:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     *     long h = 1234;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     *     long[] words = toLongArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     *     for (int i = words.length; --i >= 0; )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     *         h ^= words[i] * (i + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     *     return (int)((h >> 32) ^ h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * Note that the hash code changes if the set of bits is altered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * @return the hash code value for this bit set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        long h = 1234;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        for (int i = wordsInUse; --i >= 0; )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            h ^= words[i] * (i + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        return (int)((h >> 32) ^ h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * Returns the number of bits of space actually in use by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * {@code BitSet} to represent bit values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * The maximum element in the set is the size - 1st element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     * @return the number of bits currently in this bit set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        return words.length * BITS_PER_WORD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * Compares this object against the specified object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * The result is {@code true} if and only if the argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     * not {@code null} and is a {@code Bitset} object that has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * exactly the same set of bits set to {@code true} as this bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * set. That is, for every nonnegative {@code int} index {@code k},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     * <pre>((BitSet)obj).get(k) == this.get(k)</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     * must be true. The current sizes of the two bit sets are not compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     * @param  obj the object to compare with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * @return {@code true} if the objects are the same;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     *         {@code false} otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * @see    #size()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        if (!(obj instanceof BitSet))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        if (this == obj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        BitSet set = (BitSet) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        set.checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        if (wordsInUse != set.wordsInUse)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        // Check words in use by both BitSets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        for (int i = 0; i < wordsInUse; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            if (words[i] != set.words[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * Cloning this {@code BitSet} produces a new {@code BitSet}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * that is equal to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     * The clone of the bit set is another bit set that has exactly the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * same bits set to {@code true} as this bit set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     * @return a clone of this bit set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * @see    #size()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        if (! sizeIsSticky)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
            trimToSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            BitSet result = (BitSet) super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            result.words = words.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            result.checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        } catch (CloneNotSupportedException e) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
  1095
            throw new InternalError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     * Attempts to reduce internal storage used for the bits in this bit set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * Calling this method may, but is not required to, affect the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * returned by a subsequent call to the {@link #size()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
    private void trimToSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        if (wordsInUse != words.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
            words = Arrays.copyOf(words, wordsInUse);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     * Save the state of the {@code BitSet} instance to a stream (i.e.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     * serialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
    private void writeObject(ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        if (! sizeIsSticky)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            trimToSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        ObjectOutputStream.PutField fields = s.putFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        fields.put("bits", words);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        s.writeFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
     * Reconstitute the {@code BitSet} instance from a stream (i.e.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     * deserialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        ObjectInputStream.GetField fields = s.readFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        words = (long[]) fields.get("bits", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        // Assume maximum length then find real length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        // because recalculateWordsInUse assumes maintenance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        // or reduction in logical size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        wordsInUse = words.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        recalculateWordsInUse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        sizeIsSticky = (words.length > 0 && words[words.length-1] == 0L); // heuristic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     * Returns a string representation of this bit set. For every index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     * for which this {@code BitSet} contains a bit in the set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * state, the decimal representation of that index is included in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * the result. Such indices are listed in order from lowest to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * highest, separated by ",&nbsp;" (a comma and a space) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     * surrounded by braces, resulting in the usual mathematical
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     * notation for a set of integers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     * <p>Example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * BitSet drPepper = new BitSet();</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * Now {@code drPepper.toString()} returns "{@code {}}".<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     * drPepper.set(2);</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * Now {@code drPepper.toString()} returns "{@code {2}}".<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     * drPepper.set(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     * drPepper.set(10);</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * Now {@code drPepper.toString()} returns "{@code {2, 4, 10}}".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     * @return a string representation of this bit set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        int numBits = (wordsInUse > 128) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
            cardinality() : wordsInUse * BITS_PER_WORD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        StringBuilder b = new StringBuilder(6*numBits + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        b.append('{');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        int i = nextSetBit(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        if (i != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
            b.append(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            for (i = nextSetBit(i+1); i >= 0; i = nextSetBit(i+1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
                int endOfRun = nextClearBit(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
                do { b.append(", ").append(i); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
                while (++i < endOfRun);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        b.append('}');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        return b.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
}