src/java.base/share/classes/java/util/BitSet.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 57956 e0b8b019d2f5
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
55274
8cd2d6dee328 8225397: Integer value miscalculation in toString() method of BitSet
igerasim
parents: 50610
diff changeset
     2
 * Copyright (c) 1995, 2019, 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;
42157
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
    32
import java.util.function.IntConsumer;
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
    33
import java.util.stream.IntStream;
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
    34
import java.util.stream.StreamSupport;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * This class implements a vector of bits that grows as needed. Each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * component of the bit set has a {@code boolean} value. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * bits of a {@code BitSet} are indexed by nonnegative integers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Individual indexed bits can be examined, set, or cleared. One
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * {@code BitSet} may be used to modify the contents of another
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * {@code BitSet} through logical AND, logical inclusive OR, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * logical exclusive OR operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p>By default, all bits in the set initially have the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p>Every bit set has a current size, which is the number of bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * of space currently in use by the bit set. Note that the size is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * related to the implementation of a bit set, so it may change with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * implementation. The length of a bit set relates to logical length
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * of a bit set and is defined independently of implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <p>Unless otherwise noted, passing a null parameter to any of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * methods in a {@code BitSet} will result in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * {@code NullPointerException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <p>A {@code BitSet} is not safe for multithreaded use without
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * external synchronization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @author  Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @author  Michael McCloskey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @author  Martin Buchholz
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24257
diff changeset
    64
 * @since   1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
public class BitSet implements Cloneable, java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * BitSets are packed into arrays of "words."  Currently a word is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * a long, which consists of 64 bits, requiring 6 address bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * The choice of word size is determined purely by performance concerns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29816
diff changeset
    72
    private static final int ADDRESS_BITS_PER_WORD = 6;
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29816
diff changeset
    73
    private static final int BITS_PER_WORD = 1 << ADDRESS_BITS_PER_WORD;
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 29816
diff changeset
    74
    private static final int BIT_INDEX_MASK = BITS_PER_WORD - 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /* Used to shift left or right for a partial word mask */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private static final long WORD_MASK = 0xffffffffffffffffL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @serialField bits long[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * The bits in this BitSet.  The ith bit is stored in bits[i/64] at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * bit position i % 64 (where bit position 0 refers to the least
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * significant bit and 63 refers to the most significant bit).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 55274
diff changeset
    86
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private static final ObjectStreamField[] serialPersistentFields = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        new ObjectStreamField("bits", long[].class),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * The internal field corresponding to the serialField "bits".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private long[] words;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * The number of words in the logical size of this BitSet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private transient int wordsInUse = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Whether the size of "words" is user-specified.  If so, we assume
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * the user knows what he's doing and try harder to preserve it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private transient boolean sizeIsSticky = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /* use serialVersionUID from JDK 1.0.2 for interoperability */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 55274
diff changeset
   108
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private static final long serialVersionUID = 7997698588986878753L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Given a bit index, return word index containing it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private static int wordIndex(int bitIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return bitIndex >> ADDRESS_BITS_PER_WORD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Every public method must preserve these invariants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private void checkInvariants() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        assert(wordsInUse == 0 || words[wordsInUse - 1] != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        assert(wordsInUse >= 0 && wordsInUse <= words.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        assert(wordsInUse == words.length || words[wordsInUse] == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Sets the field wordsInUse to the logical size in words of the bit set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * WARNING:This method assumes that the number of words actually in use is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * less than or equal to the current value of wordsInUse!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private void recalculateWordsInUse() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        // Traverse the bitset until a used word is found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        for (i = wordsInUse-1; i >= 0; i--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            if (words[i] != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        wordsInUse = i+1; // The new logical size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Creates a new bit set. All bits are initially {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public BitSet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        initWords(BITS_PER_WORD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        sizeIsSticky = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * Creates a bit set whose initial size is large enough to explicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * represent bits with indices in the range {@code 0} through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * {@code nbits-1}. All bits are initially {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param  nbits the initial size of the bit set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @throws NegativeArraySizeException if the specified initial size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *         is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public BitSet(int nbits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        // nbits can't be negative; size 0 is OK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (nbits < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            throw new NegativeArraySizeException("nbits < 0: " + nbits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        initWords(nbits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        sizeIsSticky = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    private void initWords(int nbits) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        words = new long[wordIndex(nbits-1) + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * Creates a bit set using words as the internal representation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * The last word (if there is one) must be non-zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    private BitSet(long[] words) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        this.words = words;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        this.wordsInUse = words.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Returns a new bit set containing all the bits in the given long array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * <p>More precisely,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * <br>{@code BitSet.valueOf(longs).get(n) == ((longs[n/64] & (1L<<(n%64))) != 0)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * <br>for all {@code n < 64 * longs.length}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * <p>This method is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * {@code BitSet.valueOf(LongBuffer.wrap(longs))}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @param longs a long array containing a little-endian representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *        of a sequence of bits to be used as the initial bits of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *        new bit set
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
   195
     * @return a {@code BitSet} containing all the bits in the long array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public static BitSet valueOf(long[] longs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        for (n = longs.length; n > 0 && longs[n - 1] == 0; n--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        return new BitSet(Arrays.copyOf(longs, n));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * Returns a new bit set containing all the bits in the given long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * buffer between its position and limit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * <p>More precisely,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * <br>{@code BitSet.valueOf(lb).get(n) == ((lb.get(lb.position()+n/64) & (1L<<(n%64))) != 0)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * <br>for all {@code n < 64 * lb.remaining()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * <p>The long buffer is not modified by this method, and no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * reference to the buffer is retained by the bit set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @param lb a long buffer containing a little-endian representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *        of a sequence of bits between its position and limit, to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *        used as the initial bits of the new bit set
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
   219
     * @return a {@code BitSet} containing all the bits in the buffer in the
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
   220
     *         specified range
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    public static BitSet valueOf(LongBuffer lb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        lb = lb.slice();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        for (n = lb.remaining(); n > 0 && lb.get(n - 1) == 0; n--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        long[] words = new long[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        lb.get(words);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        return new BitSet(words);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Returns a new bit set containing all the bits in the given byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * <p>More precisely,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * <br>{@code BitSet.valueOf(bytes).get(n) == ((bytes[n/8] & (1<<(n%8))) != 0)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * <br>for all {@code n <  8 * bytes.length}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * <p>This method is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * {@code BitSet.valueOf(ByteBuffer.wrap(bytes))}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @param bytes a byte array containing a little-endian
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *        representation of a sequence of bits to be used as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *        initial bits of the new bit set
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
   246
     * @return a {@code BitSet} containing all the bits in the byte array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public static BitSet valueOf(byte[] bytes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        return BitSet.valueOf(ByteBuffer.wrap(bytes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * Returns a new bit set containing all the bits in the given byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * buffer between its position and limit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * <p>More precisely,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * <br>{@code BitSet.valueOf(bb).get(n) == ((bb.get(bb.position()+n/8) & (1<<(n%8))) != 0)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * <br>for all {@code n < 8 * bb.remaining()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * <p>The byte buffer is not modified by this method, and no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * reference to the buffer is retained by the bit set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @param bb a byte buffer containing a little-endian representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *        of a sequence of bits between its position and limit, to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *        used as the initial bits of the new bit set
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
   267
     * @return a {@code BitSet} containing all the bits in the buffer in the
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18822
diff changeset
   268
     *         specified range
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    public static BitSet valueOf(ByteBuffer bb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        bb = bb.slice().order(ByteOrder.LITTLE_ENDIAN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        for (n = bb.remaining(); n > 0 && bb.get(n - 1) == 0; n--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        long[] words = new long[(n + 7) / 8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        bb.limit(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        while (bb.remaining() >= 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            words[i++] = bb.getLong();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        for (int remaining = bb.remaining(), j = 0; j < remaining; j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            words[i] |= (bb.get() & 0xffL) << (8 * j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        return new BitSet(words);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * Returns a new byte array containing all the bits in this bit set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * <p>More precisely, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * <br>{@code byte[] bytes = s.toByteArray();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * <br>then {@code bytes.length == (s.length()+7)/8} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * <br>{@code s.get(n) == ((bytes[n/8] & (1<<(n%8))) != 0)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * <br>for all {@code n < 8 * bytes.length}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @return a byte array containing a little-endian representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *         of all the bits in this bit set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    public byte[] toByteArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        int n = wordsInUse;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        if (n == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            return new byte[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        int len = 8 * (n-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        for (long x = words[n - 1]; x != 0; x >>>= 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            len++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        byte[] bytes = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        ByteBuffer bb = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        for (int i = 0; i < n - 1; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            bb.putLong(words[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        for (long x = words[n - 1]; x != 0; x >>>= 8)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            bb.put((byte) (x & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        return bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * Returns a new long array containing all the bits in this bit set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * <p>More precisely, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * <br>{@code long[] longs = s.toLongArray();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * <br>then {@code longs.length == (s.length()+63)/64} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * <br>{@code s.get(n) == ((longs[n/64] & (1L<<(n%64))) != 0)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * <br>for all {@code n < 64 * longs.length}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @return a long array containing a little-endian representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *         of all the bits in this bit set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    public long[] toLongArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        return Arrays.copyOf(words, wordsInUse);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * Ensures that the BitSet can hold enough words.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @param wordsRequired the minimum acceptable number of words.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    private void ensureCapacity(int wordsRequired) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        if (words.length < wordsRequired) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            // Allocate larger of doubled size or required size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            int request = Math.max(2 * words.length, wordsRequired);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            words = Arrays.copyOf(words, request);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            sizeIsSticky = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * Ensures that the BitSet can accommodate a given wordIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * temporarily violating the invariants.  The caller must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * restore the invariants before returning to the user,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * possibly using recalculateWordsInUse().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @param wordIndex the index to be accommodated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    private void expandTo(int wordIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        int wordsRequired = wordIndex+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        if (wordsInUse < wordsRequired) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            ensureCapacity(wordsRequired);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            wordsInUse = wordsRequired;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * Checks that fromIndex ... toIndex is a valid range of bit indices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    private static void checkRange(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        if (fromIndex < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            throw new IndexOutOfBoundsException("fromIndex < 0: " + fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        if (toIndex < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            throw new IndexOutOfBoundsException("toIndex < 0: " + toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        if (fromIndex > toIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            throw new IndexOutOfBoundsException("fromIndex: " + fromIndex +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                                                " > toIndex: " + toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * Sets the bit at the specified index to the complement of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * current value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @param  bitIndex the index of the bit to flip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * @throws IndexOutOfBoundsException if the specified index is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    public void flip(int bitIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        if (bitIndex < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            throw new IndexOutOfBoundsException("bitIndex < 0: " + bitIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        int wordIndex = wordIndex(bitIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        expandTo(wordIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        words[wordIndex] ^= (1L << bitIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        recalculateWordsInUse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * Sets each bit from the specified {@code fromIndex} (inclusive) to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * specified {@code toIndex} (exclusive) to the complement of its current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * @param  fromIndex index of the first bit to flip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @param  toIndex index after the last bit to flip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * @throws IndexOutOfBoundsException if {@code fromIndex} is negative,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *         or {@code toIndex} is negative, or {@code fromIndex} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *         larger than {@code toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    public void flip(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        checkRange(fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        if (fromIndex == toIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        int startWordIndex = wordIndex(fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        int endWordIndex   = wordIndex(toIndex - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        expandTo(endWordIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        long firstWordMask = WORD_MASK << fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        long lastWordMask  = WORD_MASK >>> -toIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        if (startWordIndex == endWordIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            // Case 1: One word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            words[startWordIndex] ^= (firstWordMask & lastWordMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            // Case 2: Multiple words
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            // Handle first word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            words[startWordIndex] ^= firstWordMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            // Handle intermediate words, if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            for (int i = startWordIndex+1; i < endWordIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                words[i] ^= WORD_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            // Handle last word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            words[endWordIndex] ^= lastWordMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        recalculateWordsInUse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * Sets the bit at the specified index to {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * @param  bitIndex a bit index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * @throws IndexOutOfBoundsException if the specified index is negative
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24257
diff changeset
   443
     * @since  1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    public void set(int bitIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        if (bitIndex < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            throw new IndexOutOfBoundsException("bitIndex < 0: " + bitIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        int wordIndex = wordIndex(bitIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        expandTo(wordIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        words[wordIndex] |= (1L << bitIndex); // Restores invariants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * Sets the bit at the specified index to the specified value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * @param  bitIndex a bit index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * @param  value a boolean value to set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * @throws IndexOutOfBoundsException if the specified index is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    public void set(int bitIndex, boolean value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        if (value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            set(bitIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            clear(bitIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * Sets the bits from the specified {@code fromIndex} (inclusive) to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * specified {@code toIndex} (exclusive) to {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * @param  fromIndex index of the first bit to be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * @param  toIndex index after the last bit to be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * @throws IndexOutOfBoundsException if {@code fromIndex} is negative,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     *         or {@code toIndex} is negative, or {@code fromIndex} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     *         larger than {@code toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    public void set(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        checkRange(fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        if (fromIndex == toIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        // Increase capacity if necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        int startWordIndex = wordIndex(fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        int endWordIndex   = wordIndex(toIndex - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        expandTo(endWordIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        long firstWordMask = WORD_MASK << fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        long lastWordMask  = WORD_MASK >>> -toIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        if (startWordIndex == endWordIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            // Case 1: One word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            words[startWordIndex] |= (firstWordMask & lastWordMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            // Case 2: Multiple words
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            // Handle first word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            words[startWordIndex] |= firstWordMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            // Handle intermediate words, if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            for (int i = startWordIndex+1; i < endWordIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                words[i] = WORD_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            // Handle last word (restores invariants)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            words[endWordIndex] |= lastWordMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * Sets the bits from the specified {@code fromIndex} (inclusive) to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * specified {@code toIndex} (exclusive) to the specified value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * @param  fromIndex index of the first bit to be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * @param  toIndex index after the last bit to be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * @param  value value to set the selected bits to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * @throws IndexOutOfBoundsException if {@code fromIndex} is negative,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     *         or {@code toIndex} is negative, or {@code fromIndex} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     *         larger than {@code toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    public void set(int fromIndex, int toIndex, boolean value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        if (value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            set(fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            clear(fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * Sets the bit specified by the index to {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * @param  bitIndex the index of the bit to be cleared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * @throws IndexOutOfBoundsException if the specified index is negative
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 24257
diff changeset
   539
     * @since  1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    public void clear(int bitIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        if (bitIndex < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            throw new IndexOutOfBoundsException("bitIndex < 0: " + bitIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        int wordIndex = wordIndex(bitIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        if (wordIndex >= wordsInUse)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        words[wordIndex] &= ~(1L << bitIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        recalculateWordsInUse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * Sets the bits from the specified {@code fromIndex} (inclusive) to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * specified {@code toIndex} (exclusive) to {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * @param  fromIndex index of the first bit to be cleared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * @param  toIndex index after the last bit to be cleared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * @throws IndexOutOfBoundsException if {@code fromIndex} is negative,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     *         or {@code toIndex} is negative, or {@code fromIndex} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     *         larger than {@code toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    public void clear(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        checkRange(fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        if (fromIndex == toIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        int startWordIndex = wordIndex(fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        if (startWordIndex >= wordsInUse)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        int endWordIndex = wordIndex(toIndex - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        if (endWordIndex >= wordsInUse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            toIndex = length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            endWordIndex = wordsInUse - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        long firstWordMask = WORD_MASK << fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        long lastWordMask  = WORD_MASK >>> -toIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        if (startWordIndex == endWordIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            // Case 1: One word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            words[startWordIndex] &= ~(firstWordMask & lastWordMask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            // Case 2: Multiple words
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            // Handle first word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            words[startWordIndex] &= ~firstWordMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            // Handle intermediate words, if any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            for (int i = startWordIndex+1; i < endWordIndex; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                words[i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            // Handle last word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            words[endWordIndex] &= ~lastWordMask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        recalculateWordsInUse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * Sets all of the bits in this BitSet to {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        while (wordsInUse > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            words[--wordsInUse] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * Returns the value of the bit with the specified index. The value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * is {@code true} if the bit with the index {@code bitIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * is currently set in this {@code BitSet}; otherwise, the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * is {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * @param  bitIndex   the bit index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * @return the value of the bit with the specified index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * @throws IndexOutOfBoundsException if the specified index is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    public boolean get(int bitIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        if (bitIndex < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            throw new IndexOutOfBoundsException("bitIndex < 0: " + bitIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        int wordIndex = wordIndex(bitIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        return (wordIndex < wordsInUse)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            && ((words[wordIndex] & (1L << bitIndex)) != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * Returns a new {@code BitSet} composed of bits from this {@code BitSet}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * from {@code fromIndex} (inclusive) to {@code toIndex} (exclusive).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * @param  fromIndex index of the first bit to include
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * @param  toIndex index after the last bit to include
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * @return a new {@code BitSet} from a range of this {@code BitSet}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * @throws IndexOutOfBoundsException if {@code fromIndex} is negative,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     *         or {@code toIndex} is negative, or {@code fromIndex} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     *         larger than {@code toIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    public BitSet get(int fromIndex, int toIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        checkRange(fromIndex, toIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        int len = length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        // If no set bits in range return empty bitset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        if (len <= fromIndex || fromIndex == toIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            return new BitSet(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        // An optimization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        if (toIndex > len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            toIndex = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        BitSet result = new BitSet(toIndex - fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        int targetWords = wordIndex(toIndex - fromIndex - 1) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        int sourceIndex = wordIndex(fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        boolean wordAligned = ((fromIndex & BIT_INDEX_MASK) == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        // Process all words but the last word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        for (int i = 0; i < targetWords - 1; i++, sourceIndex++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            result.words[i] = wordAligned ? words[sourceIndex] :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                (words[sourceIndex] >>> fromIndex) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                (words[sourceIndex+1] << -fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        // Process the last word
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        long lastWordMask = WORD_MASK >>> -toIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        result.words[targetWords - 1] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            ((toIndex-1) & BIT_INDEX_MASK) < (fromIndex & BIT_INDEX_MASK)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            ? /* straddles source words */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            ((words[sourceIndex] >>> fromIndex) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
             (words[sourceIndex+1] & lastWordMask) << -fromIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            ((words[sourceIndex] & lastWordMask) >>> fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        // Set wordsInUse correctly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        result.wordsInUse = targetWords;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        result.recalculateWordsInUse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        result.checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * Returns the index of the first bit that is set to {@code true}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * that occurs on or after the specified starting index. If no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * bit exists then {@code -1} is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * <p>To iterate over the {@code true} bits in a {@code BitSet},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * use the following loop:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     *     // operate on index i here
24257
f524e23d7f7b 8040806: BitSet.toString() can throw IndexOutOfBoundsException
igerasim
parents: 23010
diff changeset
   702
     *     if (i == Integer.MAX_VALUE) {
f524e23d7f7b 8040806: BitSet.toString() can throw IndexOutOfBoundsException
igerasim
parents: 23010
diff changeset
   703
     *         break; // or (i+1) would overflow
f524e23d7f7b 8040806: BitSet.toString() can throw IndexOutOfBoundsException
igerasim
parents: 23010
diff changeset
   704
     *     }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * @param  fromIndex the index to start checking from (inclusive)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * @return the index of the next set bit, or {@code -1} if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     *         is no such bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * @throws IndexOutOfBoundsException if the specified index is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    public int nextSetBit(int fromIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        if (fromIndex < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            throw new IndexOutOfBoundsException("fromIndex < 0: " + fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        int u = wordIndex(fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        if (u >= wordsInUse)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        long word = words[u] & (WORD_MASK << fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
            if (word != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                return (u * BITS_PER_WORD) + Long.numberOfTrailingZeros(word);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            if (++u == wordsInUse)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            word = words[u];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * Returns the index of the first bit that is set to {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * that occurs on or after the specified starting index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * @param  fromIndex the index to start checking from (inclusive)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * @return the index of the next clear bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * @throws IndexOutOfBoundsException if the specified index is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    public int nextClearBit(int fromIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        // Neither spec nor implementation handle bitsets of maximal length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        // See 4816253.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        if (fromIndex < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            throw new IndexOutOfBoundsException("fromIndex < 0: " + fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        int u = wordIndex(fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        if (u >= wordsInUse)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            return fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        long word = ~words[u] & (WORD_MASK << fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            if (word != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                return (u * BITS_PER_WORD) + Long.numberOfTrailingZeros(word);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            if (++u == wordsInUse)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                return wordsInUse * BITS_PER_WORD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            word = ~words[u];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * Returns the index of the nearest bit that is set to {@code true}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * that occurs on or before the specified starting index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * If no such bit exists, or if {@code -1} is given as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * starting index, then {@code -1} is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * <p>To iterate over the {@code true} bits in a {@code BitSet},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * use the following loop:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * for (int i = bs.length(); (i = bs.previousSetBit(i-1)) >= 0; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     *     // operate on index i here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * @param  fromIndex the index to start checking from (inclusive)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * @return the index of the previous set bit, or {@code -1} if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     *         is no such bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * @throws IndexOutOfBoundsException if the specified index is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     *         than {@code -1}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * @since  1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    public int previousSetBit(int fromIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        if (fromIndex < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            if (fromIndex == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            throw new IndexOutOfBoundsException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                "fromIndex < -1: " + fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        int u = wordIndex(fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        if (u >= wordsInUse)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            return length() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        long word = words[u] & (WORD_MASK >>> -(fromIndex+1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            if (word != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                return (u+1) * BITS_PER_WORD - 1 - Long.numberOfLeadingZeros(word);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            if (u-- == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            word = words[u];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * Returns the index of the nearest bit that is set to {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     * that occurs on or before the specified starting index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * If no such bit exists, or if {@code -1} is given as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * starting index, then {@code -1} is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * @param  fromIndex the index to start checking from (inclusive)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * @return the index of the previous clear bit, or {@code -1} if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     *         is no such bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * @throws IndexOutOfBoundsException if the specified index is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     *         than {@code -1}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * @since  1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    public int previousClearBit(int fromIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        if (fromIndex < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            if (fromIndex == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            throw new IndexOutOfBoundsException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                "fromIndex < -1: " + fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        int u = wordIndex(fromIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        if (u >= wordsInUse)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            return fromIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        long word = ~words[u] & (WORD_MASK >>> -(fromIndex+1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            if (word != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                return (u+1) * BITS_PER_WORD -1 - Long.numberOfLeadingZeros(word);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            if (u-- == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            word = ~words[u];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * Returns the "logical size" of this {@code BitSet}: the index of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * the highest set bit in the {@code BitSet} plus one. Returns zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * if the {@code BitSet} contains no set bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * @return the logical size of this {@code BitSet}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    public int length() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        if (wordsInUse == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        return BITS_PER_WORD * (wordsInUse - 1) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            (BITS_PER_WORD - Long.numberOfLeadingZeros(words[wordsInUse - 1]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * Returns true if this {@code BitSet} contains no bits that are set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * to {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * @return boolean indicating whether this {@code BitSet} is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        return wordsInUse == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * Returns true if the specified {@code BitSet} has any bits set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * {@code true} that are also set to {@code true} in this {@code BitSet}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * @param  set {@code BitSet} to intersect with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * @return boolean indicating whether this {@code BitSet} intersects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     *         the specified {@code BitSet}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
    public boolean intersects(BitSet set) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        for (int i = Math.min(wordsInUse, set.wordsInUse) - 1; i >= 0; i--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            if ((words[i] & set.words[i]) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * Returns the number of bits set to {@code true} in this {@code BitSet}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * @return the number of bits set to {@code true} in this {@code BitSet}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * @since  1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    public int cardinality() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        int sum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        for (int i = 0; i < wordsInUse; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            sum += Long.bitCount(words[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        return sum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * Performs a logical <b>AND</b> of this target bit set with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * argument bit set. This bit set is modified so that each bit in it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * has the value {@code true} if and only if it both initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * had the value {@code true} and the corresponding bit in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * bit set argument also had the value {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     * @param set a bit set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    public void and(BitSet set) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        if (this == set)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        while (wordsInUse > set.wordsInUse)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            words[--wordsInUse] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        // Perform logical AND on words in common
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        for (int i = 0; i < wordsInUse; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            words[i] &= set.words[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        recalculateWordsInUse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * Performs a logical <b>OR</b> of this bit set with the bit set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * argument. This bit set is modified so that a bit in it has the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * value {@code true} if and only if it either already had the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * value {@code true} or the corresponding bit in the bit set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     * argument has the value {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * @param set a bit set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    public void or(BitSet set) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        if (this == set)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        int wordsInCommon = Math.min(wordsInUse, set.wordsInUse);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        if (wordsInUse < set.wordsInUse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            ensureCapacity(set.wordsInUse);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            wordsInUse = set.wordsInUse;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        // Perform logical OR on words in common
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        for (int i = 0; i < wordsInCommon; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            words[i] |= set.words[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        // Copy any remaining words
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        if (wordsInCommon < set.wordsInUse)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            System.arraycopy(set.words, wordsInCommon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                             words, wordsInCommon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                             wordsInUse - wordsInCommon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        // recalculateWordsInUse() is unnecessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * Performs a logical <b>XOR</b> of this bit set with the bit set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * argument. This bit set is modified so that a bit in it has the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * value {@code true} if and only if one of the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * statements holds:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * <li>The bit initially has the value {@code true}, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     *     corresponding bit in the argument has the value {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * <li>The bit initially has the value {@code false}, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     *     corresponding bit in the argument has the value {@code true}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * @param  set a bit set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
    public void xor(BitSet set) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        int wordsInCommon = Math.min(wordsInUse, set.wordsInUse);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        if (wordsInUse < set.wordsInUse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
            ensureCapacity(set.wordsInUse);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            wordsInUse = set.wordsInUse;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        // Perform logical XOR on words in common
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        for (int i = 0; i < wordsInCommon; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            words[i] ^= set.words[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        // Copy any remaining words
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        if (wordsInCommon < set.wordsInUse)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            System.arraycopy(set.words, wordsInCommon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                             words, wordsInCommon,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                             set.wordsInUse - wordsInCommon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        recalculateWordsInUse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * Clears all of the bits in this {@code BitSet} whose corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * bit is set in the specified {@code BitSet}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * @param  set the {@code BitSet} with which to mask this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     *         {@code BitSet}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
    public void andNot(BitSet set) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        // Perform logical (a & !b) on words in common
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        for (int i = Math.min(wordsInUse, set.wordsInUse) - 1; i >= 0; i--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            words[i] &= ~set.words[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        recalculateWordsInUse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * Returns the hash code value for this bit set. The hash code depends
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * only on which bits are set within this {@code BitSet}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     * <p>The hash code is defined to be the result of the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * calculation:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     *  <pre> {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     *     long h = 1234;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     *     long[] words = toLongArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     *     for (int i = words.length; --i >= 0; )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     *         h ^= words[i] * (i + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     *     return (int)((h >> 32) ^ h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * }}</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * Note that the hash code changes if the set of bits is altered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     * @return the hash code value for this bit set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        long h = 1234;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        for (int i = wordsInUse; --i >= 0; )
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            h ^= words[i] * (i + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        return (int)((h >> 32) ^ h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * Returns the number of bits of space actually in use by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * {@code BitSet} to represent bit values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     * The maximum element in the set is the size - 1st element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     * @return the number of bits currently in this bit set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        return words.length * BITS_PER_WORD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * Compares this object against the specified object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * The result is {@code true} if and only if the argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * not {@code null} and is a {@code Bitset} object that has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * exactly the same set of bits set to {@code true} as this bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * set. That is, for every nonnegative {@code int} index {@code k},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * <pre>((BitSet)obj).get(k) == this.get(k)</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * must be true. The current sizes of the two bit sets are not compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * @param  obj the object to compare with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     * @return {@code true} if the objects are the same;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     *         {@code false} otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * @see    #size()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        if (!(obj instanceof BitSet))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        if (this == obj)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        BitSet set = (BitSet) obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        set.checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        if (wordsInUse != set.wordsInUse)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        // Check words in use by both BitSets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        for (int i = 0; i < wordsInUse; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            if (words[i] != set.words[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * Cloning this {@code BitSet} produces a new {@code BitSet}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * that is equal to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * The clone of the bit set is another bit set that has exactly the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     * same bits set to {@code true} as this bit set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * @return a clone of this bit set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     * @see    #size()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        if (! sizeIsSticky)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
            trimToSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            BitSet result = (BitSet) super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            result.words = words.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
            result.checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        } catch (CloneNotSupportedException e) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
  1109
            throw new InternalError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     * Attempts to reduce internal storage used for the bits in this bit set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     * Calling this method may, but is not required to, affect the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     * returned by a subsequent call to the {@link #size()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
    private void trimToSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        if (wordsInUse != words.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
            words = Arrays.copyOf(words, wordsInUse);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * Save the state of the {@code BitSet} instance to a stream (i.e.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     * serialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 55274
diff changeset
  1129
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
    private void writeObject(ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        if (! sizeIsSticky)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
            trimToSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        ObjectOutputStream.PutField fields = s.putFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        fields.put("bits", words);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        s.writeFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * Reconstitute the {@code BitSet} instance from a stream (i.e.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * deserialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 55274
diff changeset
  1147
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        ObjectInputStream.GetField fields = s.readFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
        words = (long[]) fields.get("bits", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        // Assume maximum length then find real length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        // because recalculateWordsInUse assumes maintenance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        // or reduction in logical size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        wordsInUse = words.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        recalculateWordsInUse();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        sizeIsSticky = (words.length > 0 && words[words.length-1] == 0L); // heuristic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     * Returns a string representation of this bit set. For every index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     * for which this {@code BitSet} contains a bit in the set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * state, the decimal representation of that index is included in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     * the result. Such indices are listed in order from lowest to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     * highest, separated by ",&nbsp;" (a comma and a space) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * surrounded by braces, resulting in the usual mathematical
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * notation for a set of integers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * <p>Example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * BitSet drPepper = new BitSet();</pre>
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 18829
diff changeset
  1175
     * Now {@code drPepper.toString()} returns "{@code {}}".
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * drPepper.set(2);</pre>
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 18829
diff changeset
  1178
     * Now {@code drPepper.toString()} returns "{@code {2}}".
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     * drPepper.set(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     * drPepper.set(10);</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     * Now {@code drPepper.toString()} returns "{@code {2, 4, 10}}".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * @return a string representation of this bit set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        checkInvariants();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
55274
8cd2d6dee328 8225397: Integer value miscalculation in toString() method of BitSet
igerasim
parents: 50610
diff changeset
  1189
        final int MAX_INITIAL_CAPACITY = Integer.MAX_VALUE - 8;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        int numBits = (wordsInUse > 128) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            cardinality() : wordsInUse * BITS_PER_WORD;
55274
8cd2d6dee328 8225397: Integer value miscalculation in toString() method of BitSet
igerasim
parents: 50610
diff changeset
  1192
        // Avoid overflow in the case of a humongous numBits
8cd2d6dee328 8225397: Integer value miscalculation in toString() method of BitSet
igerasim
parents: 50610
diff changeset
  1193
        int initialCapacity = (numBits <= (MAX_INITIAL_CAPACITY - 2) / 6) ?
8cd2d6dee328 8225397: Integer value miscalculation in toString() method of BitSet
igerasim
parents: 50610
diff changeset
  1194
            6 * numBits + 2 : MAX_INITIAL_CAPACITY;
8cd2d6dee328 8225397: Integer value miscalculation in toString() method of BitSet
igerasim
parents: 50610
diff changeset
  1195
        StringBuilder b = new StringBuilder(initialCapacity);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        b.append('{');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        int i = nextSetBit(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        if (i != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            b.append(i);
24257
f524e23d7f7b 8040806: BitSet.toString() can throw IndexOutOfBoundsException
igerasim
parents: 23010
diff changeset
  1201
            while (true) {
f524e23d7f7b 8040806: BitSet.toString() can throw IndexOutOfBoundsException
igerasim
parents: 23010
diff changeset
  1202
                if (++i < 0) break;
f524e23d7f7b 8040806: BitSet.toString() can throw IndexOutOfBoundsException
igerasim
parents: 23010
diff changeset
  1203
                if ((i = nextSetBit(i)) < 0) break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
                int endOfRun = nextClearBit(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                do { b.append(", ").append(i); }
24257
f524e23d7f7b 8040806: BitSet.toString() can throw IndexOutOfBoundsException
igerasim
parents: 23010
diff changeset
  1206
                while (++i != endOfRun);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        b.append('}');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        return b.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
    }
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
  1213
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
  1214
    /**
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
  1215
     * Returns a stream of indices for which this {@code BitSet}
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
  1216
     * contains a bit in the set state. The indices are returned
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
  1217
     * in order, from lowest to highest. The size of the stream
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
  1218
     * is the number of bits in the set state, equal to the value
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
  1219
     * returned by the {@link #cardinality()} method.
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
  1220
     *
42225
07097c84db68 8169808: Stream returning methods should specify if they are late binding
psandoz
parents: 42157
diff changeset
  1221
     * <p>The stream binds to this bit set when the terminal stream operation
07097c84db68 8169808: Stream returning methods should specify if they are late binding
psandoz
parents: 42157
diff changeset
  1222
     * commences (specifically, the spliterator for the stream is
45536
7ebbcd3c853f 8181824: Broken javadoc link in java.util.BitSet
psandoz
parents: 42225
diff changeset
  1223
     * <a href="Spliterator.html#binding"><em>late-binding</em></a>).  If the
42225
07097c84db68 8169808: Stream returning methods should specify if they are late binding
psandoz
parents: 42157
diff changeset
  1224
     * bit set is modified during that operation then the result is undefined.
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
  1225
     *
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
  1226
     * @return a stream of integers representing set indices
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
  1227
     * @since 1.8
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
  1228
     */
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
  1229
    public IntStream stream() {
42157
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1230
        class BitSetSpliterator implements Spliterator.OfInt {
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1231
            private int index; // current bit index for a set bit
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1232
            private int fence; // -1 until used; then one past last bit index
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1233
            private int est;   // size estimate
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1234
            private boolean root; // true if root and not split
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1235
            // root == true then size estimate is accurate
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1236
            // index == -1 or index >= fence if fully traversed
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1237
            // Special case when the max bit set is Integer.MAX_VALUE
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1238
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1239
            BitSetSpliterator(int origin, int fence, int est, boolean root) {
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1240
                this.index = origin;
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1241
                this.fence = fence;
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1242
                this.est = est;
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1243
                this.root = root;
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1244
            }
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1245
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1246
            private int getFence() {
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1247
                int hi;
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1248
                if ((hi = fence) < 0) {
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1249
                    // Round up fence to maximum cardinality for allocated words
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1250
                    // This is sufficient and cheap for sequential access
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1251
                    // When splitting this value is lowered
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1252
                    hi = fence = (wordsInUse >= wordIndex(Integer.MAX_VALUE))
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1253
                                 ? Integer.MAX_VALUE
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1254
                                 : wordsInUse << ADDRESS_BITS_PER_WORD;
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1255
                    est = cardinality();
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1256
                    index = nextSetBit(0);
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1257
                }
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1258
                return hi;
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1259
            }
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
  1260
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
  1261
            @Override
42157
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1262
            public boolean tryAdvance(IntConsumer action) {
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1263
                Objects.requireNonNull(action);
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1264
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1265
                int hi = getFence();
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1266
                int i = index;
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1267
                if (i < 0 || i >= hi) {
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1268
                    // Check if there is a final bit set for Integer.MAX_VALUE
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1269
                    if (i == Integer.MAX_VALUE && hi == Integer.MAX_VALUE) {
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1270
                        index = -1;
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1271
                        action.accept(Integer.MAX_VALUE);
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1272
                        return true;
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1273
                    }
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1274
                    return false;
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1275
                }
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1276
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1277
                index = nextSetBit(i + 1, wordIndex(hi - 1));
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1278
                action.accept(i);
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1279
                return true;
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1280
            }
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1281
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1282
            @Override
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1283
            public void forEachRemaining(IntConsumer action) {
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1284
                Objects.requireNonNull(action);
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1285
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1286
                int hi = getFence();
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1287
                int i = index;
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1288
                index = -1;
50610
9b85066e259b 8170159: Improve the performance of BitSet traversal
psandoz
parents: 47216
diff changeset
  1289
9b85066e259b 8170159: Improve the performance of BitSet traversal
psandoz
parents: 47216
diff changeset
  1290
                if (i >= 0 && i < hi) {
9b85066e259b 8170159: Improve the performance of BitSet traversal
psandoz
parents: 47216
diff changeset
  1291
                    action.accept(i++);
9b85066e259b 8170159: Improve the performance of BitSet traversal
psandoz
parents: 47216
diff changeset
  1292
9b85066e259b 8170159: Improve the performance of BitSet traversal
psandoz
parents: 47216
diff changeset
  1293
                    int u = wordIndex(i);      // next lower word bound
9b85066e259b 8170159: Improve the performance of BitSet traversal
psandoz
parents: 47216
diff changeset
  1294
                    int v = wordIndex(hi - 1); // upper word bound
9b85066e259b 8170159: Improve the performance of BitSet traversal
psandoz
parents: 47216
diff changeset
  1295
9b85066e259b 8170159: Improve the performance of BitSet traversal
psandoz
parents: 47216
diff changeset
  1296
                    words_loop:
9b85066e259b 8170159: Improve the performance of BitSet traversal
psandoz
parents: 47216
diff changeset
  1297
                    for (; u <= v && i <= hi; u++, i = u << ADDRESS_BITS_PER_WORD) {
9b85066e259b 8170159: Improve the performance of BitSet traversal
psandoz
parents: 47216
diff changeset
  1298
                        long word = words[u] & (WORD_MASK << i);
9b85066e259b 8170159: Improve the performance of BitSet traversal
psandoz
parents: 47216
diff changeset
  1299
                        while (word != 0) {
9b85066e259b 8170159: Improve the performance of BitSet traversal
psandoz
parents: 47216
diff changeset
  1300
                            i = (u << ADDRESS_BITS_PER_WORD) + Long.numberOfTrailingZeros(word);
9b85066e259b 8170159: Improve the performance of BitSet traversal
psandoz
parents: 47216
diff changeset
  1301
                            if (i >= hi) {
9b85066e259b 8170159: Improve the performance of BitSet traversal
psandoz
parents: 47216
diff changeset
  1302
                                // Break out of outer loop to ensure check of
9b85066e259b 8170159: Improve the performance of BitSet traversal
psandoz
parents: 47216
diff changeset
  1303
                                // Integer.MAX_VALUE bit set
9b85066e259b 8170159: Improve the performance of BitSet traversal
psandoz
parents: 47216
diff changeset
  1304
                                break words_loop;
9b85066e259b 8170159: Improve the performance of BitSet traversal
psandoz
parents: 47216
diff changeset
  1305
                            }
9b85066e259b 8170159: Improve the performance of BitSet traversal
psandoz
parents: 47216
diff changeset
  1306
9b85066e259b 8170159: Improve the performance of BitSet traversal
psandoz
parents: 47216
diff changeset
  1307
                            // Flip the set bit
9b85066e259b 8170159: Improve the performance of BitSet traversal
psandoz
parents: 47216
diff changeset
  1308
                            word &= ~(1L << i);
9b85066e259b 8170159: Improve the performance of BitSet traversal
psandoz
parents: 47216
diff changeset
  1309
9b85066e259b 8170159: Improve the performance of BitSet traversal
psandoz
parents: 47216
diff changeset
  1310
                            action.accept(i);
9b85066e259b 8170159: Improve the performance of BitSet traversal
psandoz
parents: 47216
diff changeset
  1311
                        }
9b85066e259b 8170159: Improve the performance of BitSet traversal
psandoz
parents: 47216
diff changeset
  1312
                    }
42157
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1313
                }
50610
9b85066e259b 8170159: Improve the performance of BitSet traversal
psandoz
parents: 47216
diff changeset
  1314
42157
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1315
                // Check if there is a final bit set for Integer.MAX_VALUE
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1316
                if (i == Integer.MAX_VALUE && hi == Integer.MAX_VALUE) {
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1317
                    action.accept(Integer.MAX_VALUE);
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1318
                }
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
  1319
            }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
  1320
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
  1321
            @Override
42157
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1322
            public OfInt trySplit() {
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1323
                int hi = getFence();
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1324
                int lo = index;
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1325
                if (lo < 0) {
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1326
                    return null;
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1327
                }
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1328
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1329
                // Lower the fence to be the upper bound of last bit set
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1330
                // The index is the first bit set, thus this spliterator
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1331
                // covers one bit and cannot be split, or two or more
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1332
                // bits
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1333
                hi = fence = (hi < Integer.MAX_VALUE || !get(Integer.MAX_VALUE))
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1334
                        ? previousSetBit(hi - 1) + 1
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1335
                        : Integer.MAX_VALUE;
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1336
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1337
                // Find the mid point
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1338
                int mid = (lo + hi) >>> 1;
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1339
                if (lo >= mid) {
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1340
                    return null;
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
  1341
                }
42157
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1342
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1343
                // Raise the index of this spliterator to be the next set bit
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1344
                // from the mid point
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1345
                index = nextSetBit(mid, wordIndex(hi - 1));
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1346
                root = false;
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1347
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1348
                // Don't lower the fence (mid point) of the returned spliterator,
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1349
                // traversal or further splitting will do that work
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1350
                return new BitSetSpliterator(lo, mid, est >>>= 1, false);
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1351
            }
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1352
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1353
            @Override
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1354
            public long estimateSize() {
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1355
                getFence(); // force init
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1356
                return est;
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1357
            }
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1358
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1359
            @Override
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1360
            public int characteristics() {
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1361
                // Only sized when root and not split
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1362
                return (root ? Spliterator.SIZED : 0) |
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1363
                    Spliterator.ORDERED | Spliterator.DISTINCT | Spliterator.SORTED;
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1364
            }
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1365
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1366
            @Override
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1367
            public Comparator<? super Integer> getComparator() {
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1368
                return null;
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
  1369
            }
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
  1370
        }
42157
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1371
        return StreamSupport.intStream(new BitSetSpliterator(0, -1, 0, true), false);
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1372
    }
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
  1373
42157
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1374
    /**
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1375
     * Returns the index of the first bit that is set to {@code true}
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1376
     * that occurs on or after the specified starting index and up to and
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1377
     * including the specified word index
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1378
     * If no such bit exists then {@code -1} is returned.
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1379
     *
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1380
     * @param  fromIndex the index to start checking from (inclusive)
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1381
     * @param  toWordIndex the last word index to check (inclusive)
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1382
     * @return the index of the next set bit, or {@code -1} if there
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1383
     *         is no such bit
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1384
     */
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1385
    private int nextSetBit(int fromIndex, int toWordIndex) {
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1386
        int u = wordIndex(fromIndex);
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1387
        // Check if out of bounds
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1388
        if (u > toWordIndex)
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1389
            return -1;
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1390
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1391
        long word = words[u] & (WORD_MASK << fromIndex);
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1392
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1393
        while (true) {
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1394
            if (word != 0)
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1395
                return (u * BITS_PER_WORD) + Long.numberOfTrailingZeros(word);
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1396
            // Check if out of bounds
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1397
            if (++u > toWordIndex)
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1398
                return -1;
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1399
            word = words[u];
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1400
        }
17421
f3fbcfe6e2cf 8012645: Stream methods on BitSet, Random, ThreadLocalRandom, ZipFile
mduigou
parents: 14342
diff changeset
  1401
    }
42157
3e87fa9d8226 8072784: Better spliterator implementation for BitSet.stream()
psandoz
parents: 32649
diff changeset
  1402
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
}