created build pipeline to allow unit testing

This commit is contained in:
2021-01-01 21:38:20 +01:00
parent 41ff431bdd
commit 047e9ee732
19 changed files with 99 additions and 6 deletions

View File

@@ -0,0 +1 @@
add_library(sets STATIC FineList.cpp LazyList.cpp OptimisticList.cpp MultiSet.cpp Node.cpp Set.cpp)

View File

@@ -0,0 +1,91 @@
#pragma once
#include <bits/stdc++.h>
#include"Node.cpp"
#include"Set.cpp"
class FineSet:public Set {
Node* first;
public:
FineSet();
bool add(int element);
bool rmv(int element);
bool ctn(int element);
};
FineSet::FineSet() : Set("FineSet") { }
bool FineSet::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();
}
if (c->is_equal(element)) {
c->unlock();
p->unlock();
return false;
} else {
Node* n = new Node(element);
n->next = c;
p->next = n;
c->unlock();
p->unlock();
return true;
}
}
bool FineSet::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 FineSet::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;
}
}

View File

@@ -0,0 +1,91 @@
#pragma once
#include <bits/stdc++.h>
#include"Node.cpp"
#include"Set.cpp"
class LazySet:public Set {
protected:
Node* first;
void locate(int element, Node* prev, Node* cur);
public:
LazySet();
bool add(int element);
bool rmv(int element);
bool ctn(int element);
};
LazySet::LazySet() : Set("LazySet") { }
bool LazySet::add(int element) {
Node* p;
Node* c;
locate(element, p, c);
if (c->is_equal(element)) {
p->unlock();
c->unlock();
return false;
} else {
Node* n = new Node(element);
p->next = n;
p->unlock();
c->unlock();
return true;
}
}
bool LazySet::rmv(int element) {
Node* p;
Node* c;
locate(element, p, c);
if (c->is_equal(element)) {
c->deleted = true;
p->next = c->next;
p->unlock();
c->unlock();
return true;
} else {
p->unlock();
c->unlock();
return false;
}
}
bool LazySet::ctn(int element) {
Node* c = first;
while (c->is_smaller_than(element)) {
c = c->next;
}
if (!c->is_equal(element) || c->deleted) {
return false;
} else {
return true;
}
}
void LazySet::locate(int element, Node* prev, Node* cur) {
prev = Node::Dummy();
cur = Node::Dummy();
Node* p = first;
Node* c = p->next;
while (c->is_smaller_than(element)) {
p = c;
c = c->next;
}
p->lock();
c->lock();
if (!p->deleted && !c->deleted && p->next == c) {
prev = p;
cur = c;
}
p->unlock();
c->unlock();
}

View File

@@ -0,0 +1,84 @@
#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;
}
}

58
Assignment_2/lib/Node.cpp Normal file
View File

@@ -0,0 +1,58 @@
#pragma once
#include <mutex>
class Node {
protected:
int data;
bool is_dummy;
std::mutex mut;
public:
Node* next;
bool deleted;
Node(int element);
static Node* Dummy();
// Dummy();
void lock();
void unlock();
bool is_smaller_than(int n);
bool is_smaller_than(Node* n);
bool is_equal(int n);
};
Node::Node(int element){
data = element;
is_dummy = false;
deleted = false;
}
Node* Node::Dummy(){
Node* n = new Node(0);
n->is_dummy = true;
return n;
}
bool Node::is_smaller_than(Node* n) {
return this->is_smaller_than(n->data);
}
bool Node::is_smaller_than(int n) {
// Everything is smaller than a dummy -> false
return this->data < n && !this->is_dummy;
}
bool Node::is_equal(int n) {
// Dummies are not equal to any number -> false
return this->data == n && !this->is_dummy;
}
void Node::lock() {
this->mut.lock();
}
void Node::unlock() {
this->mut.unlock();
}

View File

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

23
Assignment_2/lib/Set.cpp Normal file
View File

@@ -0,0 +1,23 @@
#pragma once
#include "Node.cpp"
class Set {
protected:
std::string name;
Set(std::string _name);
Node* first;
public:
bool add(int element) { return false; };
bool rmv(int element) { return false; };
bool ctn(int element) { return false; };
std::string get_name() { return name; };
};
Set::Set(std::string _name){
name = _name;
first = Node::Dummy();
Node* last = Node::Dummy();
first->next = last;
}