mirror of
https://github.com/konsoletyper/teavm.git
synced 2024-11-27 01:30:35 +08:00
classlib: add BitSet stream API (#674)
This commit is contained in:
parent
ca76ea45cf
commit
ffe3f34731
@ -15,8 +15,11 @@
|
|||||||
*/
|
*/
|
||||||
package org.teavm.classlib.java.util;
|
package org.teavm.classlib.java.util;
|
||||||
|
|
||||||
|
import java.util.function.IntPredicate;
|
||||||
import org.teavm.classlib.java.io.TSerializable;
|
import org.teavm.classlib.java.io.TSerializable;
|
||||||
import org.teavm.classlib.java.lang.*;
|
import org.teavm.classlib.java.lang.*;
|
||||||
|
import org.teavm.classlib.java.util.stream.TIntStream;
|
||||||
|
import org.teavm.classlib.java.util.stream.intimpl.TSimpleIntStreamImpl;
|
||||||
import org.teavm.interop.Rename;
|
import org.teavm.interop.Rename;
|
||||||
|
|
||||||
public class TBitSet extends TObject implements TCloneable, TSerializable {
|
public class TBitSet extends TObject implements TCloneable, TSerializable {
|
||||||
@ -40,7 +43,7 @@ public class TBitSet extends TObject implements TCloneable, TSerializable {
|
|||||||
public static TBitSet valueOf(long[] longs) {
|
public static TBitSet valueOf(long[] longs) {
|
||||||
int[] ints = new int[longs.length * 2];
|
int[] ints = new int[longs.length * 2];
|
||||||
for (int i = 0; i < longs.length; ++i) {
|
for (int i = 0; i < longs.length; ++i) {
|
||||||
ints[i * 2 + 1] = (int) longs[i];
|
ints[i * 2] = (int) longs[i];
|
||||||
ints[i * 2 + 1] = (int) (longs[i] >>> TInteger.SIZE);
|
ints[i * 2 + 1] = (int) (longs[i] >>> TInteger.SIZE);
|
||||||
}
|
}
|
||||||
return new TBitSet(ints);
|
return new TBitSet(ints);
|
||||||
@ -511,6 +514,29 @@ public class TBitSet extends TObject implements TCloneable, TSerializable {
|
|||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public TIntStream stream() {
|
||||||
|
return new BitSetStream();
|
||||||
|
}
|
||||||
|
|
||||||
|
private class BitSetStream extends TSimpleIntStreamImpl {
|
||||||
|
private int current;
|
||||||
|
|
||||||
|
private BitSetStream() {
|
||||||
|
this.current = nextSetBit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean next(IntPredicate consumer) {
|
||||||
|
while (current >= 0) {
|
||||||
|
if (!consumer.test(current)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
current = nextSetBit(current + 1);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Rename("clone")
|
@Rename("clone")
|
||||||
public TObject clone0() {
|
public TObject clone0() {
|
||||||
return new TBitSet(TArrays.copyOf(data, data.length));
|
return new TBitSet(TArrays.copyOf(data, data.length));
|
||||||
|
@ -47,6 +47,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.teavm.classlib.java.util;
|
package org.teavm.classlib.java.util;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertArrayEquals;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
@ -98,6 +99,24 @@ public class BitSetTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void constructFromLongs() {
|
||||||
|
BitSet bs = BitSet.valueOf(new long[] { 7, 2, 5, 1L << 36 });
|
||||||
|
assertTrue(bs.get(0) && bs.get(1) && bs.get(2) && bs.get(Long.SIZE + 1)
|
||||||
|
&& bs.get(2 * Long.SIZE) && bs.get(2 * Long.SIZE + 2) && bs.get(3 * Long.SIZE + 36));
|
||||||
|
assertFalse(bs.get(3) || bs.get(Long.SIZE + 6) || bs.get(2 * Long.SIZE + 15) || bs.get(3 * Long.SIZE));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testStream() {
|
||||||
|
assertArrayEquals(new int[] { 0, 1, 2, Long.SIZE + 1, 2 * Long.SIZE, 2 * Long.SIZE + 2, 3 * Long.SIZE + 36 },
|
||||||
|
BitSet.valueOf(new long[] { 7, 2, 5, 1L << 36 }).stream().toArray());
|
||||||
|
BitSet bs = new BitSet();
|
||||||
|
assertEquals(0, bs.stream().count());
|
||||||
|
bs.set(1);
|
||||||
|
assertArrayEquals(new int[] { 1 }, bs.stream().toArray());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void clonePerformed() {
|
public void clonePerformed() {
|
||||||
BitSet bs;
|
BitSet bs;
|
||||||
|
Loading…
Reference in New Issue
Block a user