You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

65 lines
1.6 KiB

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class CustomExtension
{
/// <summary>
/// insert value to list and sort.
/// </summary>
static public void OrderInsert<T>(this List<T> arr, T value, Func<T, T, int> comparison)
{
arr.Add(value);
for (int i = arr.Count - 1; i > 0; --i)
{
if (comparison(arr[i - 1], arr[i]) > 0)
{
T tmp = arr[i];
arr[i - 1] = arr[i];
arr[i] = tmp;
}
else
{
break;
}
}
}
/// <summary>
/// insert value to dictionary, if key is exist, replace value.
/// </summary>
static public void SafeInsert<TKey, TValue>(this IDictionary<TKey, TValue> dic, KeyValuePair<TKey, TValue> kvPair)
{
if (dic.ContainsKey(kvPair.Key))
dic[kvPair.Key] = kvPair.Value;
else
dic.Add(kvPair);
}
/// <summary>
/// insert value to dictionary, if key is exist, replace value.
/// </summary>
static public void SafeInsert<TKey, TValue>(this IDictionary<TKey, TValue> dic, TKey key, TValue value)
{
if (dic.ContainsKey(key))
dic[key] = value;
else
dic.Add(key, value);
}
/// <summary>
/// get value from dictionary, if key is not exist, return default value.
/// </summary>
static public TValue SafeGet<TKey, TValue>(this IDictionary<TKey, TValue> dic, TKey key)
{
if (dic.ContainsKey(key))
return dic[key];
else
return default;
}
}