using System; using System.Collections.Generic; namespace Nanomesh { public static class CollectionUtils { public static T[] ToArray(this HashSet items, ref T[] array) { int i = 0; foreach (T item in items) { array[i++] = item; } return array; } public static bool TryAdd(this Dictionary dictionary, K key, V value) { if (dictionary.ContainsKey(key)) { return false; } dictionary.Add(key, value); return true; } public static bool TryAdd(this Dictionary dictionary, K key, Func valueFactory) { if (dictionary.ContainsKey(key)) { return false; } dictionary.Add(key, valueFactory(key)); return true; } public static V GetOrAdd(this Dictionary dictionary, K key, V value) { if (dictionary.TryGetValue(key, out V existingValue)) { return existingValue; } dictionary.Add(key, value); return value; } } }