theraot / Theraot

Backporting .NET and more: LINQ expressions in .net 2.0 - nuget Theraot.Core available.
MIT License
162 stars 30 forks source link

.NET 4.7.2 Targeting #115

Closed NN--- closed 4 years ago

NN--- commented 4 years ago

Missing .NET 4.7.2 targeting, #114

Additional collection APIs

.NET Framework 4.7.2 adds a number of new APIs to the SortedSet<T> and HashSet<T> types. These include:

    TryGetValue methods, which extend the try pattern used in other collection types to these two types. The methods are:
        public bool HashSet<T>.TryGetValue(T equalValue, out T actualValue)
        public bool SortedSet<T>.TryGetValue(T equalValue, out T actualValue)

    Enumerable.To* extension methods, which convert a collection to a HashSet<T>:
        public static HashSet<TSource> ToHashSet<TSource>(this IEnumerable<TSource> source)
        public static HashSet<TSource> ToHashSet<TSource>(this IEnumerable<TSource> source, IEqualityComparer<TSource> comparer)

    New HashSet<T> constructors that let you set the collection's capacity, which yields a performance benefit when you know the size of the HashSet<T> in advance:
        public HashSet(int capacity)
        public HashSet(int capacity, IEqualityComparer<T> comparer)

The ConcurrentDictionary<TKey,TValue> class includes new overloads of the AddOrUpdate and GetOrAdd methods to retrieve a value from the dictionary or to add it if it is not found, and to add a value to the dictionary or to update it if it already exists.

https://docs.microsoft.com/en-us/dotnet/framework/whats-new/#core-472 https://docs.microsoft.com/en-us/dotnet/standard/frameworks

theraot commented 4 years ago

Ideas for HashSet.TryGetValue as extension method? It appears that it requires to iterate over the whole HashSet.

Remember that TryGetValue is meant to return the value that was inserted.

I'm thinking that it can be implemented by creating a copy of the HashSet and then using InsersectWith.

NN--- commented 4 years ago

What do you mean ? Isn't TryGetValue simply call to Contains and return the value if exists ?

TryGetValue()
{
  if (hashSet.Contains(equalValue)) { actualValue = equalValue; return true; }
  actualValue = default(T);
  return false;
}
theraot commented 4 years ago

Nope, it returns the value that was inserted, not the value that was used to check. Yes, those values are equal (according to the comparer), however they might still not be the same.

NN--- commented 4 years ago

I see. Technically you can get the internals using reflection since the implementation won't change :) Iterating over HashSet is better than copying, since copying requires iterating anyway, no ?

theraot commented 4 years ago

I think copying is better. The copy constructor of the hashset has a path dedicated to copying a hashset that would be better than copying to array or using GetEnumerator. Similarly IntersectWith has a fast path for HashSet intersecting another HashSet.

I suppose I should test and see what is actually faster.

NN--- commented 4 years ago

Let's do anything simple and then optimize.

theraot commented 4 years ago

Nuget Version 3.1.1