[Difficult] [Need help] Fast way to avoid duplicate values for same key (NativeMultiHashMap)

I need a fast way to check if a value already exists for a key before adding to a NativeMultiHashmap.

Thank you for your help.

I think you have to iterate the values for the key. One option is use something like a NativeHashMap<int2,byte> to track entries. The int2 containing the hashcode for the key and value.

1 Like

Here’s a simple way (sorry that I was away for the end of your last thread):

  1. Create your target NativeMultiHashMap<KeyType, ValueType> (the one you want to fill with unique values)
  2. Create a companion NativeHashMap<ValueType, bool>
  3. When you add a new Value to your NativeMultiHashMap, also add it as a key to your NativeHashMap.
  4. Before you add your next value to the NativeMultiHashMap, check to see if your NativeHashMap already contains it as a key. If it does, then you’ve already added it.

This is essentially using the NativeHashMap as a HashSet (since Unity has not implemented a NativeHashSet). There’s a great community-made NativeHashSet implementation that can be found on these forums. But honestly, NativeHashMap<ValueType, bool> does the trick. It doesn’t matter what the bool values are - you’ll only ever be checking to see if that NativeHashMap already contains your values as keys (checking for membership).

I can’t predict the performance of this solution, but functionally, I have seen it work. Good luck!

1 Like

Thank you for your help! Seems like a clean approach!

How shall I check if the nativehashmap already contains the key/value? I’m using TryAdd with the (key/value) hashed. if the tryadd returns false then the key value is not added to the nativemultihashmap.
What do you think if this approach? Is this a good implementation of your idea?

Thank you so much for your help!

I don’t get what you are trying to do. if you don’t want to had multiple values for the same key, why not use the nativehashmap dirreclty ? the try/add does it for you…

It seems like you are trying to prevent the duplicate entry on both key and value.
If so, I would suggest still using the nativehashmap with a tuple as the key.

Something like : NativeHashMap<Tuple<KEY,VALUE>,VALUE>.

1 Like

:frowning: Sadly Tuple is a class instead of a strcut…

I can’t even use return anonymous tuplet in jobs. just because it is class…

1 Like

I have a dirty trick…
Make an assembly Reference like this.
5709457--597742--upload_2020-4-14_17-11-28.png

And Add a source file like InternalsVisibleTo.cs in the same folder
and finally, add this to the file

using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("NameOfYourAssembly")]

Then you suddenly have internal access to all collections.
go search for the value in.
5709457--597748--upload_2020-4-14_17-14-58.png

3 Likes

Ok, soory, use ValueTuple https://docs.microsoft.com/fr-fr/dotnet/api/system.valuetuple?view=netstandard-2.0

1 Like

This is a slick alternative to Reflection and Cecil when it comes to extending functionality of packages without having to maintain a copy of the package. I’m mildly annoyed I didn’t even think of this. Thanks!

2 Likes