3 回答

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個(gè)贊
using System;using System.Collections.Generic;using System.Text;class BiDictionary<TFirst, TSecond>{ IDictionary<TFirst, IList<TSecond>> firstToSecond = new Dictionary<TFirst, IList<TSecond>>(); IDictionary<TSecond, IList<TFirst>> secondToFirst = new Dictionary<TSecond, IList<TFirst>>(); private static IList<TFirst> EmptyFirstList = new TFirst[0]; private static IList<TSecond> EmptySecondList = new TSecond[0]; public void Add(TFirst first, TSecond second) { IList<TFirst> firsts; IList<TSecond> seconds; if (!firstToSecond.TryGetValue(first, out seconds)) { seconds = new List<TSecond>(); firstToSecond[first] = seconds; } if (!secondToFirst.TryGetValue(second, out firsts)) { firsts = new List<TFirst>(); secondToFirst[second] = firsts; } seconds.Add(second); firsts.Add(first); } // Note potential ambiguity using indexers (e.g. mapping from int to int) // Hence the methods as well... public IList<TSecond> this[TFirst first] { get { return GetByFirst(first); } } public IList<TFirst> this[TSecond second] { get { return GetBySecond(second); } } public IList<TSecond> GetByFirst(TFirst first) { IList<TSecond> list; if (!firstToSecond.TryGetValue(first, out list)) { return EmptySecondList; } return new List<TSecond>(list); // Create a copy for sanity } public IList<TFirst> GetBySecond(TSecond second) { IList<TFirst> list; if (!secondToFirst.TryGetValue(second, out list)) { return EmptyFirstList; } return new List<TFirst>(list); // Create a copy for sanity }}class Test{ static void Main() { BiDictionary<int, string> greek = new BiDictionary<int, string>(); greek.Add(1, "Alpha"); greek.Add(2, "Beta"); greek.Add(5, "Beta"); ShowEntries(greek, "Alpha"); ShowEntries(greek, "Beta"); ShowEntries(greek, "Gamma"); } static void ShowEntries(BiDictionary<int, string> dict, string key) { IList<int> values = dict[key]; StringBuilder builder = new StringBuilder(); foreach (int value in values) { if (builder.Length != 0) { builder.Append(", "); } builder.Append(value); } Console.WriteLine("{0}: [{1}]", key, builder); }}

TA貢獻(xiàn)1155條經(jīng)驗(yàn) 獲得超0個(gè)贊
我剛剛注意到您想要從Value映射到多個(gè)鍵-我把這個(gè)解決方案留給單個(gè)值版本,但是接下來我將為多條目雙向映射添加另一個(gè)答案。
using System;using System.Collections.Generic;class BiDictionary<TFirst, TSecond>{ IDictionary<TFirst, TSecond> firstToSecond = new Dictionary<TFirst, TSecond>(); IDictionary<TSecond, TFirst> secondToFirst = new Dictionary<TSecond, TFirst>(); public void Add(TFirst first, TSecond second) { if (firstToSecond.ContainsKey(first) || secondToFirst.ContainsKey(second)) { throw new ArgumentException("Duplicate first or second"); } firstToSecond.Add(first, second); secondToFirst.Add(second, first); } public bool TryGetByFirst(TFirst first, out TSecond second) { return firstToSecond.TryGetValue(first, out second); } public bool TryGetBySecond(TSecond second, out TFirst first) { return secondToFirst.TryGetValue(second, out first); }}class Test{ static void Main() { BiDictionary<int, string> greek = new BiDictionary<int, string>(); greek.Add(1, "Alpha"); greek.Add(2, "Beta"); int x; greek.TryGetBySecond("Beta", out x); Console.WriteLine(x); }}

TA貢獻(xiàn)1886條經(jīng)驗(yàn) 獲得超2個(gè)贊
var greek = new Dictionary<int, string> { { 1, "Alpha" }, { 2, "Alpha" } };
greek.WhatDoIPutHere("Alpha")
?
IEnumerable<T>
IEnumerable<TKey> KeysFromValue<TKey, TValue>(this Dictionary<TKey, TValue> dict, TValue val){ if (dict == null) { throw new ArgumentNullException("dict"); } return dict.Keys.Where(k => dict[k] == val);}var keys = greek.KeysFromValue("Beta"); int exceptionIfNotExactlyOne = greek.KeysFromValue("Beta").Single();
- 3 回答
- 0 關(guān)注
- 661 瀏覽
添加回答
舉報(bào)