Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / WCF / Serialization / System / Xml / XmlBinaryWriterSession.cs / 1305376 / XmlBinaryWriterSession.cs
//------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------- namespace System.Xml { using System; using System.Xml; using System.Collections; using System.Diagnostics; using System.Runtime.Serialization; using System.Collections.Generic; using System.Collections.ObjectModel; public class XmlBinaryWriterSession { PriorityDictionarystrings; PriorityDictionary maps; int nextKey; public XmlBinaryWriterSession() { this.nextKey = 0; this.maps = new PriorityDictionary (); this.strings = new PriorityDictionary (); } public virtual bool TryAdd(XmlDictionaryString value, out int key) { IntArray keys; if (value == null) throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value"); if (maps.TryGetValue(value.Dictionary, out keys)) { key = (keys[value.Key] - 1); if (key != -1) { // If the key is already set, then something is wrong throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.XmlKeyAlreadyExists))); } key = Add(value.Value); keys[value.Key] = (key + 1); return true; } key = Add(value.Value); keys = AddKeys(value.Dictionary, value.Key + 1); keys[value.Key] = (key + 1); return true; } int Add(string s) { int key = this.nextKey++; strings.Add(s, key); return key; } IntArray AddKeys(IXmlDictionary dictionary, int minCount) { IntArray keys = new IntArray(Math.Max(minCount, 16)); maps.Add(dictionary, keys); return keys; } public void Reset() { nextKey = 0; maps.Clear(); strings.Clear(); } internal bool TryLookup(XmlDictionaryString s, out int key) { IntArray keys; if (maps.TryGetValue(s.Dictionary, out keys)) { key = (keys[s.Key] - 1); if (key != -1) { return true; } } if (strings.TryGetValue(s.Value, out key)) { if (keys == null) { keys = AddKeys(s.Dictionary, s.Key + 1); } keys[s.Key] = (key + 1); return true; } key = -1; return false; } class PriorityDictionary where K : class { Dictionary dictionary; Entry[] list; int listCount; int now; public PriorityDictionary() { list = new Entry[16]; } public void Clear() { now = 0; listCount = 0; Array.Clear(list, 0, list.Length); if (dictionary != null) dictionary.Clear(); } public bool TryGetValue(K key, out V value) { for (int i = 0; i < listCount; i++) { if (list[i].Key == key) { value = list[i].Value; list[i].Time = Now; return true; } } for (int i = 0; i < listCount; i++) { if (list[i].Key.Equals(key)) { value = list[i].Value; list[i].Time = Now; return true; } } if (dictionary == null) { value = default(V); return false; } if (!dictionary.TryGetValue(key, out value)) { return false; } int minIndex = 0; int minTime = list[0].Time; for (int i = 1; i < listCount; i++) { if (list[i].Time < minTime) { minIndex = i; minTime = list[i].Time; } } list[minIndex].Key = key; list[minIndex].Value = value; list[minIndex].Time = Now; return true; } public void Add(K key, V value) { if (listCount < list.Length) { list[listCount].Key = key; list[listCount].Value = value; listCount++; } else { if (dictionary == null) { dictionary = new Dictionary (); for (int i = 0; i < listCount; i++) { dictionary.Add(list[i].Key, list[i].Value); } } dictionary.Add(key, value); } } int Now { get { if (++now == int.MaxValue) { DecreaseAll(); } return now; } } void DecreaseAll() { for (int i = 0; i < listCount; i++) { list[i].Time /= 2; } now /= 2; } struct Entry { public K Key; public V Value; public int Time; } } class IntArray { int[] array; public IntArray(int size) { this.array = new int[size]; } public int this[int index] { get { if (index >= array.Length) return 0; return array[index]; } set { if (index >= array.Length) { int[] newArray = new int[Math.Max(index + 1, array.Length * 2)]; Array.Copy(array, newArray, array.Length); array = newArray; } array[index] = value; } } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------- namespace System.Xml { using System; using System.Xml; using System.Collections; using System.Diagnostics; using System.Runtime.Serialization; using System.Collections.Generic; using System.Collections.ObjectModel; public class XmlBinaryWriterSession { PriorityDictionary strings; PriorityDictionary maps; int nextKey; public XmlBinaryWriterSession() { this.nextKey = 0; this.maps = new PriorityDictionary (); this.strings = new PriorityDictionary (); } public virtual bool TryAdd(XmlDictionaryString value, out int key) { IntArray keys; if (value == null) throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value"); if (maps.TryGetValue(value.Dictionary, out keys)) { key = (keys[value.Key] - 1); if (key != -1) { // If the key is already set, then something is wrong throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.XmlKeyAlreadyExists))); } key = Add(value.Value); keys[value.Key] = (key + 1); return true; } key = Add(value.Value); keys = AddKeys(value.Dictionary, value.Key + 1); keys[value.Key] = (key + 1); return true; } int Add(string s) { int key = this.nextKey++; strings.Add(s, key); return key; } IntArray AddKeys(IXmlDictionary dictionary, int minCount) { IntArray keys = new IntArray(Math.Max(minCount, 16)); maps.Add(dictionary, keys); return keys; } public void Reset() { nextKey = 0; maps.Clear(); strings.Clear(); } internal bool TryLookup(XmlDictionaryString s, out int key) { IntArray keys; if (maps.TryGetValue(s.Dictionary, out keys)) { key = (keys[s.Key] - 1); if (key != -1) { return true; } } if (strings.TryGetValue(s.Value, out key)) { if (keys == null) { keys = AddKeys(s.Dictionary, s.Key + 1); } keys[s.Key] = (key + 1); return true; } key = -1; return false; } class PriorityDictionary where K : class { Dictionary dictionary; Entry[] list; int listCount; int now; public PriorityDictionary() { list = new Entry[16]; } public void Clear() { now = 0; listCount = 0; Array.Clear(list, 0, list.Length); if (dictionary != null) dictionary.Clear(); } public bool TryGetValue(K key, out V value) { for (int i = 0; i < listCount; i++) { if (list[i].Key == key) { value = list[i].Value; list[i].Time = Now; return true; } } for (int i = 0; i < listCount; i++) { if (list[i].Key.Equals(key)) { value = list[i].Value; list[i].Time = Now; return true; } } if (dictionary == null) { value = default(V); return false; } if (!dictionary.TryGetValue(key, out value)) { return false; } int minIndex = 0; int minTime = list[0].Time; for (int i = 1; i < listCount; i++) { if (list[i].Time < minTime) { minIndex = i; minTime = list[i].Time; } } list[minIndex].Key = key; list[minIndex].Value = value; list[minIndex].Time = Now; return true; } public void Add(K key, V value) { if (listCount < list.Length) { list[listCount].Key = key; list[listCount].Value = value; listCount++; } else { if (dictionary == null) { dictionary = new Dictionary (); for (int i = 0; i < listCount; i++) { dictionary.Add(list[i].Key, list[i].Value); } } dictionary.Add(key, value); } } int Now { get { if (++now == int.MaxValue) { DecreaseAll(); } return now; } } void DecreaseAll() { for (int i = 0; i < listCount; i++) { list[i].Time /= 2; } now /= 2; } struct Entry { public K Key; public V Value; public int Time; } } class IntArray { int[] array; public IntArray(int size) { this.array = new int[size]; } public int this[int index] { get { if (index >= array.Length) return 0; return array[index]; } set { if (index >= array.Length) { int[] newArray = new int[Math.Max(index + 1, array.Length * 2)]; Array.Copy(array, newArray, array.Length); array = newArray; } array[index] = value; } } } } } // 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
- HealthMonitoringSectionHelper.cs
- AbstractSvcMapFileLoader.cs
- ProtocolsConfiguration.cs
- EventToken.cs
- TrackingParameters.cs
- Utility.cs
- BamlLocalizerErrorNotifyEventArgs.cs
- SafeEventLogReadHandle.cs
- WebPartCancelEventArgs.cs
- CompositeTypefaceMetrics.cs
- Helpers.cs
- InputBinding.cs
- MessageBox.cs
- AsynchronousChannel.cs
- ObjectContextServiceProvider.cs
- Common.cs
- TextEndOfParagraph.cs
- XmlAggregates.cs
- LocatorGroup.cs
- ContextMenuStrip.cs
- MetadataImporterQuotas.cs
- GPPOINTF.cs
- TemplatePagerField.cs
- Speller.cs
- XNodeSchemaApplier.cs
- StylusEventArgs.cs
- SoapFault.cs
- TrustManagerMoreInformation.cs
- CounterSet.cs
- cookiecollection.cs
- SerializerProvider.cs
- SystemIPv6InterfaceProperties.cs
- ConnectionStringsExpressionEditor.cs
- StreamResourceInfo.cs
- CategoryGridEntry.cs
- JsonEnumDataContract.cs
- SqlDependencyListener.cs
- DesignerSerializationVisibilityAttribute.cs
- SpecialFolderEnumConverter.cs
- DeploymentSection.cs
- SharedPersonalizationStateInfo.cs
- Literal.cs
- UnmanagedMemoryStreamWrapper.cs
- ScopelessEnumAttribute.cs
- Rectangle.cs
- BufferedGraphicsManager.cs
- XmlSchemaProviderAttribute.cs
- WebBrowserHelper.cs
- TextFormatterContext.cs
- GridLengthConverter.cs
- DecoratedNameAttribute.cs
- PrintDialog.cs
- PerspectiveCamera.cs
- TypeGeneratedEventArgs.cs
- SchemaNames.cs
- DeviceSpecificDialogCachedState.cs
- TextElementEnumerator.cs
- XmlUtilWriter.cs
- PageAsyncTaskManager.cs
- Constraint.cs
- EntityDataSourceChangingEventArgs.cs
- ValueTypeFixupInfo.cs
- QualifiedCellIdBoolean.cs
- Schedule.cs
- XmlAttribute.cs
- SortExpressionBuilder.cs
- NativeCppClassAttribute.cs
- DataViewManager.cs
- ProtectedProviderSettings.cs
- RequestQueryParser.cs
- HostExecutionContextManager.cs
- SmiEventStream.cs
- DesignerVerbCollection.cs
- Selector.cs
- DockingAttribute.cs
- BufferModeSettings.cs
- TrustLevelCollection.cs
- DefaultShape.cs
- ModulesEntry.cs
- MenuItemStyleCollection.cs
- CapabilitiesUse.cs
- Rotation3D.cs
- FullTextState.cs
- FormViewModeEventArgs.cs
- ToolStripRenderer.cs
- ListChangedEventArgs.cs
- Timeline.cs
- XmlWrappingReader.cs
- SchemaTypeEmitter.cs
- PrivacyNoticeElement.cs
- MetadataCollection.cs
- FileNotFoundException.cs
- IssuedTokenClientCredential.cs
- StringBuilder.cs
- OdbcConnectionFactory.cs
- Queue.cs
- ComboBox.cs
- OleDbException.cs
- EntityClassGenerator.cs
- BaseDataListComponentEditor.cs