From 03b857ec45ffe3d3c183e165b235e92e18ccffab Mon Sep 17 00:00:00 2001 From: Olga Zaykova Date: Fri, 9 Oct 2020 14:17:13 +0300 Subject: [PATCH 1/4] add solution --- module-1/homework/BigInteger/biginteger.cpp | 297 ++++++++++++++++++++ module-1/homework/BigInteger/biginteger.h | 57 ++++ 2 files changed, 354 insertions(+) create mode 100644 module-1/homework/BigInteger/biginteger.cpp create mode 100644 module-1/homework/BigInteger/biginteger.h diff --git a/module-1/homework/BigInteger/biginteger.cpp b/module-1/homework/BigInteger/biginteger.cpp new file mode 100644 index 00000000..dcbbdb08 --- /dev/null +++ b/module-1/homework/BigInteger/biginteger.cpp @@ -0,0 +1,297 @@ +#include "biginteger.h" + +void BigInteger::convertNum(std::vector &number, std::string& strNum) { + for (long long i = strNum.size(); i > 0; i -= 9) { + if (i >= 9) { + number.push_back(atoi(strNum.substr(i - 9, 9).c_str())); + } else { + number.push_back(atoi(strNum.substr(0, i).c_str())); + } + } +} + + +BigInteger::BigInteger(int integer) { + isNegative = integer < 0; + + std::string tmp = std::to_string(integer); + number.clear(); + convertNum(number, tmp); +} + +BigInteger::BigInteger(std::string num) { + if (num.size() == 0) { + isNegative = 0; + number = std::vector(1, 0); + } else { + isNegative = (num[0] == '-'); + convertNum(number, num); + } + +} + +BigInteger &BigInteger::operator=(int integer) { + isNegative = integer < 0; + + std::string tmp = std::to_string(integer); + number.clear(); + for (int i=(int)tmp.length(); i > 0; i -= 9) { + if (i < 9) { + number.push_back(atoi(tmp.substr(0, i).c_str())); + } else { + number.push_back(atoi(tmp.substr(i - 9, 9).c_str())); + } + } + return *this; +} + +BigInteger& BigInteger::operator=(const BigInteger& num) { + isNegative = num.isNegative; + number = num.number; + return *this; +} + +std::istream& operator>>(std::istream &in, BigInteger &num) { + std::string number; + in >> number; + num = BigInteger(number); + return in; +} + +std::ostream& operator<<(std::ostream &out, const BigInteger &num) { + out << num.toString(); + return out; +} + +std::string BigInteger::toString() const { + std::string res; + if (isNegative) { + res += "-"; + } + res += number.empty() ? 0 : std::to_string(number.back()); + for (int i = (int)number.size() - 2; i >= 0; --i) { + res += std::to_string(number[i]); + } + return res; +} + +const BigInteger& operator+(const BigInteger& num) { + return num; +} + +const BigInteger BigInteger::operator-() const { + BigInteger res(!isNegative, number); + return res; +} + +bool operator==(const BigInteger& left, const BigInteger& right) { + return (left.isNegative == right.isNegative && left.number == right.number); +} + +bool operator>(const BigInteger& left, const BigInteger& right) { + if (left == right) { + return false; + } + + if (left.isNegative && !right.isNegative) { + return false; + } + + if (!left.isNegative && right.isNegative) { + return true; + } + + bool allNegative = left.isNegative && right.isNegative; + + if (left.number.size() < right.number.size()) { + return !allNegative; + } + + for (int i = left.number.size() - 1; i >= 0; --i) { + if (left.number[i] < right.number[i] && allNegative) { + return true; + } + + if (left.number[i] > right.number[i] && !allNegative) { + return true; + } + } + + return false; +} + +BigInteger operator+(const BigInteger& left, const BigInteger& right) { + if (left.isNegative) { + if (right.isNegative) { + return -((-left) + (-right)); + } + return right - (-left); + } + if (right.isNegative) + { + return right + left; + } + + BigInteger res; + + int carry = 0; + for (size_t i = 0; i < std::max(left.number.size(), right.number.size()) || carry; ++i) { + if (i == res.number.size()) { + res.number.push_back(0); + } + res.number[i] = left.number[i] + carry + (i < right.number.size() ? right.number[i] : 0); + carry = res.number[i] >= BigInteger::BASIS; + if (carry) { + res.number[i] -= BigInteger::BASIS; + } + } + return res; +} + +BigInteger operator-(const BigInteger &left, const BigInteger &right) { + if (left.isNegative) { + if (right.isNegative) { + return left + (-right); + } + return -((-left) + right); + } + if (right.isNegative) { + return left + (-right); + } + if (left < right) { + return -(right - left); + } + + BigInteger res(left); + int carry = 0; + for (int i = 0; i < std::max(res.number.size(), right.number.size()) || carry != 0; i++) { + if (i == res.number.size()) { + res.number.push_back(0); + } + res.number[i] -= (i >= right.number.size() ? 0 : right.number[i]) + carry; + carry = 0; + if (res.number[i] < 0) { + res.number[i] += BigInteger::BASIS; + carry = 1; + } + } + return res; +} + +bool operator<(const BigInteger& left, const BigInteger& right) { + return !(left > right); +} + +bool operator>=(const BigInteger& left, const BigInteger& right) { + return left > right || left == right; +} + +bool operator<=(const BigInteger& left, const BigInteger& right) { + return left < right || left == right; +} + +void BigInteger::rmZero() { + while (number.size() > 1 && number.back() == 0) { + number.pop_back(); + } + if (number.size() == 1 && number[0] == 0) { + isNegative = 0; + } +} + +BigInteger operator*(const BigInteger& left, const BigInteger& right) { + BigInteger ans; + ans.number = std::vector(left.number.size() + right.number.size()); + for (size_t i = 0; i < left.number.size(); ++i) + for (int j = 0, carry = 0; j < (int)right.number.size() || carry; ++j) { + long long cur = ans.number[i + j] + left.number[i] * 1ll * (j < (int)right.number.size() + ? right.number[j] : 0) + carry; + ans.number[i+j] = int (cur % BigInteger::BASIS); + carry = int (cur / BigInteger::BASIS); + } + while (ans.number.size() > 1 && ans.number.back() == 0) { + ans.number.pop_back(); + } + return ans; +} + +BigInteger operator/(const BigInteger& left, const BigInteger& right) { + BigInteger ans, cur; + ans.number = std::vector(left.number.size(), 0); + for (long long i = ans.number.size() - 1; i >= 0; i--) { + cur.number = std::vector(1, 0); + cur.number[0] = left.number[i]; + cur.rmZero(); + int l = 0, r = BigInteger::BASIS + 1; + while (l + 1 != r) { + int m = (l + r) / 2; + if (BigInteger(m) * right <= cur) { + l = m; + } else { + r = m; + } + } + ans.number[i] = l; + cur -= right * BigInteger(l); + } + ans.rmZero(); + return ans; +} + +BigInteger operator%(const BigInteger& left, const BigInteger& right) { + if (left.isNegative) { + return -((-left) % right); + } + return left - right * (left / right); +} + +BigInteger& BigInteger::operator%=(const BigInteger &num) { + *this = *this % num; + return *this; +} + +BigInteger& BigInteger::operator/=(const BigInteger &num) { + *this = *this / num; + return *this; +} + +BigInteger& BigInteger::operator*=(const BigInteger &num) { + *this = *this * num; + return *this; +} + +BigInteger& BigInteger::operator+=(const BigInteger &num) { + *this = *this + num; + return *this; +} + +BigInteger& BigInteger::operator-=(const BigInteger &num) { + *this = *this - num; + return *this; +} + +BigInteger::operator bool() const { + return !(*this == BigInteger(0)); +} + +BigInteger& BigInteger::operator++() { + *this = *this + BigInteger(1); + return *this; +} + +const BigInteger BigInteger::operator++(int) { + BigInteger tmp = BigInteger(*this); + *this = *this + BigInteger(1); + return tmp; +} + +BigInteger& BigInteger::operator--() { + *this = *this - BigInteger(1); + return *this; +} + +const BigInteger BigInteger::operator--(int) { + BigInteger tmp = BigInteger(*this); + *this = *this - BigInteger(1); + return tmp; +} \ No newline at end of file diff --git a/module-1/homework/BigInteger/biginteger.h b/module-1/homework/BigInteger/biginteger.h new file mode 100644 index 00000000..d910a1f9 --- /dev/null +++ b/module-1/homework/BigInteger/biginteger.h @@ -0,0 +1,57 @@ +#pragma once + +#include +#include +#include + + +class BigInteger { +public: + static const int BASIS = 1000 * 1000 * 1000; + + BigInteger (int integer); + BigInteger (std::string num); + BigInteger (bool negativity, std::vector num) : number(std::move(num)), isNegative(negativity) {}; + BigInteger (const BigInteger& cur) : isNegative(cur.isNegative), number(cur.number){}; + BigInteger() = default; + std::string toString() const; + + friend std::ostream& operator<<(std::ostream &out, const BigInteger &num); + friend std::istream& operator>>(std::istream &in, BigInteger &num); + + friend BigInteger operator+(const BigInteger& left, const BigInteger& right); + friend BigInteger operator-(const BigInteger& left, const BigInteger& right); + friend BigInteger operator/(const BigInteger& left, const BigInteger& right); + friend BigInteger operator*(const BigInteger& left, const BigInteger& right); + friend BigInteger operator%(const BigInteger& left, const BigInteger& right); + friend bool operator==(const BigInteger& left, const BigInteger& right); + friend bool operator>(const BigInteger& left, const BigInteger& right); + friend bool operator<(const BigInteger& left, const BigInteger& right); + friend bool operator>=(const BigInteger& left, const BigInteger& right); + friend bool operator<=(const BigInteger& left, const BigInteger& right); + + + BigInteger& operator=(const BigInteger& num); + BigInteger& operator=(const int num); + BigInteger& operator+=(const BigInteger& num); + BigInteger& operator-=(const BigInteger& num); + BigInteger& operator*=(const BigInteger& num); + BigInteger& operator/=(const BigInteger& num); + BigInteger& operator%=(const BigInteger& num); + + BigInteger& operator++(); + const BigInteger operator++(int); + BigInteger& operator--(); + const BigInteger operator--(int); + const BigInteger operator-() const; + + operator bool() const; +private: + void rmZero(); + + bool isNegative = false; + std::vector number; + + void convertNum(std::vector& number, std::string& strNum); +}; + From c8d746f7378c8624d8967287d3b2574536c5e3f0 Mon Sep 17 00:00:00 2001 From: Olga Zaykova Date: Sun, 18 Oct 2020 22:47:22 +0300 Subject: [PATCH 2/4] list realization --- module-1/homework/List/list.cpp | 250 +++++++++++++++++++++++++++++++- module-1/homework/List/list.h | 18 ++- 2 files changed, 263 insertions(+), 5 deletions(-) diff --git a/module-1/homework/List/list.cpp b/module-1/homework/List/list.cpp index 15252d3e..4bb88cc1 100644 --- a/module-1/homework/List/list.cpp +++ b/module-1/homework/List/list.cpp @@ -1,3 +1,251 @@ #include "list.h" +#include +#include -// Your code goes here... \ No newline at end of file +namespace task{ + list& list::operator=(const task::list &other) { + clear(); + auto tmp = other; + while (tmp.head_ != tmp.tail_) { + push_back(tmp.head_->val); + tmp.head_ = tmp.head_->next; + } + push_back(tmp.head_->val); + return *this; + } + + list::list(const task::list& other) { + size_ = 0; + auto tmp = other.head_; + while (tmp!= other.tail_) { + push_back(tmp->val); + tmp = tmp->next; + } + push_back(tmp->val); + } + + list::list(task::list &&tmp) { + size_ = tmp.size_; + head_ = tmp.head_; + tail_ = tmp.tail_; + tmp.head_ = nullptr; + tmp.tail_ = nullptr; + tmp.size_ = 0; + } + + list& list::operator=(task::list &&tmp) { + clear(); + size_ = tmp.size_; + head_ = tmp.head_; + size_ = tmp.size_; + tmp.head_ = nullptr; + tmp.tail_ = nullptr; + tmp.size_ = 0; + return *this; + } + + list::list() { + size_ = 0; + head_ = nullptr; + tail_ = nullptr; + } + + list::list(size_t count, const int &value) : size_(0){ + for (size_t i = 0; i < count; i++) { + push_back(value); + } + } + + list::~list() { + while (head_ != tail_) { + ListNode* a = head_; + head_ = head_->next; + size_--; + delete a; + } + delete head_; + } + + const int& list::front() const { + return head_->val; + } + + int& list::front() { + return head_->val; + } + + const int& list::back() const { + return tail_->val; + } + + int& list::back() { + return tail_->val; + } + + bool list::empty() const { + return size_ == 0; + } + + size_t list::size() const { + return size_; + } + + + void list::clear() { + while (size_ != 0) { + pop_back(); + } + } + + void list::push_back(const int &value) { + ListNode* a = new ListNode(value); + a->prev = tail_; + if (tail_ != nullptr) { + tail_->next = a; + } + tail_ = a; + if (size_ == 0) { + head_ = a; + } + size_++; + } + + void list::push_front(const int &value) { + ListNode* a = new ListNode(0); + a->val = value; + a->next = head_; + if (head_ != nullptr) { + head_->prev = a; + } + head_ = a; + if (size_ == 0) { + tail_ = a; + } + size_++; + } + + void list::pop_back() { + ListNode* a = tail_; + if (tail_->prev != nullptr) { + tail_->prev->next = nullptr; + } + tail_ = tail_->prev; + if (size_ == 1) { + head_ = nullptr; + } + delete a; + size_--; + } + + void list::pop_front() { + ListNode* a = head_; + if (head_->next != nullptr) { + head_->next->prev = nullptr; + } + head_ = head_->next; + if (size_ == 1) { + tail_ = nullptr; + } + delete a; + size_--; + } + + void list::resize(size_t count) { + if (count > size_) { + while (size_ != count) { + push_back(0); + } + } + } + + void list::sort() { + ListNode* tmp = head_; + std::vector order; + while (tmp != tail_) { + order.push_back(tmp->val); + tmp = tmp->next; + } + order.push_back(tail_->val); + std::sort(order.begin(), order.end()); + size_t i = 0; + tmp = head_; + while (tmp != nullptr) { + tmp->val = order[i]; + ++i; + tmp = tmp->next; + } + } + + void list::remove_node_(task::ListNode *node) { + auto tmp = head_; + while (tmp != nullptr && tmp != node) { + tmp = tmp->next; + } + + if (tmp == head_ && tmp == tail_) { + size_ = 0; + head_ = nullptr; + tail_ = nullptr; + + delete tmp; + return; + } + + if (tmp == head_) { + auto next = head_->next; + next->prev = nullptr; + head_ = next; + } else { + if (tmp == tail_) { + auto prev = tail_->prev; + prev->next = nullptr; + tail_ = prev; + } else { + auto next = tmp->next; + auto prev = tmp->prev; + prev->next = next; + next->prev = prev; + } + } + + size_--; + delete tmp; + return; + } + + void list::swap(task::list &other) { + std::swap(size_, other.size_); + std::swap(head_, other.head_); + std::swap(tail_, other.tail_); + } + + + void list::remove(const int value) { + auto tmp = head_; + while (tmp != nullptr) { + if(tmp->val == value) { + remove_node_(tmp); + tmp = head_; + continue; + } + tmp = tmp->next; + } + } + + void list::unique() { + std::set list_values; + auto tmp = head_; + while (tmp != nullptr) { + if (list_values.find(tmp->val) != list_values.end()) { + auto replace = tmp->prev; + remove_node_(tmp); + tmp = replace; + } else { + list_values.insert(tmp->val); + } + tmp = tmp->next; + } + } + + +} \ No newline at end of file diff --git a/module-1/homework/List/list.h b/module-1/homework/List/list.h index a5cbaa35..36c570a0 100644 --- a/module-1/homework/List/list.h +++ b/module-1/homework/List/list.h @@ -4,16 +4,23 @@ namespace task { + struct ListNode { + int val; + ListNode* next = nullptr; + ListNode* prev = nullptr; + ListNode(int elem): val(elem) {} + }; class list { - public: - list(); list(size_t count, const int& value = int()); + list(const list& tmp); + list(list&& tmp); ~list(); list& operator=(const list& other); + list& operator=(list&& other); int& front(); @@ -36,15 +43,18 @@ class list { void swap(list& other); - void remove(const int& value); + void remove(const int value); void unique(); void sort(); // Your code goes here?.. private: + size_t size_; + ListNode* head_ = nullptr; + ListNode* tail_ = nullptr; - // Your code goes here... + void remove_node_(ListNode* node); }; From 7e4530f4e31742252c8af8e9840abbc245ed9137 Mon Sep 17 00:00:00 2001 From: CardinalPrefect Date: Tue, 15 Dec 2020 12:44:21 +0300 Subject: [PATCH 3/4] Fixing bug in comparison test --- module-1/homework/BigInteger/tests.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/module-1/homework/BigInteger/tests.cpp b/module-1/homework/BigInteger/tests.cpp index 59d22015..bbd32180 100644 --- a/module-1/homework/BigInteger/tests.cpp +++ b/module-1/homework/BigInteger/tests.cpp @@ -62,7 +62,7 @@ TEST(Arithmetic, Test1) { bigint_a /= 42; a /= 42; - + bigint_a %= 100; a %= 100; @@ -74,7 +74,7 @@ TEST(Arithmetic, Test1) { TEST(TypeCast, Test1) { BigInteger bigint_val = 42; ASSERT_TRUE(bool(bigint_val)); - + bigint_val = 0; ASSERT_FALSE(bool(bigint_val)); } @@ -93,8 +93,7 @@ TEST(InStream, Test1) { TEST(Comparison, Test1) { - std::istringstream iss("9325492345983274589758023847509283745827349587212938129" - "348762838512387487213648172639471269348172397461297"); + std::istringstream iss("9325492345983274589758023847509283745827349587212938129 348762838512387487213648172639471269348172397461297"); BigInteger a; BigInteger b; iss >> a >> b; @@ -107,4 +106,4 @@ TEST(Comparison, Test1) { int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); -} \ No newline at end of file +} From c05806e281503df9316e0f7c584565008d171240 Mon Sep 17 00:00:00 2001 From: Olga Zaykova Date: Wed, 16 Dec 2020 22:48:22 +0300 Subject: [PATCH 4/4] TypeList implementation --- module-1/homework/TypeList/.idea/TypeList.iml | 2 ++ module-1/homework/TypeList/.idea/misc.xml | 7 +++++++ module-1/homework/TypeList/.idea/modules.xml | 8 ++++++++ module-1/homework/TypeList/.idea/vcs.xml | 6 ++++++ .../homework/TypeList/typelist/CMakeLists.txt | 2 +- module-1/homework/TypeList/typelist/append.h | 17 +++++++++++++++- module-1/homework/TypeList/typelist/erase.h | 17 +++++++++++++++- .../homework/TypeList/typelist/eraseall.h | 14 ++++++++++++- module-1/homework/TypeList/typelist/indexof.h | 20 ++++++++++++++++++- module-1/homework/TypeList/typelist/length.h | 16 +++++++++++++-- .../homework/TypeList/typelist/noduplicates.h | 10 +++++++++- module-1/homework/TypeList/typelist/replace.h | 16 +++++++++++++-- module-1/homework/TypeList/typelist/typeat.h | 13 +++++++++++- .../homework/TypeList/typelist/typelist.h | 10 +++++++--- 14 files changed, 144 insertions(+), 14 deletions(-) create mode 100644 module-1/homework/TypeList/.idea/TypeList.iml create mode 100644 module-1/homework/TypeList/.idea/misc.xml create mode 100644 module-1/homework/TypeList/.idea/modules.xml create mode 100644 module-1/homework/TypeList/.idea/vcs.xml diff --git a/module-1/homework/TypeList/.idea/TypeList.iml b/module-1/homework/TypeList/.idea/TypeList.iml new file mode 100644 index 00000000..f08604bb --- /dev/null +++ b/module-1/homework/TypeList/.idea/TypeList.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/module-1/homework/TypeList/.idea/misc.xml b/module-1/homework/TypeList/.idea/misc.xml new file mode 100644 index 00000000..8822db8f --- /dev/null +++ b/module-1/homework/TypeList/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/module-1/homework/TypeList/.idea/modules.xml b/module-1/homework/TypeList/.idea/modules.xml new file mode 100644 index 00000000..9ed454b5 --- /dev/null +++ b/module-1/homework/TypeList/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/module-1/homework/TypeList/.idea/vcs.xml b/module-1/homework/TypeList/.idea/vcs.xml new file mode 100644 index 00000000..c2365ab1 --- /dev/null +++ b/module-1/homework/TypeList/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/module-1/homework/TypeList/typelist/CMakeLists.txt b/module-1/homework/TypeList/typelist/CMakeLists.txt index e8d48ec9..3b7a2a46 100644 --- a/module-1/homework/TypeList/typelist/CMakeLists.txt +++ b/module-1/homework/TypeList/typelist/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.16) +cmake_minimum_required(VERSION 3.14) project("runner") diff --git a/module-1/homework/TypeList/typelist/append.h b/module-1/homework/TypeList/typelist/append.h index ac395d4b..264d64e2 100644 --- a/module-1/homework/TypeList/typelist/append.h +++ b/module-1/homework/TypeList/typelist/append.h @@ -3,4 +3,19 @@ #include "typelist.h" template -struct Append; \ No newline at end of file +struct Append; + +template +struct Append { + using NewTypeList = TypeList::NewTypeList>; +}; + +template +struct Append { + using NewTypeList = TypeList; +}; + +template<> +struct Append { + using NewTypeList = NullType; +}; diff --git a/module-1/homework/TypeList/typelist/erase.h b/module-1/homework/TypeList/typelist/erase.h index 7f33b81c..a9050c2f 100644 --- a/module-1/homework/TypeList/typelist/erase.h +++ b/module-1/homework/TypeList/typelist/erase.h @@ -3,4 +3,19 @@ #include "typelist.h" template -struct Erase; \ No newline at end of file +struct Erase; + +template +struct Erase { + using NewTypeList = TypeList::NewTypeList>; +}; + +template +struct Erase, TargetType> { + using NewTypeList = Tail; +}; + +template +struct Erase { + using NewTypeList = NullType; +}; \ No newline at end of file diff --git a/module-1/homework/TypeList/typelist/eraseall.h b/module-1/homework/TypeList/typelist/eraseall.h index e395227c..d96b8698 100644 --- a/module-1/homework/TypeList/typelist/eraseall.h +++ b/module-1/homework/TypeList/typelist/eraseall.h @@ -3,4 +3,16 @@ #include "typelist.h" template -struct EraseAll; \ No newline at end of file +struct EraseAll { + using NewTypeList = TypeList::NewTypeList>; +}; + +template +struct EraseAll, TargetType> { + using NewTypeList = typename EraseAll::NewTypeList; +}; + +template +struct EraseAll { + using NewTypeList = NullType; +}; \ No newline at end of file diff --git a/module-1/homework/TypeList/typelist/indexof.h b/module-1/homework/TypeList/typelist/indexof.h index e1c34184..93d9e12b 100644 --- a/module-1/homework/TypeList/typelist/indexof.h +++ b/module-1/homework/TypeList/typelist/indexof.h @@ -3,4 +3,22 @@ #include "typelist.h" template -struct IndexOf; \ No newline at end of file +struct IndexOf; + +template +struct IndexOf { // 404 not found :( + static const int pos = -1; +}; + +template +struct IndexOf, TargetType> { + static const int pos = 0; +}; + +template +struct IndexOf, TargetType> { +private: + static const int tmp = IndexOf::Tail , TargetType>::pos; +public: + static const int pos = tmp == -1 ? -1 : 1 + tmp; +}; \ No newline at end of file diff --git a/module-1/homework/TypeList/typelist/length.h b/module-1/homework/TypeList/typelist/length.h index 2f45c74b..43514993 100644 --- a/module-1/homework/TypeList/typelist/length.h +++ b/module-1/homework/TypeList/typelist/length.h @@ -2,5 +2,17 @@ #include "typelist.h" -template -struct Length; \ No newline at end of file +template +struct Length { + static const int length = 1; +}; + +template +struct Length > { + static const int length = 1 + Length::Tail>::length; +}; + +template <> +struct Length { + static const int length = 0; +}; diff --git a/module-1/homework/TypeList/typelist/noduplicates.h b/module-1/homework/TypeList/typelist/noduplicates.h index c0c696b2..e887dbc7 100644 --- a/module-1/homework/TypeList/typelist/noduplicates.h +++ b/module-1/homework/TypeList/typelist/noduplicates.h @@ -4,4 +4,12 @@ #include "typelist.h" template -struct NoDuplicates; \ No newline at end of file +struct NoDuplicates { + using NewTypeList = TypeList::NewTypeList>::NewTypeList>; +}; + +template<> +struct NoDuplicates { + using NewTypeList = NullType; +}; \ No newline at end of file diff --git a/module-1/homework/TypeList/typelist/replace.h b/module-1/homework/TypeList/typelist/replace.h index 392595b7..0a8e71a8 100644 --- a/module-1/homework/TypeList/typelist/replace.h +++ b/module-1/homework/TypeList/typelist/replace.h @@ -2,5 +2,17 @@ #include "typelist.h" -template -struct Replace; \ No newline at end of file +template +struct Replace { + using NewTypeList = TypeList::NewTypeList>; +}; + +template +struct Replace, OldType, NewType> { + using NewTypeList = TypeList::NewTypeList>; +}; + +template +struct Replace { + using NewTypeList = NullType; +}; \ No newline at end of file diff --git a/module-1/homework/TypeList/typelist/typeat.h b/module-1/homework/TypeList/typelist/typeat.h index 805f391c..2830516a 100644 --- a/module-1/homework/TypeList/typelist/typeat.h +++ b/module-1/homework/TypeList/typelist/typeat.h @@ -3,4 +3,15 @@ #include "typelist.h" template -struct TypeAt; \ No newline at end of file +struct TypeAt; + +template + +struct TypeAt, index> { + typedef typename TypeAt::Tail, index - 1>::TargetType TargetType; +}; + +template +struct TypeAt, 0> { + typedef typename TypeList::Head TargetType; +}; diff --git a/module-1/homework/TypeList/typelist/typelist.h b/module-1/homework/TypeList/typelist/typelist.h index aeb91be8..d0849d5c 100644 --- a/module-1/homework/TypeList/typelist/typelist.h +++ b/module-1/homework/TypeList/typelist/typelist.h @@ -1,6 +1,10 @@ #pragma once -template -struct TypeList; +struct NullType { +}; -struct NullType {}; \ No newline at end of file +template +struct TypeList { + using Head = head; + using Tail = tail; +}; \ No newline at end of file