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
- ClientEndpointLoader.cs
- DateTimeConstantAttribute.cs
- Positioning.cs
- TextTreeRootTextBlock.cs
- COAUTHIDENTITY.cs
- IgnoreDataMemberAttribute.cs
- TextBounds.cs
- FontFamily.cs
- DataGridViewRowDividerDoubleClickEventArgs.cs
- BufferedOutputAsyncStream.cs
- ReadOnlyTernaryTree.cs
- ExtensionFile.cs
- DbQueryCommandTree.cs
- ExitEventArgs.cs
- XmlSchemaSimpleContentExtension.cs
- TableRowCollection.cs
- Visual3D.cs
- HijriCalendar.cs
- AlternationConverter.cs
- ImageAutomationPeer.cs
- ArrayTypeMismatchException.cs
- XmlSchemaAnyAttribute.cs
- StylusCollection.cs
- TypeToken.cs
- OleDbConnectionInternal.cs
- ContextProperty.cs
- DocumentEventArgs.cs
- StrokeNodeOperations.cs
- XmlSchemaExternal.cs
- ChildrenQuery.cs
- XmlWrappingReader.cs
- TextureBrush.cs
- SqlRetyper.cs
- FileDialogCustomPlace.cs
- DataGridViewCell.cs
- TableLayoutSettingsTypeConverter.cs
- ContentType.cs
- DescriptionAttribute.cs
- RoamingStoreFile.cs
- AsyncCompletedEventArgs.cs
- CodeAttributeDeclaration.cs
- MessageSecurityVersion.cs
- NoPersistProperty.cs
- AutomationIdentifierGuids.cs
- HtmlTableRow.cs
- ButtonChrome.cs
- GenericNameHandler.cs
- HorizontalAlignConverter.cs
- MetadataWorkspace.cs
- ParserOptions.cs
- FileLoadException.cs
- HtmlDocument.cs
- GlobalItem.cs
- DrawingContextWalker.cs
- DispatcherProcessingDisabled.cs
- ForeignKeyConstraint.cs
- ModelToObjectValueConverter.cs
- DetailsViewDesigner.cs
- TdsParser.cs
- ToolboxBitmapAttribute.cs
- ForeignConstraint.cs
- ZipIOExtraFieldPaddingElement.cs
- PopOutPanel.cs
- GenerateScriptTypeAttribute.cs
- TextViewSelectionProcessor.cs
- XmlSerializationGeneratedCode.cs
- ThemeDictionaryExtension.cs
- HitTestWithPointDrawingContextWalker.cs
- XamlTreeBuilderBamlRecordWriter.cs
- InfoCardRSAPKCS1KeyExchangeDeformatter.cs
- CodeIdentifiers.cs
- TreeIterator.cs
- SynchronizationScope.cs
- BitmapEffectDrawingContextWalker.cs
- PerspectiveCamera.cs
- DesignTimeTemplateParser.cs
- SelectionGlyph.cs
- Padding.cs
- WorkflowValidationFailedException.cs
- AddIn.cs
- Span.cs
- DataAdapter.cs
- DataGridRowHeader.cs
- DataServiceHostFactory.cs
- AttachedPropertiesService.cs
- FeatureAttribute.cs
- RoleService.cs
- QilExpression.cs
- VectorAnimationUsingKeyFrames.cs
- MouseButton.cs
- SoapFault.cs
- PhoneCallDesigner.cs
- GlobalizationAssembly.cs
- OpCodes.cs
- XmlSchemaSimpleContentExtension.cs
- CfgParser.cs
- EncodingDataItem.cs
- InvalidFilterCriteriaException.cs
- TripleDES.cs
- ToolStripSettings.cs