PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/NetTopologySuite/Utilities/CollectionUtil.cs

http://nettopologysuite.googlecode.com/
C# | 181 lines | 81 code | 15 blank | 85 comment | 2 complexity | 9417817360486001b69764a3281a899f MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. #if SILVERLIGHT
  4. using ArrayList = System.Collections.Generic.List<object>;
  5. #endif
  6. namespace NetTopologySuite.Utilities
  7. {
  8. /// <summary>
  9. /// Executes a transformation function on each element of a collection
  10. /// and returns the results in a new List.
  11. /// </summary>
  12. public class CollectionUtil
  13. {
  14. /// <summary>
  15. ///
  16. /// </summary>
  17. /// <typeparam name="T"></typeparam>
  18. /// <param name="obj"></param>
  19. /// <returns></returns>
  20. public delegate T FunctionDelegate<T>(T obj);
  21. /// <summary>
  22. ///
  23. /// </summary>
  24. /// <typeparam name="T"></typeparam>
  25. /// <typeparam name="TResult"></typeparam>
  26. /// <param name="obj"></param>
  27. /// <returns></returns>
  28. public delegate TResult FunctionDelegate< T, TResult>(T obj);
  29. /// <summary>
  30. /// Executes a function on each item in a <see cref="ICollection" />
  31. /// and returns the results in a new <see cref="IList" />.
  32. /// </summary>
  33. /// <param name="coll"></param>
  34. /// <param name="func"></param>
  35. /// <returns></returns>
  36. public static IList Transform(ICollection coll, FunctionDelegate<object> func)
  37. {
  38. IList result = new ArrayList();
  39. foreach(object obj in coll)
  40. result.Add(func(obj));
  41. return result;
  42. }
  43. /// <summary>
  44. /// Executes a function on each item in a <see cref="ICollection" />
  45. /// and returns the results in a new <see cref="IList" />.
  46. /// </summary>
  47. /// <param name="coll"></param>
  48. /// <returns></returns>
  49. public static IList<T> Cast<T>(ICollection coll)
  50. {
  51. IList<T> result = new List<T>(coll.Count);
  52. foreach (var obj in coll)
  53. result.Add((T)obj);
  54. return result;
  55. }
  56. /// <summary>
  57. /// Executes a function on each item in a <see cref="ICollection{TIn}" />
  58. /// and returns the results in a new <see cref="IList{TOut}" />.
  59. /// </summary>
  60. /// <param name="coll"></param>
  61. /// <returns></returns>
  62. public static IList<TOut> Cast<TIn, TOut>(ICollection<TIn> coll)
  63. where TIn: class
  64. where TOut : class
  65. {
  66. IList<TOut> result = new List<TOut>(coll.Count);
  67. foreach (var obj in coll)
  68. result.Add(obj as TOut);
  69. return result;
  70. }
  71. /// <summary>
  72. /// Executes a function on each item in a <see cref="IList{T}" />
  73. /// and returns the results in a new <see cref="IList{T}" />.
  74. /// </summary>
  75. /// <param name="list"></param>
  76. /// <param name="function"></param>
  77. /// <returns></returns>
  78. public static IList<T> Transform<T>(IList<T> list, FunctionDelegate<T> function)
  79. {
  80. IList<T> result = new List<T>(list.Count);
  81. foreach (T item in list)
  82. result.Add(function(item));
  83. return result;
  84. }
  85. /// <summary>
  86. /// Executes a function on each item in a <see cref="IList{T}" />
  87. /// and returns the results in a new <see cref="IList{TResult}" />.
  88. /// </summary>
  89. /// <param name="list"></param>
  90. /// <param name="function"></param>
  91. /// <returns></returns>
  92. public static IList<TResult> Transform<T, TResult>(IList<T> list, FunctionDelegate<T, TResult> function)
  93. {
  94. IList<TResult> result = new List<TResult>(list.Count);
  95. foreach (T item in list)
  96. result.Add(function(item));
  97. return result;
  98. }
  99. /// <summary>
  100. /// Executes a function on each item in a <see cref="ICollection" />
  101. /// but does not accumulate the result.
  102. /// </summary>
  103. /// <param name="coll"></param>
  104. /// <param name="func"></param>
  105. public static void Apply(ICollection coll, FunctionDelegate<object> func)
  106. {
  107. foreach (object obj in coll)
  108. func(obj);
  109. }
  110. /// <summary>
  111. /// Executes a function on each item in a <see cref="IEnumerable{T}" />
  112. /// but does not accumulate the result.
  113. /// </summary>
  114. /// <param name="coll"></param>
  115. /// <param name="func"></param>
  116. public static void Apply<T>(IEnumerable<T> coll, FunctionDelegate<T> func)
  117. {
  118. foreach (var obj in coll)
  119. func(obj);
  120. }
  121. /// <summary>
  122. /// Executes a function on each item in a <see cref="ICollection" />
  123. /// and collects all the entries for which the result
  124. /// of the function is equal to <c>true</c>.
  125. /// </summary>
  126. /// <param name="coll"></param>
  127. /// <param name="func"></param>
  128. /// <returns></returns>
  129. public static IList Select(ICollection coll, FunctionDelegate<object, bool> func)
  130. {
  131. IList result = new ArrayList();
  132. foreach (object obj in coll)
  133. if (func(obj))
  134. result.Add(obj);
  135. return result;
  136. }
  137. /// <summary>
  138. /// Executes a function on each item in a <see cref="ICollection" />
  139. /// and collects all the entries for which the result
  140. /// of the function is equal to <c>true</c>.
  141. /// </summary>
  142. /// <param name="items"></param>
  143. /// <param name="func"></param>
  144. /// <returns></returns>
  145. public static IList<T> Select<T>(IEnumerable<T> items, FunctionDelegate<T, bool> func)
  146. {
  147. IList<T> result = new List<T>();
  148. foreach (var obj in items)
  149. if (func(obj)) result.Add(obj);
  150. return result;
  151. }
  152. /// <summary>
  153. /// Copies <typeparamref name="T"/>s in an array to an object array
  154. /// </summary>
  155. /// <typeparam name="TIn"></typeparam>
  156. /// <typeparam name="TOut"></typeparam>
  157. /// <param name="array">the source array</param>
  158. /// <returns>An array of objects</returns>
  159. public static TOut[] Cast<TIn,TOut>(TIn[] array)
  160. {
  161. var res = new TOut[array.Length];
  162. System.Array.Copy(array, res, array.Length);
  163. return res;
  164. }
  165. }
  166. }