-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSet.cpp
74 lines (57 loc) · 1.46 KB
/
Set.cpp
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
//
// Created by tuli on 07/11/2020.
//
#include "Set.h"
Set::Set()
:size(0), name("0")
{}
Set::Set(const Set& other)
:vic(other.vic), size(other.size) ,name(other.name)//copy ctor
{}
bool Set::operator<(const Set &b) { //operator < for set used for sorting power set
if (size < b.getSize()) return false;
else if (b.getSize() > size) return true; //by size
else {
for (int i = 0; i < size; i++) {
if (vic.get(i)>b.vic.get(i)) return true; //then by elms
else if (vic.get(i)<b.vic.get(i)) return false;
}
}
return false;
}
Set::Set(int *&elements, int count, string nom) //constructor
:name(nom){
int *temp = new int[count];
int j=0;
bool flg;
for(int i=0;i<count;i++){
flg=false;
for (int k=0;k<j;k++){ //here we are deleting duplicates
if(elements[i]==temp[k]){flg=true; break;}
}
if (!flg){temp[j]=elements[i]; j++;}
}
vic.elms=new int[j+1]; //allocating premenent array
vic.capacity=j+1; //setting size
this->size=j;
for(int i=0;i<j;i++){
vic.elms[i]=temp[i];
}
delete(elements); //delete the original arr
delete(temp); //delete temp arr
this->vic.current=j;
this->vic.Sort();
}
void Set::printSet() const{
cout << '{';
vic.print();
cout << '}';
}
void Set::add(int item) {
vic.push(item);
size++;
}
int Set::pop() { //this removes last item
size--;
return(vic.pop());
}