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
- CommandHelpers.cs
- CornerRadius.cs
- ReadOnlyHierarchicalDataSourceView.cs
- XPathNodeList.cs
- storepermissionattribute.cs
- PerfCounters.cs
- TriState.cs
- LocationSectionRecord.cs
- QueryGeneratorBase.cs
- CacheEntry.cs
- DataRelationCollection.cs
- CommonProperties.cs
- GeneratedCodeAttribute.cs
- DataObjectFieldAttribute.cs
- ListViewTableCell.cs
- StateMachineWorkflowInstance.cs
- TreeNodeStyleCollection.cs
- Event.cs
- GridViewCommandEventArgs.cs
- ParameterInfo.cs
- Compiler.cs
- WindowsSysHeader.cs
- WebPartZoneCollection.cs
- XmlSchemaRedefine.cs
- MutexSecurity.cs
- DataGridViewRowConverter.cs
- InstanceLockedException.cs
- AxHost.cs
- GlyphInfoList.cs
- RequestSecurityTokenResponse.cs
- LedgerEntry.cs
- RepeaterItemEventArgs.cs
- TreeNodeCollection.cs
- ClientSession.cs
- WindowsPen.cs
- RequiredAttributeAttribute.cs
- XmlProcessingInstruction.cs
- DeclarativeCatalogPart.cs
- COMException.cs
- MediaTimeline.cs
- DrawingContext.cs
- CalendarDayButton.cs
- CompilationRelaxations.cs
- IconHelper.cs
- RoutedEventValueSerializer.cs
- SingleAnimationBase.cs
- UserControl.cs
- BatchWriter.cs
- QueryExpr.cs
- FacetEnabledSchemaElement.cs
- hwndwrapper.cs
- XPathAncestorQuery.cs
- EncoderFallback.cs
- ScopelessEnumAttribute.cs
- PrintDocument.cs
- ImmComposition.cs
- TrackingServices.cs
- StringFunctions.cs
- GetFileNameResult.cs
- ScaleTransform.cs
- Attachment.cs
- FileDialogCustomPlace.cs
- XmlArrayItemAttribute.cs
- LinqToSqlWrapper.cs
- OutputScopeManager.cs
- SpeechSynthesizer.cs
- ResetableIterator.cs
- ElementAction.cs
- MarkedHighlightComponent.cs
- MSAANativeProvider.cs
- SapiRecognizer.cs
- MessageQueueInstaller.cs
- QEncodedStream.cs
- WebPartDisplayModeCollection.cs
- CqlErrorHelper.cs
- InheritanceUI.cs
- SQLUtility.cs
- SqlParameter.cs
- Geometry.cs
- BamlBinaryWriter.cs
- FormViewDeletedEventArgs.cs
- TextWriter.cs
- TypeTypeConverter.cs
- DispatcherTimer.cs
- StrokeCollectionConverter.cs
- OpCodes.cs
- BitStack.cs
- FreeFormDesigner.cs
- ObjectReaderCompiler.cs
- OrthographicCamera.cs
- ToolStripDesignerAvailabilityAttribute.cs
- Cursors.cs
- ObjectTag.cs
- ObjectDataSourceView.cs
- MetadataItemEmitter.cs
- CookielessHelper.cs
- VirtualPathUtility.cs
- CriticalExceptions.cs
- OdbcConnectionString.cs
- ApplicationSecurityInfo.cs