Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get original value from HashSet

Tags:

c#

hashset

UPDATE: Starting with .Net 4.7.2, HashSet.TryGetValue - docs is available.
HashSet.TryGetValue - SO post


I have a problem with HashSet because it does not provide any method similar to TryGetValue known from Dictionary. And I need such method -- passing element to find in the set, and set returning element from its collection (when found).

Sidenote -- "why do you need element from the set, you already have that element?". No, I don't, equality and identity are two different things.

HashSet is not sealed but all its fields are private, so deriving from it is pointless. I cannot use Dictionary instead because I need SetEquals method. I was thinking about grabbing a source for HashSet and adding desired method, but the license is not truly open source (I can look, but I cannot distribute/modify). I could use reflection but the arrays in HashSet are not readonly meaning I cannot bind to those fields once per instance lifetime.

And I don't want to use full blown library for just single class.

So far I am stuck with LINQ SingleOrDefault. So the question is how fix this -- have HashSet with TryGetValue?

like image 659
greenoldman Avatar asked Dec 20 '22 01:12

greenoldman


1 Answers

Probably you should switch from a HashSet to a SortedSet

There is a simple TryGetValue() for a SortedSet:

public bool TryGetValue(ref T element)
{
    var foundSet = sortedSet.GetViewBetween(element, element);
    if(foundSet.Count == 1)
    {
        element = foundSet.First();
        return true;
    }
    return false;       
}

when called, the element needs just all properties set which are used in the Comparer. It returns the element found in the Set.

like image 160
DrKoch Avatar answered Jan 27 '23 08:01

DrKoch