为什么在C#中Lookup是不可变的?

19
Dictionary 不同的是,你不能逐个添加元素来构建 Lookup。你知道原因吗? Lookup 就像 C++ 中的 multimap,为什么我们不能在 C# 中修改它呢?如果真的不能,那我们如何在 C# 中构建一个 multimap 数据结构呢?
3个回答

20
"Lookup" 和 "ILookup" 是作为 LINQ 的一部分引入的,与框架的其他方面相比,LINQ 通常采用更加函数式的方法。就个人而言,我喜欢 "Lookup" 至少在公开方面是不可变的事实,而且我期待着更多不可变集合可用
如果您想创建自己的 multimap 数据结构,只需维护一个类似于 Dictionary<TKey, List<TValue>> 的数据结构即可。您可能需要查看我的 Edulinq 实现的 Lookup 来获取一些示例代码。"

1

我曾经遇到过同样的问题和疑问。为什么Lookup是不可变的?我通过一些扩展方法解决了这个问题,使用了IDictionary。

public static void Add<TKey,TList,TItem>(this IDictionary<TKey,TList> dict,TKey key,TItem item)
        where TList : ICollection<TItem>,new()
    {
        if(!dict.ContainsKey(key))
        {
            dict.Add(key, new TList());
        }
        dict[key].Add(item);
    }

    public static void Remove<TKey, TList, TItem>(this IDictionary<TKey, TList> dict, TKey key)
        where TList : IEnumerable<TItem>, new()
    {
        if (dict.ContainsKey(key))
        {
            dict.Remove(key);
        }
    }

    public static TList Items<TKey, TList, TItem>(this IDictionary<TKey, TList> dict, TKey key)
        where TList : IEnumerable<TItem>, new()
    {
        if (dict.ContainsKey(key))
        {
            return dict[key];
        }
        return default(TList);            
    }

1
我认为将其命名为“Add”是不可取的,因为有人可能会误用内置的“Dictionary”方法“Add”。应该使用类似于“AddChild”或“AddToValueCollection”的名称。 - ErikE

1
这是我写的一个实现:

Here is an implementation I wrote

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class MultiLookup<Key, Value> : ILookup<Key, Value>
{
    Dictionary<Key, HashSet<Value>> Contents = new Dictionary<Key, HashSet<Value>>();

    public void Add(Key key, Value value)
    {
        if (!Contains(key))
        {
            Contents[key]=new HashSet<Value>();
        }
        Contents[key].Add(value);
    }

    public void Add(IEnumerable<Tuple<Key, Value>> items)
    {
        foreach (var item in items)
        {
            Add(item.Item1, item.Item2);
        }
    }

    public void Remove(Key key, Value value)
    {
        if (!Contains(key))
        {
            return;
        }
        Contents[key].Remove(value);
        if (Contents[key].Count==0)
        {
            Contents.Remove(key);
        }
    }

    public void RemoveKey(Key key)
    {
        Contents.Remove(key);
    }

    public IEnumerable<Key> Keys
    {
        get
        {
            return Contents.Keys;
        }
    }

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

    public bool Contains(Key key)
    {
        return Contents.ContainsKey(key);
    }


    private class Grouping : IGrouping<Key, Value>
    {
        public MultiLookup<Key, Value> _source;
        public Key _key;

        public Key Key
        {
            get { return _key; }
        }

        public static HashSet<Value> Empty = new HashSet<Value>();
        public IEnumerator<Value> GetEnumerator()
        {
            if (!_source.Contains(_key))
            {
                yield break;
            }
            else
            {
                foreach (var item in _source[_key])
                {
                    yield return item;
                }
            }
        }

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

    public IEnumerator<IGrouping<Key, Value>> GetEnumerator()
    {
        return (from p in Contents
               select new Grouping() { _key = p.Key, _source = this }).GetEnumerator();
    }

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


    public IEnumerable<Value> this[Key key]
    {
        get { return Contents[key]; }
    }
}

并为您提供一个测试用例(可能不是详尽无遗的),供您参考。

using FluentAssertions;
using System.Linq;
using Xunit;

public class MultiLookupSpec
{
    MultiLookup<int, string> Fixture = new MultiLookup<int,string>();

    [Fact]
    public void NewLookupShouldBeEmpty()
    {
        Fixture.Count.Should().Be(0);
    }

    [Fact]
    public void AddingANewValueToANonExistentKeyShouldCreateKeyAndAddValue()
    {
        Fixture.Add(0, "hello");
        Fixture.Count.Should().Be(1);
    }

    [Fact]
    public void AddingMultipleValuesToAKeyShouldGenerateMultipleValues()
    {
        Fixture.Add(0, "hello");
        Fixture.Add(0, "cat");
        Fixture.Add(0, "dog");

        Fixture[0].Should().BeEquivalentTo(new []{"hello", "cat", "dog"});
    }

    [Fact]
    public void RemovingAllElementsOfKeyWillAlsoRemoveKey()
    {
        Fixture.Add(0, "hello");
        Fixture.Add(0, "cat");
        Fixture.Add(0, "dog");

        Fixture.Remove(0, "dog");            
        Fixture.Remove(0, "cat");            
        Fixture.Remove(0, "hello");

        Fixture.Contains(0).Should().Be(false);
    }

    [Fact]
    public void EnumerationShouldWork()
    {
        Fixture.Add(0, "hello");
        Fixture.Add(0, "cat");
        Fixture.Add(0, "dog");
        Fixture.Add(1, "house");
        Fixture.Add(2, "pool");
        Fixture.Add(2, "office");

        Fixture.Select(s => s.Key).Should().Contain(new[] { 0, 1, 2 });
        Fixture.SelectMany(s => s).Should().Contain(new[] { "hello", "cat", "dog", "house", "pool", "office" });

    }




}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接