mirror of
https://github.com/Ellpeck/DynamicEnums.git
synced 2024-12-22 12:19:24 +01:00
Added GetUniqueValues
This commit is contained in:
parent
6305ee9d8c
commit
c57f9fdbf6
3 changed files with 56 additions and 1 deletions
|
@ -200,6 +200,23 @@ namespace DynamicEnums {
|
|||
return DynamicEnum.GetValues(typeof(T)).Cast<T>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a collection of all of the enum values that are explicitly defined for the given dynamic enum type <typeparamref name="T"/>, excluding any explicitly defined combined flags.
|
||||
/// A value counts as explicitly defined if it has been added using <see cref="Add{T}"/>, <see cref="AddValue{T}"/> or <see cref="AddFlag{T}"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type whose values to get.</typeparam>
|
||||
/// <returns>The defined values for the given type, excluding combined flags.</returns>
|
||||
public static IEnumerable<T> GetUniqueValues<T>() where T : DynamicEnum {
|
||||
var used = BigInteger.Zero;
|
||||
foreach (var value in DynamicEnum.GetValues<T>()) {
|
||||
var iValue = DynamicEnum.GetValue(value);
|
||||
if ((used & iValue) == 0) {
|
||||
yield return value;
|
||||
used |= iValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a collection of all of the enum values that are explicitly defined for the given dynamic enum type <paramref name="type"/>.
|
||||
/// A value counts as explicitly defined if it has been added using <see cref="Add{T}"/>, <see cref="AddValue{T}"/> or <see cref="AddFlag{T}"/>.
|
||||
|
|
|
@ -21,6 +21,22 @@ namespace DynamicEnums {
|
|||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns an array containing all of the values of the given enum type, excluding any explicitly defined combined flags.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type whose enum to get.</typeparam>
|
||||
/// <returns>An enumerable of the values of the enum, in declaration order, excluding combined flags.</returns>
|
||||
public static IEnumerable<T> GetUniqueValues<T>() where T : struct, Enum {
|
||||
var used = 0L;
|
||||
foreach (var value in EnumHelper.GetValues<T>()) {
|
||||
var lValue = Convert.ToInt64(value);
|
||||
if ((used & lValue) == 0) {
|
||||
yield return value;
|
||||
used |= lValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the given <paramref name="value"/> has all of the given <see cref="Enum"/> flags on it.
|
||||
/// This operation is equivalent to <see cref="Enum.HasFlag"/>, but doesn't do any additional checks, making it faster.
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using DynamicEnums;
|
||||
using NUnit.Framework;
|
||||
|
@ -9,8 +10,18 @@ public class EnumTests {
|
|||
|
||||
[Test]
|
||||
public void TestRegularEnums() {
|
||||
Assert.AreEqual(
|
||||
new[] {TestEnum.Zero, TestEnum.One, TestEnum.Two, TestEnum.Eight, TestEnum.Sixteen, TestEnum.EightSixteen, TestEnum.ThirtyTwo, TestEnum.OneTwentyEight, TestEnum.OneTwentyEightTwoOne},
|
||||
EnumHelper.GetValues<TestEnum>());
|
||||
Assert.AreEqual(
|
||||
new[] {TestEnum.Zero, TestEnum.One, TestEnum.Two, TestEnum.Eight, TestEnum.Sixteen, TestEnum.ThirtyTwo, TestEnum.OneTwentyEight},
|
||||
EnumHelper.GetUniqueValues<TestEnum>());
|
||||
|
||||
Assert.AreEqual(
|
||||
new[] {TestEnum.One, TestEnum.Two, TestEnum.Eight, TestEnum.Sixteen, TestEnum.EightSixteen},
|
||||
EnumHelper.GetFlags(TestEnum.One | TestEnum.Sixteen | TestEnum.Eight | TestEnum.Two, false));
|
||||
Assert.AreEqual(
|
||||
new[] {TestEnum.Zero, TestEnum.One, TestEnum.Two, TestEnum.Eight, TestEnum.Sixteen, TestEnum.EightSixteen},
|
||||
EnumHelper.GetFlags(TestEnum.One | TestEnum.Sixteen | TestEnum.Eight | TestEnum.Two));
|
||||
|
||||
Assert.AreEqual(
|
||||
|
@ -35,9 +46,16 @@ public class EnumTests {
|
|||
var zero = DynamicEnum.Add<TestDynamicEnum>("Zero", 0);
|
||||
var combined = DynamicEnum.Add<TestDynamicEnum>("Combined", DynamicEnum.GetValue(DynamicEnum.Or(flags[7], flags[13])));
|
||||
|
||||
DynamicEnum.Add<TestEnumWithConstructor>("Test", 10);
|
||||
var test = DynamicEnum.Add<TestEnumWithConstructor>("Test", 10);
|
||||
Assert.AreEqual(DynamicEnum.GetEnumValue<TestEnumWithConstructor>(10).ToString(), "TestModified");
|
||||
|
||||
Assert.AreEqual(
|
||||
flags.Append(zero).Append(combined),
|
||||
DynamicEnum.GetValues<TestDynamicEnum>());
|
||||
Assert.AreEqual(
|
||||
flags.Append(zero),
|
||||
DynamicEnum.GetUniqueValues<TestDynamicEnum>());
|
||||
|
||||
Assert.AreEqual(DynamicEnum.GetValue(flags[7]), BigInteger.One << 7);
|
||||
Assert.AreEqual(DynamicEnum.GetEnumValue<TestDynamicEnum>(BigInteger.One << 75), flags[75]);
|
||||
|
||||
|
@ -73,6 +91,9 @@ public class EnumTests {
|
|||
Assert.AreEqual(
|
||||
new[] {flags[0], flags[7], flags[13], combined},
|
||||
DynamicEnum.GetFlags(DynamicEnum.Or(DynamicEnum.Or(flags[0], flags[13]), flags[7]), false));
|
||||
Assert.AreEqual(
|
||||
new[] {flags[0], flags[7], flags[13], zero, combined},
|
||||
DynamicEnum.GetFlags(DynamicEnum.Or(DynamicEnum.Or(flags[0], flags[13]), flags[7])));
|
||||
|
||||
Assert.AreEqual(
|
||||
new[] {flags[0], flags[7], flags[13]},
|
||||
|
@ -82,6 +103,7 @@ public class EnumTests {
|
|||
[Flags]
|
||||
private enum TestEnum {
|
||||
|
||||
Zero = 0,
|
||||
One = 1,
|
||||
Two = 2,
|
||||
Eight = 8,
|
||||
|
|
Loading…
Reference in a new issue