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
- SchemaTableColumn.cs
- ToolStripPanelCell.cs
- DataBoundControlAdapter.cs
- DataGridViewLayoutData.cs
- ImageButton.cs
- PlatformCulture.cs
- Error.cs
- MachineSettingsSection.cs
- FolderLevelBuildProviderAppliesToAttribute.cs
- BitmapSourceSafeMILHandle.cs
- unsafenativemethodsother.cs
- Compensation.cs
- CategoryGridEntry.cs
- DescendantBaseQuery.cs
- CallInfo.cs
- ReflectionHelper.cs
- XmlHierarchicalEnumerable.cs
- AttachedPropertiesService.cs
- CharUnicodeInfo.cs
- LassoSelectionBehavior.cs
- StringHandle.cs
- LiteralDesigner.cs
- ResXFileRef.cs
- BufferModeSettings.cs
- UseAttributeSetsAction.cs
- DCSafeHandle.cs
- CheckBoxRenderer.cs
- RoleService.cs
- BindingUtils.cs
- NetNamedPipeBindingCollectionElement.cs
- StoragePropertyMapping.cs
- RadioButton.cs
- SettingsPropertyWrongTypeException.cs
- TypeListConverter.cs
- FlowPosition.cs
- DataGridViewRowContextMenuStripNeededEventArgs.cs
- WindowsIPAddress.cs
- ResXResourceSet.cs
- PropertyItemInternal.cs
- XmlSchemaComplexContentRestriction.cs
- ClientBuildManagerCallback.cs
- ComboBox.cs
- PhysicalFontFamily.cs
- UseAttributeSetsAction.cs
- DbConnectionClosed.cs
- MarkerProperties.cs
- EditorAttribute.cs
- PageParser.cs
- CryptoKeySecurity.cs
- XmlComment.cs
- NativeActivityMetadata.cs
- PinnedBufferMemoryStream.cs
- SerializationBinder.cs
- SudsCommon.cs
- KeySplineConverter.cs
- Win32KeyboardDevice.cs
- DesignerAttribute.cs
- FixedSchema.cs
- keycontainerpermission.cs
- CompilerGlobalScopeAttribute.cs
- ParameterBinding.cs
- HostProtectionPermission.cs
- ElementNotAvailableException.cs
- CommonProperties.cs
- ServiceModelSecurityTokenTypes.cs
- NameHandler.cs
- SystemWebSectionGroup.cs
- TextViewBase.cs
- XmlBufferedByteStreamReader.cs
- SectionUpdates.cs
- Types.cs
- TransportSecurityProtocolFactory.cs
- RelationalExpressions.cs
- HttpValueCollection.cs
- DataGridViewCellMouseEventArgs.cs
- SqlBinder.cs
- ChangeTracker.cs
- DbTransaction.cs
- CodeDOMUtility.cs
- ColumnCollection.cs
- WhitespaceRuleReader.cs
- WebServiceParameterData.cs
- SwitchElementsCollection.cs
- SocketPermission.cs
- PageCodeDomTreeGenerator.cs
- RecipientInfo.cs
- GuidelineCollection.cs
- FontStyleConverter.cs
- SetStateDesigner.cs
- SimpleRecyclingCache.cs
- GridViewUpdateEventArgs.cs
- ExtensionDataReader.cs
- SizeKeyFrameCollection.cs
- CollectionDataContract.cs
- BindingExpression.cs
- TextSpanModifier.cs
- XmlNamespaceMapping.cs
- ServicePoint.cs
- AuthenticationModuleElement.cs
- SplitterEvent.cs