Merge remote-tracking branch 'origin/master' into dicej-master

This commit is contained in:
Joel Dice 2012-07-09 16:46:21 -06:00
commit 3d99ff37cb
8 changed files with 164 additions and 54 deletions

View File

@ -131,13 +131,14 @@ public class ArrayList<T> extends AbstractList<T> implements java.io.Serializabl
public T remove(int index) {
T v = get(index);
if (index == size - 1) {
int newSize = size - 1;
if (index == newSize) {
array[index] = null;
} else {
System.arraycopy(array, index + 1, array, index, size - index);
System.arraycopy(array, index + 1, array, index, newSize - index);
}
int newSize = size - 1;
shrink(newSize);
size = newSize;

View File

@ -32,6 +32,11 @@ public class BitSet implements Serializable, Cloneable {
return 1L << (index % BITS_PER_LONG);
}
private static long getTrueMask(int fromIndex, int toIndex) {
int currentRange = toIndex - fromIndex;
return (MASK >>> (BITS_PER_LONG - currentRange)) << (fromIndex % BITS_PER_LONG);
}
public BitSet(int bitLength) {
if (bitLength % BITS_PER_LONG == 0) {
enlarge(longPosition(bitLength));
@ -81,33 +86,9 @@ public class BitSet implements Serializable, Cloneable {
}
}
public void flip(int index) {
flip(index, index+1);
}
public void flip(int fromIndex, int toIndex) {
if (fromIndex > toIndex || fromIndex < 0 || toIndex < 0) {
throw new IndexOutOfBoundsException();
} else if (fromIndex != toIndex) {
int basePartition = longPosition(fromIndex);
int lastPartition = longPosition(toIndex - 1); //range is [fromIndex, toIndex)
int numPartitionsToTraverse = lastPartition - basePartition + 1;
enlarge(lastPartition);
int currentFirstIndex = fromIndex;
for (int i = 0; i < numPartitionsToTraverse; ++i) {
int currentToIndex = Math.min(toIndex, (basePartition + i + 1) * BITS_PER_LONG);
int currentRange = currentToIndex - currentFirstIndex;
long mask = (((1L << currentRange) - 1L) << (currentFirstIndex % BITS_PER_LONG));
bits[i + basePartition] ^= mask;
currentFirstIndex = currentToIndex;
}
}
}
private void enlarge(int newSize) {
if (bits == null || bits.length < newSize) {
long[] newBits = new long[newSize + 1];
private void enlarge(int newPartition) {
if (bits == null || bits.length < (newPartition + 1)) {
long[] newBits = new long[newPartition + 1];
if (bits != null) {
System.arraycopy(bits, 0, newBits, 0, bits.length);
}
@ -115,13 +96,6 @@ public class BitSet implements Serializable, Cloneable {
}
}
public void clear(int index) {
int pos = longPosition(index);
if (pos < bits.length) {
bits[pos] &= (MASK ^ bitPosition(index));
}
}
public boolean get(int index) {
int pos = longPosition(index);
if (pos < bits.length) {
@ -130,6 +104,23 @@ public class BitSet implements Serializable, Cloneable {
return false;
}
public void flip(int index) {
flip(index, index+1);
}
public void flip(int fromIndex, int toIndex) {
if (fromIndex > toIndex || fromIndex < 0 || toIndex < 0) {
throw new IndexOutOfBoundsException();
} else if (fromIndex != toIndex) {
MaskInfoIterator iter = new MaskInfoIterator(fromIndex, toIndex);
enlarge(iter.getLastPartition());
while (iter.hasNext()) {
MaskInfo info = iter.next();
bits[info.partitionIndex] ^= info.mask;
}
}
}
public void set(int index) {
int pos = longPosition(index);
enlarge(pos);
@ -137,14 +128,26 @@ public class BitSet implements Serializable, Cloneable {
}
public void set(int start, int end) {
for (int i = start; i < end; i++) {
set(i);
MaskInfoIterator iter = new MaskInfoIterator(start, end);
enlarge(iter.getLastPartition());
while (iter.hasNext()) {
MaskInfo info = iter.next();
bits[info.partitionIndex] |= info.mask;
}
}
public void clear(int index) {
int pos = longPosition(index);
if (pos < bits.length) {
bits[pos] &= (MASK ^ bitPosition(index));
}
}
public void clear(int start, int end) {
for (int i = start; i < end; i++) {
clear(i);
MaskInfoIterator iter = new MaskInfoIterator(start, end);
while (iter.hasNext()) {
MaskInfo info = iter.next();
bits[info.partitionIndex] &= (MASK ^ info.mask);
}
}
@ -214,4 +217,51 @@ public class BitSet implements Serializable, Cloneable {
return numSetBits;
}
private static class MaskInfoIterator implements Iterator<MaskInfo> {
private int basePartition;
private int numPartitionsToTraverse;
private int currentPartitionOffset;
private int toIndex;
private int currentFirstIndex;
public MaskInfoIterator(int fromIndex, int toIndex) {
this.basePartition = longPosition(fromIndex);
this.numPartitionsToTraverse = longPosition(toIndex - 1) - basePartition + 1;
this.currentPartitionOffset = 0;
this.toIndex = toIndex;
this.currentFirstIndex = fromIndex;
}
public MaskInfo next() {
int currentToIndex = Math.min(toIndex, (basePartition + currentPartitionOffset + 1) * BITS_PER_LONG);
long mask = getTrueMask(currentFirstIndex, currentToIndex);
MaskInfo info = new MaskInfo(mask, basePartition + currentPartitionOffset);
currentFirstIndex = currentToIndex;
currentPartitionOffset++;
return info;
}
public boolean hasNext() {
return currentPartitionOffset < numPartitionsToTraverse;
}
public void remove() {
throw new UnsupportedOperationException();
}
public int getLastPartition() {
return basePartition + numPartitionsToTraverse - 1;
}
}
private static class MaskInfo {
public long mask;
public int partitionIndex;
public MaskInfo(long mask, int partitionIndex) {
this.mask = mask;
this.partitionIndex = partitionIndex;
}
}
}

View File

@ -53,7 +53,7 @@ public class EnumSet<T extends Enum<T>> extends AbstractSet<T> {
public static <T extends Enum<T>>EnumSet<T> allOf(Class<T> elementType) {
EnumSet<T> enumSet = createEmptyEnumSet(elementType);
addAllElementsToSet(Arrays.asList(elementType.getEnumConstants()), enumSet);
enumSet.bitset.set(0, elementType.getEnumConstants().length);
return enumSet;
}

View File

@ -407,12 +407,14 @@ ifeq ($(platform),windows)
ifeq (,$(filter mingw32 cygwin,$(build-platform)))
openjdk-extra-cflags += -I$(src)/openjdk/caseSensitive
cxx = i686-w64-mingw32-g++ -m32
cc = i686-w64-mingw32-gcc -m32
dlltool = i686-w64-mingw32-dlltool -mi386 --as-flags=--32
ar = i686-w64-mingw32-ar
ranlib = i686-w64-mingw32-ranlib
strip = i686-w64-mingw32-strip --strip-all
prefix := $(shell i686-w64-mingw32-gcc --version >/dev/null 2>&1 \
&& echo i686-w64-mingw32- || echo x86_64-w64-mingw32-)
cxx = $(prefix)g++ -m32
cc = $(prefix)gcc -m32
dlltool = $(prefix)dlltool -mi386 --as-flags=--32
ar = $(prefix)ar
ranlib = $(prefix)ranlib
strip = $(prefix)strip --strip-all
else
build-system = windows
common-cflags += "-I$(JAVA_HOME)/include/win32"

View File

@ -1816,6 +1816,15 @@ void ArgParser::printUsage(const char* exe) {
}
}
char*
myStrndup(const char* src, unsigned length)
{
char* s = static_cast<char*>(malloc(length + 1));
memcpy(s, src, length);
s[length] = 0;
return s;
}
class Arguments {
public:
@ -1843,7 +1852,7 @@ public:
return false;
}
destA = strndup(src, split - src);
destA = myStrndup(src, split - src);
destB = strdup(split + 1);
}
return true;
@ -1879,9 +1888,9 @@ public:
if(entry.value) {
if(const char* entryClassEnd = strchr(entry.value, '.')) {
entryClass = strndup(entry.value, entryClassEnd - entry.value);
entryClass = myStrndup(entry.value, entryClassEnd - entry.value);
if(const char* entryMethodEnd = strchr(entryClassEnd, '(')) {
entryMethod = strndup(entryClassEnd + 1, entryMethodEnd - entryClassEnd - 1);
entryMethod = myStrndup(entryClassEnd + 1, entryMethodEnd - entryClassEnd - 1);
entrySpec = strdup(entryMethodEnd);
} else {
entryMethod = strdup(entryClassEnd + 1);

View File

@ -8249,15 +8249,15 @@ class ArgumentList {
break;
case 'D':
addLong(arguments[index++].d);
addLong(doubleToBits(arguments[index++].d));
break;
case 'F':
addLong(arguments[index++].f);
addInt(floatToBits(arguments[index++].f));
break;
default:
addLong(arguments[index++].i);
addInt(arguments[index++].i);
break;
}
}

View File

@ -48,6 +48,11 @@ public class BitsetTest {
assertEquals("after 100, 102 is empty", 102, bits.nextClearBit(100));
testFlip();
testClear();
BitSet expandingSet = new BitSet();
//should force us to have 3 partitions.
expandingSet.set(128);
}
private static void testFlip() {
@ -76,6 +81,30 @@ public class BitsetTest {
assertTrue("70 should be false", !bitset.get(70));
}
private static void testClear() {
BitSet bitset = new BitSet();
bitset.set(0, 20);
assertCardinality(bitset, 20);
bitset.clear(1);
assertTrue("bit 1 should be 0", !bitset.get(1));
assertCardinality(bitset, 19);
bitset.clear(0, 3);
assertTrue("bit 0 should be 0", !bitset.get(0));
assertTrue("bit 1 should be 0", !bitset.get(1));
assertTrue("bit 2 should be 0", !bitset.get(2));
assertTrue("bit 3 should be 1", bitset.get(3));
assertCardinality(bitset, 17);
bitset = new BitSet(70);
bitset.flip(0, 65);
for (int i=0; i < 65; ++i) {
assertTrue("bit " + i + " should be set", bitset.get(i));
}
assertTrue("bit 65 should not be set", !bitset.get(65));
}
static void assertTrue(String msg, boolean flag) {
if (flag) {
System.out.println(msg + " : OK.");

View File

@ -145,6 +145,24 @@ public class List {
}
}
private static void testRemove() {
ArrayList<String> foo = new ArrayList<String>(2);
foo.add("Uno");
foo.add("Dos");
foo.add("Tres");
foo.add("Cuatro");
ArrayList<String> fooToRemove = new ArrayList<String>(2);
fooToRemove.add(foo.get(0));
fooToRemove.add(foo.get(1));
for (String s : fooToRemove) {
foo.remove(s);
}
expect(foo.size() == 2);
}
public static void main(String args[]) {
ArrayList<Integer> l = new ArrayList<Integer>();
l.add(1); l.add(2); l.add(3); l.add(4); l.add(5);
@ -170,5 +188,6 @@ public class List {
testIterators2(new ArrayList());
testIterators2(new LinkedList());
testGrow();
testRemove();
}
}