using System; using System.Collections.Generic; using System.Linq; namespace HeuristicLab.Problems.ProgramSynthesis.Push.Extensions { public static class EnumExtensions { public static T ToMask(this IEnumerable values) where T : struct, IConvertible { if (!typeof(T).IsEnum) throw new ArgumentException("T must be an enumerated type."); var builtValue = Enum .GetValues(typeof(T)) .Cast() .Where(values.Contains) .Aggregate(0, (current, value) => current | Convert.ToInt32(value)); return (T)Enum.Parse(typeof(T), builtValue.ToString()); } public static IEnumerable ToValues(this T flags) where T : struct, IConvertible { if (!typeof(T).IsEnum) throw new ArgumentException("T must be an enumerated type."); var inputInt = (int)(object)flags; foreach (T value in Enum.GetValues(typeof(T))) { var valueInt = (int)(object)value; if (0 != (valueInt & inputInt)) { yield return value; } } } } }