Bidirectional Map List

I need to save 2 values ​​in one list so that I have all the positions and my controls on my board in one list. I used the dictionary, but I found out that there is only one mapping of the path. Does anyone have any recommendations other than a two-dimensional array?

+5
source share
2 answers

You can use the dictionary quite easily, like two-way matching, if you don't care about the linear search performance for reverse matching (which you would get with a 2D array):

var dictionary = new Dictionary<string, int>();
// Fill it up...
int forwardMapResult = dictionary["SomeKey"];
string reverseMapResult = dictionary.Where(kvp => kvp.Value == 5).First().Key;

, - . , ​​ SQLite.

+6

:

 public class BiDirectionalDictionary<L, R>
{
    private readonly Dictionary<L, R> leftToRight = new Dictionary<L, R>();
    private readonly Dictionary<R, L> rightToLeft = new Dictionary<R, L>();
    public void Add(L leftSide, R rightSide)
    {
        if (leftToRight.ContainsKey(leftSide) ||
            rightToLeft.ContainsKey(rightSide))
            throw new DuplicateNameException();
        leftToRight.Add(leftSide, rightSide);
        rightToLeft.Add(rightSide, leftSide);
    }
    public L this[R rightSideKey]
    { get { return rightToLeft[rightSideKey]; } }
    public R this[L leftSideKey]
    { get { return leftToRight[leftSideKey]; } }
    public bool ContainsKey(L leftSideKey)
    { return leftToRight.ContainsKey(leftSideKey); }
    public bool ContainsKey(R rightSideKey)
    { return rightToLeft.ContainsKey(rightSideKey); }
}
 [Serializable]
public class DuplicateNameException : SystemException
{
    protected DuplicateNameException(
           SerializationInfo info, StreamingContext context);
    public DuplicateNameException();
    public DuplicateNameException(string s);
    public DuplicateNameException(string message, 
           Exception innerException);
}

, ... , ,

var myBiDireDict = new BiDirectionalDictionary<DateTime, DateTime>();
+7

All Articles