簡體   English   中英

從枚舉中選擇剩余元素的最優雅方法

[英]Most elegant way of choosing the remaining element from an Enum

我想從包含三個元素的枚舉中選擇第三個元素,同時知道我已經選擇了哪兩個。 比較枚舉最有效的方法是什么?

編輯:

到目前為止,我已經提出了以下建議:

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;
}

嘗試這個:

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.

簡短的代碼。

盡量不要太擔心效率。 如今,優化算法是如此強大,以至於您可能會獲得相同的匯編代碼來執行此操作,而不是嘗試手動執行。

[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);

您可能想嘗試這種方法...

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM