PageRenderTime 32ms CodeModel.GetById 14ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/Main/src/DynamicDataDisplay/Common/Auxiliary/IEnumerableExtensions.cs

#
C# | 137 lines | 94 code | 24 blank | 19 comment | 4 complexity | 66beb93291bf48290676acad713b70f4 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Linq;
  4using System.Text;
  5using System.Collections;
  6
  7namespace Microsoft.Research.DynamicDataDisplay.Common.Auxiliary
  8{
  9	/// <summary>
 10	/// Contains extension methods for IEnumerable.
 11	/// </summary>
 12	public static class IEnumerableExtensions
 13	{
 14		/// <summary>
 15		/// Gets the value indicating wheter count of elements in a given sequence is greater or equal than specified value.
 16		/// </summary>
 17		/// <typeparam name="T"></typeparam>
 18		/// <param name="enumerable">The enumerable.</param>
 19		/// <param name="count">The count.</param>
 20		/// <returns></returns>
 21		public static bool CountGreaterOrEqual<T>(this IEnumerable<T> enumerable, int count)
 22		{
 23			int counter = 0;
 24			using (var enumerator = enumerable.GetEnumerator())
 25			{
 26				while (counter < count && enumerator.MoveNext())
 27				{
 28					counter++;
 29				}
 30			}
 31
 32			return counter == count;
 33		}
 34
 35		/// <summary>
 36		/// Splits the specified source sequnce into a sequence of sequences, with the length of inner sequence not longer than 
 37		/// specified maximal count.
 38		/// </summary>
 39		/// <typeparam name="T"></typeparam>
 40		/// <param name="source">The source.</param>
 41		/// <param name="maxCount">The max count.</param>
 42		/// <returns></returns>
 43		public static IEnumerable<IEnumerable<T>> Split<T>(this IEnumerable<T> source, int maxCount)
 44		{
 45			using (var enumerator = new FixedEnumeratorWrapper<T>(source.GetEnumerator()))
 46			{
 47				do
 48				{
 49					var enumerable = new FixedEnumerable<T>(enumerator);
 50					yield return enumerable.Take(maxCount);
 51				}
 52				while (enumerator.CanMoveNext);
 53			}
 54		}
 55
 56		private sealed class FixedEnumeratorWrapper<T> : IEnumerator<T>
 57		{
 58			private readonly IEnumerator<T> enumerator;
 59
 60			public FixedEnumeratorWrapper(IEnumerator<T> enumerator)
 61			{
 62				this.enumerator = enumerator;
 63			}
 64
 65			#region IEnumerator<T> Members
 66
 67			public T Current
 68			{
 69				get { return enumerator.Current; }
 70			}
 71
 72			#endregion
 73
 74			#region IDisposable Members
 75
 76			public void Dispose()
 77			{
 78				//enumerator.Dispose();
 79			}
 80
 81			#endregion
 82
 83			#region IEnumerator Members
 84
 85			object IEnumerator.Current
 86			{
 87				get { return enumerator.Current; }
 88			}
 89
 90			private bool canMoveNext = false;
 91			public bool CanMoveNext
 92			{
 93				get { return canMoveNext; }
 94			}
 95
 96			public bool MoveNext()
 97			{
 98				canMoveNext = enumerator.MoveNext();
 99				return canMoveNext;
100			}
101
102			public void Reset()
103			{
104				enumerator.Reset();
105			}
106
107			#endregion
108		}
109
110		private sealed class FixedEnumerable<T> : IEnumerable<T>
111		{
112			private readonly IEnumerator<T> enumerator;
113			public FixedEnumerable(IEnumerator<T> enumerator)
114			{
115				this.enumerator = enumerator;
116			}
117
118			#region IEnumerable<T> Members
119
120			public IEnumerator<T> GetEnumerator()
121			{
122				return enumerator;
123			}
124
125			#endregion
126
127			#region IEnumerable Members
128
129			IEnumerator IEnumerable.GetEnumerator()
130			{
131				return enumerator;
132			}
133
134			#endregion
135		}
136	}
137}