Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Lookup.cs « Internal « Reactive « System.Reactive.Linq « Rx.NET - github.com/mono/rx.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 239f10d1c0524088197fa5dd640d4b0c0b362c11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// 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<K, E> : ILookup<K, E>
    {
        Dictionary<K, List<E>> d;

        public Lookup(IEqualityComparer<K> comparer)
        {
            d = new Dictionary<K, List<E>>(comparer);
        }

        public void Add(K key, E element)
        {
            var list = default(List<E>);
            if (!d.TryGetValue(key, out list))
                d[key] = list = new List<E>();
            list.Add(element);
        }

        public bool Contains(K key)
        {
            return d.ContainsKey(key);
        }

        public int Count
        {
            get { return d.Count; }
        }

        public IEnumerable<E> this[K key]
        {
            get { return Hide(d[key]); }
        }

        private IEnumerable<E> Hide(List<E> elements)
        {
            foreach (var x in elements)
                yield return x;
        }

        public IEnumerator<IGrouping<K, E>> GetEnumerator()
        {
            foreach (var kv in d)
                yield return new Grouping(kv);
        }

        class Grouping : IGrouping<K, E>
        {
            KeyValuePair<K, List<E>> kv;

            public Grouping(KeyValuePair<K, List<E>> kv)
            {
                this.kv = kv;
            }

            public K Key
            {
                get { return kv.Key; }
            }

            public IEnumerator<E> GetEnumerator()
            {
                return kv.Value.GetEnumerator();
            }

            Collections.IEnumerator Collections.IEnumerable.GetEnumerator()
            {
                return GetEnumerator();
            }
        }

        Collections.IEnumerator Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}