@@ -40,6 +40,32 @@ public static <K, V> TreeMap<K, V> empty(final Ord<K> keyOrd) {
4040 return new TreeMap <K , V >(Set .empty (TreeMap .<K , Option <V >>ord (keyOrd )));
4141 }
4242
43+ /**
44+ * Constructs a tree map from the given elements.
45+ *
46+ * @param keyOrd An order for the keys of the tree map.
47+ * @param p2s The elements to construct the tree map with.
48+ * @return a TreeMap with the given elements.
49+ */
50+ public static <K , V > TreeMap <K , V > treeMap (final Ord <K > keyOrd , final P2 <K , V >... p2s ) {
51+ return treeMap (keyOrd , List .list (p2s ));
52+ }
53+
54+ /**
55+ * Constructs a tree map from the given elements.
56+ *
57+ * @param keyOrd An order for the keys of the tree map.
58+ * @param list The elements to construct the tree map with.
59+ * @return a TreeMap with the given elements.
60+ */
61+ public static <K , V > TreeMap <K , V > treeMap (final Ord <K > keyOrd , final List <P2 <K , V >> list ) {
62+ TreeMap <K , V > tm = empty (keyOrd );
63+ for (final P2 <K , V > p2 : list ) {
64+ tm = tm .set (p2 ._1 (), p2 ._2 ());
65+ }
66+ return tm ;
67+ }
68+
4369 /**
4470 * Returns a potential value that the given key maps to.
4571 *
@@ -143,6 +169,9 @@ public Map<K, V> toMutableMap() {
143169 return m ;
144170 }
145171
172+ public Stream <P2 <K , V >> toStream () {
173+ return Stream .iteratorStream (iterator ());
174+ }
146175 /**
147176 * An immutable projection of the given mutable map.
148177 *
@@ -208,13 +237,50 @@ public TreeMap<K, V> update(final K k, final F<V, V> f, final V v) {
208237 * key in this map, all the elements in the second set are mapped to keys greater than the given key,
209238 * and the optional value is the value associated with the given key if present, otherwise None.
210239 */
211- public P3 <Set <V >, Option <V >, Set <V >> split (final K k ) {
212- final F <Set <P2 <K , Option <V >>>, Set <V >> getSome = F1Functions .mapSet (F1Functions .o (Option .<V >fromSome (), P2 .<K , Option <V >>__2 ())
213- , tree .ord ().comap (F1Functions .o (P .<K , Option <V >>p2 ().f (k ), Option .<V >some_ ())));
240+ public P3 <Set <V >, Option <V >, Set <V >> split (Ord <V > ord , final K k ) {
241+ final F <Set <P2 <K , Option <V >>>, Set <V >> getSome = F1Functions .mapSet (F1Functions .o (Option .<V >fromSome (), P2 .<K , Option <V >>__2 ()), ord );
214242 return tree .split (p (k , Option .<V >none ())).map1 (getSome ).map3 (getSome )
215243 .map2 (F1Functions .o (Option .<V >join (), F1Functions .mapOption (P2 .<K , Option <V >>__2 ())));
216244 }
217245
246+ /**
247+ * Internal construction of a TreeMap from the given set.
248+ * @param ord An order for the keys of the tree map.
249+ * @param s The elements to construct the tree map with.
250+ * @return a TreeMap with the given elements.
251+ */
252+ private static <K , V > TreeMap <K , V > treeMap (Ord <K > ord , Set <P2 <K , Option <V >>> s ) {
253+ TreeMap <K , V > empty = TreeMap .<K , V >empty (ord );
254+ TreeMap <K , V > tree = s .toList ().foldLeft ((tm , p2 ) -> {
255+ Option <V > opt = p2 ._2 ();
256+ if (opt .isSome ()) {
257+ return tm .set (p2 ._1 (), opt .some ());
258+ }
259+ return tm ;
260+ }, empty );
261+ return tree ;
262+ }
263+
264+ /**
265+ * Splits this TreeMap at the given key. Returns a triple of:
266+ * <ul>
267+ * <li>A tree map containing all the values of this map associated with keys less than the given key.</li>
268+ * <li>An option of a value mapped to the given key, if it exists in this map, otherwise None.
269+ * <li>A tree map containing all the values of this map associated with keys greater than the given key.</li>
270+ * </ul>
271+ *
272+ * @param k A key at which to split this map.
273+ * @return Two tree maps and an optional value, where all keys in the first tree map are mapped
274+ * to keys less than the given key in this map, all the keys in the second tree map are mapped
275+ * to keys greater than the given key, and the optional value is the value associated with the
276+ * given key if present, otherwise None.
277+ */
278+ public P3 <TreeMap <K , V >, Option <V >, TreeMap <K , V >> splitLookup (final K k ) {
279+ P3 <Set <P2 <K , Option <V >>>, Option <P2 <K , Option <V >>>, Set <P2 <K , Option <V >>>> p3 = tree .split (P .p (k , get (k )));
280+ Ord <K > o = tree .ord ().<K >comap (k2 -> P .p (k2 , Option .<V >none ()));
281+ return P .p (treeMap (o , p3 ._1 ()), get (k ), treeMap (o , p3 ._3 ()));
282+ }
283+
218284 /**
219285 * Maps the given function across the values of this TreeMap.
220286 *
0 commit comments