Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / Orcas / SP / ndp / fx / src / DataEntity / System / Data / Objects / ELinq / Visitors / LinqMaximalSubtreeNominator.cs / 3 / 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
- OutputCacheProfileCollection.cs
- ManagementQuery.cs
- TcpConnectionPool.cs
- GZipDecoder.cs
- DrawToolTipEventArgs.cs
- EntityDataSourceValidationException.cs
- ListBase.cs
- FunctionQuery.cs
- mediaclock.cs
- InplaceBitmapMetadataWriter.cs
- Vector3DCollection.cs
- BindingRestrictions.cs
- PenLineCapValidation.cs
- InfoCardRequestException.cs
- EntityContainer.cs
- PerformanceCounterLib.cs
- OracleNumber.cs
- WinEventWrap.cs
- TextCollapsingProperties.cs
- RemoteDebugger.cs
- DropDownList.cs
- DelegatingTypeDescriptionProvider.cs
- Context.cs
- EdmTypeAttribute.cs
- StorageBasedPackageProperties.cs
- QuadraticBezierSegment.cs
- ThreadInterruptedException.cs
- SocketAddress.cs
- TemplatePartAttribute.cs
- XpsPackagingPolicy.cs
- HMACSHA384.cs
- CalculatedColumn.cs
- InternalPermissions.cs
- RichTextBoxDesigner.cs
- MetabaseSettingsIis7.cs
- CommonDialog.cs
- PermissionRequestEvidence.cs
- AssemblyFilter.cs
- OpCopier.cs
- LoginUtil.cs
- TypographyProperties.cs
- DateTimeStorage.cs
- InputBindingCollection.cs
- SchemaReference.cs
- DesignerDataSourceView.cs
- ReachFixedPageSerializerAsync.cs
- Opcode.cs
- ZipFileInfo.cs
- SchemaObjectWriter.cs
- DbQueryCommandTree.cs
- ToolStripPanelCell.cs
- BindingListCollectionView.cs
- StringValueSerializer.cs
- CultureInfo.cs
- PrivilegedConfigurationManager.cs
- FixedTextSelectionProcessor.cs
- ResourcesBuildProvider.cs
- ItemsControl.cs
- SqlCommandBuilder.cs
- AttributeExtensions.cs
- HtmlAnchor.cs
- ContextMarshalException.cs
- RSAPKCS1KeyExchangeFormatter.cs
- SchemaObjectWriter.cs
- WebHeaderCollection.cs
- FormViewUpdatedEventArgs.cs
- PropertyChangedEventManager.cs
- _ListenerRequestStream.cs
- CompModSwitches.cs
- WorkflowServiceHost.cs
- SafeViewOfFileHandle.cs
- FollowerQueueCreator.cs
- CircleHotSpot.cs
- MenuItem.cs
- NonClientArea.cs
- SignedXml.cs
- CommandSet.cs
- HideDisabledControlAdapter.cs
- ItemMap.cs
- GeneralTransform2DTo3D.cs
- EntityWrapper.cs
- QuaternionKeyFrameCollection.cs
- SoapReflectionImporter.cs
- StringComparer.cs
- FileDialogPermission.cs
- ILGenerator.cs
- ExecutionPropertyManager.cs
- DataGridViewControlCollection.cs
- WebPartEditorCancelVerb.cs
- Token.cs
- EntityDataSourceValidationException.cs
- HttpStaticObjectsCollectionWrapper.cs
- FieldNameLookup.cs
- PrimitiveRenderer.cs
- CryptoHandle.cs
- GeneralTransformCollection.cs
- SqlBuffer.cs
- Bold.cs
- Content.cs
- DesignBinding.cs