Exemplo n.º 1
0
        /// <summary>
        /// Creates a new ShuffleBag with all possible enum values.
        /// </summary>
        public EnumShuffleBag()
        {
            DebugUtils.AssertIsEnumType <T>();

            _elements = new List <T>((T[])Enum.GetValues(typeof(T)));
            _cursor   = _elements.Count - 1;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a new ShuffleBag with some enum values excluded.
        /// </summary>
        /// <param name="excludedValues">The excluded values, these are not put into the bag</param>
        public EnumShuffleBag(params T[] excludedValues)
        {
            DebugUtils.AssertIsEnumType <T>();

            _elements = new List <T>();

            T[] values = (T[])Enum.GetValues(typeof(T));
            for (int i = 0; i < values.Length; i++)
            {
                bool isExcluded = false;
                for (int j = 0; j < excludedValues.Length; j++)
                {
                    if (excludedValues[j].Equals(values[i]))
                    {
                        isExcluded = true;
                        break;
                    }
                }

                if (!isExcluded)
                {
                    _elements.Add(values[i]);
                }
            }

            _cursor = _elements.Count - 1;
        }
Exemplo n.º 3
0
        public void SetEnumFlags <T>(bool value, params T[] enumFlags) where T : struct, IComparable, IConvertible, IFormattable
        {
            DebugUtils.AssertIsEnumType <T>();

            for (int i = 0; i < enumFlags.Length; i++)
            {
                SetFlag((int)(object)enumFlags[i], value);
            }
        }
Exemplo n.º 4
0
        public static BitMask EnumMask <T>(bool defaultValue = false) where T : struct, IComparable, IConvertible, IFormattable
        {
            DebugUtils.AssertIsEnumType <T>();

            BitMask mask = new BitMask(EnumUtils.GetCount(typeof(T)));

            if (defaultValue)
            {
                mask.SetAllFlags(true);
            }

            return(mask);
        }
Exemplo n.º 5
0
        public static BitMask EnumMask <T>(params T[] setEnumValues) where T : struct, IComparable, IConvertible, IFormattable
        {
            DebugUtils.AssertIsEnumType <T>();

            BitMask mask = new BitMask(EnumUtils.GetCount(typeof(T)));

            for (int i = 0; i < setEnumValues.Length; i++)
            {
                mask.SetFlag((int)(object)setEnumValues[i], true);
            }

            return(mask);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Checks the total number of items for an enum type.
        /// </summary>
        /// <typeparam name="enumType">The enum type</typeparam>
        /// <returns>The number of enum values</returns>
        public static int GetCount(Type enumType)
        {
            DebugUtils.AssertIsEnumType(enumType);

            int count;

            if (!_enumLengths.TryGetValue(enumType, out count))
            {
                count = Enum.GetValues(enumType).Length;
                _enumLengths.Add(enumType, count);
            }

            return(count);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Returns a random enum value that is not also a member of a set of excluded values.
        /// </summary>
        /// <typeparam name="T">The enum type</typeparam>
        /// <param name="excludedChoices">The list of disallowed choices</param>
        /// <returns>The chosen enum value</returns>
        public static T ChooseEnumValueExluding <T>(params T[] excludedChoices) where T : struct, IComparable, IConvertible, IFormattable
        {
            Assert.IsNotNull(excludedChoices);
            Assert.IsFalse(excludedChoices.Length >= EnumUtils.GetCount <T>());
            DebugUtils.AssertIsEnumType <T>();

            List <T> choices = new List <T>(Enum.GetValues(typeof(T)) as T[]);

            for (int i = 0; i < excludedChoices.Length; i++)
            {
                choices.Remove(excludedChoices[i]);
            }

            return(choices[Random.Range(0, choices.Count)]);
        }
Exemplo n.º 8
0
        public T[] GetTrueEnumFlags <T>() where T : struct, IComparable, IConvertible, IFormattable
        {
            DebugUtils.AssertIsEnumType <T>();

            List <T> trueFlags = new List <T>();

            for (int i = 0; i < _numFlags; i++)
            {
                if (GetFlag(i))
                {
                    trueFlags.Add((T)(object)i);
                }
            }

            return(trueFlags.ToArray());
        }
Exemplo n.º 9
0
        /// <summary>
        /// Checks the number items with positive integers as underlying values for an enum type. No.te that 0 is also treated as positive
        /// </summary>
        /// <typeparam name="T">The enum type, must have be an underlying int type</typeparam>
        /// <returns>The number of positive values</returns>
        public static int CountPositive <T>() where T : struct, IComparable, IConvertible, IFormattable
        {
            DebugUtils.AssertIsEnumType <T>();
            Assert.IsTrue(Enum.GetUnderlyingType(typeof(T)) == typeof(int));
            Array values = Enum.GetValues(typeof(T));
            int   count  = 0;

            for (int i = 0; i < values.Length; i++)
            {
                if ((int)values.GetValue(i) >= 0)
                {
                    count++;
                }
            }

            return(count);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Checks the number items with unqiue underlying values for an enum type.
        /// </summary>
        /// <typeparam name="T">The enum type</typeparam>
        /// <returns>The number of unique values</returns>
        public static int CountUnique <T>() where T : struct, IComparable, IConvertible, IFormattable
        {
            DebugUtils.AssertIsEnumType <T>();
            Array     values = Enum.GetValues(typeof(T));
            ArrayList set    = new ArrayList(values.Length);


            for (int i = 0; i < values.Length; i++)
            {
                if (!set.Contains(values.GetValue(i)))
                {
                    set.Add(values.GetValue(i));
                }
            }

            return(set.Count);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Returns a random enum value using a probalistic weight for each enum value.
        /// </summary>
        /// <typeparam name="T">The enum type</typeparam>
        /// <param name="weights">The list of weights corresponding to the list of choices
        /// <returns>The chosen enum value</returns>
        public static T ChooseEnumValue <T>(IList <float> weights) where T : struct, IComparable, IConvertible, IFormattable
        {
            DebugUtils.AssertIsEnumType <T>();
            Assert.IsNotNull(weights);
            T[] choices = Enum.GetValues(typeof(T)) as T[];

            Assert.IsTrue(choices.Length == weights.Count, "Number of weights (" + weights.Count + ") does not match number of enum values (" + choices.Length + ").");

            float totalWeight = 0;

            for (int i = 0; i < choices.Length; i++)
            {
                weights[i]   = Mathf.Max(0, weights[i]);
                totalWeight += weights[i];
            }

            if (totalWeight == 0)
            {
                throw new UnityException("Error choosing value: All probability weights are zero.");
            }

            float choice = Random.value * totalWeight;

            totalWeight = 0;

            for (int i = 0; i < choices.Length; i++)
            {
                if (weights[i] == 0)
                {
                    continue;
                }
                if (totalWeight + weights[i] > choice)
                {
                    return(choices[i]);
                }
                totalWeight += weights[i];
            }

            throw new UnityException("Choose algorithm is broken?");
        }
Exemplo n.º 12
0
        public void SetEnumFlag <T>(T enumFlag, bool value, bool expandIfNecessary = false) where T : struct, IComparable, IConvertible, IFormattable
        {
            DebugUtils.AssertIsEnumType <T>();

            SetFlag((int)(object)enumFlag, value, expandIfNecessary);
        }
Exemplo n.º 13
0
 /// <summary>
 /// Returns a random enum value with uniform probability.
 /// </summary>
 /// <typeparam name="T">The enum type</typeparam>
 /// <returns>The chosen enum value</returns>
 public static T ChooseEnumValue <T>() where T : struct, IComparable, IConvertible, IFormattable
 {
     DebugUtils.AssertIsEnumType <T>();
     T[] choices = Enum.GetValues(typeof(T)) as T[];
     return(choices[Random.Range(0, choices.Length)]);
 }
Exemplo n.º 14
0
        public void InvertEnumFlag <T>(T enumFlag) where T : struct, IComparable, IConvertible, IFormattable
        {
            DebugUtils.AssertIsEnumType <T>();

            InvertFlag((int)(object)enumFlag);
        }
Exemplo n.º 15
0
        public bool GetEnumFlagIfItExists <T>(T enumFlag) where T : struct, IComparable, IConvertible, IFormattable
        {
            DebugUtils.AssertIsEnumType <T>();

            return(GetFlagIfItExists((int)(object)enumFlag));
        }
Exemplo n.º 16
0
 /// <summary>
 /// Returns previous next value in an enum after a specific value. Or the last value if the current value is the first in the enum. ASSUMES THE ENUM VALUES START AT 0 AND ARE ALL ONE INTEGER APART.
 /// </summary>
 /// <typeparam name="T">The enum type</typeparam>
 /// <param name="currentValue">The current enum value</param>
 /// <returns>The previous (wrapping) value in the enum</returns>
 public static T GetPreviousValueWrapped <T>(T currentValue) where T : struct, IComparable, IConvertible, IFormattable
 {
     DebugUtils.AssertIsEnumType <T>();
     return((T)(object)MathUtils.Wrap((int)(object)currentValue - 1, 0, Enum.GetValues(typeof(T)).Length - 1));
 }
Exemplo n.º 17
0
        public bool HasEnumFlag <T>(T enumFlag) where T : struct, IComparable, IConvertible, IFormattable
        {
            DebugUtils.AssertIsEnumType <T>();

            return(HasFlag((int)(object)enumFlag));
        }