Partitioner.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #if NET_4_0
  27. using System;
  28. using System.Collections.Generic;
  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[] array, bool loadBalance)
  42. {
  43. return Create ((IList<TSource>)array, loadBalance);
  44. }
  45. public static OrderablePartitioner<TSource> Create<TSource> (IList<TSource> list, bool loadBalance)
  46. {
  47. return new ListPartitioner<TSource> (list);
  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. #if NET_4_5
  87. [MonoTODO]
  88. public static OrderablePartitioner<TSource> Create<TSource> (IEnumerable<TSource> source,
  89. EnumerablePartitionerOptions partitionerOptions)
  90. {
  91. throw new NotImplementedException ();
  92. }
  93. #endif
  94. }
  95. public abstract class Partitioner<TSource>
  96. {
  97. protected Partitioner ()
  98. {
  99. }
  100. public virtual IEnumerable<TSource> GetDynamicPartitions ()
  101. {
  102. if (!SupportsDynamicPartitions)
  103. throw new NotSupportedException ();
  104. return null;
  105. }
  106. public abstract IList<IEnumerator<TSource>> GetPartitions (int partitionCount);
  107. public virtual bool SupportsDynamicPartitions {
  108. get {
  109. return false;
  110. }
  111. }
  112. }
  113. }
  114. #endif