Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / ndp / fx / src / DataEntity / System / Data / Objects / ELinq / Visitors / LinqMaximalSubtreeNominator.cs / 1 / LinqMaximalSubtreeNominator.cs
//---------------------------------------------------------------------- //// Copyright (c) Microsoft Corporation. All rights reserved. // // // @owner [....], [....] //--------------------------------------------------------------------- using System.Linq.Expressions; using System.Collections.Generic; using System.Diagnostics; namespace System.Data.Objects.ELinq { ////// Goes from the bottom to top and nominates nodes where all the nodes /// below the node return true from the shouldBeNominatedDelegate /// internal sealed class LinqMaximalSubtreeNominator : ExpressionVisitor { readonly HashSet_candidates; readonly Func _shouldBeNominatedDelegate; bool _cannotBeNominated = false; // not creatable private LinqMaximalSubtreeNominator(HashSet candidates, Func shouldBeNominatedDelegate) { _candidates = candidates; _shouldBeNominatedDelegate = shouldBeNominatedDelegate; } internal static HashSet Nominate(Expression expression, HashSet candidates, Func shouldBeNominatedDelegate) { Debug.Assert(candidates != null, "Candidates hashset cannot be null"); LinqMaximalSubtreeNominator nominator = new LinqMaximalSubtreeNominator(candidates, shouldBeNominatedDelegate); nominator.Visit(expression); return nominator._candidates; } internal static HashSet FindMaximalSubtrees(Expression expression, Func shouldBeNominatedDelegate) { HashSet nominees = Nominate(expression, new HashSet (), shouldBeNominatedDelegate); return MaximalSubtreeVisitor.FindMaximalSubtrees(nominees, expression); } internal override Expression Visit(Expression exp) { if (exp != null) { bool saveCannotBeNominated = _cannotBeNominated; _cannotBeNominated = false; base.Visit(exp); if (!_cannotBeNominated) { // everyone below me can be nominated, so // see if this one can be also if (_shouldBeNominatedDelegate(exp)) { _candidates.Add(exp); } else { _cannotBeNominated = true; } } _cannotBeNominated |= saveCannotBeNominated; } return exp; } /// /// Visitor that identifies maximal subtrees given a set of nominees. It walks the tree top down /// and when it identifies a nominated node, adds it to the _subtree set and stops walking. /// private sealed class MaximalSubtreeVisitor : ExpressionVisitor { private readonly HashSet_subtrees; private readonly HashSet _nominees; private MaximalSubtreeVisitor(HashSet nominees) { _nominees = nominees; _subtrees = new HashSet (); } internal static HashSet FindMaximalSubtrees(HashSet nominees, Expression query) { MaximalSubtreeVisitor visitor = new MaximalSubtreeVisitor(nominees); visitor.Visit(query); return visitor._subtrees; } internal override Expression Visit(Expression exp) { if (exp != null && _nominees.Contains(exp)) { _subtrees.Add(exp); return exp; } return base.Visit(exp); } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //---------------------------------------------------------------------- // // Copyright (c) Microsoft Corporation. All rights reserved. // // // @owner [....], [....] //--------------------------------------------------------------------- using System.Linq.Expressions; using System.Collections.Generic; using System.Diagnostics; namespace System.Data.Objects.ELinq { ////// Goes from the bottom to top and nominates nodes where all the nodes /// below the node return true from the shouldBeNominatedDelegate /// internal sealed class LinqMaximalSubtreeNominator : ExpressionVisitor { readonly HashSet_candidates; readonly Func _shouldBeNominatedDelegate; bool _cannotBeNominated = false; // not creatable private LinqMaximalSubtreeNominator(HashSet candidates, Func shouldBeNominatedDelegate) { _candidates = candidates; _shouldBeNominatedDelegate = shouldBeNominatedDelegate; } internal static HashSet Nominate(Expression expression, HashSet candidates, Func shouldBeNominatedDelegate) { Debug.Assert(candidates != null, "Candidates hashset cannot be null"); LinqMaximalSubtreeNominator nominator = new LinqMaximalSubtreeNominator(candidates, shouldBeNominatedDelegate); nominator.Visit(expression); return nominator._candidates; } internal static HashSet FindMaximalSubtrees(Expression expression, Func shouldBeNominatedDelegate) { HashSet nominees = Nominate(expression, new HashSet (), shouldBeNominatedDelegate); return MaximalSubtreeVisitor.FindMaximalSubtrees(nominees, expression); } internal override Expression Visit(Expression exp) { if (exp != null) { bool saveCannotBeNominated = _cannotBeNominated; _cannotBeNominated = false; base.Visit(exp); if (!_cannotBeNominated) { // everyone below me can be nominated, so // see if this one can be also if (_shouldBeNominatedDelegate(exp)) { _candidates.Add(exp); } else { _cannotBeNominated = true; } } _cannotBeNominated |= saveCannotBeNominated; } return exp; } /// /// Visitor that identifies maximal subtrees given a set of nominees. It walks the tree top down /// and when it identifies a nominated node, adds it to the _subtree set and stops walking. /// private sealed class MaximalSubtreeVisitor : ExpressionVisitor { private readonly HashSet_subtrees; private readonly HashSet _nominees; private MaximalSubtreeVisitor(HashSet nominees) { _nominees = nominees; _subtrees = new HashSet (); } internal static HashSet FindMaximalSubtrees(HashSet nominees, Expression query) { MaximalSubtreeVisitor visitor = new MaximalSubtreeVisitor(nominees); visitor.Visit(query); return visitor._subtrees; } internal override Expression Visit(Expression exp) { if (exp != null && _nominees.Contains(exp)) { _subtrees.Add(exp); return exp; } return base.Visit(exp); } } } } // 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
- ByteAnimationBase.cs
- System.Data_BID.cs
- MultiBinding.cs
- FormClosingEvent.cs
- PathGeometry.cs
- BinaryKeyIdentifierClause.cs
- FragmentQueryKB.cs
- XmlTextEncoder.cs
- UnsafeNetInfoNativeMethods.cs
- BridgeDataReader.cs
- ProfileSettingsCollection.cs
- PagerSettings.cs
- SyntaxCheck.cs
- SemaphoreSecurity.cs
- XmlDataImplementation.cs
- fixedPageContentExtractor.cs
- SpeechRecognizer.cs
- AuthenticationModuleElement.cs
- XmlEventCache.cs
- StreamingContext.cs
- IndentedWriter.cs
- ClientSideQueueItem.cs
- TagPrefixCollection.cs
- lengthconverter.cs
- CollectionBase.cs
- LicFileLicenseProvider.cs
- EventlogProvider.cs
- WebPartTracker.cs
- SqlCharStream.cs
- __ConsoleStream.cs
- SqlTypesSchemaImporter.cs
- FormViewUpdateEventArgs.cs
- TextEffect.cs
- AddressingVersion.cs
- SuppressMessageAttribute.cs
- HtmlFormParameterReader.cs
- TableProviderWrapper.cs
- PreservationFileReader.cs
- DetailsViewUpdatedEventArgs.cs
- PageContentAsyncResult.cs
- DataAdapter.cs
- MessageFilterException.cs
- CollectionDataContractAttribute.cs
- TextFormatter.cs
- CreateUserWizardStep.cs
- ArraySegment.cs
- SharedPerformanceCounter.cs
- Publisher.cs
- TemplateComponentConnector.cs
- HorizontalAlignConverter.cs
- PauseStoryboard.cs
- WebPermission.cs
- MsmqDiagnostics.cs
- CounterSample.cs
- InfoCardHelper.cs
- StickyNote.cs
- RequestResponse.cs
- COM2AboutBoxPropertyDescriptor.cs
- documentsequencetextcontainer.cs
- SmiSettersStream.cs
- DataErrorValidationRule.cs
- CryptoApi.cs
- Int32KeyFrameCollection.cs
- PropertyFilter.cs
- EnumerableRowCollection.cs
- localization.cs
- ClientUrlResolverWrapper.cs
- DynamicPropertyHolder.cs
- XmlSerializationGeneratedCode.cs
- RelationHandler.cs
- DrawingCollection.cs
- ListViewItemEventArgs.cs
- ChameleonKey.cs
- FileCodeGroup.cs
- PTManager.cs
- FileLevelControlBuilderAttribute.cs
- WebControlParameterProxy.cs
- ListMarkerSourceInfo.cs
- Main.cs
- AssociationType.cs
- AvTrace.cs
- UnSafeCharBuffer.cs
- Viewport3DVisual.cs
- _AutoWebProxyScriptHelper.cs
- BufferedMessageWriter.cs
- ServerValidateEventArgs.cs
- XmlSchemaObjectCollection.cs
- SqlInternalConnectionSmi.cs
- CodeDirectoryCompiler.cs
- UIElementHelper.cs
- StylusOverProperty.cs
- StrokeCollection2.cs
- StringReader.cs
- TempFiles.cs
- DataGridRowHeaderAutomationPeer.cs
- ParsedRoute.cs
- UxThemeWrapper.cs
- UIAgentMonitor.cs
- ControlPropertyNameConverter.cs
- TextEffectResolver.cs