fun with collections

This commit is contained in:
Joel Dice 2007-07-21 21:47:08 -06:00
parent 90d60b3459
commit da17490206
4 changed files with 111 additions and 7 deletions

View File

@ -3,9 +3,9 @@ package java.util;
public interface Collection<T> extends Iterable<T> {
public int size();
public boolean add(T entry);
public boolean add(T element);
public boolean remove(T entry);
public boolean remove(T element);
public void clear();
}

View File

@ -177,10 +177,19 @@ public class HashMap<K, V> implements Map<K, V> {
public void clear() {
array = null;
size = 0;
}
public Set<Entry<K, V>> entrySet() {
return new MySet();
return new EntrySet();
}
public Set<K> keySet() {
return new KeySet();
}
public Collection<V> values() {
return new Values();
}
Iterator<Entry<K, V>> iterator() {
@ -239,7 +248,7 @@ public class HashMap<K, V> implements Map<K, V> {
}
}
private class MySet implements Set<Entry<K, V>> {
private class EntrySet implements Set<Entry<K, V>> {
public int size() {
return HashMap.this.size();
}
@ -261,6 +270,51 @@ public class HashMap<K, V> implements Map<K, V> {
}
}
private class KeySet implements Set<K> {
public int size() {
return HashMap.this.size();
}
public boolean add(K key) {
return putCell(key, null) != null;
}
public boolean remove(K key) {
return removeCell(key) != null;
}
public void clear() {
HashMap.this.clear();
}
public Iterator<K> iterator() {
return new KeyIterator(new MyIterator());
}
}
private class Values implements Collection<V> {
public int size() {
return HashMap.this.size();
}
public boolean add(V value) {
throw new UnsupportedOperationException();
}
public boolean remove(V value) {
throw new UnsupportedOperationException();
}
public void clear() {
HashMap.this.clear();
}
public Iterator<V> iterator() {
return new ValueIterator(new MyIterator());
}
}
private class MyIterator implements Iterator<Entry<K, V>> {
private int currentIndex = -1;
private int nextIndex = -1;
@ -319,4 +373,44 @@ public class HashMap<K, V> implements Map<K, V> {
}
}
}
private static class KeyIterator<K, V> implements Iterator<K> {
private final Iterator<Entry<K, V>> it;
public KeyIterator(Iterator<Entry<K, V>> it) {
this.it = it;
}
public K next() {
return it.next().getKey();
}
public boolean hasNext() {
return it.hasNext();
}
public void remove() {
it.remove();
}
}
private static class ValueIterator<K, V> implements Iterator<V> {
private final Iterator<Entry<K, V>> it;
public ValueIterator(Iterator<Entry<K, V>> it) {
this.it = it;
}
public V next() {
return it.next().getValue();
}
public boolean hasNext() {
return it.hasNext();
}
public void remove() {
it.remove();
}
}
}

View File

@ -13,6 +13,10 @@ public interface Map<K, V> {
public Set<Entry<K, V>> entrySet();
public Set<K> keySet();
public Collection<V> values();
public interface Entry<K, V> {
public K getKey();

View File

@ -76,9 +76,15 @@ arraycopy(Thread* t, jobject src, jint srcOffset, jobject dst, jint dstOffset,
{
uint8_t* sbody = &cast<uint8_t>(s, 2 * BytesPerWord);
uint8_t* dbody = &cast<uint8_t>(d, 2 * BytesPerWord);
memcpy(dbody + (dstOffset * elementSize),
sbody + (srcOffset * elementSize),
length * elementSize);
if (src == dst) {
memmove(dbody + (dstOffset * elementSize),
sbody + (srcOffset * elementSize),
length * elementSize);
} else {
memcpy(dbody + (dstOffset * elementSize),
sbody + (srcOffset * elementSize),
length * elementSize);
}
return;
}
}