我需要在C-sharp中與C++的std::multimap<K, V, Comp, Alloc>
等效。它存在於標準庫中嗎?multimap
multimap
回答
因爲它幾乎聖誕節:)
//////////////////////////////////////////////////////////////////////
// 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<TKey, TValue>"/> 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;
}
}
}
編輯:我misspoke - 我原本以爲這打破了LSP(它*可能*) - 但我認爲它沒關係,因爲它沒有隱藏任何基類方法,它們仍應按預期工作。向下投票恢復:) – mhand 2017-12-01 20:48:15
ILookup可能是你不夠好 - 但遺憾的是沒有公開的實現。就我所知,你基本上必須通過Enumerable.ToLookup。這意味着它是一種「構建一次」的地圖 - 你以後不能添加。但是,如果這就是你所需要的,那麼只需使用內置的東西就可以方便使用。
在.NET 3.5中,有一個代表這個的ILookup<,>
。常規實施(Lookup<,>
)是不可變的,但我有一個EditableLookup<,>
在MiscUtil應該很好地做這項工作。
方便,但我沒有看到許可證信息,這意味着它不是真正可用的,因爲:/ – 2018-03-01 21:51:35
你可以找到c#multimap collection @http://code.google.com/p/self-balancing-avl-tree/。它基於代碼中包含的自平衡avl樹。
考慮使用BMultiMap<K,V>
如果您的收藏整體很大。與Dictionary<K,List<V>>
相比,它可以節省大量的內存,特別是如果與每個密鑰關聯的值的數量通常很少。
- 1. Multimap的Multimap Java - 分類時間表
- 2. MultiMap/Reduce - Counts = 0?
- 3. C/C++ MultiMap庫
- 4. C++對Multimap之
- 5. C++ Multimap操作
- 6. Multimap不排序
- 7. 的Node.js - Multimap之
- 8. Multimap to JSON
- 9. MultiMap Table with Guava
- 10. Multimap和shared_ptr
- 11. std :: multimap和equal_range
- 12. multimap vs地圖集
- 13. Java Multimap搜索值
- 14. print multique包含multimap
- 15. std :: multimap的用例
- 16. C++ STL-like Bidirectional Multimap
- 17. Multimap之在Hibernate中
- 18. C++ Multimap - 在向multimap插入新值之前檢查重複項?
- 19. 訪問由嵌套multimap中的multimap :: equal_range返回的所有值
- 20. 如何將MultiMap <String,String>轉換爲MultiMap <String,Integer>?
- 21. 番石榴Multimap containsEntry問題
- 22. 從multimap中通過按鍵
- 23. 使用JSP迭代Multimap
- 24. Multimap與值HashMultiset的值
- 25. 斯卡拉 - 與Multimap之
- 26. multimap的返回內容
- 27. 番石榴Multimap列表
- 28. 使用set實例化Multimap?
- 29. Java相當於C++ multimap
- 30. 有問題的std :: multimap中
大部分時間在正常詞典中使用列表已足夠 – Casebash 2010-06-15 05:58:30