Il2CppHashMap.h 4.6 KB

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