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
- AspCompat.cs
- UnsafeNativeMethodsPenimc.cs
- ReflectionUtil.cs
- FunctionImportElement.cs
- ManualResetEvent.cs
- AuthStoreRoleProvider.cs
- ToolStripItemDesigner.cs
- ScrollChrome.cs
- OleDbError.cs
- SQLDecimalStorage.cs
- SerializerDescriptor.cs
- SimpleType.cs
- controlskin.cs
- StateBag.cs
- _CommandStream.cs
- BaseInfoTable.cs
- ErrorHandler.cs
- RunClient.cs
- precedingsibling.cs
- ExeConfigurationFileMap.cs
- PeerEndPoint.cs
- TextBreakpoint.cs
- DefaultPropertyAttribute.cs
- EntityClassGenerator.cs
- StringPropertyBuilder.cs
- Utils.cs
- XmlEventCache.cs
- WS2007HttpBindingCollectionElement.cs
- Figure.cs
- XmlStringTable.cs
- propertytag.cs
- ListenerBinder.cs
- LabelLiteral.cs
- AutomationPeer.cs
- SerializationInfoEnumerator.cs
- DataKeyArray.cs
- RangeBase.cs
- AudioFormatConverter.cs
- ApplicationException.cs
- NullableBoolConverter.cs
- DataGridViewRowPostPaintEventArgs.cs
- StrongNameKeyPair.cs
- CalendarDesigner.cs
- DodSequenceMerge.cs
- TransactionContextManager.cs
- TemplateInstanceAttribute.cs
- TemplatedWizardStep.cs
- MsmqBindingMonitor.cs
- TiffBitmapEncoder.cs
- NamespaceEmitter.cs
- CompareInfo.cs
- URLIdentityPermission.cs
- Debug.cs
- DispatcherHooks.cs
- LayoutEditorPart.cs
- CurrentChangingEventManager.cs
- LongValidatorAttribute.cs
- WorkflowInlining.cs
- PluralizationService.cs
- TextRunTypographyProperties.cs
- FileFormatException.cs
- CodeTypeMember.cs
- BaseEntityWrapper.cs
- EventTrigger.cs
- PagesChangedEventArgs.cs
- DragEventArgs.cs
- XmlSchemaSimpleContentExtension.cs
- AdornerLayer.cs
- MissingMemberException.cs
- SmtpReplyReaderFactory.cs
- VectorAnimationUsingKeyFrames.cs
- CollectionEditorDialog.cs
- XmlUnspecifiedAttribute.cs
- AsyncStreamReader.cs
- Transform3DCollection.cs
- ComponentRenameEvent.cs
- PageContentAsyncResult.cs
- CheckableControlBaseAdapter.cs
- WaitHandle.cs
- ImageListStreamer.cs
- XamlTypeMapper.cs
- NetworkCredential.cs
- Message.cs
- KeyInstance.cs
- QilReference.cs
- ExceptionNotification.cs
- FontCollection.cs
- SchemaCollectionCompiler.cs
- StylusButton.cs
- GridEntryCollection.cs
- HtmlControlPersistable.cs
- DLinqTableProvider.cs
- CompletedAsyncResult.cs
- MemberRelationshipService.cs
- LineGeometry.cs
- ThemeDirectoryCompiler.cs
- WebProxyScriptElement.cs
- DataStreams.cs
- SafeNativeMethods.cs
- handlecollector.cs