Collections.cs 2.8 KB

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