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:
Serializable
,Iterable<UnmodMap.UnEntry<K,V>>
,Map<K,V>
,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>, 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
Map.Entry<K extends Object,V extends Object>
-
Nested classes/interfaces inherited from interface org.organicdesign.fp.collections.UnmodIterable
UnmodIterable.UnIterable
-
Nested classes/interfaces inherited from interface org.organicdesign.fp.collections.UnmodMap
UnmodMap.UnEntry<K,V>
-
-
Field Summary
Fields Modifier and Type Field Description static 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").
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description @NotNull PersistentTreeMap<K,V>
assoc(K key, V val)
Returns a new map with the given key/value added.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 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(Comparator<? super K> c)
Returns a new empty PersistentTreeMap that will use the specified comparator.@NotNull 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.@NotNull ImSortedSet<Map.Entry<K,V>>
entrySet()
Returns a view of the mappings contained in this map.K
firstKey()
Returns the first key in this map or throws a NoSuchElementException if the map is empty.@NotNull Option<UnmodMap.UnEntry<K,V>>
head()
The first item in this iterable.@NotNull 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)
@NotNull UnmodSortedIterator<K>
keyIterator()
UnmodMap.UnEntry<K,V>
last()
Returns the last key/value pair in this map, or null if the map is empty.K
lastKey()
Returns the last key in this map or throws a NoSuchElementException if the map is empty.static <K extends Comparable<K>,V>
PersistentTreeMap<K,V>of(Iterable<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(Comparator<? super K> comp, Iterable<Map.Entry<K,V>> kvPairs)
Returns a new PersistentTreeMap of the specified comparator and the given key/value pairs.int
size()
Returns the number of key/value mappings in this map.@NotNull ImSortedMap<K,V>
subMap(K fromKey, K toKey)
Return the elements in this map from the start element (inclusive) to the end element (exclusive)@NotNull ImSortedMap<K,V>
tailMap(K fromKey)
Return the elements in this from the given element to the end@NotNull UnmodSortedIterator<V>
valIterator()
@NotNull 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 java.lang.Iterable
forEach, spliterator
-
Methods inherited from interface java.util.Map
equals, forEach, getOrDefault, hashCode
-
Methods inherited from interface org.organicdesign.fp.xform.Transformable
any, toImList, toImMap, toImRrbt, toImSet, toImSortedMap, toImSortedSet, toMutList, toMutMap, toMutRrbt, toMutSet, toMutSortedMap, toMutSortedSet
-
Methods inherited from interface org.organicdesign.fp.collections.UnmodIterable
concat, drop, dropWhile, filter, flatMap, fold, foldUntil, map, precat, take, takeWhile, whereNonNull
-
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 Comparable<K>,V> PersistentTreeMap<K,V> of(Iterable<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(Comparator<? super K> comp, Iterable<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 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(Comparator<? super K> c)
Returns a new empty PersistentTreeMap that will use the specified comparator.
-
entrySet
@NotNull public @NotNull ImSortedSet<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:
entrySet
in interfaceBaseMap<K,V>
- Specified by:
entrySet
in interfaceImSortedMap<K,V>
- Specified by:
entrySet
in interfaceMap<K,V>
- Specified by:
entrySet
in interfaceSortedMap<K,V>
- Specified by:
entrySet
in interfaceUnmodMap<K,V>
- Specified by:
entrySet
in interfaceUnmodSortedMap<K,V>
-
subMap
@NotNull public @NotNull ImSortedMap<K,V> subMap(K fromKey, K toKey)
Return the elements in this map from the start element (inclusive) to the end element (exclusive)
-
head
@NotNull public @NotNull Option<UnmodMap.UnEntry<K,V>> head()
The first item in this iterable.- Specified by:
head
in interfaceTransformable<K>
- Specified by:
head
in interfaceUnmodIterable<K>
- Returns:
- an eagerly evaluated result which is a single item.
-
tailMap
@NotNull public @NotNull ImSortedMap<K,V> tailMap(K fromKey)
Return the elements in this from the given element to the end
-
comparator
public 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).- Specified by:
comparator
in interfaceSortedMap<K,V>
-
assoc
@NotNull public @NotNull 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:
assoc
in interfaceBaseMap<K,V>
- Specified by:
assoc
in 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
@NotNull public @NotNull PersistentTreeMap<K,V> without(K key)
Returns a new map with the given key/value removed
-
iterator
@NotNull public @NotNull UnmodSortedIterator<UnmodMap.UnEntry<K,V>> iterator()
Returns an iterator over the UnEntries of this map in order.- Specified by:
iterator
in interfaceImSortedMap<K,V>
- Specified by:
iterator
in interfaceIterable<K>
- Specified by:
iterator
in interfaceUnmodIterable<K>
- Specified by:
iterator
in interfaceUnmodSortedIterable<K>
- Returns:
- an Iterator.
-
keyIterator
@NotNull public @NotNull UnmodSortedIterator<K> keyIterator()
- Specified by:
keyIterator
in interfaceUnmodMap<K,V>
- Specified by:
keyIterator
in interfaceUnmodSortedMap<K,V>
-
valIterator
@NotNull public @NotNull UnmodSortedIterator<V> valIterator()
- Specified by:
valIterator
in interfaceUnmodMap<K,V>
- Specified by:
valIterator
in 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.
-
-