-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset.cpp
More file actions
252 lines (202 loc) · 6.43 KB
/
set.cpp
File metadata and controls
252 lines (202 loc) · 6.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#pragma once
#include <stdexcept>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <random>
#include <limits>
#include <iostream>
#include <utility>
#define eps 0.01
using namespace std;
template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p)
{
os << "[" << p.first << ", " << p.second << "]";
return os;
}
template<typename T>
std::uniform_int_distribution<T> getDice(std::true_type)
{
return std::uniform_int_distribution<T>(std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
}
template<typename T>
std::uniform_real_distribution<T> getDice(std::false_type)
{
return std::uniform_real_distribution<T>(std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
}
template<typename T>
T random()
{
std::random_device randomDevice;
std::mt19937_64 generator(randomDevice());
auto dice = getDice<T>(std::integral_constant<bool, std::numeric_limits<T>::is_integer>());
return dice(generator);
}
template<typename T>
class Set {
private:
T* _elements;
int _len;
public:
Set() { // êîíñòðóêòîð ïî óìîë÷àíèþ
_elements = nullptr;
_len = 0;
}
Set(Set<T>& other) { //êîíñòðóêòîð êîïèðîâàíèÿ
Set<T> a;
for (int i = 0;i < other._len;i++) {
a += other[i];
}
swap(a);
}
void swap(Set& other) {
std::swap(_elements, other._elements);
std::swap(_len, other._len);
}
int get_len() {
return _len;
}
Set(const T* values, int l) { // êîíñòðóêòîð ñî çíà÷åíèÿìè
_len = 0;
_elements = new T[l];
for (int i = 0; i < l; i++) {
bool flag = true;
for (int j = 0; j < _len; j++) {
if (_elements[j] == values[i]) {
flag = false;
}
}
if (flag == true) {
_elements[_len] = values[i];
_len++;
}
}
}
Set(int len) { // êîíñòðóêòîð ñ ðàíäîìíûìè çíà÷åíèÿìè
_len = 0;
_elements = new T[len];
for (int i = 0; i < len; i++) {
T val;
while (true) {
bool flag = true;
val = random<T>();
for (int j = 0; j < _len; j++) {
if (_elements[j] == val) {
flag = false;
}
}
if (flag == true) {
break;
}
}
_elements[i] = val;
_len++;
}
}
T& operator[](int index) const { //îïåðàòîð äëÿ ïîëó÷åíèÿ ÷èñëà ïî èíäåêñó
if (index < 0 || index >= _len) {
throw std::out_of_range("Set::operator[], index is out of range");
}
return _elements[index];
}
friend Set<T> operator+(const Set<T>& first, T other) { // äîáàâëåíèå ÷èñëà âî ìíîæåñòâî +
Set<T> sec = first;
sec += other;
return sec;
}
void operator +=(T other) { // äîáàâëåíèå ÷èñëà âî ìíîæåñòâî +=
if (!num_in_set(other)) {
T* mas = new T[_len + 1];
for (int i = 0; i < _len; i++) {
mas[i] = _elements[i];
}
mas[_len] = other;
delete[] _elements;
_elements = mas;
_len++;
}
}
friend Set<T> operator-( Set<T>& first, T other) { // óäàëåíèå ÷èñëà èç ìíîæåñòâà -
Set<T> sec = first;
sec -= other;
return sec;
}
void operator -=(T other) { // óäàëåíèå ÷èñëà èç ìíîæåñòâà -=
if (num_in_set(other)) {
T* mas = new T[_len - 1];
int j = 0;
for (int i = 0; i < _len; i++) {
if (_elements[i] != other) {
mas[j] = _elements[i];
j++;
}
}
delete[] _elements;
_elements = mas;
_len--;
}
}
friend Set<T> operator*(const Set<T>& first, const Set<T>& other) { // îïåðàòîð ïåðåñå÷åíèÿ ìíîæåñòâ
T* mas = new T[first._len + other._len];
int index = 0;
for (int i = 0; i < first._len; i++) {
for (int j = 0; j < other._len; j++) {
if (first[i] == other[j]) {
mas[index] = other[i];
index++;
}
}
}
return Set<T>(mas, index);
}
friend Set<T> operator+(Set<T>& first, Set<T>& other) { // îïåðàòîð ñëîæåíèÿ ìíîæåñòâ
Set<T>new_set(first);
for (int i = 0;i < other._len; i++) {
new_set += other[i];
}
return new_set;
}
friend Set<T> operator-(Set<T>& first, Set<T>& other) { // îïåðàòîð âû÷èòàíèÿ ìíîæåñòâ
Set<T> new_set(first);
for (int i = 0; i < other._len; i++) {
new_set -= other[i];
}
return new_set;
}
bool num_in_set(T val) { // ïðîâåðêà íàëè÷èÿ ÷èñëà â ìíîæåñòâå
T* temp = _elements;
for (int i = 0;i < _len;i++) {
if (*temp == val) {
return true;
}
else
{
temp++;
}
}
return false;
}
friend std::ostream& operator<<(std::ostream& out, const Set<T>& s) { //îïåðàòîð âûâîäà
for (int i = 0; i < s._len; i++) {
out << s[i] << " ";
}
out << endl;
return out;
}
bool operator==(const Set<T>& s) {
if (_len != s._len) return false;
for (int i = 0; i < _len; i++)
{
if (abs(_elements[i] - s[i])>=eps) return false;
}
return true;
}
};
template<typename T>
bool is_equal(Set<T>& first, Set<T>& other) { //âîçâðàùàåò true åñëè êàæäûé ýëåìåíò â first âñòðå÷àåòñÿ â other
if ((first+ other).get_len() == other.get_len()) {
return true;
}
return false;
}