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
- CodeConditionStatement.cs
- PolicyManager.cs
- BaseTemplateParser.cs
- DownloadProgressEventArgs.cs
- CollectionExtensions.cs
- OdbcFactory.cs
- WorkflowCommandExtensionItem.cs
- BindingCollection.cs
- SHA1Managed.cs
- TransformerTypeCollection.cs
- InlineUIContainer.cs
- LinqDataSourceDeleteEventArgs.cs
- HTMLTextWriter.cs
- BaseUriHelper.cs
- NativeRecognizer.cs
- StringUtil.cs
- DecimalAnimationBase.cs
- BitmapEffectCollection.cs
- DataKey.cs
- ConfigXmlWhitespace.cs
- PhonemeConverter.cs
- QualificationDataItem.cs
- ItemContainerProviderWrapper.cs
- RemoteCryptoSignHashRequest.cs
- RemoteCryptoDecryptRequest.cs
- MethodAccessException.cs
- SiteMapHierarchicalDataSourceView.cs
- BufferedGraphicsContext.cs
- WebZone.cs
- _LocalDataStore.cs
- PropertyCollection.cs
- NumberFormatter.cs
- NumericUpDown.cs
- ApplicationCommands.cs
- EntityDesignerDataSourceView.cs
- Select.cs
- XmlReturnReader.cs
- AssertSection.cs
- ContextProperty.cs
- URL.cs
- WCFModelStrings.Designer.cs
- HtmlContainerControl.cs
- Attribute.cs
- DbConnectionPoolCounters.cs
- WindowProviderWrapper.cs
- WorkflowRuntime.cs
- UnsafeNativeMethods.cs
- DbConnectionPoolGroup.cs
- RijndaelManaged.cs
- InitializationEventAttribute.cs
- MissingManifestResourceException.cs
- GenericUriParser.cs
- ReadOnlyDataSourceView.cs
- _DigestClient.cs
- ClientBuildManagerTypeDescriptionProviderBridge.cs
- OneOfConst.cs
- IndicFontClient.cs
- ResetableIterator.cs
- MouseWheelEventArgs.cs
- Logging.cs
- BindingMAnagerBase.cs
- Int32RectValueSerializer.cs
- OAVariantLib.cs
- InternalDispatchObject.cs
- RTLAwareMessageBox.cs
- PropertyRef.cs
- UnrecognizedAssertionsBindingElement.cs
- UserPersonalizationStateInfo.cs
- SettingsContext.cs
- HMACSHA512.cs
- PerformanceCountersElement.cs
- ISCIIEncoding.cs
- EndpointNameMessageFilter.cs
- MetadataResolver.cs
- CustomAttributeFormatException.cs
- PngBitmapDecoder.cs
- StorageFunctionMapping.cs
- RuntimeVariablesExpression.cs
- RequestCacheEntry.cs
- WarningException.cs
- ErrorEventArgs.cs
- SplitterPanel.cs
- SchemaContext.cs
- COM2IManagedPerPropertyBrowsingHandler.cs
- PolicyUtility.cs
- FrameworkElementAutomationPeer.cs
- DataGridItem.cs
- BitmapSourceSafeMILHandle.cs
- SqlHelper.cs
- SerializationStore.cs
- RegistrationServices.cs
- XPathSelectionIterator.cs
- ImageCollectionEditor.cs
- GeometryDrawing.cs
- FileAuthorizationModule.cs
- EntryWrittenEventArgs.cs
- XmlILAnnotation.cs
- FrameworkPropertyMetadata.cs
- _DisconnectOverlappedAsyncResult.cs
- peernodeimplementation.cs