From da17490206960873f0c609ec9858a3077044d680 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Sat, 21 Jul 2007 21:47:08 -0600 Subject: [PATCH] fun with collections --- classpath/java/util/Collection.java | 4 +- classpath/java/util/HashMap.java | 98 ++++++++++++++++++++++++++++- classpath/java/util/Map.java | 4 ++ src/builtin.cpp | 12 +++- 4 files changed, 111 insertions(+), 7 deletions(-) diff --git a/classpath/java/util/Collection.java b/classpath/java/util/Collection.java index 1a79f11c62..a39e1f8344 100644 --- a/classpath/java/util/Collection.java +++ b/classpath/java/util/Collection.java @@ -3,9 +3,9 @@ package java.util; public interface Collection extends Iterable { 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(); } diff --git a/classpath/java/util/HashMap.java b/classpath/java/util/HashMap.java index abe6a3b96e..77ecae8a02 100644 --- a/classpath/java/util/HashMap.java +++ b/classpath/java/util/HashMap.java @@ -177,10 +177,19 @@ public class HashMap implements Map { public void clear() { array = null; + size = 0; } public Set> entrySet() { - return new MySet(); + return new EntrySet(); + } + + public Set keySet() { + return new KeySet(); + } + + public Collection values() { + return new Values(); } Iterator> iterator() { @@ -239,7 +248,7 @@ public class HashMap implements Map { } } - private class MySet implements Set> { + private class EntrySet implements Set> { public int size() { return HashMap.this.size(); } @@ -261,6 +270,51 @@ public class HashMap implements Map { } } + private class KeySet implements Set { + 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 iterator() { + return new KeyIterator(new MyIterator()); + } + } + + + private class Values implements Collection { + 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 iterator() { + return new ValueIterator(new MyIterator()); + } + } + private class MyIterator implements Iterator> { private int currentIndex = -1; private int nextIndex = -1; @@ -319,4 +373,44 @@ public class HashMap implements Map { } } } + + private static class KeyIterator implements Iterator { + private final Iterator> it; + + public KeyIterator(Iterator> 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 implements Iterator { + private final Iterator> it; + + public ValueIterator(Iterator> it) { + this.it = it; + } + + public V next() { + return it.next().getValue(); + } + + public boolean hasNext() { + return it.hasNext(); + } + + public void remove() { + it.remove(); + } + } } diff --git a/classpath/java/util/Map.java b/classpath/java/util/Map.java index 03c632a307..b6de098b3f 100644 --- a/classpath/java/util/Map.java +++ b/classpath/java/util/Map.java @@ -13,6 +13,10 @@ public interface Map { public Set> entrySet(); + public Set keySet(); + + public Collection values(); + public interface Entry { public K getKey(); diff --git a/src/builtin.cpp b/src/builtin.cpp index 1bfeb5b596..6eabd1cb8c 100644 --- a/src/builtin.cpp +++ b/src/builtin.cpp @@ -76,9 +76,15 @@ arraycopy(Thread* t, jobject src, jint srcOffset, jobject dst, jint dstOffset, { uint8_t* sbody = &cast(s, 2 * BytesPerWord); uint8_t* dbody = &cast(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; } }