DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
SortedSet.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
8namespace DotNetDupe {
9 namespace System {
10 namespace Collections {
11 namespace Generic {
12
19 template <typename T>
20 class SortedSet : public Object {
21 private:
22 List<T> m_lstItems;
23
24 public:
26 SortedSet() = default;
27
30 int GetCount() const { return m_lstItems.GetCount(); }
31
35 bool Add(const T& item) {
36 int index = m_lstItems.BinarySearch(item);
37 if (index >= 0) return false;
38
39 m_lstItems.Insert(~index, item);
40 return true;
41 }
42
46 bool Remove(const T& item) {
47 int index = m_lstItems.BinarySearch(item);
48 if (index < 0) return false;
49
50 m_lstItems.RemoveAt(index);
51 return true;
52 }
53
57 bool Contains(const T& item) const {
58 return m_lstItems.BinarySearch(item) >= 0;
59 }
60
62 void Clear() {
63 m_lstItems.Clear();
64 }
65
68 void UnionWith(const SortedSet<T>& other) {
69 for (int i = 0; i < other.m_lstItems.GetCount(); ++i) {
70 Add(other.m_lstItems[i]);
71 }
72 }
73
76 void IntersectWith(const SortedSet<T>& other) {
77 for (int i = m_lstItems.GetCount() - 1; i >= 0; --i) {
78 if (!other.Contains(m_lstItems[i])) {
79 Remove(m_lstItems[i]);
80 }
81 }
82 }
83
86 void ExceptWith(const SortedSet<T>& other) {
87 for (int i = 0; i < other.m_lstItems.GetCount(); ++i) {
88 Remove(other.m_lstItems[i]);
89 }
90 }
91
94 Array<T> ToArray() const {
95 return m_lstItems.ToArray();
96 }
97 };
98
99 }
100 }
101 }
102}
Provides methods for creating, manipulating, searching, and sorting arrays.
Defines common cross-platform macros, export decorators, and fundamental types.
Represents a strongly typed list of objects that can be accessed by index mirroring ....
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
Represents a strongly typed list of objects accessible by index.
Definition List.h:29
Array< T > ToArray() const
Copies the elements of the SortedSet to a new array.
Definition SortedSet.h:94
void Clear()
Removes all elements from the set.
Definition SortedSet.h:62
void UnionWith(const SortedSet< T > &other)
Modifies the current SortedSet object so that it contains all elements that are present in either the...
Definition SortedSet.h:68
int GetCount() const
Gets the number of elements in the SortedSet.
Definition SortedSet.h:30
void IntersectWith(const SortedSet< T > &other)
Modifies the current SortedSet object so that it contains only elements that are also in a specified ...
Definition SortedSet.h:76
void ExceptWith(const SortedSet< T > &other)
Removes all elements that are in a specified collection from the current SortedSet object.
Definition SortedSet.h:86
bool Add(const T &item)
Adds an element to the set and returns a value that indicates if it was successfully added.
Definition SortedSet.h:35
SortedSet()=default
Initializes a new instance of the SortedSet class.
bool Contains(const T &item) const
Determines whether the set contains a specific element.
Definition SortedSet.h:57
bool Remove(const T &item)
Removes a specified item from the SortedSet.
Definition SortedSet.h:46
Supports all classes in the DotNetDupe class hierarchy.
Definition Object.h:18