Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Core / Microsoft / Scripting / Actions / RuleCache.cs / 1305376 / RuleCache.cs
/* **************************************************************************** * * Copyright (c) Microsoft Corporation. * * This source code is subject to terms and conditions of the Microsoft Public License. A * copy of the license can be found in the License.html file at the root of this distribution. If * you cannot locate the Microsoft Public License, please send an email to * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound * by the terms of the Microsoft Public License. * * You must not remove this notice, or any other, from this software. * * * ***************************************************************************/ using System.ComponentModel; using System.Diagnostics; using System.Dynamic.Utils; namespace System.Runtime.CompilerServices { ////// This API supports the .NET Framework infrastructure and is not intended to be used directly from your code. /// Represents a cache of runtime binding rules. /// ///The delegate type. [EditorBrowsable(EditorBrowsableState.Never), DebuggerStepThrough] public class RuleCachewhere T : class { private T[] _rules = new T[0]; private readonly Object cacheLock = new Object(); private const int MaxRules = 128; internal RuleCache() { } internal T[] GetRules() { return _rules; } // move the rule +2 up. // this is called on every successful rule. internal void MoveRule(T rule, int i) { // limit search to MaxSearch elements. // Rule should not get too far unless it has been already moved up. // need a lock to make sure we are moving the right rule and not loosing any. lock (cacheLock) { const int MaxSearch = 8; int count = _rules.Length - i; if (count > MaxSearch) { count = MaxSearch; } int oldIndex = -1; int max = Math.Min(_rules.Length, i + count); for (int index = i; index < max; index++) { if (_rules[index] == rule) { oldIndex = index; break; } } if (oldIndex < 0) { return; } T oldRule = _rules[oldIndex]; _rules[oldIndex] = _rules[oldIndex - 1]; _rules[oldIndex - 1] = _rules[oldIndex - 2]; _rules[oldIndex - 2] = oldRule; } } internal void AddRule(T newRule) { // need a lock to make sure we are not loosing rules. lock (cacheLock) { _rules = AddOrInsert(_rules, newRule); } } internal void ReplaceRule(T oldRule, T newRule) { // need a lock to make sure we are replacing the right rule lock (cacheLock) { int i = Array.IndexOf(_rules, oldRule); if (i >= 0) { _rules[i] = newRule; return; // DONE } // could not find it. _rules = AddOrInsert(_rules, newRule); } } // Adds to end or or inserts items at InsertPosition private const int InsertPosition = MaxRules / 2; private static T[] AddOrInsert(T[] rules, T item) { if (rules.Length < InsertPosition) { return rules.AddLast(item); } T[] newRules; int newLength = rules.Length + 1; if (newLength > MaxRules) { newLength = MaxRules; newRules = rules; } else { newRules = new T[newLength]; } Array.Copy(rules, 0, newRules, 0, InsertPosition); newRules[InsertPosition] = item; Array.Copy(rules, InsertPosition, newRules, InsertPosition + 1, newLength - InsertPosition - 1); return newRules; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. /* **************************************************************************** * * Copyright (c) Microsoft Corporation. * * This source code is subject to terms and conditions of the Microsoft Public License. A * copy of the license can be found in the License.html file at the root of this distribution. If * you cannot locate the Microsoft Public License, please send an email to * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound * by the terms of the Microsoft Public License. * * You must not remove this notice, or any other, from this software. * * * ***************************************************************************/ using System.ComponentModel; using System.Diagnostics; using System.Dynamic.Utils; namespace System.Runtime.CompilerServices { /// /// This API supports the .NET Framework infrastructure and is not intended to be used directly from your code. /// Represents a cache of runtime binding rules. /// ///The delegate type. [EditorBrowsable(EditorBrowsableState.Never), DebuggerStepThrough] public class RuleCachewhere T : class { private T[] _rules = new T[0]; private readonly Object cacheLock = new Object(); private const int MaxRules = 128; internal RuleCache() { } internal T[] GetRules() { return _rules; } // move the rule +2 up. // this is called on every successful rule. internal void MoveRule(T rule, int i) { // limit search to MaxSearch elements. // Rule should not get too far unless it has been already moved up. // need a lock to make sure we are moving the right rule and not loosing any. lock (cacheLock) { const int MaxSearch = 8; int count = _rules.Length - i; if (count > MaxSearch) { count = MaxSearch; } int oldIndex = -1; int max = Math.Min(_rules.Length, i + count); for (int index = i; index < max; index++) { if (_rules[index] == rule) { oldIndex = index; break; } } if (oldIndex < 0) { return; } T oldRule = _rules[oldIndex]; _rules[oldIndex] = _rules[oldIndex - 1]; _rules[oldIndex - 1] = _rules[oldIndex - 2]; _rules[oldIndex - 2] = oldRule; } } internal void AddRule(T newRule) { // need a lock to make sure we are not loosing rules. lock (cacheLock) { _rules = AddOrInsert(_rules, newRule); } } internal void ReplaceRule(T oldRule, T newRule) { // need a lock to make sure we are replacing the right rule lock (cacheLock) { int i = Array.IndexOf(_rules, oldRule); if (i >= 0) { _rules[i] = newRule; return; // DONE } // could not find it. _rules = AddOrInsert(_rules, newRule); } } // Adds to end or or inserts items at InsertPosition private const int InsertPosition = MaxRules / 2; private static T[] AddOrInsert(T[] rules, T item) { if (rules.Length < InsertPosition) { return rules.AddLast(item); } T[] newRules; int newLength = rules.Length + 1; if (newLength > MaxRules) { newLength = MaxRules; newRules = rules; } else { newRules = new T[newLength]; } Array.Copy(rules, 0, newRules, 0, InsertPosition); newRules[InsertPosition] = item; Array.Copy(rules, InsertPosition, newRules, InsertPosition + 1, newLength - InsertPosition - 1); return newRules; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- FloaterBaseParagraph.cs
- DataControlReferenceCollection.cs
- ObjRef.cs
- ComponentCollection.cs
- Visual3D.cs
- PriorityQueue.cs
- MetadataUtilsSmi.cs
- ObjectStateManagerMetadata.cs
- AsyncOperationManager.cs
- ContentType.cs
- GAC.cs
- HostedElements.cs
- Encoder.cs
- WebServiceResponseDesigner.cs
- LeafCellTreeNode.cs
- SystemWebCachingSectionGroup.cs
- Model3DCollection.cs
- ErrorFormatterPage.cs
- DatePickerAutomationPeer.cs
- CommandEventArgs.cs
- ServicePointManagerElement.cs
- XmlObjectSerializerWriteContextComplex.cs
- ListViewItemEventArgs.cs
- CacheHelper.cs
- DesignerTransactionCloseEvent.cs
- SqlServer2KCompatibilityCheck.cs
- ObjectFactoryCodeDomTreeGenerator.cs
- RelationalExpressions.cs
- WebServiceFault.cs
- XmlFormatExtensionAttribute.cs
- StreamGeometry.cs
- sqlcontext.cs
- CompositionAdorner.cs
- MediaSystem.cs
- Brush.cs
- ObjectStateEntryDbDataRecord.cs
- AxHostDesigner.cs
- DbReferenceCollection.cs
- GeneralTransform3D.cs
- translator.cs
- GrammarBuilderRuleRef.cs
- ToolStripDropDownItem.cs
- DocumentApplicationJournalEntry.cs
- TCPClient.cs
- CreateUserWizardStep.cs
- RestHandlerFactory.cs
- BindableAttribute.cs
- WsdlImporterElementCollection.cs
- BlockCollection.cs
- Dictionary.cs
- SyndicationPerson.cs
- CompilerCollection.cs
- CodeDelegateInvokeExpression.cs
- _NetworkingPerfCounters.cs
- SkipStoryboardToFill.cs
- ObjectTokenCategory.cs
- HtmlWindow.cs
- Attributes.cs
- TextTreeInsertElementUndoUnit.cs
- Tokenizer.cs
- WaitHandle.cs
- Helpers.cs
- AuthenticateEventArgs.cs
- UIHelper.cs
- DistributedTransactionPermission.cs
- SparseMemoryStream.cs
- ImageDrawing.cs
- SQLInt16Storage.cs
- WpfWebRequestHelper.cs
- SqlDependencyUtils.cs
- SecurityAlgorithmSuite.cs
- XmlUtil.cs
- Vector3DAnimationBase.cs
- EdmFunctionAttribute.cs
- CreateUserErrorEventArgs.cs
- _LoggingObject.cs
- KeyboardNavigation.cs
- TabControlEvent.cs
- AsymmetricSignatureFormatter.cs
- AppDomainManager.cs
- TagMapInfo.cs
- ConnectionProviderAttribute.cs
- HttpRequestWrapper.cs
- X509ChainPolicy.cs
- ActiveXMessageFormatter.cs
- WizardPanel.cs
- WindowsRebar.cs
- Timer.cs
- PropertyGridEditorPart.cs
- ColorInterpolationModeValidation.cs
- FileSystemWatcher.cs
- XPathCompiler.cs
- PermissionToken.cs
- _AutoWebProxyScriptWrapper.cs
- AsymmetricKeyExchangeDeformatter.cs
- ShortcutKeysEditor.cs
- OperandQuery.cs
- SignatureToken.cs
- PageContentAsyncResult.cs
- SmiMetaDataProperty.cs