Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Feedback Native Hashmap add ItemAt(int index) pls

Discussion in 'Entity Component System' started by Stroustrup, Oct 6, 2020.

  1. Stroustrup

    Stroustrup

    Joined:
    May 18, 2020
    Posts:
    142
    I know there's HashMap.GetKeyValueArrays(allocator); , but that's a pretty involved process for when i just want to get the item at a known index.

    or just any random item in the hashmap
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,626
    what is an index in a hashmap? (or do you mean key)
     
  3. Lieene-Guo

    Lieene-Guo

    Joined:
    Aug 20, 2013
    Posts:
    547
    that when you use indexer. hashMap[key]
     
  4. Stroustrup

    Stroustrup

    Joined:
    May 18, 2020
    Posts:
    142
    no, like hashmap.KeyValuePairAt(0)
    i just need to get the n'th item, or the 0th item. i have to do this because of that

    Code (CSharp):
    1. static int GetAnyValidSimplex() {
    2.     NativeArray<int> keys  = _indexToSimplex.GetKeyArray(Allocator.Temp);
    3.     int              index = keys.Length % 2 == 0 ? keys.Length / 2 : (keys.Length - 1) / 2;
    4.     int              value = keys[index];
    5.     keys.Dispose();
    6.     return value;
    7. }
     
  5. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    529
    This is always expensive as the HashMap stores a linked list of sparse buckets. Also the indices are not stable. Even adding an element can change all of them.

    Do you actually need a HashMap or do you simply want to store tuples? Do you have a lot of lookups by key? How big is the map?
     
  6. Stroustrup

    Stroustrup

    Joined:
    May 18, 2020
    Posts:
    142
    wait, so you're telling me it doesn't actually use a hash and just compares the keys until you get the right key?
    The map is absolutely massive. it contains all of the tetrahedrons formed out of light probes. but i need to use some sort of map so that each tetrahedron can reference a neighbour tetrahedron so that you can traverse them and quickly find the object that needs global illumination
     

    Attached Files:

  7. Lieene-Guo

    Lieene-Guo

    Joined:
    Aug 20, 2013
    Posts:
    547
    Okay, that's internal. there could be empty slots in internal key/value array.
    random access could give you unexpected value. so you need to stick to GetKeyValueArrays.
    internally it's more like a LinkList, for fast add/remove. so when some key-value pair is remove there will be a gap. so you can not random access.
     
  8. Lieene-Guo

    Lieene-Guo

    Joined:
    Aug 20, 2013
    Posts:
    547
    It's a standard pattern for hashmap.
    If you look into System.Dictionary. It uses the same approach. Dictionary just don't tell you that it's allocating because there's GC.
     
  9. Lieene-Guo

    Lieene-Guo

    Joined:
    Aug 20, 2013
    Posts:
    547
    will use index reference or pointer if I were you. Double pointer if the content is re-allocatable.
    with linear storage NativeArray/NativeList.
    That's how unity.physic managed convex collider Face/Edge/Vertex and Voronoi Region.
     
    Stroustrup likes this.