Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. private static IEnumerable<IGrouping<int, T>> GroupConsecutive<T>(this IEnumerable<T> set, Func<T, T, bool> predicate)
  2. {
  3. var i = 0;
  4. var k = 0;
  5. var ranges = from e in set
  6. let idx = ++i
  7. let next = set.ElementAtOrDefault(idx)
  8. let key = (predicate(e, next)) ? k : k++
  9. group e by key into g
  10. select g;
  11. return ranges;
  12. }
  13.  
  14. var set = new List<bool>
  15. {
  16. true,
  17. false,
  18. false,
  19. false,
  20. true,
  21. true,
  22. false,
  23. false,
  24. };
  25. var groups = set.GroupConsecutive((b1, b2) => (b1 == b2));
  26. foreach (var g in groups)
  27. {
  28. Console.WriteLine(g.Key);
  29. foreach (var b in g)
  30. Console.WriteLine("t{0}", b);
  31. }
  32.  
  33. 0
  34. True
  35. 1
  36. False
  37. False
  38. False
  39. 2
  40. True
  41. True
  42. 3
  43. False
  44. False
  45.  
  46. public static IEnumerable<IGrouping<int, T>> GroupConsecutive<T>(this IEnumerable<T> set, Func<T, T, bool> predicate)
  47. {
  48. var i = 0;
  49. var k = 0;
  50. var ranges = from e in set
  51. let idx = ++i
  52. let next = set.ElementAtOrDefault(idx)
  53. let key = next == null ? k : (predicate(e, next)) ? k : k++
  54. group e by key into g
  55. select g;
  56. return ranges;
  57. }
  58.  
  59. last = null;
  60. foreach (var option in list)
  61. {
  62. if (last != option)
  63. newlist.Add(new Group(option, new[]));
  64. newlist.Last().Add(option);
  65. last = option;
  66. }
  67.  
  68. public class GroupConsecutiveEqualItemsConverter : IValueConverter
  69. {
  70. static readonly object UnsetValue = new object();
  71.  
  72. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  73. {
  74. IEnumerable source = value as IEnumerable;
  75. if (source == null) return DependencyProperty.UnsetValue;
  76. string propertyName = parameter as string;
  77. var result = new ObservableCollection<List<object>>();
  78.  
  79. var notify = value as INotifyCollectionChanged;
  80. if (notify != null) notify.CollectionChanged += delegate { Reload(result, source, propertyName); };
  81.  
  82. Reload(result, source, propertyName);
  83. return result;
  84. }
  85.  
  86. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  87. {
  88. throw new NotSupportedException();
  89. }
  90.  
  91. void Reload(ObservableCollection<List<object>> result, IEnumerable source, string propertyName)
  92. {
  93. result.Clear();
  94. object previous = UnsetValue;
  95. List<object> group = null;
  96. foreach (object i in source)
  97. {
  98. object current = UnsetValue;
  99. if (propertyName == null)
  100. {
  101. current = i;
  102. }
  103. else
  104. {
  105. try
  106. {
  107. var property = i.GetType().GetProperty(propertyName);
  108. if (property != null) current = property.GetValue(i, null);
  109. }
  110. catch (AmbiguousMatchException) { }
  111. }
  112. if (!object.Equals(previous, current))
  113. {
  114. if (group != null) result.Add(group);
  115. group = new List<object>();
  116. }
  117. group.Add(i);
  118. previous = current;
  119. }
  120. if (group != null && group.Count > 0) result.Add(group);
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement