// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. using System; using System.Collections.Generic; using System.Linq; namespace System.Reactive { class Lookup : ILookup { Dictionary> d; public Lookup(IEqualityComparer comparer) { d = new Dictionary>(comparer); } public void Add(K key, E element) { var list = default(List); if (!d.TryGetValue(key, out list)) d[key] = list = new List(); list.Add(element); } public bool Contains(K key) { return d.ContainsKey(key); } public int Count { get { return d.Count; } } public IEnumerable this[K key] { get { return Hide(d[key]); } } private IEnumerable Hide(List elements) { foreach (var x in elements) yield return x; } public IEnumerator> GetEnumerator() { foreach (var kv in d) yield return new Grouping(kv); } class Grouping : IGrouping { KeyValuePair> kv; public Grouping(KeyValuePair> kv) { this.kv = kv; } public K Key { get { return kv.Key; } } public IEnumerator GetEnumerator() { return kv.Value.GetEnumerator(); } Collections.IEnumerator Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); } } Collections.IEnumerator Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); } } }