Merge pull request #92 from dscho/collections

Various improvements regarding Collections
This commit is contained in:
Joshua Warner 2013-11-04 16:33:06 -08:00
commit d128838617
9 changed files with 667 additions and 2 deletions

View File

@ -65,6 +65,10 @@ public class ArrayList<T> extends AbstractList<T> implements java.io.Serializabl
return size;
}
public void ensureCapacity(int capacity) {
grow(capacity);
}
public boolean contains(Object element) {
for (int i = 0; i < size; ++i) {
if (equal(element, array[i])) {

View File

@ -10,6 +10,8 @@
package java.util;
import java.lang.reflect.Array;
public class Arrays {
private Arrays() { }
@ -341,11 +343,119 @@ public class Arrays {
array[i] = value;
}
}
public static void fill(short[] array, short value) {
for (int i=0;i<array.length;i++) {
array[i] = value;
}
}
public static void fill(byte[] array, byte value) {
for (int i=0;i<array.length;i++) {
array[i] = value;
}
}
public static void fill(boolean[] array, boolean value) {
for (int i=0;i<array.length;i++) {
array[i] = value;
}
}
public static void fill(long[] array, long value) {
for (int i=0;i<array.length;i++) {
array[i] = value;
}
}
public static void fill(float[] array, float value) {
for (int i=0;i<array.length;i++) {
array[i] = value;
}
}
public static void fill(double[] array, double value) {
for (int i=0;i<array.length;i++) {
array[i] = value;
}
}
public static <T> void fill(T[] array, T value) {
for (int i=0;i<array.length;i++) {
array[i] = value;
}
}
public static boolean[] copyOf(boolean[] array, int newLength) {
boolean[] result = new boolean[newLength];
int length = array.length > newLength ? newLength : array.length;
System.arraycopy(array, 0, result, 0, length);
return result;
}
public static byte[] copyOf(byte[] array, int newLength) {
byte[] result = new byte[newLength];
int length = array.length > newLength ? newLength : array.length;
System.arraycopy(array, 0, result, 0, length);
return result;
}
public static char[] copyOf(char[] array, int newLength) {
char[] result = new char[newLength];
int length = array.length > newLength ? newLength : array.length;
System.arraycopy(array, 0, result, 0, length);
return result;
}
public static double[] copyOf(double[] array, int newLength) {
double[] result = new double[newLength];
int length = array.length > newLength ? newLength : array.length;
System.arraycopy(array, 0, result, 0, length);
return result;
}
public static float[] copyOf(float[] array, int newLength) {
float[] result = new float[newLength];
int length = array.length > newLength ? newLength : array.length;
System.arraycopy(array, 0, result, 0, length);
return result;
}
public static int[] copyOf(int[] array, int newLength) {
int[] result = new int[newLength];
int length = array.length > newLength ? newLength : array.length;
System.arraycopy(array, 0, result, 0, length);
return result;
}
public static long[] copyOf(long[] array, int newLength) {
long[] result = new long[newLength];
int length = array.length > newLength ? newLength : array.length;
System.arraycopy(array, 0, result, 0, length);
return result;
}
public static short[] copyOf(short[] array, int newLength) {
short[] result = new short[newLength];
int length = array.length > newLength ? newLength : array.length;
System.arraycopy(array, 0, result, 0, length);
return result;
}
public static <T> T[] copyOf(T[] array, int newLength) {
Class<?> clazz = array.getClass().getComponentType();
T[] result = (T[])Array.newInstance(clazz, newLength);
int length = array.length > newLength ? newLength : array.length;
System.arraycopy(array, 0, result, 0, length);
return result;
}
public static <T, U> T[] copyOf(U[] array, int newLength,
Class<? extends T[]> newType)
{
T[] result = (T[])Array.newInstance(newType.getComponentType(), newLength);
int length = array.length > newLength ? newLength : array.length;
System.arraycopy(array, 0, result, 0, length);
return result;
}
}

View File

@ -33,6 +33,146 @@ public class Collections {
shuffle(list, new Random());
}
public static void sort(List list) {
sort(list, new Comparator() {
public int compare(Object a, Object b) {
return ((Comparable) a).compareTo(b);
}
});
}
private final static int SORT_SIZE_THRESHOLD = 16;
public static <T> void sort(List<T> list, Comparator<? super T> comparator) {
int size = list.size();
introSort(list, comparator, 0, size, size);
insertionSort(list, comparator);
}
private static <T > void introSort(List<T> list,
Comparator<? super T> comparator, int begin, int end, int limit)
{
while (end - begin > SORT_SIZE_THRESHOLD) {
if (limit == 0) {
heapSort(list, comparator, begin, end);
return;
}
limit >>= 1;
// median of three
T a = list.get(begin);
T b = list.get(begin + (end - begin) / 2 + 1);
T c = list.get(end - 1);
T median;
if (comparator.compare(a, b) < 0) {
median = comparator.compare(b, c) < 0 ?
b : (comparator.compare(a, c) < 0 ? c : a);
} else {
median = comparator.compare(b, c) > 0 ?
b : (comparator.compare(a, c) > 0 ? c : a);
}
// partition
int pivot, i = begin, j = end;
for (;;) {
while (comparator.compare(list.get(i), median) < 0) {
++i;
}
--j;
while (comparator.compare(median, list.get(j)) < 0) {
--j;
}
if (i >= j) {
pivot = i;
break;
}
T swap = list.get(i);
list.set(i, list.get(j));
list.set(j, swap);
++i;
}
introSort(list, comparator, pivot, end, limit);
end = pivot;
}
}
private static <T> void heapSort(List<T> list, Comparator<? super T> comparator,
int begin, int end)
{
int count = end - begin;
for (int i = count / 2 - 1; i >= 0; --i) {
siftDown(list, comparator, i, count, begin);
}
for (int i = count - 1; i > 0; --i) {
// swap begin and begin + i
T swap = list.get(begin + i);
list.set(begin + i, list.get(begin));
list.set(begin, swap);
siftDown(list, comparator, 0, i, begin);
}
}
private static <T> void siftDown(List<T> list, Comparator<? super T> comparator,
int i, int count, int offset)
{
T value = list.get(offset + i);
while (i < count / 2) {
int child = 2 * i + 1;
if (child + 1 < count &&
comparator.compare(list.get(child), list.get(child + 1)) < 0) {
++child;
}
if (comparator.compare(value, list.get(child)) >= 0) {
break;
}
list.set(offset + i, list.get(offset + child));
i = child;
}
list.set(offset + i, value);
}
private static <T> void insertionSort(List<T> list,
Comparator<? super T> comparator)
{
int size = list.size();
for (int j = 1; j < size; ++j) {
T t = list.get(j);
int i = j - 1;
while (i >= 0 && comparator.compare(list.get(i), t) > 0) {
list.set(i + 1, list.get(i));
--i;
}
list.set(i + 1, t);
}
}
public static <T> int binarySearch(List<T> list, T needle) {
int left = -1, right = list.size();
while (left + 1 < right) {
int middle = (left + right) >> 1;
int result = ((Comparable)needle).compareTo(list.get(middle));
if (result < 0) {
right = middle;
} else if (result > 0) {
left = middle;
} else {
return middle;
}
}
return -1 - right;
}
public static <T> void reverse(List<T> list) {
int ascending = 0, descending = list.size() - 1;
while (ascending < descending) {
T tmp = list.get(ascending);
list.set(ascending++, list.get(descending));
list.set(descending--, tmp);
}
}
static <T> T[] toArray(Collection collection, T[] array) {
Class c = array.getClass().getComponentType();

View File

@ -93,7 +93,7 @@ public class HashMap<K, V> implements Map<K, V> {
array = newArray;
}
private Cell<K, V> find(Object key) {
protected Cell<K, V> find(Object key) {
if (array != null) {
int index = helper.hash(key) & (array.length - 1);
for (Cell<K, V> c = array[index]; c != null; c = c.next()) {

View File

@ -0,0 +1,267 @@
/* Copyright (c) 2008-2013, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
package java.util;
public class LinkedHashMap<K, V> extends HashMap<K, V> {
private static class LinkedKey<K> {
private final K key;
private LinkedKey<K> previous, next;
public LinkedKey(K key) {
this.key = key;
}
public boolean equals(Object other) {
LinkedKey<K> o = (LinkedKey<K>) other;
return key.equals(o.key);
}
public int hashCode() {
return key.hashCode();
}
}
private LinkedKey first, last;
private HashMap<K, LinkedKey<K>> lookup;
public LinkedHashMap(int capacity) {
super(capacity);
lookup = new HashMap<K, LinkedKey<K>>();
}
public LinkedHashMap() {
this(0);
}
public LinkedHashMap(Map<K, V> map) {
this(map.size());
putAll(map);
}
public V put(K key, V value) {
if (!super.containsKey(key)) {
LinkedKey<K> k = new LinkedKey<K>(key);
if (first == null) {
first = k;
} else {
last.next = k;
k.previous = last;
}
last = k;
lookup.put(key, k);
}
return super.put(key, value);
}
public V remove(Object key) {
LinkedKey<K> linked = lookup.get(key);
if (linked == null) {
return null;
}
if (linked.previous == null) {
first = linked.next;
} else {
linked.previous.next = linked.next;
}
if (linked.next == null) {
last = linked.previous;
} else {
linked.next.previous = linked.previous;
}
return super.remove(key);
}
public void clear() {
first = last = null;
super.clear();
}
public Set<Entry<K, V>> entrySet() {
return new EntrySet();
}
public Set<K> keySet() {
return new KeySet();
}
public Collection<V> values() {
return new Values();
}
Iterator<Entry<K, V>> iterator() {
return new MyIterator();
}
private class EntrySet extends AbstractSet<Entry<K, V>> {
public int size() {
return LinkedHashMap.this.size();
}
public boolean isEmpty() {
return LinkedHashMap.this.isEmpty();
}
public boolean contains(Object o) {
return (o instanceof Entry<?,?>)
&& containsKey(((Entry<?,?>)o).getKey());
}
public boolean add(Entry<K, V> e) {
return put(e.getKey(), e.getValue()) != null;
}
public boolean remove(Object o) {
return (o instanceof Entry<?,?>) && remove((Entry<?,?>)o);
}
public boolean remove(Entry<K, V> e) {
return LinkedHashMap.this.remove(e.getKey()) != null;
}
public Object[] toArray() {
return toArray(new Object[size()]);
}
public <T> T[] toArray(T[] array) {
return Collections.toArray(this, array);
}
public void clear() {
LinkedHashMap.this.clear();
}
public Iterator<Entry<K, V>> iterator() {
return new MyIterator();
}
}
private class KeySet extends AbstractSet<K> {
public int size() {
return LinkedHashMap.this.size();
}
public boolean isEmpty() {
return LinkedHashMap.this.isEmpty();
}
public boolean contains(Object key) {
return containsKey(key);
}
public boolean add(K key) {
return put(key, null) != null;
}
public boolean remove(Object key) {
return LinkedHashMap.this.remove(key) != null;
}
public Object[] toArray() {
return toArray(new Object[size()]);
}
public <T> T[] toArray(T[] array) {
return Collections.toArray(this, array);
}
public void clear() {
LinkedHashMap.this.clear();
}
public Iterator<K> iterator() {
return new Collections.KeyIterator(new MyIterator());
}
}
private class Values implements Collection<V> {
public int size() {
return LinkedHashMap.this.size();
}
public boolean isEmpty() {
return LinkedHashMap.this.isEmpty();
}
public boolean contains(Object value) {
return containsValue(value);
}
public boolean containsAll(Collection<?> c) {
if (c == null) {
throw new NullPointerException("collection is null");
}
Iterator<?> it = c.iterator();
while (it.hasNext()) {
if (! contains(it.next())) {
return false;
}
}
return true;
}
public boolean add(V value) {
throw new UnsupportedOperationException();
}
public boolean addAll(Collection<? extends V> collection) {
throw new UnsupportedOperationException();
}
public boolean remove(Object value) {
throw new UnsupportedOperationException();
}
public boolean removeAll(Collection<?> c) {
throw new UnsupportedOperationException();
}
public Object[] toArray() {
return toArray(new Object[size()]);
}
public <T> T[] toArray(T[] array) {
return Collections.toArray(this, array);
}
public void clear() {
LinkedHashMap.this.clear();
}
public Iterator<V> iterator() {
return new Collections.ValueIterator(new MyIterator());
}
}
private class MyIterator implements Iterator<Entry<K, V>> {
private LinkedKey<K> current = first;
public Entry<K, V> next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
Entry<K, V> result = find(current.key);
current = current.next;
return result;
}
public boolean hasNext() {
return current != null;
}
public void remove() {
LinkedHashMap.this.remove(current == null ?
last.key : current.previous.key);
}
}
}

View File

@ -0,0 +1,89 @@
/* Copyright (c) 2008-2013, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
package java.util;
public class LinkedHashSet<T> extends AbstractSet<T> implements Set<T> {
private static final Object Value = new Object();
private final LinkedHashMap<T, Object> map;
public LinkedHashSet(Collection<? extends T> c) {
map = new LinkedHashMap(c.size());
addAll(c);
}
public LinkedHashSet(int capacity) {
map = new LinkedHashMap(capacity);
}
public LinkedHashSet() {
this(0);
}
public int size() {
return map.size();
}
public boolean isEmpty() {
return map.isEmpty();
}
public boolean contains(Object element) {
return map.containsKey(element);
}
public boolean add(T element) {
return map.put(element, Value) != Value;
}
public boolean addAll(Collection<? extends T> collection) {
boolean change = false;
for (T t: collection) if (add(t)) change = true;
return change;
}
public boolean remove(Object element) {
return map.remove(element) != Value;
}
public void clear() {
map.clear();
}
public Iterator<T> iterator() {
return new MyIterator(map.iterator());
}
public String toString() {
return Collections.toString(this);
}
private static class MyIterator<T> implements Iterator<T> {
private final Iterator<Map.Entry<T, Object>> it;
public MyIterator(Iterator<Map.Entry<T, Object>> it) {
this.it = it;
}
public T next() {
return it.next().getKey();
}
public boolean hasNext() {
return it.hasNext();
}
public void remove() {
it.remove();
}
}
}

View File

@ -56,6 +56,12 @@ public class TreeSet<T> extends AbstractSet<T> implements Collection<T> {
return new MyIterator<T>(set.first());
}
public Iterator<T> descendingIterator() {
ArrayList<T> iterable = new ArrayList<T>(this);
Collections.reverse(iterable);
return iterable.iterator();
}
public String toString() {
return Collections.toString(this);
}

View File

@ -130,6 +130,13 @@ public class Arrays {
java.util.Arrays.hashCode((Object[])null);
}
{ String[] list = new String[] { "Hello", "World", "!" };
Object[] result = java.util.Arrays.copyOf(list, 2, Object[].class);
expect(list[1] == result[1]);
expect(result.length == 2);
expect(result.getClass().getComponentType() == Object.class);
}
testSort();
}
}

View File

@ -1,9 +1,12 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
public class Collections {
public static void main(String[] args) {
testValues();
testSort();
}
@SuppressWarnings("rawtypes")
@ -23,4 +26,43 @@ public class Collections {
// expected
}
}
private static void expect(boolean v) {
if (! v) throw new RuntimeException();
}
private static <T extends Comparable<T>> void expectSorted(List<T> list) {
for (int i = 1; i < list.size(); ++i) {
expect(list.get(i - 1).compareTo(list.get(i)) <= 0);
}
}
private static int pseudoRandom(int seed) {
return 3170425 * seed + 132102;
}
private static <T extends Comparable<T>> int shuffle(List<T> list, int seed) {
for (int i = list.size(); i > 1; --i) {
int i2 = (seed < 0 ? -seed : seed) % i;
T value = list.get(i - 1);
list.set(i - 1, list.get(i2));
list.set(i2, value);
seed = pseudoRandom(seed);
}
return seed;
}
public static void testSort() {
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 64; ++i) {
list.add(Integer.valueOf(i + 1));
}
;
int random = 12345;
for (int i = 0; i < 32; ++i) {
random = shuffle(list, random);
java.util.Collections.sort(list);
expectSorted(list);
}
}
}