using Microsoft.Xna.Framework;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
namespace OpenVIII
{
///
/// class to add function to dictionary
///
///
///
public static class DictionaryEx
{
#region Methods
public static void AddRange(this Dictionary dic, Dictionary dicToAdd) => dicToAdd.ForEach(x => dic.Add(x.Key, x.Value));
public static void AddRangeNewOnly(this Dictionary dic, Dictionary dicToAdd) => dicToAdd.ForEach(x => { if (!dic.ContainsKey(x.Key)) dic.Add(x.Key, x.Value); });
public static void AddRangeOverride(this Dictionary dic, Dictionary dicToAdd) => dicToAdd.ForEach(x => dic[x.Key] = x.Value);
public static bool ContainsKeys(this Dictionary dic, IEnumerable keys)
{
bool result = false;
keys.ForEachOrBreak((x) => { result = dic.ContainsKey(x); return result; });
return result;
}
public static void ForEach(this IEnumerable source, Action action)
{
foreach (T item in source)
action(item);
}
public static void ForEachOrBreak(this IEnumerable source, Func func)
{
foreach (T item in source)
{
bool result = func(item);
if (result) break;
}
}
///
///
///
///
///
///
///
public static T GetKey(this OrderedDictionary dictionary, int index)
{
if (dictionary == null)
{
return default;
}
try
{
return (T)dictionary.Cast().ElementAt(index).Key;
}
catch (Exception)
{
return default;
}
}
///
/// Get Value from ordered dictionary
///
/// Key type
/// Value type
///
///
///
///
public static U GetValue(this OrderedDictionary dictionary, T key)
{
if (dictionary == null)
{
return default;
}
try
{
return (U)dictionary.Cast().AsQueryable().Single(kvp => ((T)kvp.Key).Equals(key)).Value;
}
catch (Exception)
{
return default;
}
}
///
/// Reverses Key and Value of dictionary.
///
///
///
///
///
public static Dictionary Reverse(this IDictionary source)
{
Dictionary dictionary = new Dictionary();
foreach (KeyValuePair entry in source)
{
if (!dictionary.ContainsKey(entry.Value))
dictionary.Add(entry.Value, entry.Key);
}
return dictionary;
}
public static Vector3 ToVector3(this Vector2 v, float z = 0f)
=> new Vector3(v.X, v.Y, z);
#endregion Methods
}
//public class _OrderedDictionay : OrderedDictionary
//{
// public _OrderedDictionay(int capacity) : base(capacity)
// {
// }
// #region Indexers
// public new KeyValuePair this[int index]
// {
// get
// {
// T key = this.GetKey(index);
// U val = this.GetValue(key);
// return new KeyValuePair(key, val);
// }
// }
// public U this[T key] => this.GetValue(key);
// #endregion Indexers
//}
}