2

我想从包含三个元素的枚举中选择第三个元素,同时知道我已经选择了哪两个。比较枚举的最有效方法是什么?

编辑:

到目前为止,我想出了以下几点:

Drawable.Row alternateChoice = (Drawable.Row)ExtensionMethods.Extensions.DefaultChoice(new List<int>() { (int)chosenEnum1, (int)chosenEnum2 }, new List<int>() { 0, 1, 2 });

Drawable.Row 是枚举,第一个列表是已经选择的,第二个列表包含可能的选择。DefaultChoice 的定义如下。我知道它具有二次时间复杂性,这就是为什么我要求更好的解决方案:

public static int DefaultChoice(List<int> chosen, List<int> choices)
{
    bool found = false;
    foreach (int choice in choices)
    {
        foreach (int chosenInt in chosen)
        {
            if (chosenInt == choice)
            {
                found = true;
                break;
            }
        }
        if (!found)
        {
            return choice;
        }
        found = false;
    }
    return -1;
}
4

3 回答 3

3

尝试这个:

List<MyEnum> selectedValues = new List<MyEnum>();
selectedValues.Add(MyEnum.firstValue); // Add selected value
selectedValues.Add(MyEnum.secondValue); // Add selected value

List<MyEnum> valuesLeftOver = Enum.GetValues(typeof(MyEnum)).Cast<MyEnum>().ToList();

valuesLeftOver = valuesLeftOver.Except(selectedValues).ToList<MyEnum>(); // This will result in the remaining items (third item) being in valuesLeftOver list.

简短而简单的代码。

尽量不要太担心效率。如今,优化算法是如此强大,以至于您可能会得到相同的汇编代码来执行此操作,而不是尝试手动执行此操作。

于 2013-02-28T00:37:38.157 回答
2
[Flags]
public enum NumberEnum : byte
{
    None = 0,
    One = 1,
    Two = 2,
    Three = 4
};       

public string GetRemainingEnumItem(NumberEnum filterFlags = 0)
{  
 if (((filterFlags & NumberEnum.One) == NumberEnum.One) && ((filterFlags & NumberEnum.Two) == NumberEnum.Two))
 {
    //1 & 2 are selected so item 3 is what you want
 }

 if (((filterFlags & NumberEnum.One) == NumberEnum.One) && ((filterFlags & NumberEnum.Three) == NumberEnum.Three))
 {
    //1 & 3 are selected so item 2 is what you want
 }

 if (((filterFlags & NumberEnum.Three) == NumberEnum.Three) && ((filterFlags & NumberEnum.Two) == NumberEnum.Two))
 {
    //2 & 3 are selected so item 1 is what you want
 }
}

这就是你所说的:

var testVal = NumberEnum.One | NumberEnum.Two;
var resultWillEqual3 = GetRemainingEnumItem(testVal);
于 2013-02-28T00:33:05.187 回答
1

您可能想尝试这种方法...

public enum Marx {chico, groucho, harpo};

public Marx OtherOne(Marx x, Marx y)
{
  return (Marx)((int)Marx.chico + (int)Marx.groucho + (int)Marx.harpo - (int)x - (int)y);
} // OtherOne

// ...

Marx a = Marx.harpo;
Marx b = Marx.chico;
Marx other = OtherOne(a, b); // picks groucho

如果您使用标记为 [Flags] 的枚举,这可能很有用...

public class Enums2
{
  [Flags] public enum Bits {none = 0, aBit = 1, bBit = 2, cBit = 4, dBit = 8};
  public static readonly Bits allBits;

  static Enums2()
  {
    allBits = Bits.none;
    foreach (Bits b in Enum.GetValues(typeof(Bits)))
    {
      allBits |= b;
    }
  } // static ctor

  public static Bits OtherBits(Bits x)
  // Returns all the Bits not on in x
  {
    return x ^ allBits;
  } // OtherBits

  // ...

  Bits    someBits = Bits.aBit | Bits.dBit;
  Bits missingBits = OtherBits(someBits); // gives bBit and cBit

} // Enums2
于 2013-02-28T08:15:25.450 回答