< prev index next >

src/java.base/share/classes/java/util/BitSet.java

Print this page


   1 /*
   2  * Copyright (c) 1995, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


1165      * surrounded by braces, resulting in the usual mathematical
1166      * notation for a set of integers.
1167      *
1168      * <p>Example:
1169      * <pre>
1170      * BitSet drPepper = new BitSet();</pre>
1171      * Now {@code drPepper.toString()} returns "{@code {}}".
1172      * <pre>
1173      * drPepper.set(2);</pre>
1174      * Now {@code drPepper.toString()} returns "{@code {2}}".
1175      * <pre>
1176      * drPepper.set(4);
1177      * drPepper.set(10);</pre>
1178      * Now {@code drPepper.toString()} returns "{@code {2, 4, 10}}".
1179      *
1180      * @return a string representation of this bit set
1181      */
1182     public String toString() {
1183         checkInvariants();
1184 
1185         final int MAX_INITIAL_CAPACITY = Integer.MAX_VALUE - 8;
1186         int numBits = (wordsInUse > 128) ?
1187             cardinality() : wordsInUse * BITS_PER_WORD;
1188         // Avoid overflow in the case of a humongous numBits
1189         int initialCapacity = (numBits <= (MAX_INITIAL_CAPACITY - 2) / 6) ?
1190             6 * numBits + 2 : MAX_INITIAL_CAPACITY;
1191         StringBuilder b = new StringBuilder(initialCapacity);
1192         b.append('{');
1193 
1194         int i = nextSetBit(0);
1195         if (i != -1) {
1196             b.append(i);
1197             while (true) {
1198                 if (++i < 0) break;
1199                 if ((i = nextSetBit(i)) < 0) break;
1200                 int endOfRun = nextClearBit(i);
1201                 do { b.append(", ").append(i); }
1202                 while (++i != endOfRun);
1203             }
1204         }
1205 
1206         b.append('}');
1207         return b.toString();
1208     }
1209 
1210     /**
1211      * Returns a stream of indices for which this {@code BitSet}


   1 /*
   2  * Copyright (c) 1995, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


1165      * surrounded by braces, resulting in the usual mathematical
1166      * notation for a set of integers.
1167      *
1168      * <p>Example:
1169      * <pre>
1170      * BitSet drPepper = new BitSet();</pre>
1171      * Now {@code drPepper.toString()} returns "{@code {}}".
1172      * <pre>
1173      * drPepper.set(2);</pre>
1174      * Now {@code drPepper.toString()} returns "{@code {2}}".
1175      * <pre>
1176      * drPepper.set(4);
1177      * drPepper.set(10);</pre>
1178      * Now {@code drPepper.toString()} returns "{@code {2, 4, 10}}".
1179      *
1180      * @return a string representation of this bit set
1181      */
1182     public String toString() {
1183         checkInvariants();
1184 

1185         int numBits = (wordsInUse > 128) ?
1186             cardinality() : wordsInUse * BITS_PER_WORD;
1187         StringBuilder b = new StringBuilder(6*numBits + 2);



1188         b.append('{');
1189 
1190         int i = nextSetBit(0);
1191         if (i != -1) {
1192             b.append(i);
1193             while (true) {
1194                 if (++i < 0) break;
1195                 if ((i = nextSetBit(i)) < 0) break;
1196                 int endOfRun = nextClearBit(i);
1197                 do { b.append(", ").append(i); }
1198                 while (++i != endOfRun);
1199             }
1200         }
1201 
1202         b.append('}');
1203         return b.toString();
1204     }
1205 
1206     /**
1207      * Returns a stream of indices for which this {@code BitSet}


< prev index next >