0

我需要一个更强大的类字典结构来获得:

  • 通过提供键值(默认字典行为);
  • 通过提供一个值(不是那么微不足道)。

另外,我想扩展这个类字典结构的功能,以便一个键可以与多个值相关联。

通过这个讨论,这个答案和另一个答案提供了实现它的工具。我决定从 BiDictionary(Jon Skeet 的回答)中删除“按索引访问”,因为歧义会比我想要的更频繁地发生(例如,将字符串映射到字符串时)。我想出的类似字典的“结构”是:

using System.Collections.Generic;

public interface IBiLookup<TLeft, TRight>
{
    IDictionary<TLeft, ICollection<TRight>> LeftToRight { get; }
    IDictionary<TRight, ICollection<TLeft>> RightToLeft { get; }

    bool TryGetByLeft(TLeft left, out ICollection<TRight> rights);
    bool TryGetByRight(TRight right, out ICollection<TLeft> lefts);

    void Add(TLeft left, TRight right);
}
public class BiLookup<TLeft, TRight> : IBiLookup<TLeft, TRight>
{
    public IDictionary<TLeft, ICollection<TRight>> LeftToRight
    {
        get { return this.leftToRight; }
    }
    public IDictionary<TRight, ICollection<TLeft>> RightToLeft
    {
        get { return this.rightToLeft; }
    }

    public bool TryGetByLeft(TLeft left, out ICollection<TRight> rights)
    {
        return LeftToRight.TryGetValue(left, out rights);
    }
    public bool TryGetByRight(TRight right, out ICollection<TLeft> lefts)
    {
        return RightToLeft.TryGetValue(right, out lefts);
    }

    public void Add(TLeft left, TRight right)
    {
        AddLeftToRight(left, right);
        AddRightToLeft(right, left);
    }

    private void AddLeftToRight(TLeft left, TRight right)
    {
        ICollection<TRight> rights;

        // 1) Is there an entry associated with the "left" value?
        // 2) If so, is the "right" value already associated?
        if (!TryGetByLeft(left, out rights))
        {
            // Then we have to add an entry in the leftToRight dictionary.
            rights = new List<TRight> { right };                
        }
        else
        {
            // So there are entries associated with the "left" value.
            // We must verify if the "right" value itself is not there.
            if (((List<TRight>)rights).FindIndex(element => element.Equals(right)) < 0)
            {
                // We don't have that association yet.
                rights.Add(right);
            }
            else
            {
                // The value is already in the list: do nothing.
                return;
            }
        }

        LeftToRight[left] = rights;
    }
    private void AddRightToLeft(TRight right, TLeft left)
    {
        ICollection<TLeft> lefts;

        // 1) Is there an entry associated with the "right" value?
        // 2) If so, is the "left" value already associated?
        if (!TryGetByRight(right, out lefts))
        {
            // Then we have to add an entry in the leftToRight dictionary.
            lefts = new List<TLeft> { left };
        }
        else
        {
            // So there are entries associated with the "right" value.
            // We must verify if the "right" value itself is not there.
            if (((List<TLeft>)lefts).FindIndex(element => element.Equals(left)) < 0)
            {
                // We don't have that association yet.
                lefts.Add(left);
            }
            else
            {
                // The value is already in the list: do nothing.
                return;
            }
        }

        RightToLeft[right] = lefts;
    }

    #region Fields

    private IDictionary<TLeft, ICollection<TRight>> leftToRight = new Dictionary<TLeft, ICollection<TRight>>();
    private IDictionary<TRight, ICollection<TLeft>> rightToLeft = new Dictionary<TRight, ICollection<TLeft>>();

    #endregion
}

我担心将 Add(...) 方法拆分为两个更具体的方法是否是一个很好的实现,因为 BiLookup 类将被广泛使用并且需要牢记性能。此外,这个线程的目的是讨论接口和类实现是否尽可能好,或者可以改进它们。

如果我将接口和“默认”实现都保存到类库项目中,那么设计是否足以重复使用?

4

1 回答 1

0

如果不实际使用它,就无法查看“设计是否足够好,可以重复使用”。您需要尝试以几种不同的方式使用您的代码(所有单元测试都算作一种),看看它是否有意义并鼓励编写可读的代码。

如果您没有任何特定要求(性能或其他),而只是确保代码的单元测试覆盖率良好,则所有测试都已通过并且您的预期使用场景已被测试覆盖。

代码注释:强制转换(即((List<TLeft>)lefts)看起来没有必要。FindIndex调用(使用 O(number_of_items) 进行线性搜索)在性能敏感代码中看不到家。

于 2013-03-05T17:29:58.267 回答