Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Extensions/EnumExtensions.cs @ 15287

Last change on this file since 15287 was 15033, checked in by pkimmesw, 8 years ago

#2665 Added missing files

File size: 1.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4
5namespace HeuristicLab.Problems.ProgramSynthesis.Push.Extensions {
6  public static class EnumExtensions {
7    public static T ToMask<T>(this IEnumerable<T> values) where T : struct, IConvertible {
8      if (!typeof(T).IsEnum)
9        throw new ArgumentException("T must be an enumerated type.");
10
11      var builtValue = Enum
12        .GetValues(typeof(T))
13        .Cast<T>()
14        .Where(values.Contains)
15        .Aggregate(0, (current, value) => current | Convert.ToInt32(value));
16
17      return (T)Enum.Parse(typeof(T), builtValue.ToString());
18    }
19
20    public static IEnumerable<T> ToValues<T>(this T flags) where T : struct, IConvertible {
21      if (!typeof(T).IsEnum)
22        throw new ArgumentException("T must be an enumerated type.");
23
24      var inputInt = (int)(object)flags;
25      foreach (T value in Enum.GetValues(typeof(T))) {
26        var valueInt = (int)(object)value;
27        if (0 != (valueInt & inputInt)) {
28          yield return value;
29        }
30      }
31    }
32  }
33}
Note: See TracBrowser for help on using the repository browser.