DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
HashSet.h
Go to the documentation of this file.
1#pragma once
2
3#include "Common.h"
4#include "System/Object.h"
5#include "System/Array.h"
7#include <new>
8#include <initializer_list>
9#include <utility>
10
11namespace DotNetDupe {
12 namespace System {
13 namespace Collections {
14 namespace Generic {
15
22 template <typename T>
23 class HashSet : public Object {
24 private:
26
27 public:
30
32 HashSet(const HashSet& other) : m_dict(other.m_dict) {}
33
35 HashSet(HashSet&& other) noexcept : m_dict(std::move(other.m_dict)) {}
36
38 HashSet& operator=(const HashSet& other) {
39 if (this != &other) {
40 m_dict = other.m_dict;
41 }
42 return *this;
43 }
44
46 HashSet& operator=(HashSet&& other) noexcept {
47 if (this != &other) {
48 m_dict = std::move(other.m_dict);
49 }
50 return *this;
51 }
52
54 ~HashSet() override {}
55
58 int GetCount() const { return m_dict.GetCount(); }
59
63 bool Add(const T& item) {
64 if (m_dict.ContainsKey(item)) return false;
65 m_dict.Add(item, true);
66 return true;
67 }
68
72 bool Remove(const T& item) {
73 return m_dict.Remove(item);
74 }
75
79 bool Contains(const T& item) const {
80 return m_dict.ContainsKey(item);
81 }
82
84 void Clear() {
85 m_dict.Clear();
86 }
87
90 void UnionWith(const HashSet<T>& other) {
91 Array<T> keys = other.m_dict.GetKeys();
92 for (int i = 0; i < keys.GetLength(); ++i) {
93 Add(keys[i]);
94 }
95 }
96
99 void IntersectWith(const HashSet<T>& other) {
100 Array<T> keys = m_dict.GetKeys();
101 for (int i = keys.GetLength() - 1; i >= 0; --i) {
102 if (!other.Contains(keys[i])) {
103 Remove(keys[i]);
104 }
105 }
106 }
107
110 void ExceptWith(const HashSet<T>& other) {
111 Array<T> keys = other.m_dict.GetKeys();
112 for (int i = 0; i < keys.GetLength(); ++i) {
113 Remove(keys[i]);
114 }
115 }
116
120 return m_dict.GetKeys();
121 }
122 };
123
124 }
125 }
126 }
127}
Provides methods for creating, manipulating, searching, and sorting arrays.
Defines common cross-platform macros, export decorators, and fundamental types.
Base object class for DotNetDupe mirroring .NET System.Object.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition Array.h:29
int GetLength() const
Gets the total number of elements in all dimensions of the Array.
Definition Array.h:142
Represents a collection of keys and values.
Definition Dictionary.h:66
int GetCount() const
Gets the number of elements that are contained in a set.
Definition HashSet.h:58
void UnionWith(const HashSet< T > &other)
Modifies the current HashSet object to contain all elements that are present in itself,...
Definition HashSet.h:90
void Clear()
Removes all elements from a HashSet object.
Definition HashSet.h:84
HashSet()
Initializes a new instance of the HashSet class that is empty.
Definition HashSet.h:29
HashSet & operator=(HashSet &&other) noexcept
Move assignment operator.
Definition HashSet.h:46
HashSet(HashSet &&other) noexcept
Move constructor.
Definition HashSet.h:35
bool Add(const T &item)
Adds the specified element to a set.
Definition HashSet.h:63
bool Remove(const T &item)
Removes the specified element from a HashSet object.
Definition HashSet.h:72
void ExceptWith(const HashSet< T > &other)
Removes all elements in the specified collection from the current HashSet object.
Definition HashSet.h:110
Array< T > ToArray() const
Copies the elements of a HashSet object to an Array.
Definition HashSet.h:119
HashSet(const HashSet &other)
Copy constructor.
Definition HashSet.h:32
void IntersectWith(const HashSet< T > &other)
Modifies the current HashSet object to contain only elements that are present in that object and in t...
Definition HashSet.h:99
HashSet & operator=(const HashSet &other)
Copy assignment operator.
Definition HashSet.h:38
bool Contains(const T &item) const
Determines whether a HashSet object contains the specified element.
Definition HashSet.h:79
Supports all classes in the DotNetDupe class hierarchy.
Definition Object.h:18