public static MapItem <K, V> Filter <K, V>(MapItem <K, V> node, Func <V, bool> pred) => node.IsEmpty ? node : pred(node.Value) ? Balance(Make(node.Key, node.Value, Filter(node.Left, pred), Filter(node.Right, pred))) : Balance(Filter(AddTreeToRight(node.Left, node.Right), pred));
public static MapItem <K, U> Map <K, V, U>(MapItem <K, V> node, Func <K, V, U> mapper) => node.IsEmpty ? MapItem <K, U> .Empty : new MapItem <K, U>(node.Height, node.Count, node.Key, mapper(node.Key, node.Value), Map(node.Left, mapper), Map(node.Right, mapper));
public static bool ForAll <K, V>(MapItem <K, V> node, Func <K, V, bool> pred) => node.IsEmpty ? true : pred(node.Key, node.Value) ? ForAll(node.Left, pred) && ForAll(node.Right, pred) : false;
public static bool Exists <K, V>(MapItem <K, V> node, Func <K, V, bool> pred) => node.IsEmpty ? false : pred(node.Key, node.Value) ? true : Exists(node.Left, pred) || Exists(node.Right, pred);
/// <summary> /// Ctor /// </summary> internal MapItem(byte height, int count, K key, V value, MapItem <K, V> left, MapItem <K, V> right) { Count = count; Height = height; Key = key; Value = value; Left = left; Right = right; }
public static MapItem <K, R> Choose <K, V, R>(MapItem <K, V> node, Func <V, Option <R> > selector) => Map(Filter(Map(node, selector), n => n.IsSome), n => n.Value);
/// <summary> /// Ctor /// </summary> internal Map(MapItem <K, V> root, bool rev) { Root = root; Rev = rev; }
internal Map <K, V> SetRoot(MapItem <K, V> root) => new Map <K, V>(root, Rev);
private MapItem <K, V> Prev(MapItem <K, V> node) => rev ? node.Right : node.Left;
/// <summary> /// Ctor /// </summary> internal Map() { Root = MapItem <K, V> .Empty; }
private MapItem <K, V> Next(MapItem <K, V> node) => rev ? node.Left : node.Right;
public static MapItem <K, V> DblRotLeft <K, V>(MapItem <K, V> node) => node.IsEmpty ? node : RotLeft(Make(node.Key, node.Value, node.Left, RotRight(node.Right)));
public static MapItem <K, V> RotLeft <K, V>(MapItem <K, V> node) => node.IsEmpty || node.Right.IsEmpty ? node : Make(node.Right.Key, node.Right.Value, Make(node.Key, node.Value, node.Left, node.Right.Left), node.Right.Right);
public static MapItem <K, V> Make <K, V>(K k, V v, MapItem <K, V> l, MapItem <K, V> r) => new MapItem <K, V>((byte)(1 + Math.Max(l.Height, r.Height)), l.Count + r.Count + 1, k, v, l, r);
public static MapItem <K, V> AddTreeToRight <K, V>(MapItem <K, V> node, MapItem <K, V> toAdd) => node.IsEmpty ? toAdd : Balance(Make(node.Key, node.Value, node.Left, AddTreeToRight(node.Right, toAdd)));