Collections.cs 2.8 KB

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