mirror of
https://github.com/corda/corda.git
synced 2025-06-15 21:58:17 +00:00
implement java.util.TreeMap
This commit is contained in:
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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) {
|
||||||
@ -338,7 +327,7 @@ public class HashMap<K, V> implements Map<K, V> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
197
classpath/java/util/TreeMap.java
Normal file
197
classpath/java/util/TreeMap.java
Normal 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -49,34 +49,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;
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user