escargot/third_party/yarr/HashSet.h
Seonghyun Kim 0f6ea4612a Fix memory error on Yarr
If we want to store WTF::String with Vector or HashSet,
we would use another type of allocator

Signed-off-by: Seonghyun Kim <sh8281.kim@samsung.com>
2024-07-25 16:29:03 +09:00

95 lines
2.8 KiB
C++

/*
* Copyright (c) 2024-present Samsung Electronics Co., Ltd
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#pragma once
#include <unordered_set>
#include <iterator>
namespace WTF {
template <typename Key, typename Allocator = std::allocator<Key>>
class HashSet : public std::unordered_set<Key, std::hash<Key>, std::equal_to<Key>, Allocator> {
public:
struct AddResult {
bool isNewEntry;
};
AddResult add(const Key& k)
{
AddResult r;
r.isNewEntry = std::unordered_set<Key, std::hash<Key>, std::equal_to<Key>, Allocator>::insert(k).second;
return r;
}
template<typename Other>
void formUnion(const Other& other)
{
for (const auto& value: other) {
add(value);
}
}
bool contains(const Key& k)
{
return std::unordered_set<Key, std::hash<Key>, std::equal_to<Key>, Allocator>::find(k) != std::unordered_set<Key, std::hash<Key>, std::equal_to<Key>, Allocator>::end();
}
bool isEmpty()
{
return std::unordered_set<Key, std::hash<Key>, std::equal_to<Key>, Allocator>::empty();
}
};
template <typename Key, typename Allocator = GCUtil::gc_malloc_allocator<Key>>
class GCHashSet : public Escargot::HashSet<Key, std::hash<Key>, std::equal_to<Key>, Allocator> {
public:
struct AddResult {
bool isNewEntry;
};
AddResult add(const Key& k)
{
AddResult r;
r.isNewEntry = Escargot::HashSet<Key, std::hash<Key>, std::equal_to<Key>, Allocator>::insert(k).second;
return r;
}
template<typename Other>
void formUnion(const Other& other)
{
for (const auto& value: other) {
add(value);
}
}
bool contains(const Key& k)
{
return Escargot::HashSet<Key, std::hash<Key>, std::equal_to<Key>, Allocator>::find(k) != Escargot::HashSet<Key, std::hash<Key>, std::equal_to<Key>, Allocator>::end();
}
bool isEmpty()
{
return Escargot::HashSet<Key, std::hash<Key>, std::equal_to<Key>, Allocator>::empty();
}
};
} // namespace WTF
using WTF::HashSet;
using WTF::GCHashSet;