Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace VoidwalkerEngine.Framework.Collections
- {
- public class EnumToggleCollection<TEnum>
- where TEnum : struct, IComparable, IConvertible, IFormattable
- {
- private static Dictionary<TEnum, int> _indexMap;
- private static TEnum[] _keys;
- private bool[] _states;
- public TEnum[] Keys
- {
- get
- {
- return _keys;
- }
- }
- public bool this[TEnum key]
- {
- get
- {
- return GetState(key);
- }
- set
- {
- SetState(key, value);
- }
- }
- public bool this[int key]
- {
- get
- {
- return GetState(Keys[key]);
- }
- set
- {
- SetState(Keys[key], value);
- }
- }
- static EnumToggleCollection()
- {
- if (!typeof(TEnum).IsEnum)
- {
- throw new Exception("Only Enum Types can be used with EnumToggleCollection!");
- }
- _keys = (TEnum[])Enum.GetValues(typeof(TEnum));
- _indexMap = new Dictionary<TEnum, int>(_keys.Length);
- for (int i = 0; i < _keys.Length; i++)
- {
- _indexMap.Add(_keys[i], 0);
- }
- }
- public EnumToggleCollection()
- {
- this._states = new bool[Keys.Length];
- }
- public EnumToggleCollection(IList<TEnum> keys, bool state = true)
- : this()
- {
- Toggle(keys, state);
- }
- /// <summary>
- /// Copies the state of another EnumToggleCollection
- /// </summary>
- /// <param name="other"></param>
- public void Copy(EnumToggleCollection<TEnum> other)
- {
- for(int i = 0; i < other._states.Length; i++)
- {
- this._states[i] = other._states[i];
- }
- }
- /// <summary>
- /// Toggles the current state of the deesired Enum key.
- /// </summary>
- /// <param name="key"></param>
- /// <param name="state"></param>
- public void Toggle(TEnum key, bool state)
- {
- this[key] = state;
- }
- /// <summary>
- /// Retrives all keys that have the desired state.
- /// </summary>
- /// <param name="state"></param>
- /// <returns></returns>
- public TEnum[] GetByState(bool state)
- {
- List<TEnum> results = new List<TEnum>();
- foreach(TEnum key in this.Keys)
- {
- if (this.GetState(key) == state)
- {
- results.Add(key);
- }
- }
- return results.ToArray();
- }
- /// <summary>
- /// Toggles the current state of the desired Enum Keys
- /// </summary>
- /// <param name="keys"></param>
- /// <param name="state"></param>
- public void Toggle(IList<TEnum> keys, bool state = true)
- {
- for (int i = 0; i < keys.Count(); i++)
- {
- Toggle(keys[i], state);
- }
- }
- /// <summary>
- /// Gets the current state of the desired Enum Key.
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- private bool GetState(TEnum key)
- {
- return this._states[_indexMap[key]];
- }
- /// <summary>
- /// Sets the current state of the desired Enum key.
- /// </summary>
- /// <param name="key"></param>
- /// <param name="state"></param>
- private void SetState(TEnum key, bool state)
- {
- this._states[_indexMap[key]] = state;
- }
- /// <summary>
- /// Clears the current state of the desired Enum key.
- /// </summary>
- /// <param name="item"></param>
- public void Clear(TEnum item)
- {
- this[item] = false;
- }
- /// <summary>
- /// Clears the current state of all existing Enum keys.
- /// </summary>
- public void Clear()
- {
- Array.Clear(this._states, 0, this.Keys.Length);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement