Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / System.Runtime.DurableInstancing / System / Runtime / DuplicateDetector.cs / 1305376 / DuplicateDetector.cs
//------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------- namespace System.Runtime { using System.Collections.Generic; class DuplicateDetectorwhere T : class { LinkedList fifoList; Dictionary > items; int capacity; object thisLock; public DuplicateDetector(int capacity) { Fx.Assert(capacity >= 0, "The capacity parameter must be a positive value."); this.capacity = capacity; this.items = new Dictionary >(); this.fifoList = new LinkedList (); this.thisLock = new object(); } public bool AddIfNotDuplicate(T value) { Fx.Assert(value != null, "The value must be non null."); bool success = false; lock (this.thisLock) { if (!this.items.ContainsKey(value)) { Add(value); success = true; } } return success; } void Add(T value) { Fx.Assert(this.items.Count == this.fifoList.Count, "The items and fifoList must be synchronized."); if (this.items.Count == this.capacity) { LinkedListNode node = this.fifoList.Last; this.items.Remove(node.Value); this.fifoList.Remove(node); } this.items.Add(value, this.fifoList.AddFirst(value)); } public bool Remove(T value) { Fx.Assert(value != null, "The value must be non null."); bool success = false; LinkedListNode node; lock (this.thisLock) { if (this.items.TryGetValue(value, out node)) { this.items.Remove(value); this.fifoList.Remove(node); success = true; } } return success; } public void Clear() { lock (this.thisLock) { this.fifoList.Clear(); this.items.Clear(); } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------- namespace System.Runtime { using System.Collections.Generic; class DuplicateDetector where T : class { LinkedList fifoList; Dictionary > items; int capacity; object thisLock; public DuplicateDetector(int capacity) { Fx.Assert(capacity >= 0, "The capacity parameter must be a positive value."); this.capacity = capacity; this.items = new Dictionary >(); this.fifoList = new LinkedList (); this.thisLock = new object(); } public bool AddIfNotDuplicate(T value) { Fx.Assert(value != null, "The value must be non null."); bool success = false; lock (this.thisLock) { if (!this.items.ContainsKey(value)) { Add(value); success = true; } } return success; } void Add(T value) { Fx.Assert(this.items.Count == this.fifoList.Count, "The items and fifoList must be synchronized."); if (this.items.Count == this.capacity) { LinkedListNode node = this.fifoList.Last; this.items.Remove(node.Value); this.fifoList.Remove(node); } this.items.Add(value, this.fifoList.AddFirst(value)); } public bool Remove(T value) { Fx.Assert(value != null, "The value must be non null."); bool success = false; LinkedListNode node; lock (this.thisLock) { if (this.items.TryGetValue(value, out node)) { this.items.Remove(value); this.fifoList.Remove(node); success = true; } } return success; } public void Clear() { lock (this.thisLock) { this.fifoList.Clear(); this.items.Clear(); } } } } // 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
- ActiveXHost.cs
- InfoCardBaseException.cs
- SynchronizationLockException.cs
- ImageCodecInfo.cs
- SqlTrackingWorkflowInstance.cs
- AsymmetricCryptoHandle.cs
- ReceiveCompletedEventArgs.cs
- CheckBoxStandardAdapter.cs
- CompareInfo.cs
- DefaultMemberAttribute.cs
- ControlBindingsConverter.cs
- Page.cs
- SchemaExporter.cs
- WebSysDescriptionAttribute.cs
- SQLMembershipProvider.cs
- WebPartEditorApplyVerb.cs
- RightsManagementPermission.cs
- MaskedTextBox.cs
- TypeConverterHelper.cs
- DiscreteKeyFrames.cs
- FunctionCommandText.cs
- EdmProperty.cs
- _NTAuthentication.cs
- Empty.cs
- StylusButton.cs
- TextElementCollection.cs
- SrgsRule.cs
- WorkerRequest.cs
- ApplyTemplatesAction.cs
- AsyncSerializedWorker.cs
- AppDomain.cs
- DataServiceProviderWrapper.cs
- ImageSourceValueSerializer.cs
- XPathQilFactory.cs
- DataControlFieldCollection.cs
- VisualStyleInformation.cs
- EntityStoreSchemaGenerator.cs
- DiscoveryClientDocuments.cs
- SmiEventSink.cs
- XmlDesigner.cs
- SeekableReadStream.cs
- DiscreteKeyFrames.cs
- BaseCodePageEncoding.cs
- ConnectionManagementSection.cs
- prefixendpointaddressmessagefiltertable.cs
- Attributes.cs
- SqlError.cs
- DataGridViewRowStateChangedEventArgs.cs
- TypedElement.cs
- DeclarativeCatalogPart.cs
- WhitespaceRuleReader.cs
- HMACSHA256.cs
- TextServicesLoader.cs
- ConfigurationElement.cs
- AuthenticationConfig.cs
- InstrumentationTracker.cs
- LongValidator.cs
- Thread.cs
- EpmTargetPathSegment.cs
- ToolStripMenuItem.cs
- WindowsPen.cs
- CharacterMetricsDictionary.cs
- RC2CryptoServiceProvider.cs
- ShellProvider.cs
- Environment.cs
- WindowsTreeView.cs
- ExpressionBindingCollection.cs
- SendKeys.cs
- DataGridAddNewRow.cs
- ConditionalAttribute.cs
- CheckBoxAutomationPeer.cs
- OdbcDataAdapter.cs
- LightweightCodeGenerator.cs
- ComponentChangedEvent.cs
- DiagnosticStrings.cs
- GridEntryCollection.cs
- WSFederationHttpBinding.cs
- Baml2006ReaderContext.cs
- DataControlLinkButton.cs
- MarginCollapsingState.cs
- ByteStreamMessageEncodingElement.cs
- TcpWorkerProcess.cs
- WindowsScrollBarBits.cs
- SafeCancelMibChangeNotify.cs
- CommonServiceBehaviorElement.cs
- SqlDataSourceCache.cs
- DataGridCaption.cs
- OdbcEnvironmentHandle.cs
- FixUp.cs
- AnnotationObservableCollection.cs
- XmlDataSourceNodeDescriptor.cs
- ProcessThreadCollection.cs
- TransactionChannelFaultConverter.cs
- DelimitedListTraceListener.cs
- TokenizerHelper.cs
- Win32Native.cs
- Parameter.cs
- PolicyManager.cs
- KeyGestureValueSerializer.cs
- TreeNodeStyle.cs