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
- AggregateNode.cs
- Attachment.cs
- MessageBox.cs
- DataServiceException.cs
- CurrencyManager.cs
- DataSourceXmlElementAttribute.cs
- WpfKnownType.cs
- SoapConverter.cs
- Automation.cs
- IndicCharClassifier.cs
- CompilationRelaxations.cs
- recordstatefactory.cs
- ReceiveActivityValidator.cs
- SchemaMerger.cs
- ActivatableWorkflowsQueryResult.cs
- DataGridViewAutoSizeModeEventArgs.cs
- SettingsPropertyNotFoundException.cs
- DataSourceHelper.cs
- X509Certificate2.cs
- SectionVisual.cs
- TileBrush.cs
- SqlUnionizer.cs
- XsltContext.cs
- DeadCharTextComposition.cs
- GenericAuthenticationEventArgs.cs
- TypeElement.cs
- InternalsVisibleToAttribute.cs
- EpmSourceTree.cs
- LayoutTableCell.cs
- PageAsyncTaskManager.cs
- counter.cs
- ClassHandlersStore.cs
- AuthenticationSection.cs
- RegexGroupCollection.cs
- BufferedStream.cs
- MessageQueueCriteria.cs
- DayRenderEvent.cs
- _HeaderInfo.cs
- FontInfo.cs
- CustomValidator.cs
- ToolStripProgressBar.cs
- MemoryMappedFile.cs
- HttpContextServiceHost.cs
- Object.cs
- RadioButton.cs
- ZipIOExtraField.cs
- BinaryObjectWriter.cs
- WebServiceReceive.cs
- __TransparentProxy.cs
- CalendarDay.cs
- ReadWriteObjectLock.cs
- MemberHolder.cs
- InfoCardBaseException.cs
- StorageScalarPropertyMapping.cs
- Column.cs
- CipherData.cs
- MethodBuilder.cs
- ISFClipboardData.cs
- Byte.cs
- BamlLocalizabilityResolver.cs
- ContentElement.cs
- ViewKeyConstraint.cs
- GeneralTransform3DTo2D.cs
- ColorAnimationBase.cs
- MessageContractMemberAttribute.cs
- TextEditorContextMenu.cs
- FlowDocumentPage.cs
- MissingManifestResourceException.cs
- RegexTree.cs
- Converter.cs
- OleStrCAMarshaler.cs
- ImportFileRequest.cs
- WebPartDeleteVerb.cs
- IDQuery.cs
- TextDecorationCollectionConverter.cs
- EntityStoreSchemaGenerator.cs
- DesignerDataSourceView.cs
- SafeHGlobalHandleCritical.cs
- DefaultTraceListener.cs
- TextParaLineResult.cs
- StructuralCache.cs
- AnimationClock.cs
- DataTableClearEvent.cs
- SchemaMerger.cs
- ObjectViewEntityCollectionData.cs
- ListManagerBindingsCollection.cs
- CounterSampleCalculator.cs
- PropertyGeneratedEventArgs.cs
- CompilerLocalReference.cs
- SqlClientWrapperSmiStream.cs
- RegexMatch.cs
- ListBoxItemAutomationPeer.cs
- Identity.cs
- ResourceExpressionBuilder.cs
- TextPenaltyModule.cs
- NativeStructs.cs
- ConfigXmlCDataSection.cs
- SignedXml.cs
- GeometryModel3D.cs
- AnimationTimeline.cs