Il2CppHashSet.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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_set.h"
  7. #else
  8. #include "../../external/google/sparsehash/dense_hash_set.h"
  9. #endif
  10. #pragma pop_macro("GROUP_SIZE")
  11. #include "KeyWrapper.h"
  12. #include "os/FastReaderReaderWriterLock.h"
  13. template<class Value,
  14. class HashFcn = SPARSEHASH_HASH<Value>,
  15. class EqualKey = std::equal_to<Value>,
  16. class Alloc = GOOGLE_NAMESPACE::libc_allocator_with_realloc<KeyWrapper<Value> > >
  17. #if IL2CPP_USE_SPARSEHASH
  18. class Il2CppHashSet : public GOOGLE_NAMESPACE::sparse_hash_set<KeyWrapper<Value>, HashFcn, typename KeyWrapper<Value>::template EqualsComparer<EqualKey>, Alloc>
  19. #else
  20. class Il2CppHashSet : public GOOGLE_NAMESPACE::dense_hash_set<KeyWrapper<Value>, HashFcn, typename KeyWrapper<Value>::template EqualsComparer<EqualKey>, Alloc>
  21. #endif
  22. {
  23. private:
  24. #if IL2CPP_USE_SPARSEHASH
  25. typedef GOOGLE_NAMESPACE::sparse_hash_set<KeyWrapper<Value>, HashFcn, typename KeyWrapper<Value>::template EqualsComparer<EqualKey>, Alloc> Base;
  26. #else
  27. typedef GOOGLE_NAMESPACE::dense_hash_set<KeyWrapper<Value>, HashFcn, typename KeyWrapper<Value>::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 Il2CppHashSet(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. Il2CppHashSet(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. };
  57. template<class Value,
  58. class HashFcn = SPARSEHASH_HASH<Value>,
  59. class EqualKey = std::equal_to<Value>,
  60. class Alloc = GOOGLE_NAMESPACE::libc_allocator_with_realloc<KeyWrapper<Value> > >
  61. class Il2CppReaderWriterLockedHashSet
  62. {
  63. public:
  64. typedef typename Il2CppHashSet<Value, HashFcn, EqualKey, Alloc>::key_type key_type;
  65. typedef typename Il2CppHashSet<Value, HashFcn, EqualKey, Alloc>::size_type size_type;
  66. typedef typename Il2CppHashSet<Value, HashFcn, EqualKey, Alloc>::const_iterator const_iterator;
  67. typedef typename Il2CppHashSet<Value, HashFcn, EqualKey, Alloc>::iterator iterator;
  68. typedef typename Il2CppHashSet<Value, HashFcn, EqualKey, Alloc>::hasher hasher;
  69. explicit Il2CppReaderWriterLockedHashSet(size_type n = 0,
  70. const hasher& hf = hasher(),
  71. const EqualKey& eql = EqualKey()) :
  72. hashSet(n, hf, eql)
  73. {
  74. }
  75. bool TryGet(const Value& findValue, Value* value)
  76. {
  77. il2cpp::os::FastReaderReaderWriterAutoSharedLock readerLock(&lock);
  78. const_iterator iter = hashSet.find(findValue);
  79. if (iter != hashSet.end())
  80. {
  81. *value = *iter;
  82. return true;
  83. }
  84. return false;
  85. }
  86. bool Add(const Value& value)
  87. {
  88. il2cpp::os::FastReaderReaderWriterAutoExclusiveLock writerLock(&lock);
  89. return hashSet.insert(value).second;
  90. }
  91. Value GetOrAdd(const Value& value)
  92. {
  93. il2cpp::os::FastReaderReaderWriterAutoExclusiveLock writerLock(&lock);
  94. auto inserted = hashSet.insert(value);
  95. if (inserted.second)
  96. return value;
  97. return *(inserted.first);
  98. }
  99. void Clear()
  100. {
  101. il2cpp::os::FastReaderReaderWriterAutoExclusiveLock writerLock(&lock);
  102. hashSet.clear();
  103. }
  104. void Resize(size_t size)
  105. {
  106. il2cpp::os::FastReaderReaderWriterAutoExclusiveLock writerLock(&lock);
  107. hashSet.resize(size);
  108. }
  109. private:
  110. il2cpp::os::FastReaderReaderWriterLock lock;
  111. Il2CppHashSet<Value, HashFcn, EqualKey, Alloc> hashSet;
  112. };