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
- MarshalByValueComponent.cs
- DataGridViewCellValueEventArgs.cs
- SettingsPropertyNotFoundException.cs
- TextAnchor.cs
- KeyValueInternalCollection.cs
- DataSourceXmlTextReader.cs
- SynchronizedDispatch.cs
- RootBrowserWindowProxy.cs
- CommonDialog.cs
- LinkConverter.cs
- RegistryExceptionHelper.cs
- MenuItemCollection.cs
- NavigationPropertySingletonExpression.cs
- ServiceModelConfigurationSectionGroup.cs
- ViewLoader.cs
- IteratorFilter.cs
- glyphs.cs
- ScriptServiceAttribute.cs
- NeutralResourcesLanguageAttribute.cs
- TableLayoutCellPaintEventArgs.cs
- CookielessHelper.cs
- EntityDataSourceContainerNameItem.cs
- AnnotationAuthorChangedEventArgs.cs
- FontStyle.cs
- UndoEngine.cs
- MessageSmuggler.cs
- InternalCache.cs
- OperationContractGenerationContext.cs
- Double.cs
- InternalRelationshipCollection.cs
- QueryInterceptorAttribute.cs
- BodyGlyph.cs
- RegistryKey.cs
- IListConverters.cs
- MemoryMappedFile.cs
- ReadContentAsBinaryHelper.cs
- DataGridAutomationPeer.cs
- RewritingSimplifier.cs
- GridView.cs
- CDSsyncETWBCLProvider.cs
- DomainLiteralReader.cs
- NamespaceTable.cs
- RightsManagementInformation.cs
- CodePageEncoding.cs
- TableParaClient.cs
- FormViewUpdatedEventArgs.cs
- ExceptionHandlers.cs
- RepeatButton.cs
- ProviderSettingsCollection.cs
- ADRoleFactoryConfiguration.cs
- TextServicesDisplayAttribute.cs
- TreeView.cs
- ToolStripControlHost.cs
- ObjectStateManagerMetadata.cs
- HtmlTableCellCollection.cs
- ThumbAutomationPeer.cs
- TemplateApplicationHelper.cs
- MULTI_QI.cs
- Timer.cs
- TagNameToTypeMapper.cs
- TextRenderer.cs
- TypedAsyncResult.cs
- ProgressBar.cs
- VScrollBar.cs
- OperationFormatter.cs
- AudioStateChangedEventArgs.cs
- Random.cs
- ContextMarshalException.cs
- CodeTypeReference.cs
- FormViewCommandEventArgs.cs
- DateTimeOffsetConverter.cs
- Blend.cs
- PenLineCapValidation.cs
- UnauthorizedWebPart.cs
- ISAPIRuntime.cs
- NextPreviousPagerField.cs
- PropertyEntry.cs
- BitmapEffect.cs
- DynamicResourceExtension.cs
- RayMeshGeometry3DHitTestResult.cs
- XmlToDatasetMap.cs
- AuthenticateEventArgs.cs
- HostProtectionException.cs
- BuildResultCache.cs
- BrowserCapabilitiesCodeGenerator.cs
- Style.cs
- AsyncResult.cs
- IriParsingElement.cs
- ImplicitInputBrush.cs
- DeflateEmulationStream.cs
- SendActivityDesigner.cs
- Pool.cs
- _ProxyRegBlob.cs
- DiagnosticSection.cs
- FastPropertyAccessor.cs
- ScriptReferenceBase.cs
- InternalMappingException.cs
- MailAddressParser.cs
- IntSecurity.cs
- MobileControlPersister.cs