Merge branch 'master' into gnu

Conflicts:

	classpath/java/util/TreeSet.java
This commit is contained in:
Joel Dice 2009-07-25 18:38:57 -06:00
commit f869e5be21
10 changed files with 378 additions and 72 deletions

View File

@ -626,7 +626,17 @@ Java_java_nio_channels_SocketSelector_natDoSocketSelect(JNIEnv *e, jclass,
} }
#endif #endif
timeval time = { interval / 1000, (interval % 1000) * 1000 }; timeval time;
if (interval > 0) {
time.tv_sec = interval / 1000;
time.tv_usec = (interval % 1000) * 1000;
} else if (interval < 0) {
time.tv_sec = 0;
time.tv_usec = 0;
} else {
time.tv_sec = INT32_MAX;
time.tv_usec = 0;
}
int r = ::select(max + 1, &(s->read), &(s->write), &(s->except), &time); int r = ::select(max + 1, &(s->read), &(s->write), &(s->except), &time);
if (r < 0) { if (r < 0) {

View File

@ -42,7 +42,11 @@ public abstract class Selector {
public abstract Selector wakeup(); public abstract Selector wakeup();
public abstract int selectNow() throws IOException;
public abstract int select(long interval) throws IOException; public abstract int select(long interval) throws IOException;
public abstract int select() throws IOException;
public abstract void close(); public abstract void close();
} }

View File

@ -48,7 +48,21 @@ class SocketSelector extends Selector {
} }
} }
public synchronized int selectNow() throws IOException {
return doSelect(-1);
}
public synchronized int select() throws IOException {
return doSelect(0);
}
public synchronized int select(long interval) throws IOException { public synchronized int select(long interval) throws IOException {
if (interval < 0) throw new IllegalArgumentException();
return doSelect(interval);
}
public int doSelect(long interval) throws IOException {
selectedKeys.clear(); selectedKeys.clear();
if (clearWoken()) return 0; if (clearWoken()) return 0;

View File

@ -64,6 +64,22 @@ public class Collections {
return sb.toString(); return sb.toString();
} }
static String toString(Map m) {
StringBuilder sb = new StringBuilder();
sb.append("{");
for (Iterator<Map.Entry> it = m.entrySet().iterator(); it.hasNext();) {
Map.Entry e = it.next();
sb.append(e.getKey())
.append("=")
.append(e.getValue());
if (it.hasNext()) {
sb.append(",");
}
}
sb.append("}");
return sb.toString();
}
static class IteratorEnumeration<T> implements Enumeration<T> { static class IteratorEnumeration<T> implements Enumeration<T> {
private final Iterator<T> it; private final Iterator<T> it;
@ -312,4 +328,44 @@ public class Collections {
public static <T> Set<T> unmodifiableSet(Set<T> hs) { public static <T> Set<T> unmodifiableSet(Set<T> hs) {
return new UnmodifiableSet<T>(hs); return new UnmodifiableSet<T>(hs);
} }
static class KeyIterator<K, V> implements Iterator<K> {
private final Iterator<Map.Entry<K, V>> it;
public KeyIterator(Iterator<Map.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();
}
}
static class ValueIterator<K, V> implements Iterator<V> {
private final Iterator<Map.Entry<K, V>> it;
public ValueIterator(Iterator<Map.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

@ -40,19 +40,7 @@ public class HashMap<K, V> implements Map<K, V> {
} }
public String toString() { public String toString() {
StringBuilder sb = new StringBuilder(); return Collections.toString(this);
sb.append("{");
for (Iterator<Entry<K, V>> it = iterator(); it.hasNext();) {
Entry<K, V> e = it.next();
sb.append(e.getKey())
.append("=")
.append(e.getValue());
if (it.hasNext()) {
sb.append(",");
}
}
sb.append("}");
return sb.toString();
} }
private static int nextPowerOfTwo(int n) { private static int nextPowerOfTwo(int n) {
@ -324,7 +312,8 @@ public class HashMap<K, V> implements Map<K, V> {
} }
public boolean contains(Object o) { public boolean contains(Object o) {
return (o instanceof Entry<?,?>) ? containsKey(((Entry<?,?>)o).getKey()) : false; return (o instanceof Entry<?,?>)
&& containsKey(((Entry<?,?>)o).getKey());
} }
public boolean add(Entry<K, V> e) { public boolean add(Entry<K, V> e) {
@ -337,9 +326,9 @@ public class HashMap<K, V> implements Map<K, V> {
return change; return change;
} }
public boolean remove(Object o) { public boolean remove(Object o) {
return (o instanceof Entry<?,?>) ? remove((Entry<?,?>)o) : false; return (o instanceof Entry<?,?>) && remove((Entry<?,?>)o);
} }
public boolean remove(Entry<K, V> e) { public boolean remove(Entry<K, V> e) {
return removeCell(e.getKey()) != null; return removeCell(e.getKey()) != null;
@ -394,11 +383,10 @@ public class HashMap<K, V> implements Map<K, V> {
} }
public Iterator<K> iterator() { public Iterator<K> iterator() {
return new KeyIterator(new MyIterator()); return new Collections.KeyIterator(new MyIterator());
} }
} }
private class Values implements Collection<V> { private class Values implements Collection<V> {
public int size() { public int size() {
return HashMap.this.size(); return HashMap.this.size();
@ -433,7 +421,7 @@ public class HashMap<K, V> implements Map<K, V> {
} }
public Iterator<V> iterator() { public Iterator<V> iterator() {
return new ValueIterator(new MyIterator()); return new Collections.ValueIterator(new MyIterator());
} }
} }
@ -495,44 +483,4 @@ 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

@ -0,0 +1,197 @@
/* Copyright (c) 2008, 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 TreeMap<K,V> implements Map<K,V> {
private TreeSet<MyEntry<K,V>> set;
public TreeMap(final Comparator<K> comparator) {
set = new TreeSet(new Comparator<MyEntry<K,V>>() {
public int compare(MyEntry<K,V> a, MyEntry<K,V> b) {
return comparator.compare(a.key, b.key);
}
});
}
public TreeMap() {
this(new Comparator<K>() {
public int compare(K a, K b) {
return ((Comparable) a).compareTo(b);
}
});
}
public String toString() {
return Collections.toString(this);
}
public V get(Object key) {
MyEntry<K,V> e = set.find(new MyEntry(key, null));
return e == null ? null : e.value;
}
public V put(K key, V value) {
MyEntry<K,V> e = set.addAndReplace(new MyEntry(key, value));
return e == null ? null : e.value;
}
public void putAll(Map<? extends K,? extends V> elts) {
for (Map.Entry<? extends K, ? extends V> entry : elts.entrySet()) {
put(entry.getKey(), entry.getValue());
}
}
public V remove(Object key) {
MyEntry<K,V> e = set.removeAndReturn(new MyEntry(key, null));
return e == null ? null : e.value;
}
public void clear() {
set.clear();
}
public int size() {
return set.size();
}
public boolean isEmpty() {
return size() == 0;
}
public boolean containsKey(Object key) {
return set.contains(new MyEntry(key, null));
}
private boolean equal(Object a, Object b) {
return a == null ? b == null : a.equals(b);
}
public boolean containsValue(Object value) {
for (V v: values()) {
if (equal(v, value)) {
return true;
}
}
return false;
}
public Set<Entry<K, V>> entrySet() {
return (Set<Entry<K, V>>) (Set) set;
}
public Set<K> keySet() {
return new KeySet();
}
public Collection<V> values() {
return new Values();
}
private static class MyEntry<K,V> implements Entry<K,V> {
public final K key;
public V value;
public MyEntry(K key, V value) {
this.key = key;
this.value = value;
}
public K getKey() {
return key;
}
public V getValue() {
return value;
}
public void setValue(V value) {
this.value = value;
}
}
private class KeySet implements Set<K> {
public int size() {
return TreeMap.this.size();
}
public boolean isEmpty() {
return TreeMap.this.isEmpty();
}
public boolean contains(Object key) {
return containsKey(key);
}
public boolean add(K key) {
return set.addAndReplace(new MyEntry(key, null)) != null;
}
public boolean addAll(Collection<? extends K> collection) {
boolean change = false;
for (K k: collection) if (add(k)) change = true;
return change;
}
public boolean remove(Object key) {
return set.removeAndReturn(new MyEntry(key, null)) != null;
}
public <T> T[] toArray(T[] array) {
return Collections.toArray(this, array);
}
public void clear() {
TreeMap.this.clear();
}
public Iterator<K> iterator() {
return new Collections.KeyIterator(set.iterator());
}
}
private class Values implements Collection<V> {
public int size() {
return TreeMap.this.size();
}
public boolean isEmpty() {
return TreeMap.this.isEmpty();
}
public boolean contains(Object value) {
return containsValue(value);
}
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 <T> T[] toArray(T[] array) {
return Collections.toArray(this, array);
}
public void clear() {
TreeMap.this.clear();
}
public Iterator<V> iterator() {
return new Collections.ValueIterator(set.iterator());
}
}
}

View File

@ -13,7 +13,7 @@ package java.util;
import avian.PersistentSet; import avian.PersistentSet;
import avian.Cell; import avian.Cell;
public class TreeSet<T> extends AbstractSet<T> implements Collection<T> { public class TreeSet<T> extends AbstractSet<T> implements Collection<T> {
private PersistentSet<Cell<T>> set; private PersistentSet<Cell<T>> set;
private int size; private int size;
@ -52,34 +52,52 @@ public class TreeSet<T> extends AbstractSet<T> implements Collection<T> {
return false; return false;
} }
// Used by hashMaps for replacement T addAndReplace(T value) {
public void addAndReplace(T value) {
PersistentSet.Path<Cell<T>> p = set.find(new Cell(value, null)); PersistentSet.Path<Cell<T>> p = set.find(new Cell(value, null));
if (p.fresh()) { if (p.fresh()) {
set = p.add(); set = p.add();
++size; ++size;
return null;
} else { } else {
T old = p.value().value;
set = p.replaceWith(new Cell(value, null)); set = p.replaceWith(new Cell(value, null));
return old;
} }
} }
public boolean remove(Object value) { T find(T value) {
PersistentSet.Path<Cell<T>> p = set.find(new Cell(value, null));
return p.fresh() ? null : p.value().value;
}
T removeAndReturn(T value) {
Cell<T> cell = removeCell(value);
return cell == null ? null : cell.value;
}
private Cell<T> removeCell(Object value) {
PersistentSet.Path<Cell<T>> p = set.find(new Cell(value, null)); PersistentSet.Path<Cell<T>> p = set.find(new Cell(value, null));
if (p.fresh()) { if (p.fresh()) {
return false; return null;
} else { } else {
--size; --size;
Cell<T> old = p.value();
if (p.value().next != null) { if (p.value().next != null) {
set = p.replaceWith(p.value().next); set = p.replaceWith(p.value().next);
} else { } else {
set = p.remove(); set = p.remove();
} }
return true; return old;
} }
} }
public boolean remove(Object value) {
return removeCell(value) != null;
}
public int size() { public int size() {
return size; return size;
} }

View File

@ -6690,7 +6690,7 @@ class MyProcessor: public Processor {
collect(t, Heap::MinorCollection); collect(t, Heap::MinorCollection);
} }
return visitor.trace; return visitor.trace ? visitor.trace : makeArray(t, 0);
} }
virtual void initialize(BootImage* image, uint8_t* code, unsigned capacity) { virtual void initialize(BootImage* image, uint8_t* code, unsigned capacity) {

View File

@ -626,13 +626,16 @@ class MySystem: public System {
visitTarget = target; visitTarget = target;
int rv = pthread_kill(target->thread, VisitSignal); int rv = pthread_kill(target->thread, VisitSignal);
expect(this, rv == 0);
while (visitTarget) visitLock->wait(t, 0); if (rv == 0) {
while (visitTarget) visitLock->wait(t, 0);
threadVisitor = 0; threadVisitor = 0;
return 0; return 0;
} else {
return -1;
}
} }
virtual uint64_t call(void* function, uintptr_t* arguments, uint8_t* types, virtual uint64_t call(void* function, uintptr_t* arguments, uint8_t* types,

View File

@ -1,5 +1,8 @@
import java.util.Comparator; import java.util.Comparator;
import java.util.TreeSet; import java.util.TreeSet;
import java.util.TreeMap;
import java.util.Map;
import java.util.Iterator;
public class Tree { public class Tree {
private static void expect(boolean v) { private static void expect(boolean v) {
@ -17,6 +20,21 @@ public class Tree {
return sb.toString(); return sb.toString();
} }
private static String printMap(TreeMap map) {
StringBuilder sb = new StringBuilder();
for (Iterator<Map.Entry> it = map.entrySet().iterator(); it.hasNext();) {
Map.Entry e = it.next();
sb.append(e.getKey());
sb.append("=");
sb.append(e.getValue());
if (it.hasNext()) {
sb.append(", ");
}
}
return sb.toString();
}
private static void isEqual(String s1, String s2) { private static void isEqual(String s1, String s2) {
System.out.println(s1); System.out.println(s1);
expect(s1.equals(s2)); expect(s1.equals(s2));
@ -53,5 +71,43 @@ public class Tree {
expect(t2.size() == 6); expect(t2.size() == 6);
t2.add("kappa"); t2.add("kappa");
isEqual(printList(t2), "999, five, four, kappa, one, three, two"); isEqual(printList(t2), "999, five, four, kappa, one, three, two");
TreeMap<String,String> map = new TreeMap<String,String>
(new Comparator<String>() {
public int compare(String s1, String s2) {
return s1.compareTo(s2);
}
});
map.put("q", "Q");
map.put("a", "A");
map.put("b", "B");
map.put("z", "Z");
map.put("c", "C");
map.put("y", "Y");
isEqual(printMap(map), "a=A, b=B, c=C, q=Q, y=Y, z=Z");
}
private static class MyEntry<K,V> implements Map.Entry<K,V> {
public final K key;
public V value;
public MyEntry(K key, V value) {
this.key = key;
this.value = value;
}
public K getKey() {
return key;
}
public V getValue() {
return value;
}
public void setValue(V value) {
this.value = value;
}
} }
} }