School Commit Init

This commit is contained in:
2024-08-31 12:07:21 +03:00
commit 0b130ee18c
2801 changed files with 4720552 additions and 0 deletions
@@ -0,0 +1,19 @@
#include "ExtendedTest.h"
#include "ShortTest.h"
#include "SortedMap.h"
#include <iostream>
using namespace std;
int main() {
testAll();
testAllExtended();
cout << "That's all!" << endl;
system("pause");
return 0;
}
Binary file not shown.
@@ -0,0 +1,341 @@
#include <exception>
#include <assert.h>
#include <algorithm>
#include <vector>
#include <iostream>
#include "SortedMap.h"
#include "SMIterator.h"
#include "ExtendedTest.h"
using namespace std;
bool increasing(TKey c1, TKey c2) {
if (c1 <= c2) {
return true;
} else {
return false;
}
}
bool decreasing(TKey c1, TKey c2) {
if (c1 >= c2) {
return true;
} else {
return false;
}
}
void testIteratorSteps(SortedMap& m, Relation r) {
SMIterator li = m.iterator();
TElem elem = NULL_TPAIR;
int count = 0;
if (li.valid()) {
elem = li.getCurrent();
count++;
li.next();
}
while (li.valid()) {
TElem elem2 = li.getCurrent();
assert(r(elem.first, elem2.first));
elem = elem2;
count++;
li.next();
}
assert(count == m.size());
}
void testCreate() {
cout << "Test create" << endl;
SortedMap sm(increasing);
assert(sm.size() == 0);
assert(sm.isEmpty());
SMIterator it = sm.iterator();
it.first();
assert(!it.valid());
try {
it.next();
assert(false);
}
catch (exception&) {
assert(true);
}
try {
it.getCurrent();
assert(false);
}
catch (exception&) {
assert(true);
}
for (int i = 0; i < 10; i++) {
assert(sm.search(i) == NULL_TVALUE);
}
for (int i = -10; i < 10; i++) {
assert(sm.remove(i) == NULL_TVALUE);
}
}
void testSearch(Relation r) {
cout << "Test search" << endl;
SortedMap sm(r);
int cMin = 0;
int cMax = 10;
try {
for (int i = cMin; i <= cMax; i++) {
sm.add(i, i + 1);
}
assert(true);
} catch (exception&) {
assert(false);
}
int intervalDim = 10;
for (int i = cMin; i <= cMax; i++) {
assert(sm.search(i) == i + 1);
}
testIteratorSteps(sm, r);
for (int i = cMin - intervalDim; i < cMin; i++) {
assert(sm.search(i) == NULL_TVALUE);
}
for (int i = cMax + 1; i < cMax + intervalDim; i++) {
assert(sm.search(i) == NULL_TVALUE);
}
}
void testSearch() {
testSearch(increasing);
testSearch(decreasing);
}
vector<int> keysInRandomOrder(int cMin, int cMax) {
vector<int> keys;
for (int c = cMin; c <= cMax; c++) {
keys.push_back(c);
}
int n = keys.size();
for (int i = 0; i < n - 1; i++) {
int j = i + rand() % (n - i);
swap(keys[i], keys[j]);
}
return keys;
}
void populateSMEmpty(SortedMap& sm, int cMin, int cMax) {
vector<int> keys = keysInRandomOrder(cMin, cMax);
int n = keys.size();
for (int i = 0; i < n; i++) {
assert(sm.add(keys[i], keys[i]) == NULL_TVALUE);
}
}
void rePopulateSMShift(SortedMap& sm, int cMin, int cMax, int shift) {
vector<int> keys = keysInRandomOrder(cMin, cMax);
int n = keys.size();
for (int i = 0; i < n; i++) {
assert(sm.add(keys[i], keys[i] - shift) == keys[i]);
}
}
void populateSMShift(SortedMap& sm, int cMin, int cMax, int shift) {
vector<int> keys = keysInRandomOrder(cMin, cMax);
int n = keys.size();
for (int i = 0; i < n; i++) {
sm.add(keys[i], keys[i] - shift);
}
}
void testAddAndSearch(Relation r) {
cout << "Test add and search" << endl;
SortedMap sm(r);
int cMin = 100;
int cMax = 200;
populateSMEmpty(sm, cMin, cMax);
testIteratorSteps(sm, r);
for (int c = cMin; c <= cMax; c++) {
assert(sm.search(c) == c);
}
assert(sm.size() == (cMax - cMin + 1));
rePopulateSMShift(sm, cMin, cMax, 1);
assert(sm.size() == (cMax - cMin + 1));
testIteratorSteps(sm, r);
populateSMShift(sm, 2 * cMax, 3 * cMax, 2 * cMax - cMin);
for (int c = 2 * cMax; c <= 3 * cMax; c++) {
assert(sm.search(c) == c - 2 * cMax + cMin);
}
testIteratorSteps(sm, r);
assert(sm.size() == (cMax - cMin + 1) + (cMax + 1));
SMIterator it = sm.iterator();
it.first();
if (it.valid()) {
TKey cPrec = it.getCurrent().first;
assert(sm.search(cPrec) != NULL_TVALUE);
it.next();
while (it.valid()) {
TKey c = it.getCurrent().first;
assert(r(cPrec, c));
assert(sm.search(c) != NULL_TVALUE);
cPrec = c;
it.next();
}
}
}
void testAdd() {
testAddAndSearch(increasing);
testAddAndSearch(decreasing);
}
void testRemoveAndSearch(Relation r) {
cout << "Test remove and search" << endl;
SortedMap sm(r);
int cMin = 10;
int cMax = 20;
populateSMEmpty(sm, cMin, cMax);
testIteratorSteps(sm, r);
for (int c = cMax + 1; c <= 2 * cMax; c++) {
assert(sm.remove(c) == NULL_TVALUE);
testIteratorSteps(sm, r);
}
int dim = cMax - cMin + 1;
assert(sm.size() == dim);
for (int c = cMin; c <= cMax; c++) {
assert(sm.remove(c) == c);
assert(sm.search(c) == NULL_TVALUE);
SMIterator it = sm.iterator();
it.first();
if (it.valid()) {
TKey cPrec = it.getCurrent().first;
it.next();
while (it.valid()) {
TKey c = it.getCurrent().first;
assert(r(cPrec, c));
cPrec = c;
it.next();
}
}
dim--;
assert(sm.size() == dim);
}
for (int c = cMin; c <= cMax; c++) {
assert(sm.remove(c) == NULL_TVALUE);
}
assert(sm.isEmpty());
assert(sm.size() == 0);
}
void testRemove() {
testRemoveAndSearch(increasing);
testRemoveAndSearch(decreasing);
}
void testIterator(Relation r) {
cout << "Test iterator" << endl;
SortedMap sm(r);
SMIterator it = sm.iterator();
assert(!it.valid());
it.first();
assert(!it.valid());
int cMin = 100;
int cMax = 300;
vector<int> keys = keysInRandomOrder(cMin, cMax);
int n = keys.size();
for (int i = 0; i < n; i++) {
assert(sm.add(keys[i], keys[n - i - 1]) == NULL_TVALUE);
}
SMIterator itSM = sm.iterator();
assert(itSM.valid());
itSM.first();
assert(itSM.valid());
TKey cPrec = itSM.getCurrent().first;
for (int i=1; i<100; i++){
assert(cPrec == itSM.getCurrent().first);
}
itSM.next();
while (itSM.valid()) {
TKey c = itSM.getCurrent().first;
assert(cMin <= c && c <= cMax);
assert(sm.search(c) != NULL_TVALUE);
assert(r(cPrec, c));
cPrec = c;
itSM.next();
}
}
void testQuantity(){
cout << "Test quantity" << endl;
SortedMap sm(increasing);
int cMin = -3000;
int cMax = 3000;
vector<int> keys = keysInRandomOrder(cMin, cMax);
populateSMEmpty(sm, cMin, cMax);
for (int c = cMin; c <= cMax; c++){
assert(sm.search(c) == c);
}
testIteratorSteps(sm, increasing);
assert(sm.size() == cMax - cMin + 1);
SMIterator it = sm.iterator();
assert(it.valid());
it.first();
assert(it.valid());
for (int i = 0; i < sm.size(); i++) {
it.next();
}
assert(!it.valid());
it.first();
while (it.valid()){
TKey c = it.getCurrent().first;
assert(sm.search(c) == c);
TValue v = it.getCurrent().second;
assert(c == v);
it.next();
}
assert(!it.valid());
for (int c = cMin-100; c <= cMax+100; c++){
sm.remove(c);
assert(sm.search(c) == NULL_TVALUE);
testIteratorSteps(sm, increasing);
}
assert(sm.size() == 0);
assert(sm.isEmpty());
}
void testIterator() {
testIterator(increasing);
testIterator(decreasing);
}
void testExtra(){
cout << "Test extra" << endl;
SortedMap sm(increasing);
sm.add(1, 1);
sm.add(2, 2);
sm.add(3, 3);
sm.add(4, 3);
vector<TValue> v = sm.valueBag();
assert(v.size() == 3);
assert(v[0] == 1);
assert(v[1] == 2);
assert(v[2] == 3);
}
void testAllExtended() {
testCreate();
testAdd();
testSearch();
testRemove();
testIterator();
testQuantity();
testExtra();
}
@@ -0,0 +1,4 @@
#pragma once
void testAllExtended();
@@ -0,0 +1,39 @@
#include "SMIterator.h"
#include "SortedMap.h"
#include <exception>
using namespace std;
SMIterator::SMIterator(const SortedMap& m) : map(m){
//Complexity: O(1)
this->current = this->map.head;
}
void SMIterator::first(){
//Complexity: O(1)
this->current = this->map.head;
}
void SMIterator::next(){
//Complexity: O(1)
if (this->current == nullptr)
throw exception();
this->current = this->current->next;
}
bool SMIterator::valid() const{
//Complexity: O(1)
if(this->current == nullptr)
return false;
return true;
}
TElem SMIterator::getCurrent() const{
//Complexity: O(1)
if(this->current==nullptr)
throw exception();
return this->current->element;
}
@@ -0,0 +1,20 @@
#pragma once
#include "SortedMap.h"
//DO NOT CHANGE THIS PART
class SMIterator{
friend class SortedMap;
private:
const SortedMap& map;
SMIterator(const SortedMap& mapionar);
//TODO - Representation
Node* current;
public:
void first();
void next();
bool valid() const;
TElem getCurrent() const;
};
@@ -0,0 +1,39 @@
#include <assert.h>
#include "SortedMap.h"
#include "SMIterator.h"
#include "ShortTest.h"
#include <exception>
using namespace std;
bool relatie1(TKey cheie1, TKey cheie2) {
if (cheie1 <= cheie2) {
return true;
}
else {
return false;
}
}
void testAll(){
SortedMap sm(relatie1);
assert(sm.size() == 0);
assert(sm.isEmpty());
sm.add(1,2);
assert(sm.size() == 1);
assert(!sm.isEmpty());
assert(sm.search(1)!=NULL_TVALUE);
TValue v =sm.add(1,3);
assert(v == 2);
assert(sm.search(1) == 3);
SMIterator it = sm.iterator();
it.first();
while (it.valid()){
TElem e = it.getCurrent();
assert(e.second != NULL_TVALUE);
it.next();
}
assert(sm.remove(1) == 3);
assert(sm.isEmpty());
}
@@ -0,0 +1,3 @@
#pragma once
void testAll();
@@ -0,0 +1,130 @@
#include "SMIterator.h"
#include "SortedMap.h"
#include <exception>
#include <algorithm>
using namespace std;
SortedMap::SortedMap(Relation r) {
//Complexity: O(1)
this->relation = r;
this->head = nullptr;
this->length = 0;
}
TValue SortedMap::add(TKey k, TValue v) {
//Complexity: O(n) - average/worst O(1) - best
if (this->head == nullptr){
this->head = new Node(pair<TKey, TValue>(k, v));
this->length++;
return NULL_TVALUE;
}
if (this->head->element.first == k){
TValue value = this->head->element.second;
this->head->element.second = v;
return value;
}
if(this->relation(k, this->head->element.first)){
Node* new_node = new Node(pair<TKey, TValue>(k, v));
new_node->next = this->head;
this->head = new_node;
this->length++;
return NULL_TVALUE;
}
Node* previous_node = this->head;
Node* current_node = this->head->next;
for(int i = 0; i < this->length-1; i++){
if (current_node->element.first == k){
TValue value = current_node->element.second;
current_node->element.second = v;
return value;
}
if (this->relation(k, current_node->element.first)){
Node* new_node = new Node(pair<TKey, TValue>(k, v));
new_node->next = current_node;
previous_node->next = new_node;
this->length++;
return NULL_TVALUE;
}
previous_node = current_node;
current_node = current_node->next;
}
previous_node->next = new Node(pair<TKey, TValue>(k, v));
this->length++;
return NULL_TVALUE;
}
TValue SortedMap::search(TKey k) const {
//Complexity: O(n) - average/worst O(1) - best
Node* current_node = this->head;
for(int i = 0; i< this->length; i++){
if (current_node->element.first == k)
return current_node->element.second;
current_node = current_node->next;
}
return NULL_TVALUE;
}
TValue SortedMap::remove(TKey k) {
//Complexity: O(n) - average/worst O(1) - best
if(this->head!=nullptr && this->head->element.first == k){
Node* new_head = this->head->next;
TValue value = this->head->element.second;
delete this->head;
this->head = new_head;
this->length--;
return value;
}
Node* current_node = this->head;
for(int i = 1; i< this->length; i++){
if (current_node->next->element.first == k){
Node* removed_node = current_node->next;
TValue value = removed_node->element.second;
current_node->next = current_node->next->next;
delete removed_node;
this->length--;
return value;
}
current_node = current_node->next;
}
return NULL_TVALUE;
}
int SortedMap::size() const {
//Complexity: O(1)
return this->length;
}
bool SortedMap::isEmpty() const {
//Complexity: O(1)
if (this->length == 0)
return true;
return false;
}
SMIterator SortedMap::iterator() const {
//Complexity: O(1)
return SMIterator(*this);
}
SortedMap::~SortedMap() {
//Complexity: O(n)
Node* current_node = this->head;
for(int i = 0; i < this->length; i++){
Node* next_node = current_node->next;
delete current_node;
current_node = next_node;
}
}
std::vector<TValue> SortedMap::valueBag() {
//Complexity: O(n*m) - average/worst O(n) - best
vector<TValue> values;
Node* current_node = this->head;
for(int i = 0; i < this->length; i++){
if (find(values.begin(), values.end(), current_node->element.second) == values.end())
values.push_back(current_node->element.second);
current_node = current_node->next;
}
return values;
}
@@ -0,0 +1,70 @@
#pragma once
//DO NOT INCLUDE SORTEDMAPITERATOR
#include <vector>
//DO NOT CHANGE THIS PART
typedef int TKey;
typedef int TValue;
#include <utility>
typedef std::pair<TKey, TValue> TElem;
#define NULL_TVALUE -111111
#define NULL_TPAIR pair<TKey, TValue>(-111111, -111111);
class SMIterator;
typedef bool(*Relation)(TKey, TKey);
class Node {
public:
TElem element;
Node* next;
Node(TElem e) {
element = e;
next = nullptr;
}
Node(TElem e, Node* n) {
element = e;
next = n;
}
bool operator==(Node* n) {
return this->element.first == n->element.first;
}
};
class SortedMap {
friend class SMIterator;
private:
//TODO - Representation
Node* head;
Relation relation;
int length;
public:
// implicit constructor
SortedMap(Relation r);
// adds a pair (key,value) to the map
//if the key already exists in the map, then the value associated to the key is replaced by the new value and the old value is returned
//if the key SMes not exist, a new pair is added and the value null is returned
TValue add(TKey c, TValue v);
//searches for the key and returns the value associated with the key if the map contains the key or null: NULL_TVALUE otherwise
TValue search(TKey c) const;
//removes a key from the map and returns the value associated with the key if the key existed ot null: NULL_TVALUE otherwise
TValue remove(TKey c);
//returns the number of pairs (key,value) from the map
int size() const;
//checks whether the map is empty or not
bool isEmpty() const;
// return the iterator for the map
// the iterator will return the keys following the order given by the relation
SMIterator iterator() const;
// destructor
~SortedMap();
std::vector<TValue> valueBag();
};