-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure no duplicate results are shown
- Loading branch information
Showing
7 changed files
with
106 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
|
||
namespace Pinpoint.Win.Models | ||
{ | ||
public class ObservableUniqueCollection<T> : ObservableCollection<T> | ||
{ | ||
private readonly HashSet<T> _hashSet; | ||
|
||
public ObservableUniqueCollection() : this(EqualityComparer<T>.Default) | ||
{ | ||
} | ||
|
||
public ObservableUniqueCollection(IEqualityComparer<T> equalityComparer) => _hashSet = new HashSet<T>(equalityComparer); | ||
|
||
public void AddRange(IEnumerable<T> items) | ||
{ | ||
foreach (var item in items) | ||
{ | ||
InsertItem(Count, item); | ||
} | ||
} | ||
|
||
public bool TryAdd(T item) | ||
{ | ||
var previous = Count; | ||
InsertItem(Count, item); | ||
return Count > previous; | ||
} | ||
|
||
protected override void InsertItem(int index, T item) | ||
{ | ||
if (_hashSet.Add(item)) | ||
{ | ||
base.InsertItem(index, item); | ||
} | ||
} | ||
|
||
protected override void ClearItems() | ||
{ | ||
base.ClearItems(); | ||
_hashSet.Clear(); | ||
} | ||
|
||
protected override void RemoveItem(int index) | ||
{ | ||
var item = this[index]; | ||
_hashSet.Remove(item); | ||
base.RemoveItem(index); | ||
} | ||
|
||
protected override void SetItem(int index, T item) | ||
{ | ||
if (_hashSet.Add(item)) | ||
{ | ||
var oldItem = this[index]; | ||
_hashSet.Remove(oldItem); | ||
base.SetItem(index, item); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters