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,18 @@
#include "ExtendedTest.h"
#include "ShortTest.h"
#include "Map.h"
#include <iostream>
using namespace std;
int main() {
testAll();
testAllExtended();
cout << "That's all!" << endl;
system("pause");
return 0;
}
@@ -0,0 +1,291 @@
#include "ExtendedTest.h"
#include "Map.h"
#include "MapIterator.h"
#include <assert.h>
#include <iostream>
using namespace std;
void testIteratorSteps(Map& m) {
MapIterator mi = m.iterator();
int count = 0;
while (mi.valid()) {
count++;
mi.next();
}
assert(count == m.size());
}
void testCreate() {
cout << "Test create" << endl;
Map m;
assert(m.size() == 0);
assert(m.isEmpty() == true);
for (int i = -10; i < 10; i++) {
assert(m.search(i) == NULL_TVALUE);
}
for (int i = -10; i < 10; i++) {
assert(m.search(i) == NULL_TVALUE);
}
MapIterator it = m.iterator();
assert(it.valid() == false);
try {
it.next();
assert(false);
}
catch (exception&) {
assert(true);
}
try {
it.getCurrent();
assert(false);
}
catch (exception&) {
assert(true);
}
}
void testAdd() {
cout << "Test add" << endl;
Map m;
for (int i = 0; i < 10; i++) {
assert(m.add(i, i) == NULL_TVALUE);
}
assert(m.isEmpty() == false);
assert(m.size() == 10);
for (int i = -10; i < 0; i++) {
assert(m.add(i, i) == NULL_TVALUE);
}
for (int i = 0; i < 10; i++) {
assert(m.add(i, i) == i);
}
for (int i = 10; i < 20; i++) {
assert(m.add(i, i) == NULL_TVALUE);
}
assert(m.isEmpty() == false);
assert(m.size() == 30);
for (int i = -100; i < 100; i++) {
m.add(i, i);
}
assert(m.isEmpty() == false);
assert(m.size() == 200);
for (int i = -200; i < 200; i++) {
if (i < -100) {
assert(m.search(i) == NULL_TVALUE);
}
else if (i < 0) {
assert(m.search(i) == i);
}
else if (i < 100) {
assert(m.search(i) == i);
}
else {
assert(m.search(i) == NULL_TVALUE);
}
}
for (int i = 10000; i > -10000; i--) {
m.add(i, i);
}
testIteratorSteps(m);
assert(m.size() == 20000);
}
void testRemove() {
cout << "Test remove" << endl;
Map m;
for (int i = -100; i < 100; i++) {
assert(m.remove(i) == NULL_TVALUE);
}
assert(m.size() == 0);
for (int i = -100; i < 100; i = i + 2) {
assert(m.add(i, i) == NULL_TVALUE);
}
for (int i = -100; i < 100; i++) {
if (i % 2 == 0) {
assert(m.remove(i) == i);
}
else {
assert(m.remove(i) == NULL_TVALUE);
}
}
assert(m.size() == 0);
for (int i = -100; i <= 100; i = i + 2) {
assert(m.add(i, i) == NULL_TVALUE);
}
testIteratorSteps(m);
for (int i = 100; i > -100; i--) {
if (i % 2 == 0) {
assert(m.remove(i) == i);
}
else {
assert(m.remove(i) == NULL_TVALUE);
}
testIteratorSteps(m);
}
assert(m.size() == 1);
m.remove(-100);
assert(m.size() == 0);
for (int i = -100; i < 100; i++) {
assert(m.add(i, 0) == NULL_TVALUE);
assert(m.add(i, 1) == 0);
assert(m.add(i, 2) == 1);
assert(m.add(i, 3) == 2);
assert(m.add(i, 4) == 3);
}
assert(m.size() == 200);
for (int i = -200; i < 200; i++) {
if (i < -100 || i >= 100) {
assert(m.remove(i) == NULL_TVALUE);
}
else {
assert(m.remove(i) == 4);
assert(m.remove(i) == NULL_TVALUE);
}
}
assert(m.size() == 0);
}
void testIterator() {
cout << "Test iterator" << endl;
Map m;
MapIterator it = m.iterator();
assert(it.valid() == false);
for (int i = 0; i < 100; i++) {
m.add(33, 33);
}
MapIterator it2 = m.iterator();
assert(it2.valid() == true);
TElem el = it2.getCurrent();
assert(el.first == 33);
assert(el.second == 33);
it2.next();
assert(it2.valid() == false);
try {
it2.next();
assert(false);
}
catch (exception&) {
assert(true);
}
try {
it2.getCurrent();
assert(false);
}
catch (exception&) {
assert(true);
}
it2.first();
assert(it2.valid() == true);
Map m2;
for (int i = -100; i < 100; i++) {
assert(m2.add(i, i) == NULL_TVALUE);
assert(m2.add(i, i) == i);
assert(m2.add(i, i) == i);
}
MapIterator it3 = m2.iterator();
assert(it3.valid() == true);
for (int i = 0; i < 200; i++) {
it3.next();
}
assert(it3.valid() == false);
it3.first();
assert(it3.valid() == true);
Map m3;
for (int i = 0; i < 200; i = i + 4) {
m3.add(i, i);
}
MapIterator it4 = m3.iterator();
assert(it4.valid() == true);
int count = 0;
while (it4.valid()) {
TElem e = it4.getCurrent();
assert(e.first % 4 == 0);
it4.next();
count++;
}
assert(count == 50);
}
void testQuantity() {
Map m;
cout << "Test quantity" << endl;
for (int j = 0; j < 30000; j = j + 1) {
assert(m.add(j, j) == NULL_TVALUE);
}
for (int i = 10; i >= 1; i--) {
for (int j = 0; j < 30000; j = j + i) {
assert(m.add(j, j) == j);
}
}
assert(m.size() == 30000);
MapIterator it = m.iterator();
assert(it.valid() == true);
for (int i = 0; i < m.size(); i++) {
it.next();
}
it.first();
while (it.valid()) {
TElem e = it.getCurrent();
assert(m.search(e.first) == e.first);
it.next();
}
assert(it.valid() == false);
for (int j = 30000 - 1; j >= 0; j--) {
assert(m.remove(j) == j);
}
for (int i = 0; i < 10; i++) {
for (int j = 40000; j >= -4000; j--) {
assert(m.remove(j) == NULL_TVALUE);
}
}
assert(m.size() == 0);
}
void testExtra()
{
cout << "Test extra" << endl;
Map m;
assert(m.isEmpty() == true);
assert(m.getKeyRange() == -1);
assert(m.add(1, 2) == NULL_TVALUE);
assert(m.add(1, 3) == 2);
assert(m.add(2, 3) == NULL_TVALUE);
assert(m.getKeyRange() == 1);
assert(m.add(100, 100) == NULL_TVALUE);
assert(m.getKeyRange() == 99);
}
void testAllExtended() {
testCreate();
testAdd();
testRemove();
testIterator();
testQuantity();
testExtra();
}
@@ -0,0 +1,3 @@
#pragma once
void testAllExtended();
@@ -0,0 +1,170 @@
#include "Map.h"
#include "MapIterator.h"
#include <math.h>
#include <iostream>
Map::Map() {
//O(1)
this->_capacity=INITIAL_CAPACITY;
this->_size=0;
this->_elements=new TElem[this->_capacity];
this->_next=new int[this->_capacity];
for(int i=0;i<this->_capacity;i++){
this->_elements[i]=NULL_TELEM;
this->_next[i]=-1;
}
this->_nextFree=INITIAL_CAPACITY-1;
}
Map::~Map() {
//O(1)
delete[] this->_elements;
delete[] this->_next;
}
TValue Map::add(TKey c, TValue v){
// O(1) - amortized
if(this->_size>=this->_capacity*ALPHA)
this->_resize();
int hash=abs(c % this->_capacity);
while(this->_next[hash]!=-1 && this->_elements[hash].first!=c)
hash=this->_next[hash];
if(this->_elements[hash].first==c){
TValue oldValue=this->_elements[hash].second;
this->_elements[hash].second=v;
return oldValue;
}
this->_elements[this->_nextFree]=std::make_pair(c,v);
this->_next[hash]=this->_nextFree;
while(this->_elements[this->_nextFree]!=NULL_TELEM)
this->_nextFree--;
this->_size++;
return NULL_TVALUE;
}
TValue Map::search(TKey c) const{
// O(1) - amortized
int hash = abs(c % this->_capacity);
while (this->_next[hash] != -1 && this->_elements[hash].first != c)
hash = this->_next[hash];
if (this->_elements[hash].first == c)
return this->_elements[hash].second;
return NULL_TVALUE;
}
TValue Map::remove(TKey c){
//O(1) - amortized
int hash = abs(c % this->_capacity);
int prev = -1;
while (this->_next[hash] != -1 && this->_elements[hash].first != c) {
prev = hash;
hash = this->_next[hash];
}
if (this->_elements[hash].first == c) {
TValue oldValue = this->_elements[hash].second;
if (prev == -1) {
if(this->_next[hash]!=-1){
int remove = this->_next[hash];
this->_elements[hash] = this->_elements[this->_next[hash]];
this->_next[hash] = this->_next[this->_next[hash]];
this->_next[remove] = -1;
this->_elements[remove] = NULL_TELEM;
hash = remove;
}
else
this->_elements[hash] = NULL_TELEM;
}
else {
this->_next[prev] = this->_next[hash];
this->_elements[hash] = NULL_TELEM;
this->_next[hash] = -1;
}
if(hash>this->_nextFree)
this->_nextFree = hash;
this->_size--;
return oldValue;
}
return NULL_TVALUE;
}
int Map::size() const {
//O(1)
return this->_size;
}
bool Map::isEmpty() const{
//O(1)
return (this->_size==0);
}
MapIterator Map::iterator() const {
return MapIterator(*this);
}
void Map::_resize() {
//O(n)
int oldCapacity=this->_capacity;
this->_capacity*=2;
while(true){
bool ok=true;
for(int i=2;i<=sqrt(this->_capacity);i++)
if(this->_capacity%i==0){
ok=false;
break;
}
if(ok)
break;
this->_capacity++;
}
TElem* newElements=new TElem[this->_capacity];
int* newNext=new int[this->_capacity];
for(int i=0;i<this->_capacity;i++){
newElements[i]=NULL_TELEM;
newNext[i]=-1;
}
this->_nextFree=this->_capacity-1;
for(int i=0;i<oldCapacity;i++){
if(this->_elements[i]!=NULL_TELEM){
int hash=abs(this->_elements[i].first % this->_capacity);
while(newNext[hash]!=-1)
hash=this->_next[hash];
if(newElements[hash]==NULL_TELEM){
newElements[hash]=this->_elements[i];
newNext[hash]=-1;
}
else{
newElements[this->_nextFree]=this->_elements[i];
newNext[hash]=this->_nextFree;
while(newElements[this->_nextFree]!=NULL_TELEM)
this->_nextFree--;
}
}
}
delete[] this->_elements;
delete[] this->_next;
this->_elements=newElements;
this->_next=newNext;
}
int Map::getKeyRange() const{
//O(n)
MapIterator it = this->iterator();
if (!it.valid())
return -1;
int min = it.getCurrent().first;
int max = it.getCurrent().first;
while (it.valid()) {
if (it.getCurrent().first < min)
min = it.getCurrent().first;
if (it.getCurrent().first > max)
max = it.getCurrent().first;
it.next();
}
return max - min;
}
@@ -0,0 +1,66 @@
#pragma once
#include <utility>
using namespace std;
//DO NOT INCLUDE MAPITERATOR
//DO NOT CHANGE THIS PART
typedef int TKey;
typedef int TValue;
typedef std::pair<TKey, TValue> TElem;
#define NULL_TVALUE -111111
#define NULL_TELEM pair<TKey, TValue>(-111111, -111111)
#define ALPHA 0.75
#define INITIAL_CAPACITY 2300000
class MapIterator;
class Map {
//DO NOT CHANGE THIS PART
friend class MapIterator;
private:
//TODO - Representation
TElem* _elements;
int* _next;
int _size;
int _capacity;
int _nextFree;
void _resize();
public:
// implicit constructor
Map();
// 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 does 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;
//returns an iterator for the map
MapIterator iterator() const;
int getKeyRange() const;
// destructor
~Map();
};
@@ -0,0 +1,55 @@
#include "Map.h"
#include "MapIterator.h"
#include <exception>
using namespace std;
MapIterator::MapIterator(const Map& d) : map(d)
{
//O(1)
this->_current=0;
while (this->_current<this->map._capacity && this->map._elements[this->_current]==NULL_TELEM)
{
this->_current++;
}
this->_first=this->_current;
}
void MapIterator::first() {
//O(1)
this->_current=this->_first;
}
void MapIterator::next() {
//O(1)
if(this->valid()){
this->_current++;
while (this->_current<this->map._capacity && this->map._elements[this->_current]==NULL_TELEM)
{
this->_current++;
}
}
else
throw exception();
}
TElem MapIterator::getCurrent(){
//O(1)
if(this->valid())
return this->map._elements[this->_current];
throw exception();
}
bool MapIterator::valid() const {
//O(1)
if(this->_current<this->map._capacity && this->map._elements[this->_current]!=NULL_TELEM)
return true;
return false;
}
@@ -0,0 +1,21 @@
#pragma once
#include "Map.h"
class MapIterator
{
//DO NOT CHANGE THIS PART
friend class Map;
private:
const Map& map;
//TODO - Representation
int _current;
int _first;
MapIterator(const Map& m);
public:
void first();
void next();
TElem getCurrent();
bool valid() const;
};
@@ -0,0 +1,41 @@
#include "ShortTest.h"
#include <assert.h>
#include "Map.h"
#include "MapIterator.h"
void testAll() { //call each function to see if it is implemented
Map m;
assert(m.isEmpty() == true);
assert(m.size() == 0); //add elements
assert(m.add(5,5)==NULL_TVALUE);
assert(m.add(1,111)==NULL_TVALUE);
assert(m.add(10,110)==NULL_TVALUE);
assert(m.add(7,7)==NULL_TVALUE);
assert(m.add(1,1)==111);
assert(m.add(10,10)==110);
assert(m.add(-3,-3)==NULL_TVALUE);
assert(m.size() == 5);
assert(m.search(10) == 10);
assert(m.search(16) == NULL_TVALUE);
assert(m.remove(1) == 1);
assert(m.remove(6) == NULL_TVALUE);
assert(m.size() == 4);
TElem e;
MapIterator id = m.iterator();
id.first();
int s1 = 0, s2 = 0;
while (id.valid()) {
e = id.getCurrent();
s1 += e.first;
s2 += e.second;
id.next();
}
assert(s1 == 19);
assert(s2 == 19);
}
@@ -0,0 +1,4 @@
#pragma once
void testAll();