Files
UU_la_parallel_programming_…/Assignment_2/MultiSet.cpp
2021-01-01 19:00:03 +01:00

84 lines
1.5 KiB
C++

#pragma once
#include <bits/stdc++.h>
#include"Node.cpp"
#include"Set.cpp"
class MultiSet:public Set {
Node* first;
public:
MultiSet();
bool add(int element);
bool rmv(int element);
bool ctn(int element);
};
MultiSet::MultiSet() : Set("MultiSet") { }
bool MultiSet::add(int element) {
this->first->lock();
Node* p = this->first;
Node* c = p->next;
c->lock();
while (c->is_smaller_than(element)) {
p->unlock();
p = c;
c = c->next;
c->lock();
}
Node* n = new Node(element);
n->next = c;
p->next = n;
c->unlock();
p->unlock();
return true;
}
bool MultiSet::rmv(int element) {
this->first->lock();
Node* p = this->first;
Node* c = p->next;
c->lock();
while (c->is_smaller_than(element)) {
p->unlock();
c = c->next;
c->lock();
}
if (c->is_equal(element)) {
p->next = c->next;
c->unlock();
p->unlock();
return true;
} else {
c->unlock();
p->unlock();
return false;
}
}
bool MultiSet::ctn(int element) {
this->first->lock();
Node* p = this->first;
Node* c = p->next;
c->lock();
while (c->is_smaller_than(element)) {
p->unlock();
c = c->next;
c->lock();
}
if (c->is_equal(element)) {
c->unlock();
p->unlock();
return true;
} else {
c->unlock();
p->unlock();
return false;
}
}