Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / Orcas / QFE / 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
- UnregisterInfo.cs
- JsonReader.cs
- Content.cs
- ContentPlaceHolder.cs
- DataPagerCommandEventArgs.cs
- AliasExpr.cs
- DeploymentExceptionMapper.cs
- TypeInfo.cs
- GuidTagList.cs
- PngBitmapDecoder.cs
- FeatureAttribute.cs
- VerifyHashRequest.cs
- SoapSchemaMember.cs
- ListViewPagedDataSource.cs
- SizeAnimationClockResource.cs
- NodeLabelEditEvent.cs
- TypeSystem.cs
- XmlSchemaExporter.cs
- CleanUpVirtualizedItemEventArgs.cs
- NullableDoubleSumAggregationOperator.cs
- PropertyMapper.cs
- mda.cs
- KeyedPriorityQueue.cs
- TemplateBindingExtensionConverter.cs
- SqlTriggerContext.cs
- MessageHeaderException.cs
- PropertiesTab.cs
- SequenceFullException.cs
- SqlCachedBuffer.cs
- AccessibleObject.cs
- StylusOverProperty.cs
- StorageMappingItemCollection.cs
- RootProfilePropertySettingsCollection.cs
- WebPartDisplayModeEventArgs.cs
- Site.cs
- Trace.cs
- PropertySegmentSerializationProvider.cs
- FontStyleConverter.cs
- XmlKeywords.cs
- Debugger.cs
- BlobPersonalizationState.cs
- DbProviderFactories.cs
- PowerStatus.cs
- _UncName.cs
- AccessibleObject.cs
- StringSource.cs
- ObjectSecurity.cs
- ConstrainedDataObject.cs
- IPPacketInformation.cs
- ReflectionPermission.cs
- SchemaNotation.cs
- XsltQilFactory.cs
- DataGridColumnFloatingHeader.cs
- ReplyChannel.cs
- OperationCanceledException.cs
- DockEditor.cs
- WindowsFont.cs
- WebPartAddingEventArgs.cs
- PrintDocument.cs
- Matrix3DConverter.cs
- BuildDependencySet.cs
- SplineQuaternionKeyFrame.cs
- ServiceHttpHandlerFactory.cs
- OneOf.cs
- VirtualDirectoryMappingCollection.cs
- ObjectDataSourceView.cs
- XmlDictionaryReaderQuotasElement.cs
- ASCIIEncoding.cs
- PropertyChangedEventArgs.cs
- RulePatternOps.cs
- SerializableAttribute.cs
- HttpContext.cs
- NativeRecognizer.cs
- IISMapPath.cs
- ExpandSegmentCollection.cs
- URLEditor.cs
- TreeViewCancelEvent.cs
- FlagsAttribute.cs
- ProcessInputEventArgs.cs
- SqlDataAdapter.cs
- TextEffect.cs
- LoginView.cs
- HttpListener.cs
- DateTimeFormatInfo.cs
- EntityDescriptor.cs
- ConstraintEnumerator.cs
- ReliabilityContractAttribute.cs
- MenuStrip.cs
- HiddenField.cs
- ImageDrawing.cs
- FlowDocumentReader.cs
- NativeCompoundFileAPIs.cs
- HashRepartitionEnumerator.cs
- NativeWindow.cs
- SafeNativeMethods.cs
- BooleanFacetDescriptionElement.cs
- DocumentEventArgs.cs
- ImageBrush.cs
- RNGCryptoServiceProvider.cs
- ConfigurationManager.cs