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 / Common / Utils / Helpers.cs / 1 / Helpers.cs
//---------------------------------------------------------------------- //// Copyright (c) Microsoft Corporation. All rights reserved. // // // @owner [....] // @backupOwner [....] //--------------------------------------------------------------------- using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.Globalization; using System.Diagnostics; namespace System.Data.Common.Utils { // Miscellaneous helper routines internal static class Helpers { #region Trace methods // effects: Trace args according to the CLR format string with a new line internal static void FormatTraceLine(string format, params object[] args) { Trace.WriteLine(String.Format(CultureInfo.InvariantCulture, format, args)); } // effects: Trace the string with a new line internal static void StringTrace(string arg) { Trace.Write(arg); } // effects: Trace the string without adding a new line internal static void StringTraceLine(string arg) { Trace.WriteLine(arg); } #endregion #region Misc Helpers // effects: compares two sets using the given comparer - removes // duplicates if they exist internal static bool IsSetEqual(IEnumerable list1, IEnumerable list2, IEqualityComparer comparer) { Set set1 = new Set (list1, comparer); Set set2 = new Set (list2, comparer); return set1.SetEquals(set2); } // effects: Given a stream of values of type "SubType", returns a // stream of values of type "SuperType" where SuperType is a // superclass/supertype of SubType internal static IEnumerable AsSuperTypeList (IEnumerable values) where SubType : SuperType { foreach (SubType value in values) { yield return value; } } /// /// Builds a balanced binary tree with the specified nodes as leaves. /// Note that the current elements of ///MAY be overwritten /// as the leaves are combined to produce the tree. /// The type of each node in the tree /// The leaf nodes to combine into an balanced binary tree /// A function that produces a new node that is the combination of the two specified argument nodes ///The single node that is the root of the balanced binary tree internal static TNode BuildBalancedTreeInPlace(IList nodes, Func combinator) { EntityUtil.CheckArgumentNull(nodes, "nodes"); EntityUtil.CheckArgumentNull(combinator, "combinator"); Debug.Assert(nodes.Count > 0, "At least one node is required"); // If only one node is present, return the single node. if (nodes.Count == 1) { return nodes[0]; } // For the two-node case, simply combine the two nodes and return the result. if (nodes.Count == 2) { return combinator(nodes[0], nodes[1]); } // // Build the balanced tree in a bottom-up fashion. // On each iteration, an even number of nodes are paired off using the // combinator function, reducing the total number of available leaf nodes // by half each time. If the number of nodes in an iteration is not even, // the 'last' node in the set is omitted, then combined with the last pair // that is produced. // Nodes are collected from left to right with newly combined nodes overwriting // nodes from the previous iteration that have already been consumed (as can // be seen by 'writePos' lagging 'readPos' in the main statement of the loop below). // When a single available leaf node remains, this node is the root of the // balanced binary tree and can be returned to the caller. // int nodesToPair = nodes.Count; while (nodesToPair != 1) { bool combineModulo = ((nodesToPair & 0x1) == 1); if (combineModulo) { nodesToPair--; } int writePos = 0; for (int readPos = 0; readPos < nodesToPair; readPos += 2) { nodes[writePos++] = combinator(nodes[readPos], nodes[readPos + 1]); } if (combineModulo) { int updatePos = writePos - 1; nodes[updatePos] = combinator(nodes[updatePos], nodes[nodesToPair]); } nodesToPair /= 2; } return nodes[0]; } #endregion } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //---------------------------------------------------------------------- // // Copyright (c) Microsoft Corporation. All rights reserved. // // // @owner [....] // @backupOwner [....] //--------------------------------------------------------------------- using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.Globalization; using System.Diagnostics; namespace System.Data.Common.Utils { // Miscellaneous helper routines internal static class Helpers { #region Trace methods // effects: Trace args according to the CLR format string with a new line internal static void FormatTraceLine(string format, params object[] args) { Trace.WriteLine(String.Format(CultureInfo.InvariantCulture, format, args)); } // effects: Trace the string with a new line internal static void StringTrace(string arg) { Trace.Write(arg); } // effects: Trace the string without adding a new line internal static void StringTraceLine(string arg) { Trace.WriteLine(arg); } #endregion #region Misc Helpers // effects: compares two sets using the given comparer - removes // duplicates if they exist internal static bool IsSetEqual(IEnumerable list1, IEnumerable list2, IEqualityComparer comparer) { Set set1 = new Set (list1, comparer); Set set2 = new Set (list2, comparer); return set1.SetEquals(set2); } // effects: Given a stream of values of type "SubType", returns a // stream of values of type "SuperType" where SuperType is a // superclass/supertype of SubType internal static IEnumerable AsSuperTypeList (IEnumerable values) where SubType : SuperType { foreach (SubType value in values) { yield return value; } } /// /// Builds a balanced binary tree with the specified nodes as leaves. /// Note that the current elements of ///MAY be overwritten /// as the leaves are combined to produce the tree. /// The type of each node in the tree /// The leaf nodes to combine into an balanced binary tree /// A function that produces a new node that is the combination of the two specified argument nodes ///The single node that is the root of the balanced binary tree internal static TNode BuildBalancedTreeInPlace(IList nodes, Func combinator) { EntityUtil.CheckArgumentNull(nodes, "nodes"); EntityUtil.CheckArgumentNull(combinator, "combinator"); Debug.Assert(nodes.Count > 0, "At least one node is required"); // If only one node is present, return the single node. if (nodes.Count == 1) { return nodes[0]; } // For the two-node case, simply combine the two nodes and return the result. if (nodes.Count == 2) { return combinator(nodes[0], nodes[1]); } // // Build the balanced tree in a bottom-up fashion. // On each iteration, an even number of nodes are paired off using the // combinator function, reducing the total number of available leaf nodes // by half each time. If the number of nodes in an iteration is not even, // the 'last' node in the set is omitted, then combined with the last pair // that is produced. // Nodes are collected from left to right with newly combined nodes overwriting // nodes from the previous iteration that have already been consumed (as can // be seen by 'writePos' lagging 'readPos' in the main statement of the loop below). // When a single available leaf node remains, this node is the root of the // balanced binary tree and can be returned to the caller. // int nodesToPair = nodes.Count; while (nodesToPair != 1) { bool combineModulo = ((nodesToPair & 0x1) == 1); if (combineModulo) { nodesToPair--; } int writePos = 0; for (int readPos = 0; readPos < nodesToPair; readPos += 2) { nodes[writePos++] = combinator(nodes[readPos], nodes[readPos + 1]); } if (combineModulo) { int updatePos = writePos - 1; nodes[updatePos] = combinator(nodes[updatePos], nodes[nodesToPair]); } nodesToPair /= 2; } return nodes[0]; } #endregion } } // 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
- TraceContextEventArgs.cs
- ColorKeyFrameCollection.cs
- ThemeDirectoryCompiler.cs
- Vector3DKeyFrameCollection.cs
- DataGridCellClipboardEventArgs.cs
- Pts.cs
- ListSourceHelper.cs
- TextMetrics.cs
- RemoveStoryboard.cs
- SecureStringHasher.cs
- RepeaterItem.cs
- ErrorView.xaml.cs
- DelimitedListTraceListener.cs
- ExitEventArgs.cs
- Bezier.cs
- Set.cs
- Operator.cs
- BaseTemplateCodeDomTreeGenerator.cs
- WebPartsPersonalization.cs
- PEFileReader.cs
- TrackingParameters.cs
- DataGridTable.cs
- UriTemplateLiteralQueryValue.cs
- PermissionSetTriple.cs
- OracleParameterCollection.cs
- BlobPersonalizationState.cs
- HtmlInputControl.cs
- GeometryDrawing.cs
- SerialReceived.cs
- PersonalizationAdministration.cs
- SafeCryptoHandles.cs
- ScriptResourceHandler.cs
- RolePrincipal.cs
- RoutedEventHandlerInfo.cs
- Transform.cs
- ScriptReference.cs
- TouchEventArgs.cs
- ModuleConfigurationInfo.cs
- DesignerToolStripControlHost.cs
- RTLAwareMessageBox.cs
- RegionInfo.cs
- _WinHttpWebProxyDataBuilder.cs
- XmlSignificantWhitespace.cs
- ContentPresenter.cs
- UserThread.cs
- OpenTypeLayoutCache.cs
- RSAPKCS1SignatureDeformatter.cs
- ProfileSettingsCollection.cs
- SqlXml.cs
- NameGenerator.cs
- GregorianCalendarHelper.cs
- SQlBooleanStorage.cs
- Compilation.cs
- Util.cs
- ConversionHelper.cs
- MemberAccessException.cs
- InputLanguageEventArgs.cs
- RowSpanVector.cs
- NeutralResourcesLanguageAttribute.cs
- ComponentResourceKey.cs
- TextContainerChangedEventArgs.cs
- ObjectParameterCollection.cs
- WorkflowMessageEventArgs.cs
- UpdateExpressionVisitor.cs
- OrderedEnumerableRowCollection.cs
- StateItem.cs
- ServiceContractDetailViewControl.cs
- InstanceKeyCollisionException.cs
- SqlConnectionStringBuilder.cs
- WindowsRichEdit.cs
- InternalConfigRoot.cs
- StylusPoint.cs
- DrawingImage.cs
- XmlC14NWriter.cs
- XmlSchemaObjectCollection.cs
- Logging.cs
- GridViewRow.cs
- Mapping.cs
- UnsafeCollabNativeMethods.cs
- GridViewHeaderRowPresenter.cs
- FormatterConverter.cs
- ManagementBaseObject.cs
- DbMetaDataColumnNames.cs
- QueryOperator.cs
- AccessControlEntry.cs
- UInt32.cs
- MetadataSerializer.cs
- StyleXamlTreeBuilder.cs
- RadioButton.cs
- EffectiveValueEntry.cs
- SecurityPolicySection.cs
- SSmlParser.cs
- SortedDictionary.cs
- SoapMessage.cs
- Typeface.cs
- HttpMethodAttribute.cs
- DocumentViewerAutomationPeer.cs
- DecimalAnimation.cs
- SQLString.cs
- OTFRasterizer.cs