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
- ContentType.cs
- DataSetMappper.cs
- ExistsInCollection.cs
- ColumnBinding.cs
- PropertyGridView.cs
- UnsafeNativeMethods.cs
- ThreadNeutralSemaphore.cs
- HiddenField.cs
- TransactionInterop.cs
- contentDescriptor.cs
- SolidColorBrush.cs
- ChangeProcessor.cs
- CallbackValidatorAttribute.cs
- MenuRendererClassic.cs
- ApplicationDirectory.cs
- TableLayoutRowStyleCollection.cs
- followingsibling.cs
- RootProfilePropertySettingsCollection.cs
- TaskScheduler.cs
- ExecutionEngineException.cs
- BinaryReader.cs
- ProxyHelper.cs
- TimeSpanMinutesOrInfiniteConverter.cs
- TreeNodeBindingCollection.cs
- OracleCommandSet.cs
- ChtmlTextWriter.cs
- StreamUpgradeInitiator.cs
- XmlWriterTraceListener.cs
- InvalidWMPVersionException.cs
- GacUtil.cs
- TableLayoutPanel.cs
- IChannel.cs
- TrustSection.cs
- ObjectPersistData.cs
- UserValidatedEventArgs.cs
- ArcSegment.cs
- CngUIPolicy.cs
- DataContractSerializer.cs
- CssStyleCollection.cs
- SecurityProtocolFactory.cs
- TemplateNameScope.cs
- WindowHideOrCloseTracker.cs
- MediaContext.cs
- EventDescriptor.cs
- TraceFilter.cs
- IsolatedStorageException.cs
- ReadOnlyCollectionBase.cs
- DeviceSpecificDialogCachedState.cs
- embossbitmapeffect.cs
- TabletDeviceInfo.cs
- MsmqIntegrationElement.cs
- FrameworkReadOnlyPropertyMetadata.cs
- PeerMessageDispatcher.cs
- RSAPKCS1KeyExchangeFormatter.cs
- DiscoveryExceptionDictionary.cs
- ActivityAction.cs
- IgnoreFlushAndCloseStream.cs
- ProfileElement.cs
- PersonalizationDictionary.cs
- QilUnary.cs
- PtsHelper.cs
- SqlMethodTransformer.cs
- Line.cs
- RangeValidator.cs
- InsufficientExecutionStackException.cs
- CompilationLock.cs
- PropertyMappingExceptionEventArgs.cs
- nulltextnavigator.cs
- TracingConnectionInitiator.cs
- AttachInfo.cs
- DbConnectionPoolGroup.cs
- Propagator.JoinPropagator.JoinPredicateVisitor.cs
- TextViewSelectionProcessor.cs
- LoggedException.cs
- WindowsHyperlink.cs
- StorageAssociationSetMapping.cs
- Add.cs
- GeneralTransform3DTo2D.cs
- documentsequencetextview.cs
- UInt64.cs
- DataGridViewCellCancelEventArgs.cs
- StatusBarPanel.cs
- ClusterSafeNativeMethods.cs
- UnsafeNativeMethods.cs
- RequestResizeEvent.cs
- DictionaryEntry.cs
- PropertyPath.cs
- TableLayoutRowStyleCollection.cs
- BinHexDecoder.cs
- EventLog.cs
- MruCache.cs
- SqlPersistenceWorkflowInstanceDescription.cs
- TextBox.cs
- ArrangedElementCollection.cs
- BindStream.cs
- NativeMethodsCLR.cs
- VariableAction.cs
- MessageBodyDescription.cs
- NavigatorInvalidBodyAccessException.cs
- AutomationEvent.cs