OrderedEnumerableRowCollection.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // OrderedEnumerableRowCollection.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2008 Novell, Inc. http://www.novell.com
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections;
  31. using System.Collections.Generic;
  32. namespace System.Data
  33. {
  34. public sealed class OrderedEnumerableRowCollection<TRow>
  35. : EnumerableRowCollection<TRow>
  36. {
  37. internal static OrderedEnumerableRowCollection<TRow> Create<TRow, TKey> (IEnumerable<TRow> source, Func<TRow, TKey> keySelector, IComparer<TKey> comparer, bool descending)
  38. {
  39. var sorter = new SortComparer<TRow> ();
  40. sorter.AddSort<TKey> (keySelector, comparer, descending);
  41. return new OrderedEnumerableRowCollection<TRow> (new SortedEnumerable <TRow> (source, sorter));
  42. }
  43. internal static OrderedEnumerableRowCollection<TRow> AddSort<TRow, TKey> (OrderedEnumerableRowCollection<TRow> source, Func<TRow, TKey> keySelector, IComparer<TKey> comparer, bool descending)
  44. {
  45. source.source.Sorter.AddSort<TKey> (keySelector, comparer, descending);
  46. return source;
  47. }
  48. OrderedEnumerableRowCollection (SortedEnumerable<TRow> source)
  49. : base (source)
  50. {
  51. this.source = source;
  52. }
  53. SortedEnumerable<TRow> source;
  54. }
  55. class SortComparer<TRow> : IComparer<TRow>
  56. {
  57. public SortComparer ()
  58. {
  59. }
  60. List<Comparison<TRow>> comparers = new List<Comparison<TRow>> ();
  61. public void AddSort (Comparison<TRow> comparer)
  62. {
  63. comparers.Add (comparer);
  64. }
  65. public void AddSort<TKey> (Func<TRow, TKey> keySelector, IComparer<TKey> comparer, bool descending)
  66. {
  67. if (keySelector == null)
  68. throw new ArgumentNullException ("keySelector");
  69. if (comparer == null)
  70. comparer = Comparer<TKey>.Default;
  71. comparers.Add (delegate (TRow r1, TRow r2) {
  72. int ret = comparer.Compare (keySelector (r1), keySelector (r2));
  73. return descending ? -ret : ret;
  74. });
  75. }
  76. public int Compare (TRow r1, TRow r2)
  77. {
  78. foreach (var c in comparers) {
  79. int ret = c (r1, r2);
  80. if (ret != 0)
  81. return ret;
  82. }
  83. return 0;
  84. }
  85. }
  86. class SortedEnumerable<TRow> : IEnumerable<TRow>
  87. {
  88. IEnumerable<TRow> source;
  89. SortComparer<TRow> sorter;
  90. public SortedEnumerable (IEnumerable<TRow> source, SortComparer<TRow> sorter)
  91. {
  92. this.source = source;
  93. this.sorter = sorter;
  94. }
  95. public SortComparer<TRow> Sorter {
  96. get { return sorter; }
  97. }
  98. public IEnumerator<TRow> GetEnumerator ()
  99. {
  100. var list = new List<TRow> ();
  101. foreach (TRow row in source)
  102. list.Add (row);
  103. list.Sort (sorter);
  104. for (int i = 0, c = list.Count; i < c; i++)
  105. yield return list [i];
  106. }
  107. IEnumerator IEnumerable.GetEnumerator ()
  108. {
  109. return GetEnumerator ();
  110. }
  111. }
  112. }