diff options
Diffstat (limited to 'plugins/java-decompiler/engine/src/org/jetbrains/java/decompiler/util')
8 files changed, 2342 insertions, 0 deletions
diff --git a/plugins/java-decompiler/engine/src/org/jetbrains/java/decompiler/util/DataInputFullStream.java b/plugins/java-decompiler/engine/src/org/jetbrains/java/decompiler/util/DataInputFullStream.java new file mode 100644 index 000000000000..011eddea5c92 --- /dev/null +++ b/plugins/java-decompiler/engine/src/org/jetbrains/java/decompiler/util/DataInputFullStream.java @@ -0,0 +1,55 @@ +/* + * Copyright 2000-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jetbrains.java.decompiler.util; + +import java.io.ByteArrayInputStream; +import java.io.DataInputStream; +import java.io.IOException; + +public class DataInputFullStream extends DataInputStream { + + public DataInputFullStream(byte[] bytes) { + super(new ByteArrayInputStream(bytes)); + } + + public int readFull(byte[] b) throws IOException { + int length = b.length; + byte[] temp = new byte[length]; + int pos = 0; + + int bytes_read; + while (true) { + bytes_read = read(temp, 0, length - pos); + if (bytes_read == -1) { + return -1; + } + + System.arraycopy(temp, 0, b, pos, bytes_read); + pos += bytes_read; + if (pos == length) { + break; + } + } + + return length; + } + + public void discard(int n) throws IOException { + if (super.skip(n) != n) { + throw new IOException("Skip failed"); + } + } +} diff --git a/plugins/java-decompiler/engine/src/org/jetbrains/java/decompiler/util/FastFixedSetFactory.java b/plugins/java-decompiler/engine/src/org/jetbrains/java/decompiler/util/FastFixedSetFactory.java new file mode 100644 index 000000000000..a26b77e95ad7 --- /dev/null +++ b/plugins/java-decompiler/engine/src/org/jetbrains/java/decompiler/util/FastFixedSetFactory.java @@ -0,0 +1,360 @@ +/* + * Copyright 2000-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jetbrains.java.decompiler.util; + +import java.util.*; + +public class FastFixedSetFactory<E> { + + private VBStyleCollection<int[], E> colValuesInternal = new VBStyleCollection<int[], E>(); + + private int dataLength; + + public FastFixedSetFactory(Collection<E> set) { + + dataLength = set.size() / 32 + 1; + + int index = 0; + int mask = 1; + + for (E element : set) { + + int block = index / 32; + + if (index % 32 == 0) { + mask = 1; + } + + colValuesInternal.putWithKey(new int[]{block, mask}, element); + + index++; + mask <<= 1; + } + } + + public FastFixedSet<E> spawnEmptySet() { + return new FastFixedSet<E>(this); + } + + private int getDataLength() { + return dataLength; + } + + private VBStyleCollection<int[], E> getInternalValuesCollection() { + return colValuesInternal; + } + + public static class FastFixedSet<E> implements Iterable<E> { + + private FastFixedSetFactory<E> factory; + + private VBStyleCollection<int[], E> colValuesInternal; + + private int[] data; + + + private FastFixedSet(FastFixedSetFactory<E> factory) { + this.factory = factory; + this.colValuesInternal = factory.getInternalValuesCollection(); + this.data = new int[factory.getDataLength()]; + } + + public FastFixedSet<E> getCopy() { + + FastFixedSet<E> copy = new FastFixedSet<E>(factory); + + int arrlength = data.length; + int[] cpdata = new int[arrlength]; + System.arraycopy(data, 0, cpdata, 0, arrlength); + copy.setData(cpdata); + + return copy; + } + + public void setAllElements() { + + int[] lastindex = colValuesInternal.get(colValuesInternal.size() - 1); + + for (int i = lastindex[0] - 1; i >= 0; i--) { + data[i] = 0xFFFFFFFF; + } + + data[lastindex[0]] = lastindex[1] | (lastindex[1] - 1); + } + + public void add(E element) { + int[] index = colValuesInternal.getWithKey(element); + data[index[0]] |= index[1]; + } + + public void addAll(Collection<E> set) { + for (E element : set) { + add(element); + } + } + + public void remove(E element) { + int[] index = colValuesInternal.getWithKey(element); + data[index[0]] &= ~index[1]; + } + + public void removeAll(Collection<E> set) { + for (E element : set) { + remove(element); + } + } + + public boolean contains(E element) { + int[] index = colValuesInternal.getWithKey(element); + return (data[index[0]] & index[1]) != 0; + } + + public boolean contains(FastFixedSet<E> set) { + int[] extdata = set.getData(); + int[] intdata = data; + + for (int i = intdata.length - 1; i >= 0; i--) { + if ((extdata[i] & ~intdata[i]) != 0) { + return false; + } + } + + return true; + } + + public void union(FastFixedSet<E> set) { + int[] extdata = set.getData(); + int[] intdata = data; + + for (int i = intdata.length - 1; i >= 0; i--) { + intdata[i] |= extdata[i]; + } + } + + public void intersection(FastFixedSet<E> set) { + int[] extdata = set.getData(); + int[] intdata = data; + + for (int i = intdata.length - 1; i >= 0; i--) { + intdata[i] &= extdata[i]; + } + } + + public void symdiff(FastFixedSet<E> set) { + int[] extdata = set.getData(); + int[] intdata = data; + + for (int i = intdata.length - 1; i >= 0; i--) { + intdata[i] ^= extdata[i]; + } + } + + public void complement(FastFixedSet<E> set) { + int[] extdata = set.getData(); + int[] intdata = data; + + for (int i = intdata.length - 1; i >= 0; i--) { + intdata[i] &= ~extdata[i]; + } + } + + + public boolean equals(Object o) { + if (o == this) return true; + if (o == null || !(o instanceof FastFixedSet)) return false; + + int[] extdata = ((FastFixedSet)o).getData(); + int[] intdata = data; + + for (int i = intdata.length - 1; i >= 0; i--) { + if (intdata[i] != extdata[i]) { + return false; + } + } + + return true; + } + + public boolean isEmpty() { + int[] intdata = data; + + for (int i = intdata.length - 1; i >= 0; i--) { + if (intdata[i] != 0) { + return false; + } + } + + return true; + } + + public Iterator<E> iterator() { + return new FastFixedSetIterator<E>(this); + } + + public Set<E> toPlainSet() { + return toPlainCollection(new HashSet<E>()); + } + + public List<E> toPlainList() { + return toPlainCollection(new ArrayList<E>()); + } + + + private <T extends Collection<E>> T toPlainCollection(T cl) { + + int[] intdata = data; + for (int bindex = 0; bindex < intdata.length; bindex++) { + int block = intdata[bindex]; + if (block != 0) { + int index = bindex << 5; // * 32 + for (int i = 31; i >= 0; i--) { + if ((block & 1) != 0) { + cl.add(colValuesInternal.getKey(index)); + } + index++; + block >>>= 1; + } + } + } + + return cl; + } + + public String toBinary() { + + StringBuilder buffer = new StringBuilder(); + int[] intdata = data; + + for (int i = 0; i < intdata.length; i++) { + buffer.append(" ").append(Integer.toBinaryString(intdata[i])); + } + + return buffer.toString(); + } + + public String toString() { + + StringBuilder buffer = new StringBuilder("{"); + + int[] intdata = data; + boolean first = true; + + for (int i = colValuesInternal.size() - 1; i >= 0; i--) { + int[] index = colValuesInternal.get(i); + + if ((intdata[index[0]] & index[1]) != 0) { + if (first) { + first = false; + } + else { + buffer.append(","); + } + buffer.append(colValuesInternal.getKey(i)); + } + } + + buffer.append("}"); + + return buffer.toString(); + } + + private int[] getData() { + return data; + } + + private void setData(int[] data) { + this.data = data; + } + + public FastFixedSetFactory<E> getFactory() { + return factory; + } + } + + public static class FastFixedSetIterator<E> implements Iterator<E> { + + private VBStyleCollection<int[], E> colValuesInternal; + private int[] data; + private int size; + + private int pointer = -1; + private int next_pointer = -1; + + private FastFixedSetIterator(FastFixedSet<E> set) { + colValuesInternal = set.getFactory().getInternalValuesCollection(); + data = set.getData(); + size = colValuesInternal.size(); + } + + private int getNextIndex(int index) { + + index++; + int ret = index; + int bindex = index / 32; + int dindex = index % 32; + + while (bindex < data.length) { + int block = data[bindex]; + + if (block != 0) { + block >>>= dindex; + while (dindex < 32) { + if ((block & 1) != 0) { + return ret; + } + block >>>= 1; + dindex++; + ret++; + } + } + else { + ret += (32 - dindex); + } + + dindex = 0; + bindex++; + } + + return -1; + } + + public boolean hasNext() { + next_pointer = getNextIndex(pointer); + return (next_pointer >= 0); + } + + public E next() { + if (next_pointer >= 0) { + pointer = next_pointer; + } + else { + pointer = getNextIndex(pointer); + if (pointer == -1) { + pointer = size; + } + } + + next_pointer = -1; + return pointer < size ? colValuesInternal.getKey(pointer) : null; + } + + public void remove() { + int[] index = colValuesInternal.get(pointer); + data[index[0]] &= ~index[1]; + } + } +} + diff --git a/plugins/java-decompiler/engine/src/org/jetbrains/java/decompiler/util/FastSetFactory.java b/plugins/java-decompiler/engine/src/org/jetbrains/java/decompiler/util/FastSetFactory.java new file mode 100644 index 000000000000..e9cce03b827c --- /dev/null +++ b/plugins/java-decompiler/engine/src/org/jetbrains/java/decompiler/util/FastSetFactory.java @@ -0,0 +1,489 @@ +/* + * Copyright 2000-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jetbrains.java.decompiler.util; + +import java.util.Collection; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +public class FastSetFactory<E> { + + private VBStyleCollection<int[], E> colValuesInternal = new VBStyleCollection<int[], E>(); + + private int lastBlock; + + private int lastMask; + + public FastSetFactory(Collection<E> set) { + + int block = -1; + int mask = -1; + int index = 0; + + for (E element : set) { + + block = index / 32; + + if (index % 32 == 0) { + mask = 1; + } + else { + mask <<= 1; + } + + colValuesInternal.putWithKey(new int[]{block, mask}, element); + + index++; + } + + lastBlock = block; + lastMask = mask; + } + + private int[] addElement(E element) { + + if (lastMask == -1 || lastMask == 0x80000000) { + lastMask = 1; + lastBlock++; + } + else { + lastMask <<= 1; + } + + int[] pointer = new int[]{lastBlock, lastMask}; + colValuesInternal.putWithKey(pointer, element); + + return pointer; + } + + public FastSet<E> spawnEmptySet() { + return new FastSet<E>(this); + } + + public int getLastBlock() { + return lastBlock; + } + + public int getLastMask() { + return lastMask; + } + + private VBStyleCollection<int[], E> getInternalValuesCollection() { + return colValuesInternal; + } + + + public static class FastSet<E> implements Iterable<E> { + + private FastSetFactory<E> factory; + + private VBStyleCollection<int[], E> colValuesInternal; + + private int[] data; + + private FastSet(FastSetFactory<E> factory) { + this.factory = factory; + this.colValuesInternal = factory.getInternalValuesCollection(); + this.data = new int[factory.getLastBlock() + 1]; + } + + public FastSet<E> getCopy() { + + FastSet<E> copy = new FastSet<E>(factory); + + int arrlength = data.length; + int[] cpdata = new int[arrlength]; + + System.arraycopy(data, 0, cpdata, 0, arrlength); + copy.setData(cpdata); + + return copy; + } + + private int[] ensureCapacity(int index) { + + int newlength = data.length; + if (newlength == 0) { + newlength = 1; + } + + while (newlength <= index) { + newlength *= 2; + } + + int[] newdata = new int[newlength]; + System.arraycopy(data, 0, newdata, 0, data.length); + + return data = newdata; + } + + public void add(E element) { + int[] index = colValuesInternal.getWithKey(element); + + if (index == null) { + index = factory.addElement(element); + } + + if (index[0] >= data.length) { + ensureCapacity(index[0]); + } + + data[index[0]] |= index[1]; + } + + public void setAllElements() { + + int lastblock = factory.getLastBlock(); + int lastmask = factory.getLastMask(); + + if (lastblock >= data.length) { + ensureCapacity(lastblock); + } + + for (int i = lastblock - 1; i >= 0; i--) { + data[i] = 0xFFFFFFFF; + } + + data[lastblock] = lastmask | (lastmask - 1); + } + + public void addAll(Set<E> set) { + for (E element : set) { + add(element); + } + } + + public void remove(E element) { + int[] index = colValuesInternal.getWithKey(element); + + if (index == null) { + index = factory.addElement(element); + } + + if (index[0] < data.length) { + data[index[0]] &= ~index[1]; + } + } + + public void removeAll(Set<E> set) { + for (E element : set) { + remove(element); + } + } + + public boolean contains(E element) { + int[] index = colValuesInternal.getWithKey(element); + + if (index == null) { + index = factory.addElement(element); + } + + return index[0] >= data.length ? false : ((data[index[0]] & index[1]) != 0); + } + + public boolean contains(FastSet<E> set) { + int[] extdata = set.getData(); + int[] intdata = data; + + int minlength = Math.min(extdata.length, intdata.length); + + for (int i = minlength - 1; i >= 0; i--) { + if ((extdata[i] & ~intdata[i]) != 0) { + return false; + } + } + + for (int i = extdata.length - 1; i >= minlength; i--) { + if (extdata[i] != 0) { + return false; + } + } + + return true; + } + + public void union(FastSet<E> set) { + + int[] extdata = set.getData(); + int[] intdata = data; + + int minlength = Math.min(extdata.length, intdata.length); + + for (int i = minlength - 1; i >= 0; i--) { + intdata[i] |= extdata[i]; + } + + boolean expanded = false; + for (int i = extdata.length - 1; i >= minlength; i--) { + if (extdata[i] != 0) { + if (!expanded) { + intdata = ensureCapacity(extdata.length - 1); + } + intdata[i] = extdata[i]; + } + } + } + + public void intersection(FastSet<E> set) { + int[] extdata = set.getData(); + int[] intdata = data; + + int minlength = Math.min(extdata.length, intdata.length); + + for (int i = minlength - 1; i >= 0; i--) { + intdata[i] &= extdata[i]; + } + + for (int i = intdata.length - 1; i >= minlength; i--) { + intdata[i] = 0; + } + } + + public void symdiff(FastSet<E> set) { + int[] extdata = set.getData(); + int[] intdata = data; + + int minlength = Math.min(extdata.length, intdata.length); + + for (int i = minlength - 1; i >= 0; i--) { + intdata[i] ^= extdata[i]; + } + + boolean expanded = false; + for (int i = extdata.length - 1; i >= minlength; i--) { + if (extdata[i] != 0) { + if (!expanded) { + intdata = ensureCapacity(extdata.length - 1); + } + intdata[i] = extdata[i]; + } + } + } + + public void complement(FastSet<E> set) { + int[] extdata = set.getData(); + int[] intdata = data; + + int minlength = Math.min(extdata.length, intdata.length); + + for (int i = minlength - 1; i >= 0; i--) { + intdata[i] &= ~extdata[i]; + } + } + + + public boolean equals(Object o) { + if (o == this) return true; + if (o == null || !(o instanceof FastSet)) return false; + + int[] longdata = ((FastSet)o).getData(); + int[] shortdata = data; + + if (data.length > longdata.length) { + shortdata = longdata; + longdata = data; + } + + for (int i = shortdata.length - 1; i >= 0; i--) { + if (shortdata[i] != longdata[i]) { + return false; + } + } + + for (int i = longdata.length - 1; i >= shortdata.length; i--) { + if (longdata[i] != 0) { + return false; + } + } + + return true; + } + + public int getCardinality() { + + boolean found = false; + int[] intdata = data; + + for (int i = intdata.length - 1; i >= 0; i--) { + int block = intdata[i]; + if (block != 0) { + if (found) { + return 2; + } + else { + if ((block & (block - 1)) == 0) { + found = true; + } + else { + return 2; + } + } + } + } + + return found ? 1 : 0; + } + + public int size() { + + int size = 0; + int[] intdata = data; + + for (int i = intdata.length - 1; i >= 0; i--) { + size += Integer.bitCount(intdata[i]); + } + + return size; + } + + public boolean isEmpty() { + int[] intdata = data; + + for (int i = intdata.length - 1; i >= 0; i--) { + if (intdata[i] != 0) { + return false; + } + } + + return true; + } + + public Iterator<E> iterator() { + return new FastSetIterator<E>(this); + } + + public Set<E> toPlainSet() { + HashSet<E> set = new HashSet<E>(); + + int[] intdata = data; + + int size = data.length * 32; + if (size > colValuesInternal.size()) { + size = colValuesInternal.size(); + } + + for (int i = size - 1; i >= 0; i--) { + int[] index = colValuesInternal.get(i); + + if ((intdata[index[0]] & index[1]) != 0) { + set.add(colValuesInternal.getKey(i)); + } + } + + return set; + } + + public String toBinary() { + + StringBuilder buffer = new StringBuilder(); + int[] intdata = data; + + for (int i = 0; i < intdata.length; i++) { + buffer.append(" ").append(Integer.toBinaryString(intdata[i])); + } + + return buffer.toString(); + } + + private int[] getData() { + return data; + } + + private void setData(int[] data) { + this.data = data; + } + + public int[] getLoad() { + int[] intdata = data; + int notempty = 0; + + for (int i = 0; i < intdata.length; i++) { + if (intdata[i] != 0) { + notempty++; + } + } + + return new int[]{intdata.length, notempty}; + } + + public FastSetFactory<E> getFactory() { + return factory; + } + } + + public static class FastSetIterator<E> implements Iterator<E> { + + private VBStyleCollection<int[], E> colValuesInternal; + private int[] data; + private int size; + + private int pointer = -1; + private int next_pointer = -1; + + private FastSetIterator(FastSet<E> set) { + colValuesInternal = set.getFactory().getInternalValuesCollection(); + data = set.getData(); + + size = colValuesInternal.size(); + int datasize = data.length * 32; + + if (datasize < size) { + size = datasize; + } + } + + public boolean hasNext() { + + next_pointer = pointer; + + while (++next_pointer < size) { + int[] index = colValuesInternal.get(next_pointer); + if ((data[index[0]] & index[1]) != 0) { + return true; + } + } + + next_pointer = -1; + return false; + } + + public E next() { + if (next_pointer >= 0) { + pointer = next_pointer; + } + else { + while (++pointer < size) { + int[] index = colValuesInternal.get(pointer); + if ((data[index[0]] & index[1]) != 0) { + break; + } + } + } + + next_pointer = -1; + return pointer < size ? colValuesInternal.getKey(pointer) : null; + } + + public void remove() { + int[] index = colValuesInternal.get(pointer); + data[index[0]] &= ~index[1]; + + pointer--; + } + } +} + diff --git a/plugins/java-decompiler/engine/src/org/jetbrains/java/decompiler/util/FastSparseSetFactory.java b/plugins/java-decompiler/engine/src/org/jetbrains/java/decompiler/util/FastSparseSetFactory.java new file mode 100644 index 000000000000..bf01035616a4 --- /dev/null +++ b/plugins/java-decompiler/engine/src/org/jetbrains/java/decompiler/util/FastSparseSetFactory.java @@ -0,0 +1,552 @@ +/* + * Copyright 2000-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jetbrains.java.decompiler.util; + +import java.util.Collection; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +public class FastSparseSetFactory<E> { + + private VBStyleCollection<int[], E> colValuesInternal = new VBStyleCollection<int[], E>(); + + private int lastBlock; + + private int lastMask; + + public FastSparseSetFactory(Collection<E> set) { + + int block = -1; + int mask = -1; + int index = 0; + + for (E element : set) { + + block = index / 32; + + if (index % 32 == 0) { + mask = 1; + } + else { + mask <<= 1; + } + + colValuesInternal.putWithKey(new int[]{block, mask}, element); + + index++; + } + + lastBlock = block; + lastMask = mask; + } + + private int[] addElement(E element) { + + if (lastMask == -1 || lastMask == 0x80000000) { + lastMask = 1; + lastBlock++; + } + else { + lastMask <<= 1; + } + + int[] pointer = new int[]{lastBlock, lastMask}; + colValuesInternal.putWithKey(pointer, element); + + return pointer; + } + + public FastSparseSet<E> spawnEmptySet() { + return new FastSparseSet<E>(this); + } + + public int getLastBlock() { + return lastBlock; + } + + public int getLastMask() { + return lastMask; + } + + private VBStyleCollection<int[], E> getInternalValuesCollection() { + return colValuesInternal; + } + + + public static class FastSparseSet<E> implements Iterable<E> { + + private FastSparseSetFactory<E> factory; + + private VBStyleCollection<int[], E> colValuesInternal; + + private int[] data; + private int[] next; + + private FastSparseSet(FastSparseSetFactory<E> factory) { + this.factory = factory; + this.colValuesInternal = factory.getInternalValuesCollection(); + + int length = factory.getLastBlock() + 1; + this.data = new int[length]; + this.next = new int[length]; + } + + private FastSparseSet(FastSparseSetFactory<E> factory, int[] data, int[] next) { + this.factory = factory; + this.colValuesInternal = factory.getInternalValuesCollection(); + + this.data = data; + this.next = next; + } + + public FastSparseSet<E> getCopy() { + + int arrlength = data.length; + int[] cpdata = new int[arrlength]; + int[] cpnext = new int[arrlength]; + + System.arraycopy(data, 0, cpdata, 0, arrlength); + System.arraycopy(next, 0, cpnext, 0, arrlength); + + return new FastSparseSet<E>(factory, cpdata, cpnext); + } + + private int[] ensureCapacity(int index) { + + int newlength = data.length; + if (newlength == 0) { + newlength = 1; + } + + while (newlength <= index) { + newlength *= 2; + } + + int[] newdata = new int[newlength]; + System.arraycopy(data, 0, newdata, 0, data.length); + data = newdata; + + int[] newnext = new int[newlength]; + System.arraycopy(next, 0, newnext, 0, next.length); + next = newnext; + + return newdata; + } + + public void add(E element) { + int[] index = colValuesInternal.getWithKey(element); + + if (index == null) { + index = factory.addElement(element); + } + + int block = index[0]; + if (block >= data.length) { + ensureCapacity(block); + } + + data[block] |= index[1]; + + changeNext(next, block, next[block], block); + } + + public void setAllElements() { + + int lastblock = factory.getLastBlock(); + int lastmask = factory.getLastMask(); + + if (lastblock >= data.length) { + ensureCapacity(lastblock); + } + + for (int i = lastblock - 1; i >= 0; i--) { + data[i] = 0xFFFFFFFF; + next[i] = i + 1; + } + + data[lastblock] = lastmask | (lastmask - 1); + next[lastblock] = 0; + } + + public void addAll(Set<E> set) { + for (E element : set) { + add(element); + } + } + + public void remove(E element) { + int[] index = colValuesInternal.getWithKey(element); + + if (index == null) { + index = factory.addElement(element); + } + + int block = index[0]; + if (block < data.length) { + data[block] &= ~index[1]; + + if (data[block] == 0) { + changeNext(next, block, block, next[block]); + } + } + } + + public void removeAll(Set<E> set) { + for (E element : set) { + remove(element); + } + } + + public boolean contains(E element) { + int[] index = colValuesInternal.getWithKey(element); + + if (index == null) { + index = factory.addElement(element); + } + + return index[0] >= data.length ? false : ((data[index[0]] & index[1]) != 0); + } + + public boolean contains(FastSparseSet<E> set) { + int[] extdata = set.getData(); + int[] intdata = data; + + int minlength = Math.min(extdata.length, intdata.length); + + for (int i = minlength - 1; i >= 0; i--) { + if ((extdata[i] & ~intdata[i]) != 0) { + return false; + } + } + + for (int i = extdata.length - 1; i >= minlength; i--) { + if (extdata[i] != 0) { + return false; + } + } + + return true; + } + + private void setNext() { + + int link = 0; + for (int i = data.length - 1; i >= 0; i--) { + next[i] = link; + if (data[i] != 0) { + link = i; + } + } + } + + private static void changeNext(int[] arrnext, int key, int oldnext, int newnext) { + for (int i = key - 1; i >= 0; i--) { + if (arrnext[i] == oldnext) { + arrnext[i] = newnext; + } + else { + break; + } + } + } + + public void union(FastSparseSet<E> set) { + + int[] extdata = set.getData(); + int[] extnext = set.getNext(); + int[] intdata = data; + int intlength = intdata.length; + + int pointer = 0; + do { + if (pointer >= intlength) { + intdata = ensureCapacity(extdata.length - 1); + } + + boolean nextrec = (intdata[pointer] == 0); + intdata[pointer] |= extdata[pointer]; + + if (nextrec) { + changeNext(next, pointer, next[pointer], pointer); + } + + pointer = extnext[pointer]; + } + while (pointer != 0); + } + + public void intersection(FastSparseSet<E> set) { + int[] extdata = set.getData(); + int[] intdata = data; + + int minlength = Math.min(extdata.length, intdata.length); + + for (int i = minlength - 1; i >= 0; i--) { + intdata[i] &= extdata[i]; + } + + for (int i = intdata.length - 1; i >= minlength; i--) { + intdata[i] = 0; + } + + setNext(); + } + + public void symdiff(FastSparseSet<E> set) { + int[] extdata = set.getData(); + int[] intdata = data; + + int minlength = Math.min(extdata.length, intdata.length); + + for (int i = minlength - 1; i >= 0; i--) { + intdata[i] ^= extdata[i]; + } + + boolean expanded = false; + for (int i = extdata.length - 1; i >= minlength; i--) { + if (extdata[i] != 0) { + if (!expanded) { + intdata = ensureCapacity(extdata.length - 1); + } + intdata[i] = extdata[i]; + } + } + + setNext(); + } + + public void complement(FastSparseSet<E> set) { + + int[] extdata = set.getData(); + int[] intdata = data; + int extlength = extdata.length; + + int pointer = 0; + do { + if (pointer >= extlength) { + break; + } + + intdata[pointer] &= ~extdata[pointer]; + if (intdata[pointer] == 0) { + changeNext(next, pointer, pointer, next[pointer]); + } + + pointer = next[pointer]; + } + while (pointer != 0); + } + + + public boolean equals(Object o) { + if (o == this) return true; + if (o == null || !(o instanceof FastSparseSet)) return false; + + int[] longdata = ((FastSparseSet)o).getData(); + int[] shortdata = data; + + if (data.length > longdata.length) { + shortdata = longdata; + longdata = data; + } + + for (int i = shortdata.length - 1; i >= 0; i--) { + if (shortdata[i] != longdata[i]) { + return false; + } + } + + for (int i = longdata.length - 1; i >= shortdata.length; i--) { + if (longdata[i] != 0) { + return false; + } + } + + return true; + } + + public int getCardinality() { + + boolean found = false; + int[] intdata = data; + + for (int i = intdata.length - 1; i >= 0; i--) { + int block = intdata[i]; + if (block != 0) { + if (found) { + return 2; + } + else { + if ((block & (block - 1)) == 0) { + found = true; + } + else { + return 2; + } + } + } + } + + return found ? 1 : 0; + } + + public boolean isEmpty() { + return data.length == 0 || (next[0] == 0 && data[0] == 0); + } + + public Iterator<E> iterator() { + return new FastSparseSetIterator<E>(this); + } + + public Set<E> toPlainSet() { + HashSet<E> set = new HashSet<E>(); + + int[] intdata = data; + + int size = data.length * 32; + if (size > colValuesInternal.size()) { + size = colValuesInternal.size(); + } + + for (int i = size - 1; i >= 0; i--) { + int[] index = colValuesInternal.get(i); + + if ((intdata[index[0]] & index[1]) != 0) { + set.add(colValuesInternal.getKey(i)); + } + } + + return set; + } + + public String toString() { + return toPlainSet().toString(); + } + + public String toBinary() { + + StringBuilder buffer = new StringBuilder(); + int[] intdata = data; + + for (int i = 0; i < intdata.length; i++) { + buffer.append(" ").append(Integer.toBinaryString(intdata[i])); + } + + return buffer.toString(); + } + + private int[] getData() { + return data; + } + + private int[] getNext() { + return next; + } + + public int[] getLoad() { + int[] intdata = data; + int notempty = 0; + + for (int i = 0; i < intdata.length; i++) { + if (intdata[i] != 0) { + notempty++; + } + } + + return new int[]{intdata.length, notempty}; + } + + public FastSparseSetFactory<E> getFactory() { + return factory; + } + } + + public static class FastSparseSetIterator<E> implements Iterator<E> { + + private VBStyleCollection<int[], E> colValuesInternal; + private int[] data; + private int[] next; + private int size; + + private int pointer = -1; + private int next_pointer = -1; + + private FastSparseSetIterator(FastSparseSet<E> set) { + colValuesInternal = set.getFactory().getInternalValuesCollection(); + data = set.getData(); + next = set.getNext(); + size = colValuesInternal.size(); + } + + private int getNextIndex(int index) { + + index++; + int bindex = index >>> 5; + int dindex = index & 0x1F; + + while (bindex < data.length) { + int block = data[bindex]; + + if (block != 0) { + block >>>= dindex; + while (dindex < 32) { + if ((block & 1) != 0) { + return (bindex << 5) + dindex; + } + block >>>= 1; + dindex++; + } + } + + dindex = 0; + bindex = next[bindex]; + + if (bindex == 0) { + break; + } + } + + return -1; + } + + public boolean hasNext() { + next_pointer = getNextIndex(pointer); + return (next_pointer >= 0); + } + + public E next() { + if (next_pointer >= 0) { + pointer = next_pointer; + } + else { + pointer = getNextIndex(pointer); + if (pointer == -1) { + pointer = size; + } + } + + next_pointer = -1; + return pointer < size ? colValuesInternal.getKey(pointer) : null; + } + + public void remove() { + int[] index = colValuesInternal.get(pointer); + data[index[0]] &= ~index[1]; + } + } +} + diff --git a/plugins/java-decompiler/engine/src/org/jetbrains/java/decompiler/util/InterpreterUtil.java b/plugins/java-decompiler/engine/src/org/jetbrains/java/decompiler/util/InterpreterUtil.java new file mode 100644 index 000000000000..0047bb792c87 --- /dev/null +++ b/plugins/java-decompiler/engine/src/org/jetbrains/java/decompiler/util/InterpreterUtil.java @@ -0,0 +1,175 @@ +/* + * Copyright 2000-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jetbrains.java.decompiler.util; + +import org.jetbrains.java.decompiler.main.DecompilerContext; +import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences; + +import java.io.*; +import java.nio.channels.FileChannel; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +public class InterpreterUtil { + public static final boolean IS_WINDOWS = System.getProperty("os.name", "").startsWith("Windows"); + + private static final int CHANNEL_WINDOW_SIZE = IS_WINDOWS ? 64 * 1024 * 1024 - (32 * 1024) : 64 * 1024 * 1024; // magic number for Windows + private static final int BUFFER_SIZE = 16* 1024; + + public static void copyFile(File in, File out) throws IOException { + FileInputStream inStream = new FileInputStream(in); + try { + FileOutputStream outStream = new FileOutputStream(out); + try { + FileChannel inChannel = inStream.getChannel(); + FileChannel outChannel = outStream.getChannel(); + long size = inChannel.size(), position = 0; + while (position < size) { + position += inChannel.transferTo(position, CHANNEL_WINDOW_SIZE, outChannel); + } + } + finally { + outStream.close(); + } + } + finally { + inStream.close(); + } + } + + public static void copyStream(InputStream in, OutputStream out) throws IOException { + byte[] buffer = new byte[BUFFER_SIZE]; + int len; + while ((len = in.read(buffer)) >= 0) { + out.write(buffer, 0, len); + } + } + + public static byte[] getBytes(ZipFile archive, ZipEntry entry) throws IOException { + return readAndClose(archive.getInputStream(entry), entry.getSize()); + } + + public static byte[] getBytes(File file) throws IOException { + return readAndClose(new FileInputStream(file), file.length()); + } + + private static byte[] readAndClose(InputStream stream, long length) throws IOException { + try { + byte[] bytes = new byte[(int)length]; + if (stream.read(bytes) != length) { + throw new IOException("premature end of stream"); + } + return bytes; + } + finally { + stream.close(); + } + } + + public static String getIndentString(int length) { + if (length == 0) return ""; + StringBuilder buf = new StringBuilder(); + appendIndent(buf, length); + return buf.toString(); + } + + public static void appendIndent(StringBuilder buffer, int length) { + if (length == 0) return; + String indent = (String)DecompilerContext.getProperty(IFernflowerPreferences.INDENT_STRING); + while (length-- > 0) { + buffer.append(indent); + } + } + + public static boolean equalSets(Collection<?> c1, Collection<?> c2) { + if (c1 == null) { + return c2 == null; + } + else if (c2 == null) { + return false; + } + + if (c1.size() != c2.size()) { + return false; + } + + HashSet<Object> set = new HashSet<Object>(c1); + set.removeAll(c2); + return (set.size() == 0); + } + + public static boolean equalObjects(Object first, Object second) { + return first == null ? second == null : first.equals(second); + } + + public static boolean equalObjectArrays(Object[] first, Object[] second) { + if (first == null || second == null) { + return equalObjects(first, second); + } + else { + if (first.length != second.length) { + return false; + } + + for (int i = 0; i < first.length; i++) { + if (!equalObjects(first[i], second[i])) { + return false; + } + } + } + + return true; + } + + public static boolean equalLists(List<?> first, List<?> second) { + if (first == null) { + return second == null; + } + else if (second == null) { + return false; + } + + if (first.size() == second.size()) { + for (int i = 0; i < first.size(); i++) { + if (!equalObjects(first.get(i), second.get(i))) { + return false; + } + } + return true; + } + + return false; + } + + public static boolean isPrintableUnicode(char c) { + int t = Character.getType(c); + return t != Character.UNASSIGNED && t != Character.LINE_SEPARATOR && t != Character.PARAGRAPH_SEPARATOR && + t != Character.CONTROL && t != Character.FORMAT && t != Character.PRIVATE_USE && t != Character.SURROGATE; + } + + public static String charToUnicodeLiteral(int value) { + String sTemp = Integer.toHexString(value); + sTemp = ("0000" + sTemp).substring(sTemp.length()); + return "\\u" + sTemp; + } + + public static String makeUniqueKey(String name, String descriptor) { + return name + " " + descriptor; + } +} diff --git a/plugins/java-decompiler/engine/src/org/jetbrains/java/decompiler/util/ListStack.java b/plugins/java-decompiler/engine/src/org/jetbrains/java/decompiler/util/ListStack.java new file mode 100644 index 000000000000..19c2416c44de --- /dev/null +++ b/plugins/java-decompiler/engine/src/org/jetbrains/java/decompiler/util/ListStack.java @@ -0,0 +1,93 @@ +/* + * Copyright 2000-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jetbrains.java.decompiler.util; + +import java.util.ArrayList; + +public class ListStack<T> extends ArrayList<T> { + + protected int pointer = 0; + + public ListStack() { + super(); + } + + public ListStack(ArrayList<T> list) { + super(list); + } + + public ListStack<T> clone() { + ListStack<T> newstack = new ListStack<T>(this); + newstack.pointer = this.pointer; + return newstack; + } + + public T push(T item) { + this.add(item); + pointer++; + return item; + } + + public T pop() { + pointer--; + T o = this.get(pointer); + this.remove(pointer); + return o; + } + + public T pop(int count) { + T o = null; + for (int i = count; i > 0; i--) { + o = this.pop(); + } + return o; + } + + public void remove() { + pointer--; + this.remove(pointer); + } + + public void removeMultiple(int count) { + while (count > 0) { + pointer--; + this.remove(pointer); + count--; + } + } + + public boolean empty() { + return (pointer == 0); + } + + public int getPointer() { + return pointer; + } + + public T getByOffset(int offset) { + return this.get(pointer + offset); + } + + public void insertByOffset(int offset, T item) { + this.add(pointer + offset, item); + pointer++; + } + + public void clear() { + super.clear(); + pointer = 0; + } +} diff --git a/plugins/java-decompiler/engine/src/org/jetbrains/java/decompiler/util/SFormsFastMapDirect.java b/plugins/java-decompiler/engine/src/org/jetbrains/java/decompiler/util/SFormsFastMapDirect.java new file mode 100644 index 000000000000..996607a2bdbc --- /dev/null +++ b/plugins/java-decompiler/engine/src/org/jetbrains/java/decompiler/util/SFormsFastMapDirect.java @@ -0,0 +1,414 @@ +/* + * Copyright 2000-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jetbrains.java.decompiler.util; + +import org.jetbrains.java.decompiler.modules.decompiler.exps.VarExprent; +import org.jetbrains.java.decompiler.util.FastSparseSetFactory.FastSparseSet; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map.Entry; +import java.util.Set; + +public class SFormsFastMapDirect { + + private int size; + + @SuppressWarnings("unchecked") private FastSparseSet<Integer>[][] elements = new FastSparseSet[3][]; + + private int[][] next = new int[3][]; + + public SFormsFastMapDirect() { + this(true); + } + + private SFormsFastMapDirect(boolean initialize) { + if (initialize) { + for (int i = 2; i >= 0; i--) { + @SuppressWarnings("unchecked") FastSparseSet<Integer>[] empty = new FastSparseSet[0]; + elements[i] = empty; + next[i] = new int[0]; + } + } + } + + public SFormsFastMapDirect(SFormsFastMapDirect map) { + for (int i = 2; i >= 0; i--) { + FastSparseSet<Integer>[] arr = map.elements[i]; + int[] arrnext = map.next[i]; + + int length = arr.length; + @SuppressWarnings("unchecked") FastSparseSet<Integer>[] arrnew = new FastSparseSet[length]; + int[] arrnextnew = new int[length]; + + System.arraycopy(arr, 0, arrnew, 0, length); + System.arraycopy(arrnext, 0, arrnextnew, 0, length); + + elements[i] = arrnew; + next[i] = arrnextnew; + + size = map.size; + } + } + + public SFormsFastMapDirect getCopy() { + + SFormsFastMapDirect map = new SFormsFastMapDirect(false); + map.size = size; + + FastSparseSet[][] mapelements = map.elements; + int[][] mapnext = map.next; + + for (int i = 2; i >= 0; i--) { + FastSparseSet<Integer>[] arr = elements[i]; + int length = arr.length; + + if (length > 0) { + int[] arrnext = next[i]; + + @SuppressWarnings("unchecked") FastSparseSet<Integer>[] arrnew = new FastSparseSet[length]; + int[] arrnextnew = new int[length]; + + System.arraycopy(arrnext, 0, arrnextnew, 0, length); + + mapelements[i] = arrnew; + mapnext[i] = arrnextnew; + + int pointer = 0; + do { + FastSparseSet<Integer> set = arr[pointer]; + if (set != null) { + arrnew[pointer] = set.getCopy(); + } + + pointer = arrnext[pointer]; + } + while (pointer != 0); + } + else { + mapelements[i] = new FastSparseSet[0]; + mapnext[i] = new int[0]; + } + } + + return map; + } + + public int size() { + return size; + } + + public boolean isEmpty() { + return size == 0; + } + + public void put(int key, FastSparseSet<Integer> value) { + putInternal(key, value, false); + } + + public void remove(int key) { + putInternal(key, null, true); + } + + public void removeAllFields() { + FastSparseSet<Integer>[] arr = elements[2]; + int[] arrnext = next[2]; + + for (int i = arr.length - 1; i >= 0; i--) { + FastSparseSet<Integer> val = arr[i]; + if (val != null) { + arr[i] = null; + size--; + } + arrnext[i] = 0; + } + } + + public void putInternal(final int key, final FastSparseSet<Integer> value, boolean remove) { + + int index = 0; + int ikey = key; + if (ikey < 0) { + index = 2; + ikey = -ikey; + } + else if (ikey >= VarExprent.STACK_BASE) { + index = 1; + ikey -= VarExprent.STACK_BASE; + } + + FastSparseSet<Integer>[] arr = elements[index]; + if (ikey >= arr.length) { + if (remove) { + return; + } + else { + arr = ensureCapacity(index, ikey + 1, false); + } + } + + FastSparseSet<Integer> oldval = arr[ikey]; + arr[ikey] = value; + + int[] arrnext = next[index]; + + if (oldval == null && value != null) { + size++; + changeNext(arrnext, ikey, arrnext[ikey], ikey); + } + else if (oldval != null && value == null) { + size--; + changeNext(arrnext, ikey, ikey, arrnext[ikey]); + } + } + + private static void changeNext(int[] arrnext, int key, int oldnext, int newnext) { + for (int i = key - 1; i >= 0; i--) { + if (arrnext[i] == oldnext) { + arrnext[i] = newnext; + } + else { + break; + } + } + } + + public boolean containsKey(int key) { + return get(key) != null; + } + + public FastSparseSet<Integer> get(int key) { + + int index = 0; + if (key < 0) { + index = 2; + key = -key; + } + else if (key >= VarExprent.STACK_BASE) { + index = 1; + key -= VarExprent.STACK_BASE; + } + + FastSparseSet<Integer>[] arr = elements[index]; + + if (key < arr.length) { + return arr[key]; + } + return null; + } + + public void complement(SFormsFastMapDirect map) { + + for (int i = 2; i >= 0; i--) { + FastSparseSet<Integer>[] lstOwn = elements[i]; + + if (lstOwn.length == 0) { + continue; + } + + FastSparseSet<Integer>[] lstExtern = map.elements[i]; + int[] arrnext = next[i]; + + int pointer = 0; + do { + FastSparseSet<Integer> first = lstOwn[pointer]; + + if (first != null) { + if (pointer >= lstExtern.length) { + break; + } + FastSparseSet<Integer> second = lstExtern[pointer]; + + if (second != null) { + first.complement(second); + if (first.isEmpty()) { + lstOwn[pointer] = null; + size--; + changeNext(arrnext, pointer, pointer, arrnext[pointer]); + } + } + } + + pointer = arrnext[pointer]; + } + while (pointer != 0); + } + } + + public void intersection(SFormsFastMapDirect map) { + + for (int i = 2; i >= 0; i--) { + FastSparseSet<Integer>[] lstOwn = elements[i]; + + if (lstOwn.length == 0) { + continue; + } + + FastSparseSet<Integer>[] lstExtern = map.elements[i]; + int[] arrnext = next[i]; + + int pointer = 0; + do { + FastSparseSet<Integer> first = lstOwn[pointer]; + + if (first != null) { + FastSparseSet<Integer> second = null; + if (pointer < lstExtern.length) { + second = lstExtern[pointer]; + } + + if (second != null) { + first.intersection(second); + } + + if (second == null || first.isEmpty()) { + lstOwn[pointer] = null; + size--; + changeNext(arrnext, pointer, pointer, arrnext[pointer]); + } + } + + pointer = arrnext[pointer]; + } + while (pointer != 0); + } + } + + public void union(SFormsFastMapDirect map) { + + for (int i = 2; i >= 0; i--) { + FastSparseSet<Integer>[] lstExtern = map.elements[i]; + + if (lstExtern.length == 0) { + continue; + } + + FastSparseSet<Integer>[] lstOwn = elements[i]; + int[] arrnext = next[i]; + int[] arrnextExtern = map.next[i]; + + int pointer = 0; + do { + if (pointer >= lstOwn.length) { + lstOwn = ensureCapacity(i, lstExtern.length, true); + arrnext = next[i]; + } + + FastSparseSet<Integer> second = lstExtern[pointer]; + + if (second != null) { + FastSparseSet<Integer> first = lstOwn[pointer]; + + if (first == null) { + lstOwn[pointer] = second.getCopy(); + size++; + changeNext(arrnext, pointer, arrnext[pointer], pointer); + } + else { + first.union(second); + } + } + + pointer = arrnextExtern[pointer]; + } + while (pointer != 0); + } + } + + public String toString() { + + StringBuilder buffer = new StringBuilder("{"); + + List<Entry<Integer, FastSparseSet<Integer>>> lst = entryList(); + if (lst != null) { + boolean first = true; + for (Entry<Integer, FastSparseSet<Integer>> entry : lst) { + if (!first) { + buffer.append(", "); + } + else { + first = false; + } + + Set<Integer> set = entry.getValue().toPlainSet(); + buffer.append(entry.getKey()).append("={").append(set.toString()).append("}"); + } + } + + buffer.append("}"); + return buffer.toString(); + } + + public List<Entry<Integer, FastSparseSet<Integer>>> entryList() { + List<Entry<Integer, FastSparseSet<Integer>>> list = new ArrayList<Entry<Integer, FastSparseSet<Integer>>>(); + + for (int i = 2; i >= 0; i--) { + int ikey = 0; + for (final FastSparseSet<Integer> ent : elements[i]) { + if (ent != null) { + final int key = i == 0 ? ikey : (i == 1 ? ikey + VarExprent.STACK_BASE : -ikey); + + list.add(new Entry<Integer, FastSparseSet<Integer>>() { + + private Integer var = key; + private FastSparseSet<Integer> val = ent; + + public Integer getKey() { + return var; + } + + public FastSparseSet<Integer> getValue() { + return val; + } + + public FastSparseSet<Integer> setValue(FastSparseSet<Integer> newvalue) { + return null; + } + }); + } + + ikey++; + } + } + + return list; + } + + private FastSparseSet<Integer>[] ensureCapacity(int index, int size, boolean exact) { + + FastSparseSet<Integer>[] arr = elements[index]; + int[] arrnext = next[index]; + + int minsize = size; + if (!exact) { + minsize = 2 * arr.length / 3 + 1; + if (size > minsize) { + minsize = size; + } + } + + @SuppressWarnings("unchecked") FastSparseSet<Integer>[] arrnew = new FastSparseSet[minsize]; + System.arraycopy(arr, 0, arrnew, 0, arr.length); + + int[] arrnextnew = new int[minsize]; + System.arraycopy(arrnext, 0, arrnextnew, 0, arrnext.length); + + elements[index] = arrnew; + next[index] = arrnextnew; + + return arrnew; + } +} diff --git a/plugins/java-decompiler/engine/src/org/jetbrains/java/decompiler/util/VBStyleCollection.java b/plugins/java-decompiler/engine/src/org/jetbrains/java/decompiler/util/VBStyleCollection.java new file mode 100644 index 000000000000..cdc176263cd2 --- /dev/null +++ b/plugins/java-decompiler/engine/src/org/jetbrains/java/decompiler/util/VBStyleCollection.java @@ -0,0 +1,204 @@ +/* + * Copyright 2000-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jetbrains.java.decompiler.util; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; + +public class VBStyleCollection<E, K> extends ArrayList<E> { + + private HashMap<K, Integer> map = new HashMap<K, Integer>(); + + private ArrayList<K> lstKeys = new ArrayList<K>(); + + public VBStyleCollection() { + super(); + } + + public VBStyleCollection(int initialCapacity) { + super(initialCapacity); + lstKeys = new ArrayList<K>(initialCapacity); + map = new HashMap<K, Integer>(initialCapacity); + } + + public VBStyleCollection(Collection<E> c) { + super(c); + } + + public boolean add(E element) { + lstKeys.add(null); + super.add(element); + return true; + } + + public boolean remove(Object element) { // TODO: error on void remove(E element) + throw new RuntimeException("not implemented!"); + } + + public boolean addAll(Collection<? extends E> c) { + for (int i = c.size() - 1; i >= 0; i--) { + lstKeys.add(null); + } + return super.addAll(c); + } + + public void addAllWithKey(VBStyleCollection<E, K> c) { + for (int i = 0; i < c.size(); i++) { + addWithKey(c.get(i), c.getKey(i)); + } + } + + public void addAllWithKey(Collection<E> elements, Collection<K> keys) { + int index = super.size(); + + for (K key : keys) { + map.put(key, index++); + } + + super.addAll(elements); + lstKeys.addAll(keys); + } + + public void addWithKey(E element, K key) { + map.put(key, super.size()); + super.add(element); + lstKeys.add(key); + } + + // TODO: speed up the method + public E putWithKey(E element, K key) { + Integer index = map.get(key); + if (index == null) { + addWithKey(element, key); + } + else { + return super.set(index, element); + } + return null; + } + + public void add(int index, E element) { + addToListIndex(index, 1); + lstKeys.add(index, null); + super.add(index, element); + } + + public void addWithKeyAndIndex(int index, E element, K key) { + addToListIndex(index, 1); + map.put(key, new Integer(index)); + super.add(index, element); + lstKeys.add(index, key); + } + + public void removeWithKey(K key) { + int index = map.get(key).intValue(); + addToListIndex(index + 1, -1); + super.remove(index); + lstKeys.remove(index); + map.remove(key); + } + + public E remove(int index) { + addToListIndex(index + 1, -1); + K obj = lstKeys.get(index); + if (obj != null) { + map.remove(obj); + } + lstKeys.remove(index); + return super.remove(index); + } + + public E getWithKey(K key) { + Integer index = map.get(key); + if (index == null) { + return null; + } + return super.get(index.intValue()); + } + + public int getIndexByKey(K key) { + return map.get(key).intValue(); + } + + public E getLast() { + return super.get(super.size() - 1); + } + + public boolean containsKey(K key) { + return map.containsKey(key); + } + + public void clear() { + map.clear(); + lstKeys.clear(); + super.clear(); + } + + public VBStyleCollection<E, K> clone() { + VBStyleCollection<E, K> c = new VBStyleCollection<E, K>(); + c.addAll(new ArrayList<E>(this)); + c.setMap(new HashMap<K, Integer>(map)); + c.setLstKeys(new ArrayList<K>(lstKeys)); + return c; + } + + public void swap(int index1, int index2) { + + Collections.swap(this, index1, index2); + Collections.swap(lstKeys, index1, index2); + + K key = lstKeys.get(index1); + if (key != null) { + map.put(key, new Integer(index1)); + } + + key = lstKeys.get(index2); + if (key != null) { + map.put(key, new Integer(index2)); + } + } + + public HashMap<K, Integer> getMap() { + return map; + } + + public void setMap(HashMap<K, Integer> map) { + this.map = map; + } + + public K getKey(int index) { + return lstKeys.get(index); + } + + public ArrayList<K> getLstKeys() { + return lstKeys; + } + + public void setLstKeys(ArrayList<K> lstKeys) { + this.lstKeys = lstKeys; + } + + private void addToListIndex(int index, int diff) { + for (int i = lstKeys.size() - 1; i >= index; i--) { + K obj = lstKeys.get(i); + if (obj != null) { + map.put(obj, new Integer(i + diff)); + } + } + } +} |