Advertisement
Krythic

EnumToggleCollection

Nov 9th, 2020
1,058
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace VoidwalkerEngine.Framework.Collections
  6. {
  7.     public class EnumToggleCollection<TEnum>
  8.         where TEnum : struct, IComparable, IConvertible, IFormattable
  9.     {
  10.         private static Dictionary<TEnum, int> _indexMap;
  11.         private static TEnum[] _keys;
  12.         private bool[] _states;
  13.  
  14.         public TEnum[] Keys
  15.         {
  16.             get
  17.             {
  18.                 return _keys;
  19.             }
  20.         }
  21.  
  22.         public bool this[TEnum key]
  23.         {
  24.             get
  25.             {
  26.                 return GetState(key);
  27.             }
  28.             set
  29.             {
  30.                 SetState(key, value);
  31.             }
  32.         }
  33.  
  34.         public bool this[int key]
  35.         {
  36.             get
  37.             {
  38.                 return GetState(Keys[key]);
  39.             }
  40.             set
  41.             {
  42.                 SetState(Keys[key], value);
  43.             }
  44.         }
  45.  
  46.         static EnumToggleCollection()
  47.         {
  48.             if (!typeof(TEnum).IsEnum)
  49.             {
  50.                 throw new Exception("Only Enum Types can be used with EnumToggleCollection!");
  51.             }
  52.             _keys = (TEnum[])Enum.GetValues(typeof(TEnum));
  53.             _indexMap = new Dictionary<TEnum, int>(_keys.Length);
  54.             for (int i = 0; i < _keys.Length; i++)
  55.             {
  56.                 _indexMap.Add(_keys[i], 0);
  57.             }
  58.         }
  59.  
  60.         public EnumToggleCollection()
  61.         {
  62.             this._states = new bool[Keys.Length];
  63.         }
  64.  
  65.         public EnumToggleCollection(IList<TEnum> keys, bool state = true)
  66.             : this()
  67.         {
  68.             Toggle(keys, state);
  69.         }
  70.  
  71.         /// <summary>
  72.         /// Copies the state of another EnumToggleCollection
  73.         /// </summary>
  74.         /// <param name="other"></param>
  75.         public void Copy(EnumToggleCollection<TEnum> other)
  76.         {
  77.             for(int i = 0; i < other._states.Length; i++)
  78.             {
  79.                 this._states[i] = other._states[i];
  80.             }
  81.         }
  82.  
  83.         /// <summary>
  84.         /// Toggles the current state of the deesired Enum key.
  85.         /// </summary>
  86.         /// <param name="key"></param>
  87.         /// <param name="state"></param>
  88.         public void Toggle(TEnum key, bool state)
  89.         {
  90.             this[key] = state;
  91.         }
  92.  
  93.         /// <summary>
  94.         /// Retrives all keys that have the desired state.
  95.         /// </summary>
  96.         /// <param name="state"></param>
  97.         /// <returns></returns>
  98.         public TEnum[] GetByState(bool state)
  99.         {
  100.             List<TEnum> results = new List<TEnum>();
  101.             foreach(TEnum key in this.Keys)
  102.             {
  103.                 if (this.GetState(key) == state)
  104.                 {
  105.                     results.Add(key);
  106.                 }
  107.             }
  108.             return results.ToArray();
  109.         }
  110.  
  111.         /// <summary>
  112.         /// Toggles the current state of the desired Enum Keys
  113.         /// </summary>
  114.         /// <param name="keys"></param>
  115.         /// <param name="state"></param>
  116.         public void Toggle(IList<TEnum> keys, bool state = true)
  117.         {
  118.             for (int i = 0; i < keys.Count(); i++)
  119.             {
  120.                 Toggle(keys[i], state);
  121.             }
  122.         }
  123.  
  124.         /// <summary>
  125.         /// Gets the current state of the desired Enum Key.
  126.         /// </summary>
  127.         /// <param name="key"></param>
  128.         /// <returns></returns>
  129.         private bool GetState(TEnum key)
  130.         {
  131.             return this._states[_indexMap[key]];
  132.         }
  133.  
  134.         /// <summary>
  135.         /// Sets the current state of the desired Enum key.
  136.         /// </summary>
  137.         /// <param name="key"></param>
  138.         /// <param name="state"></param>
  139.         private void SetState(TEnum key, bool state)
  140.         {
  141.             this._states[_indexMap[key]] = state;
  142.         }
  143.  
  144.         /// <summary>
  145.         /// Clears the current state of the desired Enum key.
  146.         /// </summary>
  147.         /// <param name="item"></param>
  148.         public void Clear(TEnum item)
  149.         {
  150.             this[item] = false;
  151.         }
  152.  
  153.         /// <summary>
  154.         /// Clears the current state of all existing Enum keys.
  155.         /// </summary>
  156.         public void Clear()
  157.         {
  158.             Array.Clear(this._states, 0, this.Keys.Length);
  159.         }
  160.     }
  161. }
  162.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement