Partitioner.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // Partitioner.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. using Partitioners;
  32. public static class Partitioner
  33. {
  34. public static OrderablePartitioner<TSource> Create<TSource> (IEnumerable<TSource> source)
  35. {
  36. IList<TSource> tempIList = source as IList<TSource>;
  37. if (tempIList != null)
  38. return Create (tempIList, true);
  39. return new EnumerablePartitioner<TSource> (source);
  40. }
  41. public static OrderablePartitioner<TSource> Create<TSource> (TSource[] source, bool loadBalance)
  42. {
  43. return Create ((IList<TSource>)source, loadBalance);
  44. }
  45. public static OrderablePartitioner<TSource> Create<TSource> (IList<TSource> source, bool loadBalance)
  46. {
  47. return new ListPartitioner<TSource> (source);
  48. }
  49. public static OrderablePartitioner<Tuple<int, int>> Create (int fromInclusive,
  50. int toExclusive)
  51. {
  52. // This formula that is somewhat non-straighforward was guessed based on MS output
  53. int rangeSize = (toExclusive - fromInclusive) / (Environment.ProcessorCount * 3);
  54. if (rangeSize < 1)
  55. rangeSize = 1;
  56. return Create (fromInclusive, toExclusive, rangeSize);
  57. }
  58. public static OrderablePartitioner<Tuple<int, int>> Create (int fromInclusive,
  59. int toExclusive,
  60. int rangeSize)
  61. {
  62. if (fromInclusive >= toExclusive)
  63. throw new ArgumentOutOfRangeException ("toExclusive");
  64. if (rangeSize <= 0)
  65. throw new ArgumentOutOfRangeException ("rangeSize");
  66. return new UserRangePartitioner (fromInclusive, toExclusive, rangeSize);
  67. }
  68. public static OrderablePartitioner<Tuple<long, long>> Create (long fromInclusive,
  69. long toExclusive)
  70. {
  71. long rangeSize = (toExclusive - fromInclusive) / (Environment.ProcessorCount * 3);
  72. if (rangeSize < 1)
  73. rangeSize = 1;
  74. return Create (fromInclusive, toExclusive, rangeSize);
  75. }
  76. public static OrderablePartitioner<Tuple<long, long>> Create (long fromInclusive,
  77. long toExclusive,
  78. long rangeSize)
  79. {
  80. if (rangeSize <= 0)
  81. throw new ArgumentOutOfRangeException ("rangeSize");
  82. if (fromInclusive >= toExclusive)
  83. throw new ArgumentOutOfRangeException ("toExclusive");
  84. return new UserLongRangePartitioner (fromInclusive, toExclusive, rangeSize);
  85. }
  86. }
  87. public abstract class Partitioner<TSource>
  88. {
  89. protected Partitioner ()
  90. {
  91. }
  92. public virtual IEnumerable<TSource> GetDynamicPartitions ()
  93. {
  94. if (!SupportsDynamicPartitions)
  95. throw new NotSupportedException ();
  96. return null;
  97. }
  98. public abstract IList<IEnumerator<TSource>> GetPartitions (int partitionCount);
  99. public virtual bool SupportsDynamicPartitions {
  100. get {
  101. return false;
  102. }
  103. }
  104. }
  105. }
  106. #endif