Copyright 2008 - 2010 Tony Morris, Runar Bjarnason, Tom Adams, Brad Clow, Ricky Clarkson, Nick Partridge, Jason Zaugg

This software is released under an open source BSD licence.

fj.data
Class TreeMap<K,V>

java.lang.Object
  extended by fj.data.TreeMap<K,V>
All Implemented Interfaces:
Iterable<P2<K,V>>

public final class TreeMap<K,V>
extends Object
implements Iterable<P2<K,V>>

An immutable, in-memory map, backed by a red-black tree.


Method Summary
 boolean contains(K k)
          Determines if the given key value exists in this tree map.
 TreeMap<K,V> delete(K k)
          Deletes the entry in the tree map that corresponds to the given key.
static
<K,V> TreeMap<K,V>
empty(Ord<K> keyOrd)
          Constructs an empty tree map.
static
<K,V> TreeMap<K,V>
fromMutableMap(Ord<K> ord, Map<K,V> m)
          An immutable projection of the given mutable map.
 F<K,Option<V>> get()
          Returns a first-class version of the get method for this TreeMap.
 Option<V> get(K k)
          Returns a potential value that the given key maps to.
 boolean isEmpty()
          Determines if this tree map has any entries.
 Iterator<P2<K,V>> iterator()
          Returns an iterator for this map's key-value pairs.
 List<K> keys()
          Returns all keys in this tree map.
<W> TreeMap<K,W>
map(F<V,W> f)
          Maps the given function across the values of this TreeMap.
 TreeMap<K,V> set(K k, V v)
          Inserts the given key and value association into the tree map.
 int size()
          Returns the number of entries in this tree map.
 P3<Set<V>,Option<V>,Set<V>> split(K k)
          Splits this TreeMap at the given key.
 Map<K,V> toMutableMap()
          A mutable map projection of this tree map.
 P2<Boolean,TreeMap<K,V>> update(K k, F<V,V> f)
          Modifies the value for the given key, if present, by applying the given function to it.
 TreeMap<K,V> update(K k, F<V,V> f, V v)
          Modifies the value for the given key, if present, by applying the given function to it, or inserts the given value if the key is not present.
 List<V> values()
          Returns all values in this tree map.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

empty

public static <K,V> TreeMap<K,V> empty(Ord<K> keyOrd)
Constructs an empty tree map.

Parameters:
keyOrd - An order for the keys of the tree map.
Returns:
an empty TreeMap with the given key order.

get

public Option<V> get(K k)
Returns a potential value that the given key maps to.

Parameters:
k - The key to look up in the tree map.
Returns:
A potential value for the given key.

set

public TreeMap<K,V> set(K k,
                        V v)
Inserts the given key and value association into the tree map. If the given key is already mapped to a value, the old value is replaced with the given one.

Parameters:
k - The key to insert.
v - The value to insert.
Returns:
A new tree map with the given value mapped to the given key.

delete

public TreeMap<K,V> delete(K k)
Deletes the entry in the tree map that corresponds to the given key.

Parameters:
k - The key to delete from this tree map.
Returns:
A new tree map with the entry corresponding to the given key removed.

size

public int size()
Returns the number of entries in this tree map.

Returns:
The number of entries in this tree map.

isEmpty

public boolean isEmpty()
Determines if this tree map has any entries.

Returns:
true if this tree map has no entries, false otherwise.

values

public List<V> values()
Returns all values in this tree map.

Returns:
All values in this tree map.

keys

public List<K> keys()
Returns all keys in this tree map.

Returns:
All keys in this tree map.

contains

public boolean contains(K k)
Determines if the given key value exists in this tree map.

Parameters:
k - The key value to look for in this tree map.
Returns:
true if this tree map contains the given key, false otherwise.

iterator

public Iterator<P2<K,V>> iterator()
Returns an iterator for this map's key-value pairs. This method exists to permit the use in a for-each loop.

Specified by:
iterator in interface Iterable<P2<K,V>>
Returns:
A iterator for this map's key-value pairs.

toMutableMap

public Map<K,V> toMutableMap()
A mutable map projection of this tree map.

Returns:
A new mutable map isomorphic to this tree map.

fromMutableMap

public static <K,V> TreeMap<K,V> fromMutableMap(Ord<K> ord,
                                                Map<K,V> m)
An immutable projection of the given mutable map.

Parameters:
ord - An order for the map's keys.
m - A mutable map to project to an immutable one.
Returns:
A new immutable tree map isomorphic to the given mutable map.

get

public F<K,Option<V>> get()
Returns a first-class version of the get method for this TreeMap.

Returns:
a functional representation of this TreeMap.

update

public P2<Boolean,TreeMap<K,V>> update(K k,
                                       F<V,V> f)
Modifies the value for the given key, if present, by applying the given function to it.

Parameters:
k - The key for the value to modify.
f - A function with which to modify the value.
Returns:
A new tree map with the value for the given key transformed by the given function, paired with True if the map was modified, otherwise False.

update

public TreeMap<K,V> update(K k,
                           F<V,V> f,
                           V v)
Modifies the value for the given key, if present, by applying the given function to it, or inserts the given value if the key is not present.

Parameters:
k - The key for the value to modify.
f - A function with which to modify the value.
v - A value to associate with the given key if the key is not already present.
Returns:
A new tree map with the value for the given key transformed by the given function.

split

public P3<Set<V>,Option<V>,Set<V>> split(K k)
Splits this TreeMap at the given key. Returns a triple of:

Parameters:
k - A key at which to split this map.
Returns:
Two sets and an optional value, where all elements in the first set are mapped to keys less than the given key in this map, all the elements in the second set are mapped to keys greater than the given key, and the optional value is the value associated with the given key if present, otherwise None.

map

public <W> TreeMap<K,W> map(F<V,W> f)
Maps the given function across the values of this TreeMap.

Parameters:
f - A function to apply to the values of this TreeMap.
Returns:
A new TreeMap with the values transformed by the given function.

Copyright 2008 - 2010 Tony Morris, Runar Bjarnason, Tom Adams, Brad Clow, Ricky Clarkson, Nick Partridge, Jason Zaugg

This software is released under an open source BSD licence.