PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/ExtensionMethods/ExtensionMethods.Dictionary.cs

http://github.com/fredericaltorres/DynamicSugarNet
C# | 196 lines | 126 code | 20 blank | 50 comment | 18 complexity | 1d00a33895d2da0e7c8648c4060bf4c2 MD5 | raw file
  1. using System;
  2. using System.Text;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.IO;
  6. using System.Collections;
  7. using System.Text.RegularExpressions;
  8. namespace DynamicSugar {
  9. public static class ExtensionMethods_Dictionary {
  10. public static string PreProcess<K, V>(this IDictionary<K, V> d, string template, params object[] args) {
  11. var d2 = new Dictionary<string,object>();
  12. foreach(var e in d)
  13. d2.Add(e.Key.ToString(), e.Value);
  14. return ExtendedFormat.Format(template, d2);
  15. }
  16. public static bool Include<K, V>(this IDictionary<K, V> d, object anonymousType) {
  17. if (anonymousType is IDictionary<K, V>)
  18. return d.Include((IDictionary<K, V>)anonymousType);
  19. else {
  20. var d1 = DS.Dictionary<V>(anonymousType);
  21. return d.Include(d1);
  22. }
  23. }
  24. public static bool Include<K, V>(this IDictionary<K, V> d, IDictionary<K, V> includedDictionary)
  25. {
  26. foreach (var k in includedDictionary.Keys)
  27. if (d.ContainsKey(k))
  28. {
  29. if (!d[k].Equals(includedDictionary[k]))
  30. return false;
  31. }
  32. else return false;
  33. return true;
  34. }
  35. public static bool Include<K, V>(this IDictionary<K, V> d, params K[] listOfKeys) {
  36. return d.Include(listOfKeys.ToList());
  37. }
  38. public static bool Include<K, V>(this IDictionary<K, V> d, List<K> listOfKeys)
  39. {
  40. foreach (var k in listOfKeys)
  41. if (!d.ContainsKey(k))
  42. return false;
  43. return true;
  44. }
  45. /// <summary>
  46. /// Clone a IDictionary Of K,V
  47. /// </summary>
  48. /// <typeparam name="K"></typeparam>
  49. /// <typeparam name="V"></typeparam>
  50. /// <param name="d"></param>
  51. /// <returns></returns>
  52. public static IDictionary<K,V> Clone<K,V>(this IDictionary<K,V> d) {
  53. IDictionary<K,V> cloned = new Dictionary<K,V>();
  54. foreach(var k in d.Keys)
  55. cloned.Add(k, d[k]);
  56. return cloned;
  57. }
  58. /// <summary>
  59. /// Remove entries from the dictionary
  60. /// </summary>
  61. /// <typeparam name="K"></typeparam>
  62. /// <typeparam name="V"></typeparam>
  63. /// <param name="d"></param>
  64. /// <param name="entrieToRemove">Dictionary containing the entries to remove</param>
  65. /// <returns></returns>
  66. public static IDictionary<K,V> Remove<K,V>(this IDictionary<K,V> d, IDictionary<K,V> entrieToRemove) {
  67. Dictionary<K,V> newD = new Dictionary<K,V>();
  68. foreach(var k in d.Keys)
  69. if(!entrieToRemove.ContainsKey(k))
  70. newD.Add(k, d[k]);
  71. return newD;
  72. }
  73. public static IDictionary<K,V> Remove<K,V>(this IDictionary<K,V> d, List<K> keysToRemove) {
  74. Dictionary<K,V> newD = new Dictionary<K,V>();
  75. foreach(var k in d.Keys)
  76. if(!keysToRemove.Contains(k))
  77. newD.Add(k, d[k]);
  78. return newD;
  79. }
  80. /// <summary>
  81. ///
  82. /// </summary>
  83. /// <typeparam name="K"></typeparam>
  84. /// <typeparam name="V"></typeparam>
  85. /// <param name="d"></param>
  86. /// <param name="d2"></param>
  87. /// <param name="overWrite"></param>
  88. /// <returns></returns>
  89. public static IDictionary<K,V> Add<K,V>(this IDictionary<K,V> d, IDictionary<K,V> d2, bool overWrite = true) {
  90. var newD = d.Clone();
  91. foreach(var k in d2.Keys){
  92. if(newD.ContainsKey(k)){
  93. if(overWrite){
  94. newD.Remove(k);
  95. newD.Add(k, d2[k]);
  96. }
  97. else{
  98. // Do nothing, keep the current entry
  99. }
  100. }
  101. else newD.Add(k, d2[k]);
  102. }
  103. return newD;
  104. }
  105. /// <summary>
  106. /// Return the key which has the greatest value, based on a list of keys
  107. /// </summary>
  108. /// <typeparam name="K"></typeparam>
  109. /// <typeparam name="V"></typeparam>
  110. /// <param name="d"></param>
  111. /// <param name="keys"></param>
  112. /// <returns></returns>
  113. public static K Max<K,V>(this Dictionary<K,V> d, List<K> keys = null) where V : IComparable
  114. {
  115. K maxKey = default(K);
  116. V maxOcc = default(V);
  117. if(keys==null)
  118. keys = d.Keys.ToList();
  119. foreach(var ww in keys){
  120. if(d.ContainsKey(ww)){
  121. V counter = d[ww];
  122. if(counter.CompareTo(maxOcc)>0){
  123. maxKey = ww;
  124. maxOcc = counter;
  125. }
  126. }
  127. }
  128. return maxKey;
  129. }
  130. /// <summary>
  131. ///
  132. /// </summary>
  133. /// <typeparam name="K"></typeparam>
  134. /// <typeparam name="V"></typeparam>
  135. /// <param name="d"></param>
  136. /// <param name="keys"></param>
  137. /// <returns></returns>
  138. public static K Min<K,V>(this Dictionary<K,V> d, List<K> keys = null) where V : IComparable
  139. {
  140. K maxKey = default(K);
  141. V maxOcc = default(V);
  142. if(keys==null)
  143. keys = d.Keys.ToList();
  144. bool initialized = false;
  145. foreach(var ww in keys){
  146. if(initialized){
  147. if(d.ContainsKey(ww)){
  148. V counter = d[ww];
  149. if(counter.CompareTo(maxOcc)<0){
  150. maxKey = ww;
  151. maxOcc = counter;
  152. }
  153. }
  154. }
  155. else{
  156. maxKey = ww;
  157. maxOcc = d[ww];
  158. initialized = true;
  159. }
  160. }
  161. return maxKey;
  162. }
  163. /// <summary>
  164. /// Return the value of a key, if the key does not exist return the default value.
  165. /// </summary>
  166. /// <typeparam name="K"></typeparam>
  167. /// <typeparam name="V"></typeparam>
  168. /// <param name="d"></param>
  169. /// <param name="key">The key to search for</param>
  170. /// <param name="defaultValue">The default value</param>
  171. /// <returns></returns>
  172. public static V Get<K,V>(this Dictionary<K,V> d, K key, V defaultValue) {
  173. return d.ContainsKey(key) ? d[key] : defaultValue;
  174. }
  175. }
  176. }