Package org.organicdesign.fp.collections
Class PersistentTreeMap<K,V>
- java.lang.Object
-
- org.organicdesign.fp.collections.AbstractUnmodIterable<UnmodMap.UnEntry<K,V>>
-
- org.organicdesign.fp.collections.AbstractUnmodMap<K,V>
-
- org.organicdesign.fp.collections.PersistentTreeMap<K,V>
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Iterable<UnmodMap.UnEntry<K,V>>,java.util.Map<K,V>,java.util.SortedMap<K,V>,BaseMap<K,V>,ImSortedMap<K,V>,Sized,UnmodIterable<UnmodMap.UnEntry<K,V>>,UnmodMap<K,V>,UnmodSortedIterable<UnmodMap.UnEntry<K,V>>,UnmodSortedMap<K,V>,Transformable<UnmodMap.UnEntry<K,V>>
public class PersistentTreeMap<K,V> extends AbstractUnmodMap<K,V> implements ImSortedMap<K,V>, java.io.Serializable
Persistent Red Black Tree. Note that instances of this class are constant values i.e. add/remove etc return new values. See Okasaki, Kahrs, Larsen et al This file is a derivative work based on a Clojure collection licensed under the Eclipse Public License 1.0 Copyright Rich Hickey- Author:
- Rich Hickey: Original author, Glen Peterson: Added generic types, static factories, custom serialization, and made Nodes extend Tuple2. All errors are Glen's.
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface java.util.Map
java.util.Map.Entry<K extends java.lang.Object,V extends java.lang.Object>
-
Nested classes/interfaces inherited from interface org.organicdesign.fp.collections.UnmodMap
UnmodMap.UnEntry<K,V>
-
-
Field Summary
Fields Modifier and Type Field Description static PersistentTreeMapEMPTYBe extremely careful with this because it uses the default comparator, which only works for items that implement Comparable (have a "natural ordering").
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description PersistentTreeMap<K,V>assoc(K key, V val)Returns a new map with the given key/value added.java.util.Comparator<? super K>comparator()Returns the comparator used to order the keys in this map, or null if it uses Fn2.DEFAULT_COMPARATOR (for compatibility with java.util.SortedMap).static <K extends java.lang.Comparable<K>,V>
PersistentTreeMap<K,V>empty()Be extremely careful with this because it uses the default comparator, which only works for items that implement Comparable (have a "natural ordering").static <K,V>
PersistentTreeMap<K,V>empty(java.util.Comparator<? super K> c)Returns a new empty PersistentTreeMap that will use the specified comparator.Option<UnmodMap.UnEntry<K,V>>entry(K key)Returns an Option of the key/value pair matching the given key, or Option.none() if the key is not found.ImSortedSet<java.util.Map.Entry<K,V>>entrySet()Returns a view of the mappings contained in this map.KfirstKey()Returns the first key in this map or throws a NoSuchElementException if the map is empty.Option<UnmodMap.UnEntry<K,V>>head()The first item in this iterable.UnmodSortedIterator<UnmodMap.UnEntry<K,V>>iterator()Returns an iterator over the UnEntries of this map in order.<R> UnmodSortedIterator<R>iterator(Fn1<org.organicdesign.fp.collections.PersistentTreeMap.Node<K,V>,R> aFn)UnmodSortedIterator<K>keyIterator()UnmodMap.UnEntry<K,V>last()Returns the last key/value pair in this map, or null if the map is empty.KlastKey()Returns the last key in this map or throws a NoSuchElementException if the map is empty.static <K extends java.lang.Comparable<K>,V>
PersistentTreeMap<K,V>of(java.lang.Iterable<java.util.Map.Entry<K,V>> es)Returns a new PersistentTreeMap of the given comparable keys and their paired values, skipping any null Entries.static <K,V>
PersistentTreeMap<K,V>ofComp(java.util.Comparator<? super K> comp, java.lang.Iterable<java.util.Map.Entry<K,V>> kvPairs)Returns a new PersistentTreeMap of the specified comparator and the given key/value pairs.intsize()Returns the number of key/value mappings in this map.ImSortedMap<K,V>subMap(K fromKey, K toKey)Return the elements in this map from the start element (inclusive) to the end element (exclusive)ImSortedMap<K,V>tailMap(K fromKey)Return the elements in this from the given element to the endUnmodSortedIterator<V>valIterator()PersistentTreeMap<K,V>without(K key)Returns a new map with the given key/value removed-
Methods inherited from class org.organicdesign.fp.collections.AbstractUnmodMap
equals
-
Methods inherited from class org.organicdesign.fp.collections.AbstractUnmodIterable
hashCode, toString
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.organicdesign.fp.collections.ImSortedMap
assoc, containsKey, get, getOrElse, headMap, keySet
-
Methods inherited from interface org.organicdesign.fp.xform.Transformable
toImList, toImMap, toImRrbt, toImSet, toImSortedMap, toImSortedSet, toMutableList, toMutableMap, toMutableRrbt, toMutableSet, toMutableSortedMap, toMutableSortedSet
-
Methods inherited from interface org.organicdesign.fp.collections.UnmodIterable
concat, drop, dropWhile, filter, flatMap, fold, foldUntil, map, precat, take, takeWhile
-
Methods inherited from interface org.organicdesign.fp.collections.UnmodMap
clear, compute, computeIfAbsent, computeIfPresent, containsValue, isEmpty, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll
-
Methods inherited from interface org.organicdesign.fp.collections.UnmodSortedMap
values
-
-
-
-
Field Detail
-
EMPTY
public static final PersistentTreeMap EMPTY
Be extremely careful with this because it uses the default comparator, which only works for items that implement Comparable (have a "natural ordering"). An attempt to use it with other items will blow up at runtime. Either a withComparator() method will be added, or this will be removed.
-
-
Method Detail
-
of
public static <K extends java.lang.Comparable<K>,V> PersistentTreeMap<K,V> of(java.lang.Iterable<java.util.Map.Entry<K,V>> es)
Returns a new PersistentTreeMap of the given comparable keys and their paired values, skipping any null Entries.
-
ofComp
public static <K,V> PersistentTreeMap<K,V> ofComp(java.util.Comparator<? super K> comp, java.lang.Iterable<java.util.Map.Entry<K,V>> kvPairs)
Returns a new PersistentTreeMap of the specified comparator and the given key/value pairs.- Parameters:
comp- A comparator (on the keys) that defines the sort order inside the new map. This becomes a permanent part of the map and all sub-maps or appended maps derived from it. If you want to use a null key, make sure the comparator treats nulls correctly in all circumstances!kvPairs- Key/value pairs (to go into the map). In the case of a duplicate key, later values in the input list overwrite the earlier ones. The resulting map can contain zero or one null key (if your comparator knows how to sort nulls) and any number of null values. Null k/v pairs will be silently ignored.- Returns:
- a new PersistentTreeMap of the specified comparator and the given key/value pairs
-
empty
public static <K extends java.lang.Comparable<K>,V> PersistentTreeMap<K,V> empty()
Be extremely careful with this because it uses the default comparator, which only works for items that implement Comparable (have a "natural ordering"). An attempt to use it with other items will blow up at runtime. Either a withComparator() method will be added, or this will be removed.
-
empty
public static <K,V> PersistentTreeMap<K,V> empty(java.util.Comparator<? super K> c)
Returns a new empty PersistentTreeMap that will use the specified comparator.
-
entrySet
public ImSortedSet<java.util.Map.Entry<K,V>> entrySet()
Returns a view of the mappings contained in this map. The set should actually contain UnmodMap.UnEntry items, but that return signature is illegal in Java, so you'll just have to remember.- Specified by:
entrySetin interfaceBaseMap<K,V>- Specified by:
entrySetin interfaceImSortedMap<K,V>- Specified by:
entrySetin interfacejava.util.Map<K,V>- Specified by:
entrySetin interfacejava.util.SortedMap<K,V>- Specified by:
entrySetin interfaceUnmodMap<K,V>- Specified by:
entrySetin interfaceUnmodSortedMap<K,V>
-
subMap
public ImSortedMap<K,V> subMap(K fromKey, K toKey)
Return the elements in this map from the start element (inclusive) to the end element (exclusive)- Specified by:
subMapin interfaceImSortedMap<K,V>- Specified by:
subMapin interfacejava.util.SortedMap<K,V>- Specified by:
subMapin interfaceUnmodSortedMap<K,V>
-
head
public Option<UnmodMap.UnEntry<K,V>> head()
The first item in this iterable.- Specified by:
headin interfaceTransformable<K>- Specified by:
headin interfaceUnmodIterable<K>- Returns:
- an eagerly evaluated result which is a single item.
-
tailMap
public ImSortedMap<K,V> tailMap(K fromKey)
Return the elements in this from the given element to the end- Specified by:
tailMapin interfaceImSortedMap<K,V>- Specified by:
tailMapin interfacejava.util.SortedMap<K,V>- Specified by:
tailMapin interfaceUnmodSortedMap<K,V>
-
comparator
public java.util.Comparator<? super K> comparator()
Returns the comparator used to order the keys in this map, or null if it uses Fn2.DEFAULT_COMPARATOR (for compatibility with java.util.SortedMap).
-
assoc
public PersistentTreeMap<K,V> assoc(K key, V val)
Returns a new map with the given key/value added. If the key exists in this map, the new value overwrites the old one. If the key exists with the same value (based on the address of that value in memory, not an equals test), the old map is returned unchanged.- Specified by:
associn interfaceBaseMap<K,V>- Specified by:
associn interfaceImSortedMap<K,V>- Parameters:
key- the key used to look up the value. In the case of a duplicate key, later values overwrite the earlier ones. The resulting map can contain zero or one null key (if your comparator knows how to sort nulls) and any number of null values.val- the value to store in this key.- Returns:
- a new PersistentTreeMap of the specified comparator and the given key/value pairs
-
without
public PersistentTreeMap<K,V> without(K key)
Returns a new map with the given key/value removed
-
iterator
public UnmodSortedIterator<UnmodMap.UnEntry<K,V>> iterator()
Returns an iterator over the UnEntries of this map in order.- Specified by:
iteratorin interfaceImSortedMap<K,V>- Specified by:
iteratorin interfacejava.lang.Iterable<K>- Specified by:
iteratorin interfaceUnmodIterable<K>- Specified by:
iteratorin interfaceUnmodSortedIterable<K>- Returns:
- an Iterator.
-
keyIterator
public UnmodSortedIterator<K> keyIterator()
- Specified by:
keyIteratorin interfaceUnmodMap<K,V>- Specified by:
keyIteratorin interfaceUnmodSortedMap<K,V>
-
valIterator
public UnmodSortedIterator<V> valIterator()
- Specified by:
valIteratorin interfaceUnmodMap<K,V>- Specified by:
valIteratorin interfaceUnmodSortedMap<K,V>
-
iterator
public <R> UnmodSortedIterator<R> iterator(Fn1<org.organicdesign.fp.collections.PersistentTreeMap.Node<K,V>,R> aFn)
-
firstKey
public K firstKey()
Returns the first key in this map or throws a NoSuchElementException if the map is empty.
-
lastKey
public K lastKey()
Returns the last key in this map or throws a NoSuchElementException if the map is empty.
-
last
public UnmodMap.UnEntry<K,V> last()
Returns the last key/value pair in this map, or null if the map is empty.
-
size
public int size()
Returns the number of key/value mappings in this map.
-
-