Code:
/ 4.0 / 4.0 / untmp / 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 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
- SynchronizationContext.cs
- SubstitutionDesigner.cs
- ValidatorCollection.cs
- httpstaticobjectscollection.cs
- ModifierKeysConverter.cs
- ContextDataSourceContextData.cs
- RadioButtonFlatAdapter.cs
- XmlTypeMapping.cs
- DesignerCatalogPartChrome.cs
- Control.cs
- AnimationTimeline.cs
- MessageBox.cs
- StorageComplexTypeMapping.cs
- Int32KeyFrameCollection.cs
- GeneratedContractType.cs
- DataTableClearEvent.cs
- EntityDataSourceState.cs
- RuntimeArgumentHandle.cs
- GridSplitter.cs
- NetStream.cs
- CheckBoxFlatAdapter.cs
- WindowsRichEditRange.cs
- DrawingState.cs
- Inflater.cs
- TextCollapsingProperties.cs
- SmiContext.cs
- ToolBarButtonClickEvent.cs
- WeakRefEnumerator.cs
- GridViewRowPresenterBase.cs
- Converter.cs
- BufferBuilder.cs
- MemberExpressionHelper.cs
- FormsIdentity.cs
- Ipv6Element.cs
- WinFormsComponentEditor.cs
- QueryOutputWriterV1.cs
- LoggedException.cs
- XmlQueryCardinality.cs
- HandleInitializationContext.cs
- CodeStatement.cs
- ListSourceHelper.cs
- SqlDataAdapter.cs
- EUCJPEncoding.cs
- PersonalizationEntry.cs
- EnumValidator.cs
- SoapServerMessage.cs
- HtmlLink.cs
- SqlNode.cs
- Double.cs
- BufferedStream.cs
- CryptoHandle.cs
- DataContext.cs
- Permission.cs
- XmlSerializerNamespaces.cs
- PrintingPermissionAttribute.cs
- ObjectConverter.cs
- EntityDataSourceView.cs
- MenuStrip.cs
- JoinSymbol.cs
- MiniParameterInfo.cs
- ContentType.cs
- WebBrowsableAttribute.cs
- IndexerNameAttribute.cs
- JournalNavigationScope.cs
- HostExecutionContextManager.cs
- _ChunkParse.cs
- DependencyPropertyValueSerializer.cs
- Empty.cs
- WindowPattern.cs
- BindingBase.cs
- RoleExceptions.cs
- PointAnimationUsingPath.cs
- DesignObjectWrapper.cs
- ListBox.cs
- CommandField.cs
- CodeBlockBuilder.cs
- SQLRoleProvider.cs
- InputProcessorProfilesLoader.cs
- ByteStreamMessageUtility.cs
- ScaleTransform.cs
- GridViewCancelEditEventArgs.cs
- WebServiceEnumData.cs
- SqlDataSourceConnectionPanel.cs
- ToolStripContentPanel.cs
- MediaEntryAttribute.cs
- RightsManagementEncryptionTransform.cs
- xml.cs
- RequestNavigateEventArgs.cs
- ScriptReference.cs
- XDRSchema.cs
- AllMembershipCondition.cs
- TemplateXamlTreeBuilder.cs
- MatrixAnimationBase.cs
- DLinqTableProvider.cs
- ConfigXmlCDataSection.cs
- CommandDevice.cs
- PerformanceCounterCategory.cs
- X509ServiceCertificateAuthentication.cs
- ConfigXmlComment.cs
- CodeConditionStatement.cs