using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; namespace SharpCifs.Util.Sharpen { internal static class Collections { static readonly IList Empty = new T[0]; public static IList EmptySet { get { return Empty; } } } public static class Collections { public static bool AddAll(ICollection list, IEnumerable toAdd) { foreach (T t in toAdd) list.Add(t); return true; } public static TV Remove(IDictionary map, TK toRemove) where TK : class { TV local; if (map.TryGetValue(toRemove, out local)) { map.Remove(toRemove); return local; } return default(TV); } public static T[] ToArray(ICollection list) { T[] array = new T[list.Count]; list.CopyTo(array, 0); return array; } public static T[] ToArray(List list) { T[] array = new T[list.Count]; for (int c = 0; c < list.Count; c++) { array[c] = (T)list[c]; } return array; } public static TU[] ToArray(ICollection list, TU[] res) where T : TU { if (res.Length < list.Count) res = new TU[list.Count]; int n = 0; foreach (T t in list) res[n++] = t; if (res.Length > list.Count) res[list.Count] = default(T); return res; } public static IDictionary EmptyMap() { return new Dictionary(); } public static IList EmptyList() { return Collections.EmptySet; } public static ICollection EmptySet() { return Collections.EmptySet; } public static IList NCopies(int n, T elem) { List list = new List(n); while (n-- > 0) { list.Add(elem); } return list; } public static void Reverse(IList list) { int end = list.Count - 1; int index = 0; while (index < end) { T tmp = list[index]; list[index] = list[end]; list[end] = tmp; ++index; --end; } } public static ICollection Singleton(T item) { List list = new List(1); list.Add(item); return list; } public static IList SingletonList(T item) { List list = new List(1); list.Add(item); return list; } public static IList SynchronizedList(IList list) { return new SynchronizedList(list); } public static ICollection UnmodifiableCollection(ICollection list) { return list; } public static IList UnmodifiableList(IList list) { return new ReadOnlyCollection(list); } public static ICollection UnmodifiableSet(ICollection list) { return list; } public static IDictionary UnmodifiableMap(IDictionary dict) { return dict; } } }