using System.Collections.Generic;
namespace NEG.Utils.Collections
{
    public static class DictionaryExtensions
    {
        /// 
        /// Adds given value to a dictionary if there was no element at given , replaces element with  otherwise.
        /// 
        /// true if element was added, false if it was replaced
        public static bool AddOrUpdate(this Dictionary dict, K key, V value)
        {
            if (dict.ContainsKey(key))
            {
                dict[key] = value;
                return false;
            }
            else
            {
                dict.Add(key, value);
                return true;
            }
        }
        /// 
        /// Gets a value from the dictionary under a specified key or adds it if did not exist and returns .
        /// 
        /// value under a given  if it exists,  otherwise
        public static V GetOrSetToDefault(this Dictionary dict, K key, V defaultValue)
        {
            if (dict.TryGetValue(key, out V value))
            {
                return value;
            }
            dict.Add(key, defaultValue);
            return defaultValue;
        }
    }
}