Collections.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. namespace SharpCifs.Util.Sharpen
  5. {
  6. internal static class Collections<T>
  7. {
  8. static readonly IList<T> Empty = new T[0];
  9. public static IList<T> EmptySet
  10. {
  11. get { return Empty; }
  12. }
  13. }
  14. public static class Collections
  15. {
  16. public static bool AddAll<T>(ICollection<T> list, IEnumerable toAdd)
  17. {
  18. foreach (T t in toAdd)
  19. list.Add(t);
  20. return true;
  21. }
  22. public static TV Remove<TK, TV>(IDictionary<TK, TV> map, TK toRemove) where TK : class
  23. {
  24. TV local;
  25. if (map.TryGetValue(toRemove, out local))
  26. {
  27. map.Remove(toRemove);
  28. return local;
  29. }
  30. return default(TV);
  31. }
  32. public static T[] ToArray<T>(ICollection<T> list)
  33. {
  34. T[] array = new T[list.Count];
  35. list.CopyTo(array, 0);
  36. return array;
  37. }
  38. public static T[] ToArray<T>(List<object> list)
  39. {
  40. T[] array = new T[list.Count];
  41. for (int c = 0; c < list.Count; c++)
  42. {
  43. array[c] = (T)list[c];
  44. }
  45. return array;
  46. }
  47. public static TU[] ToArray<T, TU>(ICollection<T> list, TU[] res) where T : TU
  48. {
  49. if (res.Length < list.Count)
  50. res = new TU[list.Count];
  51. int n = 0;
  52. foreach (T t in list)
  53. res[n++] = t;
  54. if (res.Length > list.Count)
  55. res[list.Count] = default(T);
  56. return res;
  57. }
  58. public static IDictionary<TK, TV> EmptyMap<TK, TV>()
  59. {
  60. return new Dictionary<TK, TV>();
  61. }
  62. public static IList<T> EmptyList<T>()
  63. {
  64. return Collections<T>.EmptySet;
  65. }
  66. public static ICollection<T> EmptySet<T>()
  67. {
  68. return Collections<T>.EmptySet;
  69. }
  70. public static IList<T> NCopies<T>(int n, T elem)
  71. {
  72. List<T> list = new List<T>(n);
  73. while (n-- > 0)
  74. {
  75. list.Add(elem);
  76. }
  77. return list;
  78. }
  79. public static void Reverse<T>(IList<T> list)
  80. {
  81. int end = list.Count - 1;
  82. int index = 0;
  83. while (index < end)
  84. {
  85. T tmp = list[index];
  86. list[index] = list[end];
  87. list[end] = tmp;
  88. ++index;
  89. --end;
  90. }
  91. }
  92. public static ICollection<T> Singleton<T>(T item)
  93. {
  94. List<T> list = new List<T>(1);
  95. list.Add(item);
  96. return list;
  97. }
  98. public static IList<T> SingletonList<T>(T item)
  99. {
  100. List<T> list = new List<T>(1);
  101. list.Add(item);
  102. return list;
  103. }
  104. public static IList<T> SynchronizedList<T>(IList<T> list)
  105. {
  106. return new SynchronizedList<T>(list);
  107. }
  108. public static ICollection<T> UnmodifiableCollection<T>(ICollection<T> list)
  109. {
  110. return list;
  111. }
  112. public static IList<T> UnmodifiableList<T>(IList<T> list)
  113. {
  114. return new ReadOnlyCollection<T>(list);
  115. }
  116. public static ICollection<T> UnmodifiableSet<T>(ICollection<T> list)
  117. {
  118. return list;
  119. }
  120. public static IDictionary<TK, TV> UnmodifiableMap<TK, TV>(IDictionary<TK, TV> dict)
  121. {
  122. return dict;
  123. }
  124. }
  125. }