11#include <promeki/config.h>
12#if PROMEKI_ENABLE_CORE
13#include <unordered_set>
14#include <initializer_list>
19PROMEKI_NAMESPACE_BEGIN
35template <
typename T>
class HashSet {
36 PROMEKI_SHARED_FINAL(HashSet)
39 using Ptr = SharedPtr<HashSet>;
42 using Data = std::unordered_set<T>;
48 using Iterator =
typename Data::iterator;
51 using ConstIterator =
typename Data::const_iterator;
57 HashSet(
const HashSet &other) : d(other.d) {}
60 HashSet(HashSet &&other) noexcept : d(std::move(other.d)) {}
66 HashSet(std::initializer_list<T> initList) : d(initList) {}
72 HashSet &operator=(
const HashSet &other) {
78 HashSet &operator=(HashSet &&other)
noexcept {
79 d = std::move(other.d);
86 Iterator begin() noexcept {
return d.begin(); }
89 ConstIterator begin() const noexcept {
return d.cbegin(); }
92 ConstIterator cbegin() const noexcept {
return d.cbegin(); }
95 ConstIterator constBegin() const noexcept {
return d.cbegin(); }
98 Iterator end() noexcept {
return d.end(); }
101 ConstIterator end() const noexcept {
return d.cend(); }
104 ConstIterator cend() const noexcept {
return d.cend(); }
107 ConstIterator constEnd() const noexcept {
return d.cend(); }
112 bool isEmpty() const noexcept {
return d.empty(); }
115 size_t size() const noexcept {
return d.size(); }
120 bool contains(
const T &value)
const {
return d.find(value) != d.end(); }
129 bool insert(
const T &value) {
return d.insert(value).second; }
136 bool insert(T &&value) {
return d.insert(std::move(value)).second; }
143 bool remove(
const T &value) {
return d.erase(value) > 0; }
150 Iterator remove(Iterator pos) {
return d.erase(pos); }
153 void clear() noexcept {
162 void swap(HashSet &other)
noexcept {
174 HashSet unite(
const HashSet &other)
const {
176 for (
const auto &v : other.d) ret.d.insert(v);
185 HashSet intersect(
const HashSet &other)
const {
187 for (
const auto &v : d) {
188 if (other.contains(v)) ret.d.insert(v);
198 HashSet subtract(
const HashSet &other)
const {
200 for (
const auto &v : d) {
201 if (!other.contains(v)) ret.d.insert(v);
212 List<T> toList()
const {
214 ret.reserve(d.size());
215 for (
const auto &v : d) ret.pushToBack(v);
224 template <
typename Func>
void forEach(Func &&func)
const {
225 for (
const auto &v : d) func(v);
232 friend bool operator==(
const HashSet &lhs,
const HashSet &rhs) {
return lhs.d == rhs.d; }
235 friend bool operator!=(
const HashSet &lhs,
const HashSet &rhs) {
return lhs.d != rhs.d; }