Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Core / System / Linq / Parallel / QueryOperators / Unary / SortQueryOperator.cs / 1305376 / SortQueryOperator.cs
// ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== // =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ // // SortQueryOperator.cs // //[....] // // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- using System.Collections.Generic; using System.Diagnostics.Contracts; using System.Threading; namespace System.Linq.Parallel { ////// The query operator for OrderBy and ThenBy. /// ////// internal sealed class SortQueryOperator : UnaryQueryOperator , IOrderedEnumerable { private readonly Func m_keySelector; // Key selector used when sorting. private readonly IComparer m_comparer; // Key comparison logic to use during sorting. //---------------------------------------------------------------------------------------- // Instantiates a new sort operator. // internal SortQueryOperator(IEnumerable source, Func keySelector, IComparer comparer, bool descending) : base(source, true) { Contract.Assert(keySelector != null, "key selector must not be null"); m_keySelector = keySelector; // If a comparer wasn't supplied, we use the default one for the key type. if (comparer == null) { m_comparer = Util.GetDefaultComparer (); } else { m_comparer = comparer; } if (descending) { m_comparer = new ReverseComparer (m_comparer); } SetOrdinalIndexState(OrdinalIndexState.Shuffled); } //--------------------------------------------------------------------------------------- // IOrderedEnumerable method for nesting an order by operator inside another. // IOrderedEnumerable IOrderedEnumerable .CreateOrderedEnumerable ( Func key2Selector, IComparer key2Comparer, bool descending) { key2Comparer = key2Comparer ?? Util.GetDefaultComparer (); if (descending) { key2Comparer = new ReverseComparer (key2Comparer); } IComparer > pairComparer = new PairComparer (m_comparer, key2Comparer); Func > pairKeySelector = (TInputOutput elem) => new Pair (m_keySelector(elem), key2Selector(elem)); return new SortQueryOperator >(Child, pairKeySelector, pairComparer, false); } //--------------------------------------------------------------------------------------- // Accessor the the key selector. // internal Func KeySelector { get { return m_keySelector; } } //--------------------------------------------------------------------------------------- // Accessor the the key comparer. // internal IComparer KeyComparer { get { return m_comparer; } } //---------------------------------------------------------------------------------------- // Opens the current operator. This involves opening the child operator tree, enumerating // the results, sorting them, and then returning an enumerator that walks the result. // internal override QueryResults Open(QuerySettings settings, bool preferStriping) { QueryResults childQueryResults = Child.Open(settings, false); return new SortQueryOperatorResults (childQueryResults, this, settings, preferStriping); } internal override void WrapPartitionedStream ( PartitionedStream inputStream, IPartitionedStreamRecipient recipient, bool preferStriping, QuerySettings settings) { PartitionedStream outputStream = new PartitionedStream (inputStream.PartitionCount, this.m_comparer, OrdinalIndexState); for (int i = 0; i < outputStream.PartitionCount; i++) { outputStream[i] = new SortQueryOperatorEnumerator ( inputStream[i], m_keySelector, m_comparer); } recipient.Receive (outputStream); } //--------------------------------------------------------------------------------------- // Returns an enumerable that represents the query executing sequentially. // internal override IEnumerable AsSequentialQuery(CancellationToken token) { IEnumerable wrappedChild = CancellableEnumerable.Wrap(Child.AsSequentialQuery(token), token); return wrappedChild.OrderBy(m_keySelector, m_comparer); } //---------------------------------------------------------------------------------------- // Whether this operator performs a premature merge. // internal override bool LimitsParallelism { get { return false; } } } internal class SortQueryOperatorResults : QueryResults { protected QueryResults m_childQueryResults; // Results of the child query private SortQueryOperator m_op; // Operator that generated these results private QuerySettings m_settings; // Settings collected from the query private bool m_preferStriping; // If the results are indexible, should we use striping when partitioning them internal SortQueryOperatorResults( QueryResults childQueryResults, SortQueryOperator op, QuerySettings settings, bool preferStriping) { m_childQueryResults = childQueryResults; m_op = op; m_settings = settings; m_preferStriping = preferStriping; } internal override bool IsIndexible { get { return false; } } internal override void GivePartitionedStream(IPartitionedStreamRecipient recipient) { m_childQueryResults.GivePartitionedStream(new ChildResultsRecipient(recipient, m_op, m_settings)); } private class ChildResultsRecipient : IPartitionedStreamRecipient { IPartitionedStreamRecipient m_outputRecipient; SortQueryOperator m_op; QuerySettings m_settings; internal ChildResultsRecipient(IPartitionedStreamRecipient outputRecipient, SortQueryOperator op, QuerySettings settings) { m_outputRecipient = outputRecipient; m_op = op; m_settings = settings; } public void Receive (PartitionedStream childPartitionedStream) { m_op.WrapPartitionedStream(childPartitionedStream, m_outputRecipient, false, m_settings); } } } //---------------------------------------------------------------------------------------- // This enumerator performs sorting based on a key selection and comparison routine. // internal class SortQueryOperatorEnumerator : QueryOperatorEnumerator { private readonly QueryOperatorEnumerator m_source; // Data source to sort. private readonly Func m_keySelector; // Key selector used when sorting. private readonly IComparer m_keyComparer; // Key comparison logic to use during sorting. //--------------------------------------------------------------------------------------- // Instantiates a new sort operator enumerator. // internal SortQueryOperatorEnumerator(QueryOperatorEnumerator source, Func keySelector, IComparer keyComparer) { Contract.Assert(source != null); Contract.Assert(keySelector != null, "need a key comparer"); Contract.Assert(keyComparer != null, "expected a compiled operator"); m_source = source; m_keySelector = keySelector; m_keyComparer = keyComparer; } //---------------------------------------------------------------------------------------- // Accessor for the key comparison routine. // public IComparer KeyComparer { get { return m_keyComparer; } } //--------------------------------------------------------------------------------------- // Moves to the next element in the sorted output. When called for the first time, the // descendents in the sort's child tree are executed entirely, the results accumulated // in memory, and the data sorted. // internal override bool MoveNext(ref TInputOutput currentElement, ref TSortKey currentKey) { Contract.Assert(m_source != null); TKey keyUnused = default(TKey); if (!m_source.MoveNext(ref currentElement, ref keyUnused)) { return false; } currentKey = m_keySelector(currentElement); return true; } protected override void Dispose(bool disposing) { Contract.Assert(m_source != null); m_source.Dispose(); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== // =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+ // // SortQueryOperator.cs // // [....] // // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- using System.Collections.Generic; using System.Diagnostics.Contracts; using System.Threading; namespace System.Linq.Parallel { ////// The query operator for OrderBy and ThenBy. /// ////// internal sealed class SortQueryOperator : UnaryQueryOperator , IOrderedEnumerable { private readonly Func m_keySelector; // Key selector used when sorting. private readonly IComparer m_comparer; // Key comparison logic to use during sorting. //---------------------------------------------------------------------------------------- // Instantiates a new sort operator. // internal SortQueryOperator(IEnumerable source, Func keySelector, IComparer comparer, bool descending) : base(source, true) { Contract.Assert(keySelector != null, "key selector must not be null"); m_keySelector = keySelector; // If a comparer wasn't supplied, we use the default one for the key type. if (comparer == null) { m_comparer = Util.GetDefaultComparer (); } else { m_comparer = comparer; } if (descending) { m_comparer = new ReverseComparer (m_comparer); } SetOrdinalIndexState(OrdinalIndexState.Shuffled); } //--------------------------------------------------------------------------------------- // IOrderedEnumerable method for nesting an order by operator inside another. // IOrderedEnumerable IOrderedEnumerable .CreateOrderedEnumerable ( Func key2Selector, IComparer key2Comparer, bool descending) { key2Comparer = key2Comparer ?? Util.GetDefaultComparer (); if (descending) { key2Comparer = new ReverseComparer (key2Comparer); } IComparer > pairComparer = new PairComparer (m_comparer, key2Comparer); Func > pairKeySelector = (TInputOutput elem) => new Pair (m_keySelector(elem), key2Selector(elem)); return new SortQueryOperator >(Child, pairKeySelector, pairComparer, false); } //--------------------------------------------------------------------------------------- // Accessor the the key selector. // internal Func KeySelector { get { return m_keySelector; } } //--------------------------------------------------------------------------------------- // Accessor the the key comparer. // internal IComparer KeyComparer { get { return m_comparer; } } //---------------------------------------------------------------------------------------- // Opens the current operator. This involves opening the child operator tree, enumerating // the results, sorting them, and then returning an enumerator that walks the result. // internal override QueryResults Open(QuerySettings settings, bool preferStriping) { QueryResults childQueryResults = Child.Open(settings, false); return new SortQueryOperatorResults (childQueryResults, this, settings, preferStriping); } internal override void WrapPartitionedStream ( PartitionedStream inputStream, IPartitionedStreamRecipient recipient, bool preferStriping, QuerySettings settings) { PartitionedStream outputStream = new PartitionedStream (inputStream.PartitionCount, this.m_comparer, OrdinalIndexState); for (int i = 0; i < outputStream.PartitionCount; i++) { outputStream[i] = new SortQueryOperatorEnumerator ( inputStream[i], m_keySelector, m_comparer); } recipient.Receive (outputStream); } //--------------------------------------------------------------------------------------- // Returns an enumerable that represents the query executing sequentially. // internal override IEnumerable AsSequentialQuery(CancellationToken token) { IEnumerable wrappedChild = CancellableEnumerable.Wrap(Child.AsSequentialQuery(token), token); return wrappedChild.OrderBy(m_keySelector, m_comparer); } //---------------------------------------------------------------------------------------- // Whether this operator performs a premature merge. // internal override bool LimitsParallelism { get { return false; } } } internal class SortQueryOperatorResults : QueryResults { protected QueryResults m_childQueryResults; // Results of the child query private SortQueryOperator m_op; // Operator that generated these results private QuerySettings m_settings; // Settings collected from the query private bool m_preferStriping; // If the results are indexible, should we use striping when partitioning them internal SortQueryOperatorResults( QueryResults childQueryResults, SortQueryOperator op, QuerySettings settings, bool preferStriping) { m_childQueryResults = childQueryResults; m_op = op; m_settings = settings; m_preferStriping = preferStriping; } internal override bool IsIndexible { get { return false; } } internal override void GivePartitionedStream(IPartitionedStreamRecipient recipient) { m_childQueryResults.GivePartitionedStream(new ChildResultsRecipient(recipient, m_op, m_settings)); } private class ChildResultsRecipient : IPartitionedStreamRecipient { IPartitionedStreamRecipient m_outputRecipient; SortQueryOperator m_op; QuerySettings m_settings; internal ChildResultsRecipient(IPartitionedStreamRecipient outputRecipient, SortQueryOperator op, QuerySettings settings) { m_outputRecipient = outputRecipient; m_op = op; m_settings = settings; } public void Receive (PartitionedStream childPartitionedStream) { m_op.WrapPartitionedStream(childPartitionedStream, m_outputRecipient, false, m_settings); } } } //---------------------------------------------------------------------------------------- // This enumerator performs sorting based on a key selection and comparison routine. // internal class SortQueryOperatorEnumerator : QueryOperatorEnumerator { private readonly QueryOperatorEnumerator m_source; // Data source to sort. private readonly Func m_keySelector; // Key selector used when sorting. private readonly IComparer m_keyComparer; // Key comparison logic to use during sorting. //--------------------------------------------------------------------------------------- // Instantiates a new sort operator enumerator. // internal SortQueryOperatorEnumerator(QueryOperatorEnumerator source, Func keySelector, IComparer keyComparer) { Contract.Assert(source != null); Contract.Assert(keySelector != null, "need a key comparer"); Contract.Assert(keyComparer != null, "expected a compiled operator"); m_source = source; m_keySelector = keySelector; m_keyComparer = keyComparer; } //---------------------------------------------------------------------------------------- // Accessor for the key comparison routine. // public IComparer KeyComparer { get { return m_keyComparer; } } //--------------------------------------------------------------------------------------- // Moves to the next element in the sorted output. When called for the first time, the // descendents in the sort's child tree are executed entirely, the results accumulated // in memory, and the data sorted. // internal override bool MoveNext(ref TInputOutput currentElement, ref TSortKey currentKey) { Contract.Assert(m_source != null); TKey keyUnused = default(TKey); if (!m_source.MoveNext(ref currentElement, ref keyUnused)) { return false; } currentKey = m_keySelector(currentElement); return true; } protected override void Dispose(bool disposing) { Contract.Assert(m_source != null); m_source.Dispose(); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- BrowserTree.cs
- XmlNodeReader.cs
- BindValidator.cs
- MapPathBasedVirtualPathProvider.cs
- _PooledStream.cs
- recordstatefactory.cs
- LayoutTable.cs
- ToolStripDropDownItemDesigner.cs
- PageRanges.cs
- AppLevelCompilationSectionCache.cs
- ConfigXmlWhitespace.cs
- StringSorter.cs
- TypeDescriptionProvider.cs
- SafeFileHandle.cs
- SchemaComplexType.cs
- TimeSpanValidatorAttribute.cs
- StorageMappingItemCollection.cs
- DataSetUtil.cs
- DiffuseMaterial.cs
- SamlAdvice.cs
- ArgumentOutOfRangeException.cs
- TrackingServices.cs
- _ScatterGatherBuffers.cs
- ConsumerConnectionPoint.cs
- WebBrowserNavigatingEventHandler.cs
- DocumentSequence.cs
- SerializationAttributes.cs
- ProjectionPath.cs
- CompositeDataBoundControl.cs
- SqlWriter.cs
- Image.cs
- CallContext.cs
- documentation.cs
- ImageClickEventArgs.cs
- MatrixValueSerializer.cs
- NullToBooleanConverter.cs
- TableAdapterManagerGenerator.cs
- LinkArea.cs
- RemotingAttributes.cs
- NativeMethods.cs
- WebControlsSection.cs
- DataBoundLiteralControl.cs
- ConfigXmlWhitespace.cs
- XmlSchemaDocumentation.cs
- StubHelpers.cs
- OpenTypeCommon.cs
- Atom10FormatterFactory.cs
- DataContractSet.cs
- SqlMetaData.cs
- BindingWorker.cs
- _SSPIWrapper.cs
- PersistChildrenAttribute.cs
- MutexSecurity.cs
- SQlBooleanStorage.cs
- CollectionType.cs
- WorkflowItemsPresenter.cs
- XmlQueryRuntime.cs
- DateTimeOffsetConverter.cs
- XmlNodeReader.cs
- Attributes.cs
- _RequestLifetimeSetter.cs
- CodePageUtils.cs
- RawStylusInput.cs
- BinaryCommonClasses.cs
- HotSpotCollection.cs
- EventHandlingScope.cs
- WebPartHeaderCloseVerb.cs
- WaitHandleCannotBeOpenedException.cs
- SByteConverter.cs
- DoubleLink.cs
- Regex.cs
- ProfileModule.cs
- ModelItemImpl.cs
- PerformanceCounterCategory.cs
- ObservableDictionary.cs
- XmlCDATASection.cs
- DropSource.cs
- ObjectViewListener.cs
- PerformanceCountersElement.cs
- TimeSpanMinutesOrInfiniteConverter.cs
- HttpCacheParams.cs
- TagElement.cs
- MsmqInputSessionChannel.cs
- XPathScanner.cs
- XsltOutput.cs
- RunClient.cs
- BitmapMetadataBlob.cs
- SrgsOneOf.cs
- ScrollPatternIdentifiers.cs
- DataGridViewCellValueEventArgs.cs
- FormattedTextSymbols.cs
- ExpressionNode.cs
- FieldNameLookup.cs
- XmlAttribute.cs
- MessageDecoder.cs
- ParseChildrenAsPropertiesAttribute.cs
- Decoder.cs
- RootDesignerSerializerAttribute.cs
- ImageClickEventArgs.cs
- SmtpMail.cs