| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include <Jolt/Core/HashTable.h> |
| 8 |
|
| 9 |
JPH_NAMESPACE_BEGIN |
| 10 |
|
| 11 |
|
| 12 |
template <class Key, class Value> |
| 13 |
class UnorderedMapDetail |
| 14 |
{ |
| 15 |
public: |
| 16 |
|
| 17 |
static const Key & sGetKey(const std::pair<Key, Value> &inKeyValue) |
| 18 |
{ |
| 19 |
return inKeyValue.first; |
| 20 |
} |
| 21 |
}; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
template <class Key, class Value, class Hash = JPH::Hash<Key>, class KeyEqual = std::equal_to<Key>> |
| 29 |
class UnorderedMap : public HashTable<Key, std::pair<Key, Value>, UnorderedMapDetail<Key, Value>, Hash, KeyEqual> |
| 30 |
{ |
| 31 |
using Base = HashTable<Key, std::pair<Key, Value>, UnorderedMapDetail<Key, Value>, Hash, KeyEqual>; |
| 32 |
|
| 33 |
public: |
| 34 |
using size_type = typename Base::size_type; |
| 35 |
using iterator = typename Base::iterator; |
| 36 |
using const_iterator = typename Base::const_iterator; |
| 37 |
using value_type = typename Base::value_type; |
| 38 |
|
| 39 |
Value & operator [] (const Key &inKey) |
| 40 |
{ |
| 41 |
size_type index; |
| 42 |
bool inserted = this->InsertKey(inKey, index); |
| 43 |
value_type &key_value = this->GetElement(index); |
| 44 |
if (inserted) |
| 45 |
new (&key_value) value_type(inKey, Value()); |
| 46 |
return key_value.second; |
| 47 |
} |
| 48 |
|
| 49 |
template<class... Args> |
| 50 |
std::pair<iterator, bool> try_emplace(const Key &inKey, Args &&...inArgs) |
| 51 |
{ |
| 52 |
size_type index; |
| 53 |
bool inserted = this->InsertKey(inKey, index); |
| 54 |
if (inserted) |
| 55 |
new (&this->GetElement(index)) value_type(std::piecewise_construct, std::forward_as_tuple(inKey), std::forward_as_tuple(std::forward<Args>(inArgs)...)); |
| 56 |
return std::make_pair(iterator(this, index), inserted); |
| 57 |
} |
| 58 |
|
| 59 |
template<class... Args> |
| 60 |
std::pair<iterator, bool> try_emplace(Key &&inKey, Args &&...inArgs) |
| 61 |
{ |
| 62 |
size_type index; |
| 63 |
bool inserted = this->InsertKey(inKey, index); |
| 64 |
if (inserted) |
| 65 |
new (&this->GetElement(index)) value_type(std::piecewise_construct, std::forward_as_tuple(std::move(inKey)), std::forward_as_tuple(std::forward<Args>(inArgs)...)); |
| 66 |
return std::make_pair(iterator(this, index), inserted); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
using Base::find; |
| 71 |
|
| 72 |
|
| 73 |
iterator find(const Key &inKey) |
| 74 |
{ |
| 75 |
const_iterator it = Base::find(inKey); |
| 76 |
return iterator(this, it.mIndex); |
| 77 |
} |
| 78 |
}; |
| 79 |
|
| 80 |
JPH_NAMESPACE_END |