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
- HotSpotCollection.cs
- DesignerTextWriter.cs
- ToolbarAUtomationPeer.cs
- sqlmetadatafactory.cs
- CommandDevice.cs
- SecurityContextCookieSerializer.cs
- CheckoutException.cs
- _SpnDictionary.cs
- XPathNavigator.cs
- ModelTreeEnumerator.cs
- Form.cs
- WorkflowServiceAttributes.cs
- ErrorRuntimeConfig.cs
- ChannelDemuxer.cs
- SqlCaseSimplifier.cs
- ToolStripRenderEventArgs.cs
- PlainXmlDeserializer.cs
- NameHandler.cs
- ValidateNames.cs
- MenuStrip.cs
- XmlHelper.cs
- ComboBox.cs
- SyndicationItem.cs
- Int64Animation.cs
- ClientCultureInfo.cs
- ImageList.cs
- SignedPkcs7.cs
- RouteCollection.cs
- IdentityValidationException.cs
- SqlCrossApplyToCrossJoin.cs
- XmlAttributeAttribute.cs
- EpmSyndicationContentSerializer.cs
- MobileCapabilities.cs
- HttpApplication.cs
- DataGridViewButtonCell.cs
- VisualCollection.cs
- DesignerOptionService.cs
- RepeaterDataBoundAdapter.cs
- SystemIcons.cs
- ZipIOExtraFieldPaddingElement.cs
- XsltArgumentList.cs
- MetadataItemCollectionFactory.cs
- ConfigXmlWhitespace.cs
- AuthenticationModuleElementCollection.cs
- ListViewItem.cs
- InputScopeAttribute.cs
- PeerCollaboration.cs
- EventQueueState.cs
- TableLayoutStyleCollection.cs
- RemotingConfiguration.cs
- ThreadStateException.cs
- DataListItemEventArgs.cs
- SQLResource.cs
- LinqMaximalSubtreeNominator.cs
- SafeBitVector32.cs
- TextBox.cs
- ChtmlFormAdapter.cs
- SecurityHeaderLayout.cs
- HtmlForm.cs
- StopStoryboard.cs
- WebRequestModuleElementCollection.cs
- LogicalTreeHelper.cs
- CompModSwitches.cs
- TypeConstant.cs
- Span.cs
- LogLogRecord.cs
- EnumerableRowCollection.cs
- ImportCatalogPart.cs
- DbProviderFactory.cs
- FilterException.cs
- SubstitutionList.cs
- SystemIPGlobalStatistics.cs
- FuncCompletionCallbackWrapper.cs
- FloaterBaseParaClient.cs
- AdapterDictionary.cs
- DataGridViewButtonCell.cs
- StylusEventArgs.cs
- TypedRowHandler.cs
- Processor.cs
- ExpandCollapseProviderWrapper.cs
- TraceData.cs
- SecurityChannelFaultConverter.cs
- ValueProviderWrapper.cs
- Rfc2898DeriveBytes.cs
- QilInvokeEarlyBound.cs
- PropertyKey.cs
- OperatorExpressions.cs
- Globals.cs
- ViewPort3D.cs
- DLinqDataModelProvider.cs
- MenuCommand.cs
- Exception.cs
- GeneratedContractType.cs
- Baml6Assembly.cs
- WindowsStatusBar.cs
- ButtonRenderer.cs
- ViewCellSlot.cs
- XmlIterators.cs
- TrackBarDesigner.cs
- Rect3DConverter.cs