using System;
using System.Text;
using System.IO;
using System.Collections;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;


namespace miew.Dictionary
{
	public static class Extensions
	{
		public static void RemoveWhere<TKey, TValue>(this Dictionary<TKey, TValue> d, Predicate<KeyValuePair<TKey, TValue>> f)
		{
			List<TKey> keys_to_remove = new List<TKey>();
			foreach (var kvp in d)
				if (f(kvp))
					keys_to_remove.Add(kvp.Key);
			foreach (TKey k in keys_to_remove)
				d.Remove(k);
		}

		public static void AddMany<TKey, TValue, TSrc>(
			this Dictionary<TKey, TValue> d,
			IEnumerable<TSrc> src,
			Converter<TSrc, TKey> key_selector,
			Converter<TSrc, TValue> value_selector)
		{
			foreach (TSrc e in src)
				d.Add(key_selector(e), value_selector(e));
		}

		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		/// <summary>
		/// 
		/// </summary>
		///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		public static TValue TryGetValue<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key)
		{
			TValue v;
			return dict.TryGetValue(key, out v) ? v : default(TValue);
		}
	};
}