From b71f5e3732bd914a58661ee30ccd6d5e880f2d7c Mon Sep 17 00:00:00 2001 From: balint Date: Mon, 6 May 2019 20:52:52 +0200 Subject: [PATCH 01/20] date class added --- course03/meeting02/balint_hw/main.cpp | 110 ++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 course03/meeting02/balint_hw/main.cpp diff --git a/course03/meeting02/balint_hw/main.cpp b/course03/meeting02/balint_hw/main.cpp new file mode 100644 index 0000000..9976f6c --- /dev/null +++ b/course03/meeting02/balint_hw/main.cpp @@ -0,0 +1,110 @@ +# include + +class Date +{ + private: int y,m,d; + bool yearvalid(){ + if (y>2500 || y<1600) { + return false;} + return true;} + bool monthvalid(){ + if (m<0 || m>12){return false;} + return true; + } + bool isleapyear(int y){ + if (y %4 != 0){return false;} + if (y %100 !=0) {return true;} + if (y%400 !=0) {return false;} + return true; + } + int maxDayInMonth(int y, int m){ + if (m==1 || m==3 || m==5 || m==7|| m==8||m==10 || m==12){return 31;} + if (m==4 || m==6|| m==9 || m==11){return 30;} + if (isleapyear(y)){return 29;} + return 28; + } + int dayDiff(Date first, Date second){ + if (first==second){return 0;} + if (!(firstmaxDayInMonth(y,m)) {return false;} + return true; + } + int getY() const {return y;} + int getM() const {return m;} + int getD() const {return d;} + + bool operator==(Date other){ + if (y==other.getY() && m==other.getM() && d==other.getD()){return true;} + return false; + } + bool operator<(Date other){ + if (y>other.getY()){ return false;} + if (yother.getM()){ return false;} + if (m Date: Mon, 6 May 2019 21:23:06 +0200 Subject: [PATCH 02/20] suggested changes --- course03/meeting02/balint_hw/main.cpp | 99 +++++++++++++++------------ 1 file changed, 56 insertions(+), 43 deletions(-) diff --git a/course03/meeting02/balint_hw/main.cpp b/course03/meeting02/balint_hw/main.cpp index 9976f6c..d42f15c 100644 --- a/course03/meeting02/balint_hw/main.cpp +++ b/course03/meeting02/balint_hw/main.cpp @@ -1,29 +1,46 @@ -# include +#include class Date { private: int y,m,d; - bool yearvalid(){ - if (y>2500 || y<1600) { - return false;} - return true;} - bool monthvalid(){ - if (m<0 || m>12){return false;} - return true; + + bool yearvalid() const + { + return y>=1600 && y<=2500; } - bool isleapyear(int y){ + + bool monthvalid() const { + return m >= 1 && m <= 12; + } + static bool isleapyear(int y) { if (y %4 != 0){return false;} if (y %100 !=0) {return true;} if (y%400 !=0) {return false;} return true; } - int maxDayInMonth(int y, int m){ - if (m==1 || m==3 || m==5 || m==7|| m==8||m==10 || m==12){return 31;} - if (m==4 || m==6|| m==9 || m==11){return 30;} - if (isleapyear(y)){return 29;} - return 28; + int maxDayInMonth(int y, int m) const{ + switch(m) + { + case 1: + case 3: + case 5: + case 7: + case 8: + case 10: + case 12: + return 31; + case 4: + case 6: + case 9: + case 11: + return 30; + case 2: + return isleapyear(y) ? 29 : 28; + default: + throw std::runtime_error("Invalid month value"); + }; } - int dayDiff(Date first, Date second){ + int dayDiff(Date first, Date second) const { if (first==second){return 0;} if (!(firstmaxDayInMonth(y,m)) {return false;} return true; @@ -60,39 +77,34 @@ class Date int getM() const {return m;} int getD() const {return d;} - bool operator==(Date other){ - if (y==other.getY() && m==other.getM() && d==other.getD()){return true;} - return false; + bool operator==(const Date& other) const { + return std::tie(y, m, d) == std::tie(other.y, other.m, other.d); } - bool operator<(Date other){ - if (y>other.getY()){ return false;} - if (yother.getM()){ return false;} - if (m Date: Tue, 7 May 2019 09:00:03 +0200 Subject: [PATCH 03/20] moved hw to the right directory --- course03/{meeting02 => homework00}/balint_hw/main.cpp | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename course03/{meeting02 => homework00}/balint_hw/main.cpp (100%) diff --git a/course03/meeting02/balint_hw/main.cpp b/course03/homework00/balint_hw/main.cpp similarity index 100% rename from course03/meeting02/balint_hw/main.cpp rename to course03/homework00/balint_hw/main.cpp From 1c5362b8366093019e4e2b090b2b48014edb40f1 Mon Sep 17 00:00:00 2001 From: balint Date: Tue, 7 May 2019 21:20:44 +0200 Subject: [PATCH 04/20] formatting --- course03/homework00/balint_hw/main.cpp | 226 +++++++++++++------------ 1 file changed, 117 insertions(+), 109 deletions(-) diff --git a/course03/homework00/balint_hw/main.cpp b/course03/homework00/balint_hw/main.cpp index d42f15c..99c8c53 100644 --- a/course03/homework00/balint_hw/main.cpp +++ b/course03/homework00/balint_hw/main.cpp @@ -1,123 +1,131 @@ #include -class Date -{ - private: int y,m,d; +class Date { +private: + int y, m, d; - bool yearvalid() const - { - return y>=1600 && y<=2500; - } + bool yearvalid() const { return y >= 1600 && y <= 2500; } - bool monthvalid() const { - return m >= 1 && m <= 12; - } - static bool isleapyear(int y) { - if (y %4 != 0){return false;} - if (y %100 !=0) {return true;} - if (y%400 !=0) {return false;} - return true; - } - int maxDayInMonth(int y, int m) const{ - switch(m) - { - case 1: - case 3: - case 5: - case 7: - case 8: - case 10: - case 12: - return 31; - case 4: - case 6: - case 9: - case 11: - return 30; - case 2: - return isleapyear(y) ? 29 : 28; - default: - throw std::runtime_error("Invalid month value"); - }; - } - int dayDiff(Date first, Date second) const { - if (first==second){return 0;} - if (!(first= 1 && m <= 12; } + static bool isleapyear(int y) { + if (y % 4 != 0) { + return false; + } + if (y % 100 != 0) { + return true; + } + if (y % 400 != 0) { + return false; + } + return true; + } + int maxDayInMonth(int y, int m) const { + switch (m) { + case 1: + case 3: + case 5: + case 7: + case 8: + case 10: + case 12: + return 31; + case 4: + case 6: + case 9: + case 11: + return 30; + case 2: + return isleapyear(y) ? 29 : 28; + default: + throw std::runtime_error("Invalid month value"); + }; + } + int dayDiff(Date first, Date second) const { + if (first == second) { + return 0; + } + if (!(first < second)) { + return -1 * dayDiff(second, first); + } - Date dayCnt = first.nextDay(); - int cnt = 1; - while (dayCntmaxDayInMonth(y,m)) {return false;} - return true; - } - int getY() const {return y;} - int getM() const {return m;} - int getD() const {return d;} + Date dayCnt = first.nextDay(); + int cnt = 1; + while (dayCnt < second) { + cnt++; + dayCnt = dayCnt.nextDay(); + } + return cnt; + } - bool operator==(const Date& other) const { - return std::tie(y, m, d) == std::tie(other.y, other.m, other.d); - } - bool operator<(const Date& other) const - { - return std::tie(y, m, d) < std::tie(other.y, other.m, other.d); - } - int daysSinceEpoch() const { - return dayDiff({1970, 1, 1}, *this); - } +public: + Date(int y, int m, int d) : y(y), m(m), d(d){}; + Date nextDay() const { + Date firstguess = {y, m, d + 1}; + if (!firstguess.isValid()) { + firstguess = {y, m + 1, 1}; + if (!firstguess.isValid()) { + firstguess = {y + 1, 1, 1}; + } + } + return firstguess; + } + bool isValid() const { + if (!yearvalid() || !monthvalid()) { + return false; + } + if (d < 1 || d > maxDayInMonth(y, m)) { + return false; + } + return true; + } + int getY() const { return y; } + int getM() const { return m; } + int getD() const { return d; } - std::string dayOfWeek() const { - if (!isValid()){ - throw std::runtime_error("dayOfWeek() called while !isValid()"); - } - int offset = (((daysSinceEpoch() %7) +7) %7); - switch(offset) - { - case 0: return "Thursday"; - case 1: return "Friday"; - case 2: return "Saturday"; - case 3: return "Sunday"; - case 4: return "Monday"; - case 5: return "Tuesday"; - case 6: return "Wednesday"; - default: - throw std::logic_error("Shouldn't happen"); - } - } + bool operator==(const Date &other) const { + return std::tie(y, m, d) == std::tie(other.y, other.m, other.d); + } + bool operator<(const Date &other) const { + return std::tie(y, m, d) < std::tie(other.y, other.m, other.d); + } + int daysSinceEpoch() const { return dayDiff({1970, 1, 1}, *this); } + + std::string dayOfWeek() const { + if (!isValid()) { + throw std::runtime_error("dayOfWeek() called while !isValid()"); + } + int offset = (((daysSinceEpoch() % 7) + 7) % 7); + switch (offset) { + case 0: + return "Thursday"; + case 1: + return "Friday"; + case 2: + return "Saturday"; + case 3: + return "Sunday"; + case 4: + return "Monday"; + case 5: + return "Tuesday"; + case 6: + return "Wednesday"; + default: + throw std::logic_error("Shouldn't happen"); + } + } }; -std::ostream& operator<<(std::ostream &strm, const Date &a) { - return strm << a.getY()<<"-"< Date: Sun, 12 May 2019 17:47:41 +0200 Subject: [PATCH 05/20] homework1_halfway_working_laptop_commit --- course03/homework01/balint/main.cpp | 43 +++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 course03/homework01/balint/main.cpp diff --git a/course03/homework01/balint/main.cpp b/course03/homework01/balint/main.cpp new file mode 100644 index 0000000..b37a761 --- /dev/null +++ b/course03/homework01/balint/main.cpp @@ -0,0 +1,43 @@ +#include +class LinkedListInt{ +public: + bool isEmpty() const { + std::cout<<"element is: "<(i)),next(std::make_unique(l)) {}; + LinkedListInt(int i, LinkedListInt l): elem(std::make_unique(i)),next(std::make_unique(emptyList())) {}; + + LinkedListInt(int i): elem(std::make_unique(i)),next(std::make_unique()) { + std::cout<<"element is: "< elem; + std::unique_ptr next; + +}; + +int main(){ + LinkedListInt test =LinkedListInt(); + if (test.isEmpty()){std::cout<<"test is empty"< Date: Mon, 13 May 2019 21:59:19 +0200 Subject: [PATCH 06/20] some progress with the LinkedListInt --- course03/homework01/balint/main.cpp | 78 +++++++++++++++++++---------- 1 file changed, 52 insertions(+), 26 deletions(-) diff --git a/course03/homework01/balint/main.cpp b/course03/homework01/balint/main.cpp index b37a761..743bdff 100644 --- a/course03/homework01/balint/main.cpp +++ b/course03/homework01/balint/main.cpp @@ -1,43 +1,69 @@ #include -class LinkedListInt{ +#include + +class LinkedListInt { public: - bool isEmpty() const { - std::cout<<"element is: "<(i)),next(std::make_unique(l)) {}; - LinkedListInt(int i, LinkedListInt l): elem(std::make_unique(i)),next(std::make_unique(emptyList())) {}; + LinkedListInt(int i, LinkedListInt &l) { + elem = std::make_unique(i); + if (!*(l.next)) { + next = std::make_unique(); + } else { + next = std::make_unique(*(l.elem), *(l.next)); + } + }; + + LinkedListInt(int i) { + elem = std::make_unique(i); + next = std::make_unique(); + }; - LinkedListInt(int i): elem(std::make_unique(i)),next(std::make_unique()) { - std::cout<<"element is: "< car(){return std::make_unique(*elem);} + + operator bool() const {return isEmpty();} - LinkedListInt(): elem(),next() {}; private: std::unique_ptr elem; std::unique_ptr next; +}; +LinkedListInt static cons(int i, LinkedListInt &l) { + LinkedListInt retval = LinkedListInt(i, l); + return retval; }; -int main(){ - LinkedListInt test =LinkedListInt(); - if (test.isEmpty()){std::cout<<"test is empty"< car(LinkedListInt l){return l.car();} - LinkedListInt test2 =3; - if (test2.isEmpty()){std::cout<<"test2 is empty"< Date: Wed, 15 May 2019 22:32:02 +0200 Subject: [PATCH 07/20] cleanup, copy/= constructor, destructor --- course03/homework01/balint/main.cpp | 73 ++++++++++++++++++++++++++--- 1 file changed, 66 insertions(+), 7 deletions(-) diff --git a/course03/homework01/balint/main.cpp b/course03/homework01/balint/main.cpp index 743bdff..33fe7fa 100644 --- a/course03/homework01/balint/main.cpp +++ b/course03/homework01/balint/main.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -11,10 +12,10 @@ class LinkedListInt { LinkedListInt(int i, LinkedListInt &l) { elem = std::make_unique(i); - if (!*(l.next)) { - next = std::make_unique(); - } else { + if (l) { next = std::make_unique(*(l.elem), *(l.next)); + } else { + next = std::make_unique(); } }; @@ -24,22 +25,69 @@ class LinkedListInt { }; LinkedListInt() : elem(), next(){}; - std::unique_ptr car(){return std::make_unique(*elem);} + std::unique_ptr car() { return std::make_unique(*elem); } - operator bool() const {return isEmpty();} + operator bool() const { return isEmpty(); } + + LinkedListInt cad() { + if (isEmpty()) { + return emptyList(); + } + if (next->isEmpty()) { + return emptyList(); + } else { + // return LinkedListInt(*this->car(), *((*(next)).next)); + return LinkedListInt(*next->car(), *((*(next)).next)); + } + } + + LinkedListInt &operator=(const LinkedListInt &other) { + + if (other) { + auto newelem = std::make_unique(*other.elem); + elem = std::move(newelem); + if (*(other.next)) { + auto newnext = std::make_unique(*(*other.next).elem, + *(*other.next).next); + next = std::move(newnext); + } + } + return *this; + } + LinkedListInt(const LinkedListInt &other) { + if (other) { + // LinkedListInt(*(other.elem), *(other.next)) + std::unique_ptr newelem = std::make_unique(*other.elem); + elem = std::move(newelem); + + if (*(other.next)) { + auto newnext = std::make_unique(*(*other.next).elem, + *(*other.next).next); + next = std::move(newnext); + } else { + auto newnext = std::make_unique(); + next = std::move(newnext); + } + } + } + ~LinkedListInt(){ + elem.reset(); + next.reset(); + } private: std::unique_ptr elem; std::unique_ptr next; }; -LinkedListInt static cons(int i, LinkedListInt &l) { +LinkedListInt cons(int i, LinkedListInt &l) { LinkedListInt retval = LinkedListInt(i, l); return retval; }; -std::unique_ptr car(LinkedListInt l){return l.car();} +std::unique_ptr car(LinkedListInt l) { return l.car(); } +LinkedListInt cad(LinkedListInt &l) { return l.cad(); } int main() { LinkedListInt test = LinkedListInt(); if (test.isEmpty()) { @@ -58,6 +106,8 @@ int main() { } LinkedListInt test3 = {4, test2}; + std::cout << "test3 is empty" << std::endl; + if (test3.isEmpty()) { std::cout << "test3 is empty" << std::endl; } @@ -65,5 +115,14 @@ int main() { std::cout << "test3 is NOT empty" << std::endl; } + // LinkedListInt test4 = cad(test3); + LinkedListInt test4 = test3.cad(); + if (test4.isEmpty()) { + std::cout << "test4 is empty" << std::endl; + } + if (!test4.isEmpty()) { + std::cout << "test4 is NOT empty" << std::endl; + } + return 0; } From 0b166b07e4ac2673963a66c7b4bc6d6be634521f Mon Sep 17 00:00:00 2001 From: balint Date: Wed, 15 May 2019 22:54:01 +0200 Subject: [PATCH 08/20] push back --- course03/homework01/balint/main.cpp | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/course03/homework01/balint/main.cpp b/course03/homework01/balint/main.cpp index 33fe7fa..ee76c6b 100644 --- a/course03/homework01/balint/main.cpp +++ b/course03/homework01/balint/main.cpp @@ -29,7 +29,7 @@ class LinkedListInt { operator bool() const { return isEmpty(); } - LinkedListInt cad() { + LinkedListInt cad() const { if (isEmpty()) { return emptyList(); } @@ -87,7 +87,20 @@ LinkedListInt cons(int i, LinkedListInt &l) { std::unique_ptr car(LinkedListInt l) { return l.car(); } -LinkedListInt cad(LinkedListInt &l) { return l.cad(); } +LinkedListInt cad(const LinkedListInt &l) { return l.cad(); } + +LinkedListInt pushBack(LinkedListInt& orig, int newelem){ + if (orig){ + LinkedListInt tmp1 = cad(orig); + LinkedListInt tmp2 = pushBack(tmp1, newelem); + + return LinkedListInt(*car(orig), tmp2); + } + else{ + return LinkedListInt(newelem); + } +} + int main() { LinkedListInt test = LinkedListInt(); if (test.isEmpty()) { @@ -124,5 +137,13 @@ int main() { std::cout << "test4 is NOT empty" << std::endl; } + LinkedListInt test5 = pushBack(test3, 99); + if (test5.isEmpty()) { + std::cout << "test5 is empty" << std::endl; + } + if (!test5.isEmpty()) { + std::cout << "test5 is NOT empty" << std::endl; + } + return 0; } From 693d7613cae95010fa76bc516fa9575cc91197c2 Mon Sep 17 00:00:00 2001 From: balint Date: Wed, 22 May 2019 21:28:12 +0200 Subject: [PATCH 09/20] attempt at a better linked list class running into the same issues as before --- course03/homework01/balint/CMakeLists.txt | 10 ++ course03/homework01/balint/llist.cpp | 69 +++++++++ course03/homework01/balint/main.cpp | 175 ++++------------------ course03/homework01/balint/oldmain.cpp | 149 ++++++++++++++++++ course03/homework01/balint/tests.cpp | 77 ++++++++++ 5 files changed, 338 insertions(+), 142 deletions(-) create mode 100644 course03/homework01/balint/CMakeLists.txt create mode 100644 course03/homework01/balint/llist.cpp create mode 100644 course03/homework01/balint/oldmain.cpp create mode 100644 course03/homework01/balint/tests.cpp diff --git a/course03/homework01/balint/CMakeLists.txt b/course03/homework01/balint/CMakeLists.txt new file mode 100644 index 0000000..2e3413e --- /dev/null +++ b/course03/homework01/balint/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.1) + +find_package(GTest REQUIRED) +include_directories(${GTEST_INCLUDE_DIRS}) + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -g -Wall") +add_executable(main main.cpp) + +add_executable(runTests tests.cpp) +target_link_libraries(runTests ${GTEST_LIBRARIES} pthread) diff --git a/course03/homework01/balint/llist.cpp b/course03/homework01/balint/llist.cpp new file mode 100644 index 0000000..c253ec5 --- /dev/null +++ b/course03/homework01/balint/llist.cpp @@ -0,0 +1,69 @@ +#include +#include +#include + +struct Node { + int elem; + std::unique_ptr next; + + Node() : elem(), next(){}; + Node(int i) : elem(i), next(){}; + + ~Node() { next.reset(); } + Node(const Node &other) { + if (other.elem) { + elem = other.elem; + } + if (other.next) { + next = std::make_unique(*other.next); + } + } + Node &operator=(const Node &other) { + if (other.elem) { + elem = other.elem; + } + if (other.next) { + next = std::make_unique(*other.next); + } + return *this; + } +}; + +typedef std::unique_ptr Lst; + +class LLint { +public: + operator bool() const { return list ? list->elem ? true : false : false; } + ~LLint() { list.reset(); } + LLint(const LLint &other) { list = std::make_unique(*other.list); } + LLint &operator=(const LLint &other) { + list = other.list ? std::make_unique( *other.list ): nullptr; + return *this; + } + LLint(Lst& lst) { list = lst ? std::make_unique(*lst): std::make_unique(*lst) ; } + LLint(Node n) { list = std::make_unique(n); } + Node copyNode(){return Node(*list);} + Node copyNodeNext(){return Node(*( *list ).next);} + + Lst list; +}; + +int car(LLint& l){ + if ( l ) { + return (*l.list).elem;} + else{ + throw std::runtime_error("Car called on empty list."); + } +} +LLint cdr(LLint& l){ + if (l){ + return LLint(l.list->next); + } + else{ + return LLint(Node()); + } +} +LLint cad(LLint l){ + // return LLint(*(*l.list).next); + return LLint(l.copyNodeNext()); +} diff --git a/course03/homework01/balint/main.cpp b/course03/homework01/balint/main.cpp index ee76c6b..29afa0d 100644 --- a/course03/homework01/balint/main.cpp +++ b/course03/homework01/balint/main.cpp @@ -1,149 +1,40 @@ -#include -#include -#include +#include "llist.cpp" -class LinkedListInt { -public: - bool isEmpty() const { return elem ? false : true; } - LinkedListInt static emptyList() { - LinkedListInt retval; - return retval; - }; - - LinkedListInt(int i, LinkedListInt &l) { - elem = std::make_unique(i); - if (l) { - next = std::make_unique(*(l.elem), *(l.next)); - } else { - next = std::make_unique(); - } - }; - - LinkedListInt(int i) { - elem = std::make_unique(i); - next = std::make_unique(); - }; - - LinkedListInt() : elem(), next(){}; - std::unique_ptr car() { return std::make_unique(*elem); } - - operator bool() const { return isEmpty(); } - - LinkedListInt cad() const { - if (isEmpty()) { - return emptyList(); - } - if (next->isEmpty()) { - return emptyList(); - } else { - // return LinkedListInt(*this->car(), *((*(next)).next)); - return LinkedListInt(*next->car(), *((*(next)).next)); - } - } - - LinkedListInt &operator=(const LinkedListInt &other) { - - if (other) { - auto newelem = std::make_unique(*other.elem); - elem = std::move(newelem); - if (*(other.next)) { - auto newnext = std::make_unique(*(*other.next).elem, - *(*other.next).next); - next = std::move(newnext); - } - } - return *this; - } - LinkedListInt(const LinkedListInt &other) { - if (other) { - // LinkedListInt(*(other.elem), *(other.next)) - std::unique_ptr newelem = std::make_unique(*other.elem); - elem = std::move(newelem); - - if (*(other.next)) { - auto newnext = std::make_unique(*(*other.next).elem, - *(*other.next).next); - next = std::move(newnext); - } else { - auto newnext = std::make_unique(); - next = std::move(newnext); - } - } - } - ~LinkedListInt(){ - elem.reset(); - next.reset(); - } - -private: - std::unique_ptr elem; - std::unique_ptr next; -}; - -LinkedListInt cons(int i, LinkedListInt &l) { - LinkedListInt retval = LinkedListInt(i, l); - return retval; -}; - -std::unique_ptr car(LinkedListInt l) { return l.car(); } - -LinkedListInt cad(const LinkedListInt &l) { return l.cad(); } - -LinkedListInt pushBack(LinkedListInt& orig, int newelem){ - if (orig){ - LinkedListInt tmp1 = cad(orig); - LinkedListInt tmp2 = pushBack(tmp1, newelem); - - return LinkedListInt(*car(orig), tmp2); - } - else{ - return LinkedListInt(newelem); - } -} +// TESTS int main() { - LinkedListInt test = LinkedListInt(); - if (test.isEmpty()) { - std::cout << "test is empty" << std::endl; - } - if (!test.isEmpty()) { - std::cout << "test is NOT empty" << std::endl; - } - - LinkedListInt test2 = 3; - if (test2.isEmpty()) { - std::cout << "test2 is empty" << std::endl; - } - if (!test2.isEmpty()) { - std::cout << "test2 is NOT empty" << std::endl; - } - - LinkedListInt test3 = {4, test2}; - std::cout << "test3 is empty" << std::endl; - - if (test3.isEmpty()) { - std::cout << "test3 is empty" << std::endl; - } - if (!test3) { - std::cout << "test3 is NOT empty" << std::endl; - } - - // LinkedListInt test4 = cad(test3); - LinkedListInt test4 = test3.cad(); - if (test4.isEmpty()) { - std::cout << "test4 is empty" << std::endl; - } - if (!test4.isEmpty()) { - std::cout << "test4 is NOT empty" << std::endl; + int i; + Node n = {3}; + Node m = {4}; + Lst tmp = std::make_unique(m); + Lst tmp2 = std::make_unique(n); + Node m2 = m; + Node m3 = Node(m); + // m2.next = std::move(m3); + m2.next = std::make_unique(m3); + LLint ls1 = LLint(m3); + LLint ls3 = LLint(n); + LLint ls2 = ls1; + ls2.list->next = std::move(ls3.list); + // LLint ls2_cad = cad(ls2); + ls2 = ls3; + // std::cout<<"car of ls2: "< +#include +#include + +class LinkedListInt { +public: + bool isEmpty() const { return elem ? false : true; } + LinkedListInt static emptyList() { + LinkedListInt retval; + return retval; + }; + + LinkedListInt(int i, LinkedListInt &l) { + elem = std::make_unique(i); + if (l) { + next = std::make_unique(*(l.elem), *(l.next)); + } else { + next = std::make_unique(); + } + }; + + LinkedListInt(int i) { + elem = std::make_unique(i); + next = std::make_unique(); + }; + + LinkedListInt() : elem(), next(){}; + std::unique_ptr car() { return std::make_unique(*elem); } + + operator bool() const { return isEmpty(); } + + LinkedListInt cad() const { + if (isEmpty()) { + return emptyList(); + } + if (next->isEmpty()) { + return emptyList(); + } else { + // return LinkedListInt(*this->car(), *((*(next)).next)); + return LinkedListInt(*next->car(), *((*(next)).next)); + } + } + + LinkedListInt &operator=(const LinkedListInt &other) { + + if (other) { + auto newelem = std::make_unique(*other.elem); + elem = std::move(newelem); + if (*(other.next)) { + auto newnext = std::make_unique(*(*other.next).elem, + *(*other.next).next); + next = std::move(newnext); + } + } + return *this; + } + LinkedListInt(const LinkedListInt &other) { + if (other) { + // LinkedListInt(*(other.elem), *(other.next)) + std::unique_ptr newelem = std::make_unique(*other.elem); + elem = std::move(newelem); + + if (*(other.next)) { + auto newnext = std::make_unique(*(*other.next).elem, + *(*other.next).next); + next = std::move(newnext); + } else { + auto newnext = std::make_unique(); + next = std::move(newnext); + } + } + } + ~LinkedListInt(){ + elem.reset(); + next.reset(); + } + +private: + std::unique_ptr elem; + std::unique_ptr next; +}; + +LinkedListInt cons(int i, LinkedListInt &l) { + LinkedListInt retval = LinkedListInt(i, l); + return retval; +}; + +std::unique_ptr car(LinkedListInt l) { return l.car(); } + +LinkedListInt cad(const LinkedListInt &l) { return l.cad(); } + +LinkedListInt pushBack(LinkedListInt& orig, int newelem){ + if (orig){ + LinkedListInt tmp1 = cad(orig); + LinkedListInt tmp2 = pushBack(tmp1, newelem); + + return LinkedListInt(*car(orig), tmp2); + } + else{ + return LinkedListInt(newelem); + } +} + +int main() { + LinkedListInt test = LinkedListInt(); + if (test.isEmpty()) { + std::cout << "test is empty" << std::endl; + } + if (!test.isEmpty()) { + std::cout << "test is NOT empty" << std::endl; + } + + LinkedListInt test2 = 3; + if (test2.isEmpty()) { + std::cout << "test2 is empty" << std::endl; + } + if (!test2.isEmpty()) { + std::cout << "test2 is NOT empty" << std::endl; + } + + LinkedListInt test3 = {4, test2}; + std::cout << "test3 is empty" << std::endl; + + if (test3.isEmpty()) { + std::cout << "test3 is empty" << std::endl; + } + if (!test3) { + std::cout << "test3 is NOT empty" << std::endl; + } + + // LinkedListInt test4 = cad(test3); + LinkedListInt test4 = test3.cad(); + if (test4.isEmpty()) { + std::cout << "test4 is empty" << std::endl; + } + if (!test4.isEmpty()) { + std::cout << "test4 is NOT empty" << std::endl; + } + + LinkedListInt test5 = pushBack(test3, 99); + if (test5.isEmpty()) { + std::cout << "test5 is empty" << std::endl; + } + if (!test5.isEmpty()) { + std::cout << "test5 is NOT empty" << std::endl; + } + + return 0; +} diff --git a/course03/homework01/balint/tests.cpp b/course03/homework01/balint/tests.cpp new file mode 100644 index 0000000..30f530e --- /dev/null +++ b/course03/homework01/balint/tests.cpp @@ -0,0 +1,77 @@ +#include "llist.cpp" +#include "gtest/gtest.h" + + +TEST(NodeTest, ReadingOut){ + Node m = {3}; + ASSERT_EQ(3, m.elem); + Node mcopy = m; + ASSERT_EQ(3, mcopy.elem); + Node m2 = {4}; + ASSERT_EQ(4, m2.elem); + mcopy = m2; + ASSERT_EQ(4, mcopy.elem); + Node m3 = Node(m2); + ASSERT_EQ(4, m3.elem); +} + +TEST(LLintTest, Creating){ + Node m = {3}; + LLint l1 = m; + ASSERT_EQ(3, (*l1.list).elem); + LLint l2 = l1; + ASSERT_EQ(3, (*l2.list).elem); +} + +TEST(LLintTest, cars){ + Node m = {3}; + LLint l1 = m; + ASSERT_EQ(3, car(l1)); +} + +TEST(LLintTest, CarOnEmptyThrows){ + Node m; + LLint l = m; + try { + int i = car(l); + } + catch(std::runtime_error const & err){ + EXPECT_EQ(err.what(), std::string("Car called on empty list.")); + } +} +TEST(LLintTest, cdrOnEmpty){ + Node m; + LLint l = m; + LLint cdrOfL = cdr(l); + int i=99; + if (cdrOfL){ + i=-1; + } + else{ + i=42; + } + + ASSERT_EQ(i, 42); + +} +// TEST(LLintTest, cdrOnOneElement){ +// Node m {3}; +// LLint l = m; +// LLint cdrOfL = cdr(l); +// int i=99; +// if (cdrOfL.list){ +// i=-1; +// } +// else{ +// i=42; +// } + +// ASSERT_EQ(i, 42); + +// } + + +int main(int argc, char **argv) { + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} From 00ece303d5263391750ef33ba273a349df0dc7ac Mon Sep 17 00:00:00 2001 From: balint Date: Wed, 22 May 2019 21:30:54 +0200 Subject: [PATCH 10/20] llist.cpp: empty list creation - tests pass --- course03/homework01/balint/llist.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/course03/homework01/balint/llist.cpp b/course03/homework01/balint/llist.cpp index c253ec5..3033cd1 100644 --- a/course03/homework01/balint/llist.cpp +++ b/course03/homework01/balint/llist.cpp @@ -40,7 +40,7 @@ class LLint { list = other.list ? std::make_unique( *other.list ): nullptr; return *this; } - LLint(Lst& lst) { list = lst ? std::make_unique(*lst): std::make_unique(*lst) ; } + LLint(Lst& lst) { list = lst ? std::make_unique(*lst): nullptr ; } LLint(Node n) { list = std::make_unique(n); } Node copyNode(){return Node(*list);} Node copyNodeNext(){return Node(*( *list ).next);} From a1b9314c06cc4e3b86285313db96eaf09b9267fa Mon Sep 17 00:00:00 2001 From: balint Date: Wed, 22 May 2019 21:43:47 +0200 Subject: [PATCH 11/20] added cons --- course03/homework01/balint/llist.cpp | 14 +++++++++----- course03/homework01/balint/tests.cpp | 20 +++++++------------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/course03/homework01/balint/llist.cpp b/course03/homework01/balint/llist.cpp index 3033cd1..12d0e1c 100644 --- a/course03/homework01/balint/llist.cpp +++ b/course03/homework01/balint/llist.cpp @@ -40,22 +40,23 @@ class LLint { list = other.list ? std::make_unique( *other.list ): nullptr; return *this; } - LLint(Lst& lst) { list = lst ? std::make_unique(*lst): nullptr ; } + LLint(const Lst& lst) { list = lst ? std::make_unique(*lst): nullptr ; } LLint(Node n) { list = std::make_unique(n); } Node copyNode(){return Node(*list);} Node copyNodeNext(){return Node(*( *list ).next);} + static LLint emptyList(){return LLint(nullptr);} Lst list; }; -int car(LLint& l){ +int car(const LLint& l){ if ( l ) { return (*l.list).elem;} else{ throw std::runtime_error("Car called on empty list."); } } -LLint cdr(LLint& l){ +LLint cdr(const LLint& l){ if (l){ return LLint(l.list->next); } @@ -63,7 +64,10 @@ LLint cdr(LLint& l){ return LLint(Node()); } } -LLint cad(LLint l){ +LLint cons(int i, LLint l){ // return LLint(*(*l.list).next); - return LLint(l.copyNodeNext()); + Node firstNode = Node(i); + Lst restOfList = l.list ? std::make_unique(*l.list): nullptr ; + firstNode.next = std::move(restOfList); + return firstNode; } diff --git a/course03/homework01/balint/tests.cpp b/course03/homework01/balint/tests.cpp index 30f530e..4a565ef 100644 --- a/course03/homework01/balint/tests.cpp +++ b/course03/homework01/balint/tests.cpp @@ -54,21 +54,15 @@ TEST(LLintTest, cdrOnEmpty){ ASSERT_EQ(i, 42); } -// TEST(LLintTest, cdrOnOneElement){ -// Node m {3}; -// LLint l = m; -// LLint cdrOfL = cdr(l); -// int i=99; -// if (cdrOfL.list){ -// i=-1; -// } -// else{ -// i=42; -// } +TEST(LLintTest, consReading){ -// ASSERT_EQ(i, 42); + LLint l = cons(1, cons(2, cons(3, cons(4, LLint::emptyList())))); + ASSERT_EQ(car(l), 1); + ASSERT_EQ(car(cdr(l)), 2); + ASSERT_EQ(car(cdr(cdr(l))), 3); + ASSERT_EQ(car(cdr(cdr(cdr(l)))), 4); -// } +} int main(int argc, char **argv) { From 38ee7722429ebee47cd1b3365c6c528e6aac4b99 Mon Sep 17 00:00:00 2001 From: balint Date: Wed, 22 May 2019 21:47:11 +0200 Subject: [PATCH 12/20] nth added --- course03/homework01/balint/llist.cpp | 12 ++++++++++++ course03/homework01/balint/tests.cpp | 9 +++++++++ 2 files changed, 21 insertions(+) diff --git a/course03/homework01/balint/llist.cpp b/course03/homework01/balint/llist.cpp index 12d0e1c..2bffa8b 100644 --- a/course03/homework01/balint/llist.cpp +++ b/course03/homework01/balint/llist.cpp @@ -71,3 +71,15 @@ LLint cons(int i, LLint l){ firstNode.next = std::move(restOfList); return firstNode; } + +int nth(const LLint& l, int i){ + if (i<0){ + throw std::runtime_error("Cant index with negative index"); + } + if (i==0){ + return car(l); + } + else{ + return nth(cdr(l), i-1); + } +} diff --git a/course03/homework01/balint/tests.cpp b/course03/homework01/balint/tests.cpp index 4a565ef..443211e 100644 --- a/course03/homework01/balint/tests.cpp +++ b/course03/homework01/balint/tests.cpp @@ -63,6 +63,15 @@ TEST(LLintTest, consReading){ ASSERT_EQ(car(cdr(cdr(cdr(l)))), 4); } +TEST(LLintTest, nth){ + + LLint l = cons(1, cons(2, cons(3, cons(4, LLint::emptyList())))); + ASSERT_EQ(nth(l, 0), 1); + ASSERT_EQ(nth(l, 1), 2); + ASSERT_EQ(nth(l, 2), 3); + ASSERT_EQ(nth(l, 3), 4); + +} int main(int argc, char **argv) { From 42df345328ad55518b715c6c89a246e6ee9bd8e1 Mon Sep 17 00:00:00 2001 From: balint Date: Wed, 22 May 2019 21:49:36 +0200 Subject: [PATCH 13/20] formatting --- course03/homework01/balint/llist.cpp | 40 +++++++++++----------- course03/homework01/balint/tests.cpp | 50 ++++++++++++++++------------ 2 files changed, 47 insertions(+), 43 deletions(-) diff --git a/course03/homework01/balint/llist.cpp b/course03/homework01/balint/llist.cpp index 2bffa8b..a712fe1 100644 --- a/course03/homework01/balint/llist.cpp +++ b/course03/homework01/balint/llist.cpp @@ -37,49 +37,47 @@ class LLint { ~LLint() { list.reset(); } LLint(const LLint &other) { list = std::make_unique(*other.list); } LLint &operator=(const LLint &other) { - list = other.list ? std::make_unique( *other.list ): nullptr; + list = other.list ? std::make_unique(*other.list) : nullptr; return *this; } - LLint(const Lst& lst) { list = lst ? std::make_unique(*lst): nullptr ; } + LLint(const Lst &lst) { list = lst ? std::make_unique(*lst) : nullptr; } LLint(Node n) { list = std::make_unique(n); } - Node copyNode(){return Node(*list);} - Node copyNodeNext(){return Node(*( *list ).next);} - static LLint emptyList(){return LLint(nullptr);} + Node copyNode() { return Node(*list); } + Node copyNodeNext() { return Node(*(*list).next); } + static LLint emptyList() { return LLint(nullptr); } Lst list; }; -int car(const LLint& l){ - if ( l ) { - return (*l.list).elem;} - else{ +int car(const LLint &l) { + if (l) { + return (*l.list).elem; + } else { throw std::runtime_error("Car called on empty list."); } } -LLint cdr(const LLint& l){ - if (l){ +LLint cdr(const LLint &l) { + if (l) { return LLint(l.list->next); - } - else{ + } else { return LLint(Node()); } } -LLint cons(int i, LLint l){ +LLint cons(int i, LLint l) { // return LLint(*(*l.list).next); Node firstNode = Node(i); - Lst restOfList = l.list ? std::make_unique(*l.list): nullptr ; + Lst restOfList = l.list ? std::make_unique(*l.list) : nullptr; firstNode.next = std::move(restOfList); return firstNode; } -int nth(const LLint& l, int i){ - if (i<0){ +int nth(const LLint &l, int i) { + if (i < 0) { throw std::runtime_error("Cant index with negative index"); } - if (i==0){ + if (i == 0) { return car(l); - } - else{ - return nth(cdr(l), i-1); + } else { + return nth(cdr(l), i - 1); } } diff --git a/course03/homework01/balint/tests.cpp b/course03/homework01/balint/tests.cpp index 443211e..1072efe 100644 --- a/course03/homework01/balint/tests.cpp +++ b/course03/homework01/balint/tests.cpp @@ -1,8 +1,7 @@ #include "llist.cpp" #include "gtest/gtest.h" - -TEST(NodeTest, ReadingOut){ +TEST(NodeTest, ReadingOut) { Node m = {3}; ASSERT_EQ(3, m.elem); Node mcopy = m; @@ -15,7 +14,7 @@ TEST(NodeTest, ReadingOut){ ASSERT_EQ(4, m3.elem); } -TEST(LLintTest, Creating){ +TEST(LLintTest, Creating) { Node m = {3}; LLint l1 = m; ASSERT_EQ(3, (*l1.list).elem); @@ -23,57 +22,64 @@ TEST(LLintTest, Creating){ ASSERT_EQ(3, (*l2.list).elem); } -TEST(LLintTest, cars){ +TEST(LLintTest, cars) { Node m = {3}; LLint l1 = m; ASSERT_EQ(3, car(l1)); } -TEST(LLintTest, CarOnEmptyThrows){ - Node m; +TEST(LLintTest, CarOnEmptyThrows) { + Node m; LLint l = m; try { int i = car(l); - } - catch(std::runtime_error const & err){ + i += 1; + } catch (std::runtime_error const &err) { EXPECT_EQ(err.what(), std::string("Car called on empty list.")); } } -TEST(LLintTest, cdrOnEmpty){ - Node m; +TEST(LLintTest, cdrOnEmpty) { + Node m; LLint l = m; LLint cdrOfL = cdr(l); - int i=99; - if (cdrOfL){ - i=-1; - } - else{ - i=42; + int i = 99; + if (cdrOfL) { + i = -1; + } else { + i = 42; } ASSERT_EQ(i, 42); - } -TEST(LLintTest, consReading){ +TEST(LLintTest, consReading) { LLint l = cons(1, cons(2, cons(3, cons(4, LLint::emptyList())))); ASSERT_EQ(car(l), 1); ASSERT_EQ(car(cdr(l)), 2); ASSERT_EQ(car(cdr(cdr(l))), 3); ASSERT_EQ(car(cdr(cdr(cdr(l)))), 4); - } -TEST(LLintTest, nth){ +TEST(LLintTest, nth) { LLint l = cons(1, cons(2, cons(3, cons(4, LLint::emptyList())))); ASSERT_EQ(nth(l, 0), 1); ASSERT_EQ(nth(l, 1), 2); ASSERT_EQ(nth(l, 2), 3); ASSERT_EQ(nth(l, 3), 4); - + try { + int i = nth(l, 4); + i += 1; + } catch (std::runtime_error const &err) { + EXPECT_EQ(err.what(), std::string("Car called on empty list.")); + } + try { + int i = nth(l, -1); + i += 1; + } catch (std::runtime_error const &err) { + EXPECT_EQ(err.what(), std::string("Cant index with negative index")); + } } - int main(int argc, char **argv) { testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); From 069d0420ef032bf5b124b57dabf25acc8e8ebcf0 Mon Sep 17 00:00:00 2001 From: balint Date: Wed, 22 May 2019 21:53:34 +0200 Subject: [PATCH 14/20] pushBack added --- course03/homework01/balint/llist.cpp | 10 ++++++++++ course03/homework01/balint/tests.cpp | 22 ++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/course03/homework01/balint/llist.cpp b/course03/homework01/balint/llist.cpp index a712fe1..5f8d9cd 100644 --- a/course03/homework01/balint/llist.cpp +++ b/course03/homework01/balint/llist.cpp @@ -81,3 +81,13 @@ int nth(const LLint &l, int i) { return nth(cdr(l), i - 1); } } + +LLint pushBack(const LLint& l, int i){ + if (l){ + return cons(car(l), pushBack(cdr(l), i)); + } + else{ + + return LLint(i); + } +} diff --git a/course03/homework01/balint/tests.cpp b/course03/homework01/balint/tests.cpp index 1072efe..9ddd35f 100644 --- a/course03/homework01/balint/tests.cpp +++ b/course03/homework01/balint/tests.cpp @@ -80,6 +80,28 @@ TEST(LLintTest, nth) { } } +TEST(LLintTest, pushBack) { + + LLint lshort = cons(1, cons(2, cons(3, cons(4, LLint::emptyList())))); + LLint l = pushBack(lshort, 42); + ASSERT_EQ(nth(l, 0), 1); + ASSERT_EQ(nth(l, 1), 2); + ASSERT_EQ(nth(l, 2), 3); + ASSERT_EQ(nth(l, 3), 4); + ASSERT_EQ(nth(l, 4), 42); + try { + int i = nth(l, 5); + i += 1; + } catch (std::runtime_error const &err) { + EXPECT_EQ(err.what(), std::string("Car called on empty list.")); + } + try { + int i = nth(l, -1); + i += 1; + } catch (std::runtime_error const &err) { + EXPECT_EQ(err.what(), std::string("Cant index with negative index")); + } +} int main(int argc, char **argv) { testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); From bf5523f242d4ab782222e3f622e5c7b291488de2 Mon Sep 17 00:00:00 2001 From: balint Date: Wed, 22 May 2019 21:56:14 +0200 Subject: [PATCH 15/20] len added --- course03/homework01/balint/llist.cpp | 9 +++++++++ course03/homework01/balint/tests.cpp | 10 ++++++++++ 2 files changed, 19 insertions(+) diff --git a/course03/homework01/balint/llist.cpp b/course03/homework01/balint/llist.cpp index 5f8d9cd..e6480fb 100644 --- a/course03/homework01/balint/llist.cpp +++ b/course03/homework01/balint/llist.cpp @@ -91,3 +91,12 @@ LLint pushBack(const LLint& l, int i){ return LLint(i); } } + +int len(const LLint& l){ + if (l){ + return 1 + len(cdr(l)); + } + else { + return 0; + } +} diff --git a/course03/homework01/balint/tests.cpp b/course03/homework01/balint/tests.cpp index 9ddd35f..417f579 100644 --- a/course03/homework01/balint/tests.cpp +++ b/course03/homework01/balint/tests.cpp @@ -102,6 +102,16 @@ TEST(LLintTest, pushBack) { EXPECT_EQ(err.what(), std::string("Cant index with negative index")); } } + +TEST(LLintTest, len) { + + LLint l = cons(1, cons(2, cons(3, cons(4, LLint::emptyList())))); + ASSERT_EQ(len(l), 4); + ASSERT_EQ(len(cdr(l)), 3); + ASSERT_EQ(len(cdr(cdr(l))), 2); + ASSERT_EQ(len(cdr(cdr(cdr(l)))), 1); + ASSERT_EQ(len(cdr(cdr(cdr(cdr(l))))), 0); +} int main(int argc, char **argv) { testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); From 1d9995ab0c600c24131b7f0d5bd2121789f299eb Mon Sep 17 00:00:00 2001 From: balint Date: Wed, 22 May 2019 21:57:21 +0200 Subject: [PATCH 16/20] more formatting --- course03/homework01/balint/llist.cpp | 16 +++++++--------- course03/homework01/balint/main.cpp | 13 ++++--------- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/course03/homework01/balint/llist.cpp b/course03/homework01/balint/llist.cpp index e6480fb..eb86127 100644 --- a/course03/homework01/balint/llist.cpp +++ b/course03/homework01/balint/llist.cpp @@ -82,21 +82,19 @@ int nth(const LLint &l, int i) { } } -LLint pushBack(const LLint& l, int i){ - if (l){ +LLint pushBack(const LLint &l, int i) { + if (l) { return cons(car(l), pushBack(cdr(l), i)); - } - else{ - + } else { + return LLint(i); } } -int len(const LLint& l){ - if (l){ +int len(const LLint &l) { + if (l) { return 1 + len(cdr(l)); - } - else { + } else { return 0; } } diff --git a/course03/homework01/balint/main.cpp b/course03/homework01/balint/main.cpp index 29afa0d..17f21dc 100644 --- a/course03/homework01/balint/main.cpp +++ b/course03/homework01/balint/main.cpp @@ -17,17 +17,12 @@ int main() { LLint ls2 = ls1; ls2.list->next = std::move(ls3.list); // LLint ls2_cad = cad(ls2); - ls2 = ls3; // std::cout<<"car of ls2: "< Date: Thu, 23 May 2019 21:30:45 +0200 Subject: [PATCH 17/20] even more formatting --- course03/homework01/balint/llist.cpp | 136 ++++++++++++++++----------- course03/homework01/balint/main.cpp | 16 ++-- course03/homework01/balint/tests.cpp | 124 ++++++++++++++---------- 3 files changed, 167 insertions(+), 109 deletions(-) diff --git a/course03/homework01/balint/llist.cpp b/course03/homework01/balint/llist.cpp index eb86127..a42bb6b 100644 --- a/course03/homework01/balint/llist.cpp +++ b/course03/homework01/balint/llist.cpp @@ -2,7 +2,8 @@ #include #include -struct Node { +struct Node +{ int elem; std::unique_ptr next; @@ -10,33 +11,41 @@ struct Node { Node(int i) : elem(i), next(){}; ~Node() { next.reset(); } - Node(const Node &other) { - if (other.elem) { - elem = other.elem; - } - if (other.next) { - next = std::make_unique(*other.next); - } + Node(const Node &other) + { + if (other.elem) + { + elem = other.elem; + } + if (other.next) + { + next = std::make_unique(*other.next); + } } - Node &operator=(const Node &other) { - if (other.elem) { - elem = other.elem; - } - if (other.next) { - next = std::make_unique(*other.next); - } + Node &operator=(const Node &other) + { + if (other.elem) + { + elem = other.elem; + } + if (other.next) + { + next = std::make_unique(*other.next); + } return *this; } }; typedef std::unique_ptr Lst; -class LLint { -public: +class LLint +{ + public: operator bool() const { return list ? list->elem ? true : false : false; } ~LLint() { list.reset(); } LLint(const LLint &other) { list = std::make_unique(*other.list); } - LLint &operator=(const LLint &other) { + LLint &operator=(const LLint &other) + { list = other.list ? std::make_unique(*other.list) : nullptr; return *this; } @@ -49,21 +58,30 @@ class LLint { Lst list; }; -int car(const LLint &l) { - if (l) { - return (*l.list).elem; - } else { - throw std::runtime_error("Car called on empty list."); - } +int car(const LLint &l) +{ + if (l) + { + return (*l.list).elem; + } + else + { + throw std::runtime_error("Car called on empty list."); + } } -LLint cdr(const LLint &l) { - if (l) { - return LLint(l.list->next); - } else { - return LLint(Node()); - } +LLint cdr(const LLint &l) +{ + if (l) + { + return LLint(l.list->next); + } + else + { + return LLint(Node()); + } } -LLint cons(int i, LLint l) { +LLint cons(int i, LLint l) +{ // return LLint(*(*l.list).next); Node firstNode = Node(i); Lst restOfList = l.list ? std::make_unique(*l.list) : nullptr; @@ -71,30 +89,42 @@ LLint cons(int i, LLint l) { return firstNode; } -int nth(const LLint &l, int i) { - if (i < 0) { - throw std::runtime_error("Cant index with negative index"); - } - if (i == 0) { - return car(l); - } else { - return nth(cdr(l), i - 1); - } +int nth(const LLint &l, int i) +{ + if (i < 0) + { + throw std::runtime_error("Cant index with negative index"); + } + if (i == 0) + { + return car(l); + } + else + { + return nth(cdr(l), i - 1); + } } -LLint pushBack(const LLint &l, int i) { - if (l) { - return cons(car(l), pushBack(cdr(l), i)); - } else { - - return LLint(i); - } +LLint pushBack(const LLint &l, int i) +{ + if (l) + { + return cons(car(l), pushBack(cdr(l), i)); + } + else + { + return LLint(i); + } } -int len(const LLint &l) { - if (l) { - return 1 + len(cdr(l)); - } else { - return 0; - } +int len(const LLint &l) +{ + if (l) + { + return 1 + len(cdr(l)); + } + else + { + return 0; + } } diff --git a/course03/homework01/balint/main.cpp b/course03/homework01/balint/main.cpp index 17f21dc..b6873e4 100644 --- a/course03/homework01/balint/main.cpp +++ b/course03/homework01/balint/main.cpp @@ -2,7 +2,8 @@ // TESTS -int main() { +int main() +{ int i; Node n = {3}; Node m = {4}; @@ -26,10 +27,13 @@ int main() { // Lst n; // Lst n = Node(1, Node()); // Node n = Node(); - if (ls2) { - i = 0; - } else { - i = 1; - } + if (ls2) + { + i = 0; + } + else + { + i = 1; + } std::cout << "Node is: " << i << std::endl; } diff --git a/course03/homework01/balint/tests.cpp b/course03/homework01/balint/tests.cpp index 417f579..ce1963b 100644 --- a/course03/homework01/balint/tests.cpp +++ b/course03/homework01/balint/tests.cpp @@ -1,7 +1,8 @@ -#include "llist.cpp" #include "gtest/gtest.h" +#include "llist.cpp" -TEST(NodeTest, ReadingOut) { +TEST(NodeTest, ReadingOut) +{ Node m = {3}; ASSERT_EQ(3, m.elem); Node mcopy = m; @@ -14,7 +15,8 @@ TEST(NodeTest, ReadingOut) { ASSERT_EQ(4, m3.elem); } -TEST(LLintTest, Creating) { +TEST(LLintTest, Creating) +{ Node m = {3}; LLint l1 = m; ASSERT_EQ(3, (*l1.list).elem); @@ -22,66 +24,81 @@ TEST(LLintTest, Creating) { ASSERT_EQ(3, (*l2.list).elem); } -TEST(LLintTest, cars) { +TEST(LLintTest, cars) +{ Node m = {3}; LLint l1 = m; ASSERT_EQ(3, car(l1)); } -TEST(LLintTest, CarOnEmptyThrows) { +TEST(LLintTest, CarOnEmptyThrows) +{ Node m; LLint l = m; - try { - int i = car(l); - i += 1; - } catch (std::runtime_error const &err) { - EXPECT_EQ(err.what(), std::string("Car called on empty list.")); - } + try + { + int i = car(l); + i += 1; + } + catch (std::runtime_error const &err) + { + EXPECT_EQ(err.what(), std::string("Car called on empty list.")); + } } -TEST(LLintTest, cdrOnEmpty) { +TEST(LLintTest, cdrOnEmpty) +{ Node m; LLint l = m; LLint cdrOfL = cdr(l); int i = 99; - if (cdrOfL) { - i = -1; - } else { - i = 42; - } + if (cdrOfL) + { + i = -1; + } + else + { + i = 42; + } ASSERT_EQ(i, 42); } -TEST(LLintTest, consReading) { - +TEST(LLintTest, consReading) +{ LLint l = cons(1, cons(2, cons(3, cons(4, LLint::emptyList())))); ASSERT_EQ(car(l), 1); ASSERT_EQ(car(cdr(l)), 2); ASSERT_EQ(car(cdr(cdr(l))), 3); ASSERT_EQ(car(cdr(cdr(cdr(l)))), 4); } -TEST(LLintTest, nth) { - +TEST(LLintTest, nth) +{ LLint l = cons(1, cons(2, cons(3, cons(4, LLint::emptyList())))); ASSERT_EQ(nth(l, 0), 1); ASSERT_EQ(nth(l, 1), 2); ASSERT_EQ(nth(l, 2), 3); ASSERT_EQ(nth(l, 3), 4); - try { - int i = nth(l, 4); - i += 1; - } catch (std::runtime_error const &err) { - EXPECT_EQ(err.what(), std::string("Car called on empty list.")); - } - try { - int i = nth(l, -1); - i += 1; - } catch (std::runtime_error const &err) { - EXPECT_EQ(err.what(), std::string("Cant index with negative index")); - } + try + { + int i = nth(l, 4); + i += 1; + } + catch (std::runtime_error const &err) + { + EXPECT_EQ(err.what(), std::string("Car called on empty list.")); + } + try + { + int i = nth(l, -1); + i += 1; + } + catch (std::runtime_error const &err) + { + EXPECT_EQ(err.what(), std::string("Cant index with negative index")); + } } -TEST(LLintTest, pushBack) { - +TEST(LLintTest, pushBack) +{ LLint lshort = cons(1, cons(2, cons(3, cons(4, LLint::emptyList())))); LLint l = pushBack(lshort, 42); ASSERT_EQ(nth(l, 0), 1); @@ -89,22 +106,28 @@ TEST(LLintTest, pushBack) { ASSERT_EQ(nth(l, 2), 3); ASSERT_EQ(nth(l, 3), 4); ASSERT_EQ(nth(l, 4), 42); - try { - int i = nth(l, 5); - i += 1; - } catch (std::runtime_error const &err) { - EXPECT_EQ(err.what(), std::string("Car called on empty list.")); - } - try { - int i = nth(l, -1); - i += 1; - } catch (std::runtime_error const &err) { - EXPECT_EQ(err.what(), std::string("Cant index with negative index")); - } + try + { + int i = nth(l, 5); + i += 1; + } + catch (std::runtime_error const &err) + { + EXPECT_EQ(err.what(), std::string("Car called on empty list.")); + } + try + { + int i = nth(l, -1); + i += 1; + } + catch (std::runtime_error const &err) + { + EXPECT_EQ(err.what(), std::string("Cant index with negative index")); + } } -TEST(LLintTest, len) { - +TEST(LLintTest, len) +{ LLint l = cons(1, cons(2, cons(3, cons(4, LLint::emptyList())))); ASSERT_EQ(len(l), 4); ASSERT_EQ(len(cdr(l)), 3); @@ -112,7 +135,8 @@ TEST(LLintTest, len) { ASSERT_EQ(len(cdr(cdr(cdr(l)))), 1); ASSERT_EQ(len(cdr(cdr(cdr(cdr(l))))), 0); } -int main(int argc, char **argv) { +int main(int argc, char **argv) +{ testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } From 9f2f3e88ec187b8ec199d6c31dc3855e68219ea7 Mon Sep 17 00:00:00 2001 From: balint Date: Thu, 23 May 2019 21:36:38 +0200 Subject: [PATCH 18/20] member variables are starting with m --- course03/homework01/balint/llist.cpp | 56 ++++++++++++++-------------- course03/homework01/balint/main.cpp | 6 +-- course03/homework01/balint/tests.cpp | 20 +++++----- 3 files changed, 41 insertions(+), 41 deletions(-) diff --git a/course03/homework01/balint/llist.cpp b/course03/homework01/balint/llist.cpp index a42bb6b..3ea7371 100644 --- a/course03/homework01/balint/llist.cpp +++ b/course03/homework01/balint/llist.cpp @@ -4,33 +4,33 @@ struct Node { - int elem; - std::unique_ptr next; + int melem; + std::unique_ptr mnext; - Node() : elem(), next(){}; - Node(int i) : elem(i), next(){}; + Node() : melem(), mnext(){}; + Node(int i) : melem(i), mnext(){}; - ~Node() { next.reset(); } + ~Node() { mnext.reset(); } Node(const Node &other) { - if (other.elem) + if (other.melem) { - elem = other.elem; + melem = other.melem; } - if (other.next) + if (other.mnext) { - next = std::make_unique(*other.next); + mnext = std::make_unique(*other.mnext); } } Node &operator=(const Node &other) { - if (other.elem) + if (other.melem) { - elem = other.elem; + melem = other.melem; } - if (other.next) + if (other.mnext) { - next = std::make_unique(*other.next); + mnext = std::make_unique(*other.mnext); } return *this; } @@ -41,39 +41,39 @@ typedef std::unique_ptr Lst; class LLint { public: - operator bool() const { return list ? list->elem ? true : false : false; } - ~LLint() { list.reset(); } - LLint(const LLint &other) { list = std::make_unique(*other.list); } + operator bool() const { return mlist ? mlist->melem ? true : false : false; } + ~LLint() { mlist.reset(); } + LLint(const LLint &other) { mlist = std::make_unique(*other.mlist); } LLint &operator=(const LLint &other) { - list = other.list ? std::make_unique(*other.list) : nullptr; + mlist = other.mlist ? std::make_unique(*other.mlist) : nullptr; return *this; } - LLint(const Lst &lst) { list = lst ? std::make_unique(*lst) : nullptr; } - LLint(Node n) { list = std::make_unique(n); } - Node copyNode() { return Node(*list); } - Node copyNodeNext() { return Node(*(*list).next); } + LLint(const Lst &lst) { mlist = lst ? std::make_unique(*lst) : nullptr; } + LLint(Node n) { mlist = std::make_unique(n); } + Node copyNode() { return Node(*mlist); } + Node copyNodeMnext() { return Node(*(*mlist).mnext); } static LLint emptyList() { return LLint(nullptr); } - Lst list; + Lst mlist; }; int car(const LLint &l) { if (l) { - return (*l.list).elem; + return (*l.mlist).melem; } else { - throw std::runtime_error("Car called on empty list."); + throw std::runtime_error("Car called on empty mlist."); } } LLint cdr(const LLint &l) { if (l) { - return LLint(l.list->next); + return LLint(l.mlist->mnext); } else { @@ -82,10 +82,10 @@ LLint cdr(const LLint &l) } LLint cons(int i, LLint l) { - // return LLint(*(*l.list).next); + // return LLint(*(*l.mlist).mnext); Node firstNode = Node(i); - Lst restOfList = l.list ? std::make_unique(*l.list) : nullptr; - firstNode.next = std::move(restOfList); + Lst restOfList = l.mlist ? std::make_unique(*l.mlist) : nullptr; + firstNode.mnext = std::move(restOfList); return firstNode; } diff --git a/course03/homework01/balint/main.cpp b/course03/homework01/balint/main.cpp index b6873e4..ed38b11 100644 --- a/course03/homework01/balint/main.cpp +++ b/course03/homework01/balint/main.cpp @@ -11,12 +11,12 @@ int main() Lst tmp2 = std::make_unique(n); Node m2 = m; Node m3 = Node(m); - // m2.next = std::move(m3); - m2.next = std::make_unique(m3); + // m2.mnext = std::move(m3); + m2.mnext = std::make_unique(m3); LLint ls1 = LLint(m3); LLint ls3 = LLint(n); LLint ls2 = ls1; - ls2.list->next = std::move(ls3.list); + ls2.mlist->mnext = std::move(ls3.mlist); // LLint ls2_cad = cad(ls2); // std::cout<<"car of ls2: "< Date: Thu, 23 May 2019 21:55:22 +0200 Subject: [PATCH 19/20] car now a member function --- course03/homework01/balint/llist.cpp | 27 ++++++++++++++------------- course03/homework01/balint/main.cpp | 8 ++++---- course03/homework01/balint/tests.cpp | 12 ++++++------ 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/course03/homework01/balint/llist.cpp b/course03/homework01/balint/llist.cpp index 3ea7371..e4a09f1 100644 --- a/course03/homework01/balint/llist.cpp +++ b/course03/homework01/balint/llist.cpp @@ -54,21 +54,22 @@ class LLint Node copyNode() { return Node(*mlist); } Node copyNodeMnext() { return Node(*(*mlist).mnext); } static LLint emptyList() { return LLint(nullptr); } + // int operator[](int i) const {return nth(*this, i);} + int car() const + { + if (*this) + { + return (*mlist).melem; + } + else + { + throw std::runtime_error("Car called on empty mlist."); + } + } Lst mlist; }; -int car(const LLint &l) -{ - if (l) - { - return (*l.mlist).melem; - } - else - { - throw std::runtime_error("Car called on empty mlist."); - } -} LLint cdr(const LLint &l) { if (l) @@ -97,7 +98,7 @@ int nth(const LLint &l, int i) } if (i == 0) { - return car(l); + return l.car(); } else { @@ -109,7 +110,7 @@ LLint pushBack(const LLint &l, int i) { if (l) { - return cons(car(l), pushBack(cdr(l), i)); + return cons(l.car(), pushBack(cdr(l), i)); } else { diff --git a/course03/homework01/balint/main.cpp b/course03/homework01/balint/main.cpp index ed38b11..d7b9bcb 100644 --- a/course03/homework01/balint/main.cpp +++ b/course03/homework01/balint/main.cpp @@ -18,12 +18,12 @@ int main() LLint ls2 = ls1; ls2.mlist->mnext = std::move(ls3.mlist); // LLint ls2_cad = cad(ls2); - // std::cout<<"car of ls2: "< Date: Thu, 23 May 2019 22:01:49 +0200 Subject: [PATCH 20/20] cdr is a member function now --- course03/homework01/balint/llist.cpp | 34 ++++++++++++++-------------- course03/homework01/balint/main.cpp | 4 ++-- course03/homework01/balint/tests.cpp | 16 ++++++------- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/course03/homework01/balint/llist.cpp b/course03/homework01/balint/llist.cpp index e4a09f1..45a10ce 100644 --- a/course03/homework01/balint/llist.cpp +++ b/course03/homework01/balint/llist.cpp @@ -55,32 +55,32 @@ class LLint Node copyNodeMnext() { return Node(*(*mlist).mnext); } static LLint emptyList() { return LLint(nullptr); } // int operator[](int i) const {return nth(*this, i);} - int car() const + int car() const { if (*this) { - return (*mlist).melem; + return (*mlist).melem; } else { - throw std::runtime_error("Car called on empty mlist."); + throw std::runtime_error("Car called on empty mlist."); + } + } + LLint cdr() const + { + if (*this) + { + return LLint(mlist->mnext); + } + else + { + return LLint(nullptr); } } Lst mlist; }; -LLint cdr(const LLint &l) -{ - if (l) - { - return LLint(l.mlist->mnext); - } - else - { - return LLint(Node()); - } -} LLint cons(int i, LLint l) { // return LLint(*(*l.mlist).mnext); @@ -102,7 +102,7 @@ int nth(const LLint &l, int i) } else { - return nth(cdr(l), i - 1); + return nth(l.cdr(), i - 1); } } @@ -110,7 +110,7 @@ LLint pushBack(const LLint &l, int i) { if (l) { - return cons(l.car(), pushBack(cdr(l), i)); + return cons(l.car(), pushBack(l.cdr(), i)); } else { @@ -122,7 +122,7 @@ int len(const LLint &l) { if (l) { - return 1 + len(cdr(l)); + return 1 + len(l.cdr()); } else { diff --git a/course03/homework01/balint/main.cpp b/course03/homework01/balint/main.cpp index d7b9bcb..2da18e4 100644 --- a/course03/homework01/balint/main.cpp +++ b/course03/homework01/balint/main.cpp @@ -22,8 +22,8 @@ int main() LLint consed = cons(100, cons(99, ls1)); std::cout << "First elem is: " << consed.car() << std::endl; - std::cout << "Second elem is: " << cdr(consed).car() << std::endl; - std::cout << "Third elem is: " << cdr(cdr(consed)).car() << std::endl; + std::cout << "Second elem is: " << consed.cdr().car() << std::endl; + std::cout << "Third elem is: " << consed.cdr().cdr().car() << std::endl; // Lst n; // Lst n = Node(1, Node()); // Node n = Node(); diff --git a/course03/homework01/balint/tests.cpp b/course03/homework01/balint/tests.cpp index b9a5cf4..4aa1fc4 100644 --- a/course03/homework01/balint/tests.cpp +++ b/course03/homework01/balint/tests.cpp @@ -49,7 +49,7 @@ TEST(LLintTest, cdrOnEmpty) { Node m; LLint l = m; - LLint cdrOfL = cdr(l); + LLint cdrOfL = l.cdr(); int i = 99; if (cdrOfL) { @@ -66,9 +66,9 @@ TEST(LLintTest, consReading) { LLint l = cons(1, cons(2, cons(3, cons(4, LLint::emptyList())))); ASSERT_EQ(l.car(), 1); - ASSERT_EQ(cdr(l).car(), 2); - ASSERT_EQ(cdr(cdr(l)).car(), 3); - ASSERT_EQ(cdr(cdr(cdr(l))).car(), 4); + ASSERT_EQ(l.cdr().car(), 2); + ASSERT_EQ(l.cdr().cdr().car(), 3); + ASSERT_EQ(l.cdr().cdr().cdr().car(), 4); } TEST(LLintTest, nth) { @@ -130,10 +130,10 @@ TEST(LLintTest, len) { LLint l = cons(1, cons(2, cons(3, cons(4, LLint::emptyList())))); ASSERT_EQ(len(l), 4); - ASSERT_EQ(len(cdr(l)), 3); - ASSERT_EQ(len(cdr(cdr(l))), 2); - ASSERT_EQ(len(cdr(cdr(cdr(l)))), 1); - ASSERT_EQ(len(cdr(cdr(cdr(cdr(l))))), 0); + ASSERT_EQ(len(l.cdr()), 3); + ASSERT_EQ(len(l.cdr().cdr()), 2); + ASSERT_EQ(len(l.cdr().cdr().cdr()), 1); + ASSERT_EQ(len(l.cdr().cdr().cdr().cdr()), 0); } int main(int argc, char **argv) {