C# .NET 中的多图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/380595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 01:18:20  来源:igfitidea点击:

multimap in .NET

c#multimap

提问by Schildmeijer

I need an equivalent to c++'s std::multimap<K, V, Comp, Alloc>in C-sharp. Does it exist in the standard library?

我需要一个相当于std::multimap<K, V, Comp, Alloc>C-sharp 中的 C ++ 。它是否存在于标准库中?

采纳答案by Frans Bouma

Because it's almost christmas :)

因为快到圣诞节了 :)

//////////////////////////////////////////////////////////////////////
// Algorithmia is (c) 2008 Solutions Design. All rights reserved.
// http://www.sd.nl
//////////////////////////////////////////////////////////////////////
// COPYRIGHTS:
// Copyright (c) 2008 Solutions Design. All rights reserved.
// 
// The Algorithmia library sourcecode and its accompanying tools, tests and support code
// are released under the following license: (BSD2)
// ----------------------------------------------------------------------
// Redistribution and use in source and binary forms, with or without modification, 
// are permitted provided that the following conditions are met: 
//
// 1) Redistributions of source code must retain the above copyright notice, this list of 
//    conditions and the following disclaimer. 
// 2) Redistributions in binary form must reproduce the above copyright notice, this list of 
//    conditions and the following disclaimer in the documentation and/or other materials 
//    provided with the distribution. 
// 
// THIS SOFTWARE IS PROVIDED BY SOLUTIONS DESIGN ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOLUTIONS DESIGN OR CONTRIBUTORS BE LIABLE FOR 
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
//
// The views and conclusions contained in the software and documentation are those of the authors 
// and should not be interpreted as representing official policies, either expressed or implied, 
// of Solutions Design. 
//
//////////////////////////////////////////////////////////////////////
// Contributers to the code:
//      - Frans  Bouma [FB]
//////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SD.Tools.Algorithmia.UtilityClasses;

namespace SD.Tools.Algorithmia.GeneralDataStructures
{
    /// <summary>
    /// Extension to the normal Dictionary. This class can store more than one value for every key. It keeps a HashSet for every Key value.
    /// Calling Add with the same Key and multiple values will store each value under the same Key in the Dictionary. Obtaining the values
    /// for a Key will return the HashSet with the Values of the Key. 
    /// </summary>
    /// <typeparam name="TKey">The type of the key.</typeparam>
    /// <typeparam name="TValue">The type of the value.</typeparam>
    public class MultiValueDictionary<TKey, TValue> : Dictionary<TKey, HashSet<TValue>>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="MultiValueDictionary&lt;TKey, TValue&gt;"/> class.
        /// </summary>
        public MultiValueDictionary()
            : base()
        {
        }


        /// <summary>
        /// Adds the specified value under the specified key
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        public void Add(TKey key, TValue value)
        {
            ArgumentVerifier.CantBeNull(key, "key");

            HashSet<TValue> container = null;
            if(!this.TryGetValue(key, out container))
            {
                container = new HashSet<TValue>();
                base.Add(key, container);
            }
            container.Add(value);
        }


        /// <summary>
        /// Determines whether this dictionary contains the specified value for the specified key 
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        /// <returns>true if the value is stored for the specified key in this dictionary, false otherwise</returns>
        public bool ContainsValue(TKey key, TValue value)
        {
            ArgumentVerifier.CantBeNull(key, "key");
            bool toReturn = false;
            HashSet<TValue> values = null;
            if(this.TryGetValue(key, out values))
            {
                toReturn = values.Contains(value);
            }
            return toReturn;
        }


        /// <summary>
        /// Removes the specified value for the specified key. It will leave the key in the dictionary.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        public void Remove(TKey key, TValue value)
        {
            ArgumentVerifier.CantBeNull(key, "key");

            HashSet<TValue> container = null;
            if(this.TryGetValue(key, out container))
            {
                container.Remove(value);
                if(container.Count <= 0)
                {
                    this.Remove(key);
                }
            }
        }


        /// <summary>
        /// Merges the specified multivaluedictionary into this instance.
        /// </summary>
        /// <param name="toMergeWith">To merge with.</param>
        public void Merge(MultiValueDictionary<TKey, TValue> toMergeWith)
        { 
            if(toMergeWith==null)
            {
                return;
            }

            foreach(KeyValuePair<TKey, HashSet<TValue>> pair in toMergeWith)
            {
                foreach(TValue value in pair.Value)
                {
                    this.Add(pair.Key, value);
                }
            }
        }


        /// <summary>
        /// Gets the values for the key specified. This method is useful if you want to avoid an exception for key value retrieval and you can't use TryGetValue
        /// (e.g. in lambdas)
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="returnEmptySet">if set to true and the key isn't found, an empty hashset is returned, otherwise, if the key isn't found, null is returned</param>
        /// <returns>
        /// This method will return null (or an empty set if returnEmptySet is true) if the key wasn't found, or
        /// the values if key was found.
        /// </returns>
        public HashSet<TValue> GetValues(TKey key, bool returnEmptySet)
        {
            HashSet<TValue> toReturn = null;
            if(!base.TryGetValue(key, out toReturn) && returnEmptySet)
            {
                toReturn = new HashSet<TValue>();
            }
            return toReturn;
        }
    }
}

回答by Jon Skeet

ILookupmay be good enough for you - but unfortunately there are no public implementations. You've basically got to go through Enumerable.ToLookup, as far as I'm aware. That means it's a "build once" kind of map - you can't add to it later. However, if that's all you need it's handy to be able to just use the built-in stuff.

ILookup可能对你来说已经足够了 - 但不幸的是没有公共实现。据我所知,您基本上必须通过Enumerable.ToLookup。这意味着它是一种“构建一次”类型的地图 - 以后无法添加。但是,如果这就是您所需要的,那么能够使用内置的东西就很方便了。

回答by Marc Gravell

In .NET 3.5, there is an ILookup<,>that represents this. The regular implementation (Lookup<,>) is immutable, but I have an EditableLookup<,>in MiscUtilthat should do the job nicely.

在 .NET 3.5 中,有一个ILookup<,>代表这一点。常规实现 ( Lookup<,>) 是不可变的,但我EditableLookup<,>MiscUtil中有一个应该可以很好地完成这项工作。

回答by Amitd

you can try this

你可以试试这个

C# MultiMap Generic Class

C# MultiMap 泛型类

http://dotnetperls.com/multimap

http://dotnetperls.com/multimap

Very simple multimap implementation.

非常简单的多图实现。

回答by cos

you can find a c# multimap collection @ http://code.google.com/p/self-balancing-avl-tree/. it's based on a self balancing avl tree which is also included with the code.

你可以找到 ac# multimap collection @ http://code.google.com/p/self-balancing-avl-tree/。它基于自平衡 avl 树,该树也包含在代码中。

回答by Qwertie

Consider using BMultiMap<K,V>if your collection as a whole is large. It saves a lot of memory compared to Dictionary<K,List<V>>, especially if the number of values associated with each key is often small.

BMultiMap<K,V>如果您的收藏作为一个整体很大,请考虑使用。与 相比Dictionary<K,List<V>>,它节省了大量内存,尤其是当与每个键关联的值的数量通常很少时。