public IEnumerator <KeyValuePair <TKey, TValue> > GetEnumerator() { var iterator = new TreeIterator(this); while (iterator.MoveNext()) { yield return(Kvp.Of(iterator.Current.Key, iterator.Current.Value)); } }
/// <summary> /// (Implementation) The GroupBy operator. Results are returned as a concrete key-value map, and group elements are stored as a concrete collection type. /// </summary> /// <typeparam name="TROuterMap">The type of the return map.</typeparam> /// <typeparam name="TRInnerSeq">The type of the iterable collection used to store group elements.</typeparam> /// <typeparam name="TElem2">The return type of the value selector.</typeparam> /// <typeparam name="TKey">The type of the key.</typeparam> /// <param name="mapFactory">A prototype instance of the returned key-value map, used as a builder factory.</param> /// <param name="seqFactory">A prototype instance of the grouping iterable collection, used as a builder factory.</param> /// <param name="keySelector">The key selector.</param> /// <param name="valueSelector">The value selector.</param> /// <param name="eq">The eq.</param> /// <returns></returns> protected virtual TROuterMap _GroupBy <TROuterMap, TRInnerSeq, TElem2, TKey>( TROuterMap mapFactory, TRInnerSeq seqFactory, Func <TElem, TKey> keySelector, Func <TElem, TElem2> valueSelector, IEqualityComparer <TKey> eq) where TROuterMap : IBuilderFactory <IMapBuilder <TKey, TRInnerSeq, TROuterMap> > where TRInnerSeq : IBuilderFactory <IIterableBuilder <TElem2, TRInnerSeq> > { return(_GroupBy(mapFactory, keySelector, valueSelector, (k, vs) => { var theSeq = seqFactory.ToIterable(vs); return Kvp.Of(k, theSeq); }, eq)); }
public IEnumerator <KeyValuePair <TKey, TValue> > GetEnumerator() { var iterator = new TreeIterator(this); while (iterator.MoveNext()) { var bucket = iterator.Current.Bucket; while (!bucket.IsEmpty) { yield return(Kvp.Of(bucket.Key, bucket.Value)); bucket = bucket.Next; } } }
/// <summary> /// Returns the set-thereotic intersection between the two trees, and applies the collision resolution function on each /// shared key-value pair. O(min(m+n,nlogm)) where n ≤ m. <br /> /// If the collision resolution function is null, it is ignored and the value is kept is arbitrary. /// </summary> /// <param name="other"></param> /// <param name="lineage"></param> /// <param name="collision"></param> /// <returns></returns> public Node Intersect(Node other, Lineage lineage, ValueSelector <TKey, TValue, TValue, TValue> collision = null) { var intersection = IntersectElements(other); var list = new List <KeyValuePair <TKey, TValue> >(); foreach (var pair in intersection) { var newValue = collision == null ? pair.First.Value : collision(pair.First.Key, pair.First.Value, pair.Second.Value); list.Add(Kvp.Of(pair.First.Key, newValue)); } #if ASSERTS list.Count.AssertEqual(x => x <= Count && x <= other.Count); Debug_Intersect(list.Select(x => x.Key).ToList(), other); #endif return(FromSortedList(list, 0, list.Count - 1, Comparer, lineage)); }
static void Main(string[] args) { ImmList <int> list = ImmList.Of(0, 1, 2); //access to the ends: list = list.AddLast(3).AddFirst(-1); list = list.AddLastRange(new[] { 4, 5, 6 }); list = list.AddFirstRange(list); list = list.RemoveLast().RemoveFirst(); //indexing: int firstItem = list[0]; list = list.Update(0, firstItem + 1); list = list.Insert(0, firstItem); //slices: ImmList <int> sublist = list[1, 3]; //slices in range [1, 3], inclusive. ImmSet <int> set = ImmSet.Of(0, 1, 2); //add and remove: set = set.Add(3).Remove(0); //set-theoretic operations: set = set.Union(new[] { 5, 6 }); set = set.Except(new[] { 5 }); set = set.Intersect(set); ImmMap <int, int> map = ImmMap.Of(Kvp.Of(1, 1), Kvp.Of(2, 2), Kvp.Of(3, 3)); map = map.Add(4, 4); map = map.Remove(2); int value = map[1]; }
/// <summary> /// Tries to find a value matching the specified key. O(logn) /// </summary> /// <param name="key"></param> /// <returns></returns> public Optional <KeyValuePair <TKey, TValue> > FindKvp(TKey key) { var cur = this; while (!cur.IsEmpty) { var r = Comparer.Compare(key, cur.Key); if (r < 0) { cur = cur.Left; } else if (r > 0) { cur = cur.Right; } else { return(Kvp.Of(cur.Key, cur.Value)); } } return(Optional.None); }
public static void Set <TKey, TValue>(this IAnyMapBuilder <TKey, TValue> builder, TKey key, TValue value) { builder.Add(Kvp.Of(key, value)); }
public static Option <KeyValuePair <TKey, TValue> > Kvp <TKey, TValue>(TKey key, TValue value) { return(Kvp <TKey, TValue>(key)(value)); }