Il2CppHashMap.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #pragma once
  2. // Mono code also has define for GROUP_SIZE, so we need to wrap its usage here
  3. #pragma push_macro("GROUP_SIZE")
  4. #undef GROUP_SIZE
  5. #if IL2CPP_USE_SPARSEHASH
  6. #include "../../external/google/sparsehash/sparse_hash_map.h"
  7. #else
  8. #include "../../external/google/sparsehash/dense_hash_map.h"
  9. #endif
  10. #pragma pop_macro("GROUP_SIZE")
  11. #include "KeyWrapper.h"
  12. #include "os/FastReaderReaderWriterLock.h"
  13. #include "Memory.h"
  14. template<class Key, class T,
  15. class HashFcn = SPARSEHASH_HASH<Key>,
  16. class EqualKey = std::equal_to<Key>,
  17. class Alloc = libc_allocator_with_realloc_il2cpp<std::pair<const KeyWrapper<Key>, T> > >
  18. #if IL2CPP_USE_SPARSEHASH
  19. class Il2CppHashMap : public GOOGLE_NAMESPACE::sparse_hash_map<KeyWrapper<Key>, T, HashFcn, typename KeyWrapper<Key>::template EqualsComparer<EqualKey>, Alloc>
  20. #else
  21. class Il2CppHashMap : public GOOGLE_NAMESPACE::dense_hash_map<KeyWrapper<Key>, T, HashFcn, typename KeyWrapper<Key>::template EqualsComparer<EqualKey>, Alloc>
  22. #endif
  23. {
  24. private:
  25. #if IL2CPP_USE_SPARSEHASH
  26. typedef GOOGLE_NAMESPACE::sparse_hash_map<KeyWrapper<Key>, T, HashFcn, typename KeyWrapper<Key>::template EqualsComparer<EqualKey>, Alloc> Base;
  27. #else
  28. typedef GOOGLE_NAMESPACE::dense_hash_map<KeyWrapper<Key>, T, HashFcn, typename KeyWrapper<Key>::template EqualsComparer<EqualKey>, Alloc> Base;
  29. #endif
  30. public:
  31. typedef typename Base::size_type size_type;
  32. typedef typename Base::hasher hasher;
  33. typedef typename Base::key_equal key_equal;
  34. typedef typename Base::key_type key_type;
  35. explicit Il2CppHashMap(size_type n = 0,
  36. const hasher& hf = hasher(),
  37. const EqualKey& eql = EqualKey()) :
  38. Base(n, hf, key_equal(eql))
  39. {
  40. Base::set_deleted_key(key_type(key_type::KeyType_Deleted));
  41. #if !IL2CPP_USE_SPARSEHASH
  42. Base::set_empty_key(key_type(key_type::KeyType_Empty));
  43. #endif
  44. }
  45. template<class InputIterator>
  46. Il2CppHashMap(InputIterator f, InputIterator l,
  47. size_type n = 0,
  48. const hasher& hf = hasher(),
  49. const EqualKey& eql = EqualKey()) :
  50. Base(f, l, n, hf, key_equal(eql))
  51. {
  52. Base::set_deleted_key(key_type(key_type::KeyType_Deleted));
  53. #if !IL2CPP_USE_SPARSEHASH
  54. Base::set_empty_key(key_type(key_type::KeyType_Empty));
  55. #endif
  56. }
  57. void add(const key_type& key, const T& value)
  58. {
  59. Base::insert(std::make_pair(key, value));
  60. }
  61. };
  62. template<class Key, class T,
  63. class HashFcn = SPARSEHASH_HASH<Key>,
  64. class EqualKey = std::equal_to<Key>,
  65. class Alloc = libc_allocator_with_realloc_il2cpp<std::pair<const KeyWrapper<Key>, T> > >
  66. class Il2CppReaderWriterLockedHashMap
  67. {
  68. public:
  69. typedef typename Il2CppHashMap<Key, T, HashFcn, EqualKey, Alloc>::key_type key_type;
  70. typedef typename Il2CppHashMap<Key, T, HashFcn, EqualKey, Alloc>::size_type size_type;
  71. typedef typename Il2CppHashMap<Key, T, HashFcn, EqualKey, Alloc>::const_iterator const_iterator;
  72. typedef typename Il2CppHashMap<Key, T, HashFcn, EqualKey, Alloc>::iterator iterator;
  73. typedef typename Il2CppHashMap<Key, T, HashFcn, EqualKey, Alloc>::hasher hasher;
  74. explicit Il2CppReaderWriterLockedHashMap(size_type n = 0,
  75. const hasher& hf = hasher(),
  76. const EqualKey& eql = EqualKey()) :
  77. hashMap(n, hf, eql)
  78. {
  79. }
  80. bool TryGet(const key_type& key, T* value)
  81. {
  82. il2cpp::os::FastReaderReaderWriterAutoSharedLock readerLock(&lock);
  83. const_iterator iter = hashMap.find(key);
  84. if (iter != hashMap.end())
  85. {
  86. *value = iter->second;
  87. return true;
  88. }
  89. return false;
  90. }
  91. bool Add(const key_type& key, const T& value)
  92. {
  93. il2cpp::os::FastReaderReaderWriterAutoExclusiveLock writerLock(&lock);
  94. return hashMap.insert(std::make_pair(key, value)).second;
  95. }
  96. void Clear()
  97. {
  98. il2cpp::os::FastReaderReaderWriterAutoExclusiveLock writerLock(&lock);
  99. hashMap.clear();
  100. }
  101. void Remove(const key_type& key)
  102. {
  103. il2cpp::os::FastReaderReaderWriterAutoExclusiveLock readerLock(&lock);
  104. hashMap.erase(key);
  105. }
  106. // This function takes no locks, some other lock must be used to protect accesses
  107. iterator UnlockedBegin()
  108. {
  109. return hashMap.begin();
  110. }
  111. // This function takes no locks, some other lock must be used to protect accesses
  112. iterator UnlockedEnd()
  113. {
  114. return hashMap.end();
  115. }
  116. private:
  117. il2cpp::os::FastReaderReaderWriterLock lock;
  118. Il2CppHashMap<Key, T, HashFcn, EqualKey, Alloc> hashMap;
  119. };