1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- public class KVPair<Key, Value>
- {
- public Key key;
- public Value value;
- public KVPair()
- {
- }
- public KVPair(Key key, Value value)
- {
- this.key = key;
- this.value = value;
- }
- }
- public static class DictionaryExtension
- {
- public static bool IsAvailable<Key, Value>(this Dictionary<Key, Value> dictionary)
- {
- return dictionary != null && dictionary.Count > 0;
- }
- public static KVPair<Key, Value> GetAtIndex<Key, Value>(this Dictionary<Key, Value> dictionary, int index)
- {
- List<Key> keys = dictionary.Keys.ToList();
- List<Value> values = dictionary.Values.ToList();
- KVPair<Key, Value> kvPair = new KVPair<Key, Value>();
- kvPair.key = keys[index];
- kvPair.value = values[index];
- return kvPair;
- }
- public static int MyMin<Key, Value>(this Dictionary<Key, Value> dictionary, Func<Key, Value, float> func)
- {
- List<Key> keys = dictionary.Keys.ToList();
- List<Value> values = dictionary.Values.ToList();
- float min = func(keys[0], values[0]);
- int minIndex = 0;
- for (int i = 1; i < keys.Count; i++)
- {
- if (min > func(keys[i], values[i]))
- {
- min = func(keys[i], values[i]);
- minIndex = i;
- }
- }
- return minIndex;
- }
- public static int MyMax<Key, Value>(this Dictionary<Key, Value> dictionary, Func<Key, Value, float> func)
- {
- List<Key> keys = dictionary.Keys.ToList();
- List<Value> values = dictionary.Values.ToList();
- float max = func(keys[0], values[0]);
- int maxIndex = 0;
- for (int i = 1; i < keys.Count; i++)
- {
- if (max < func(keys[i], values[i]))
- {
- max = func(keys[i], values[i]);
- maxIndex = i;
- }
- }
- return maxIndex;
- }
- }
|