OrderablePartitioner.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // OrderablePartitioner.cs
  3. //
  4. // Author:
  5. // Jérémie "Garuma" Laval <[email protected]>
  6. //
  7. // Copyright (c) 2009 Jérémie "Garuma" Laval
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. using System;
  27. using System.Collections.Generic;
  28. #if NET_4_0 || BOOTSTRAP_NET_4_0
  29. namespace System.Collections.Concurrent
  30. {
  31. public abstract class OrderablePartitioner<TSource> : Partitioner<TSource>
  32. {
  33. bool keysOrderedInEachPartition;
  34. bool keysOrderedAcrossPartitions;
  35. bool keysNormalized;
  36. protected OrderablePartitioner (bool keysOrderedInEachPartition,
  37. bool keysOrderedAcrossPartitions,
  38. bool keysNormalized) : base ()
  39. {
  40. this.keysOrderedInEachPartition = keysOrderedInEachPartition;
  41. this.keysOrderedAcrossPartitions = keysOrderedAcrossPartitions;
  42. this.keysNormalized = keysNormalized;
  43. }
  44. public override IEnumerable<TSource> GetDynamicPartitions ()
  45. {
  46. foreach (KeyValuePair<long, TSource> item in GetOrderableDynamicPartitions ())
  47. yield return item.Value;
  48. }
  49. public override IList<IEnumerator<TSource>> GetPartitions (int partitionCount)
  50. {
  51. IEnumerator<TSource>[] temp = new IEnumerator<TSource>[partitionCount];
  52. IList<IEnumerator<KeyValuePair<long, TSource>>> enumerators
  53. = GetOrderablePartitions (partitionCount);
  54. for (int i = 0; i < enumerators.Count; i++)
  55. temp[i] = new ProxyEnumerator (enumerators[i]);
  56. return temp;
  57. }
  58. IEnumerator<TSource> GetProxyEnumerator (IEnumerator<KeyValuePair<long, TSource>> enumerator)
  59. {
  60. while (enumerator.MoveNext ())
  61. yield return enumerator.Current.Value;
  62. }
  63. public abstract IList<IEnumerator<KeyValuePair<long, TSource>>> GetOrderablePartitions(int partitionCount);
  64. public virtual IEnumerable<KeyValuePair<long, TSource>> GetOrderableDynamicPartitions()
  65. {
  66. if (!SupportsDynamicPartitions)
  67. throw new NotSupportedException ();
  68. return null;
  69. }
  70. public bool KeysOrderedInEachPartition {
  71. get {
  72. return keysOrderedInEachPartition;
  73. }
  74. }
  75. public bool KeysOrderedAcrossPartitions {
  76. get {
  77. return keysOrderedAcrossPartitions;
  78. }
  79. }
  80. public bool KeysNormalized {
  81. get {
  82. return keysNormalized;
  83. }
  84. }
  85. class ProxyEnumerator : IEnumerator<TSource>, IDisposable
  86. {
  87. IEnumerator<KeyValuePair<long, TSource>> internalEnumerator;
  88. internal ProxyEnumerator (IEnumerator<KeyValuePair<long, TSource>> enumerator)
  89. {
  90. internalEnumerator = enumerator;
  91. }
  92. public void Dispose ()
  93. {
  94. internalEnumerator.Dispose ();
  95. }
  96. public bool MoveNext ()
  97. {
  98. if (!internalEnumerator.MoveNext ())
  99. return false;
  100. Current = internalEnumerator.Current.Value;
  101. return true;
  102. }
  103. public void Reset ()
  104. {
  105. internalEnumerator.Reset ();
  106. }
  107. object IEnumerator.Current {
  108. get {
  109. return Current;
  110. }
  111. }
  112. public TSource Current {
  113. get;
  114. private set;
  115. }
  116. }
  117. }
  118. }
  119. #endif