Skip to content

ArgoreOfficial/unordered_array

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

unordered_array

Why?

I needed a container that met these specific requirements

  • Must have a continuous block of memory
  • Keys/indices must only be invalidated when the user calls erase() or clear()
  • non-size_t keys, eg. uint16_t

It's essentially a hybrid of std::vector and std::unordered_map

Usage

Note: Keys must be integer values, or castable to size_t

typedef uint16_t ID;
struct SomeObject 
{
    SomeObject( const char* _name, int _coolness ) 
        : name{ _name }, coolness{ _coolness } 
    { }

    const char* name;
    int coolness;
};


arg::unordered_array<ID, SomeObject> objects;
ID id1 = objects.emplace( "object1", 42 );
ID id2 = objects.emplace( "object2", 11 );

objects.erase( id1 );
id1 = 0; // make sure to invalidate key

objects.at( id2 ).name = "coolerObject2";
objects[ id2 ].coolness = 9000;
objects.size();  // element size of buffer (size in bytes/size of element)
objects.count(); // number of currently allocated elements in the array

Note on multithreading

Just like other continous-memory-containers, the memory buffer may be reallocated whenever a new object is emplaced. This will invalidate any pointers or references to said object
aka; use mutexes as you would with a vector
Keys will always remain valid

About

Continuous unordered array, guaranteeing key validity

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published