From ca04ca08786c5ac214ce75249df63266ab33064d Mon Sep 17 00:00:00 2001 From: mille-nium Date: Sun, 15 Apr 2018 16:35:54 +0300 Subject: [PATCH 1/5] Implemented 'List' class --- lib/list.cpp | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/list.h | 18 ++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 lib/list.cpp create mode 100644 lib/list.h diff --git a/lib/list.cpp b/lib/list.cpp new file mode 100644 index 0000000..1d9aa77 --- /dev/null +++ b/lib/list.cpp @@ -0,0 +1,69 @@ +#include +#include "list.h" + +struct list_node { + int *slice; + list_node *next; +}; + + +list::list() { + head = NULL; + tail = NULL; + this->length = 0; +} + +void list::push(int *slice) { + list_node* temp = new list_node; + temp->slice = slice; + temp->next = NULL; + + if (!head) { + head = temp; + tail = temp; + } else { + tail->next = temp; + tail = tail->next; + } + + this->length++; +} + +void list::pop() { + if (!head) return; + + list_node *temp = head; + + while (temp->next->next != tail) temp = temp->next; + + delete temp->next->slice; + delete temp->next; + + tail = temp; + tail->next = NULL; + + this->length--; +} + +void list::clear() { + list_node *temp; + temp = head; + + while (head) { + temp = head; + head = head->next; + delete temp; + } + + this->length = 0; +} + +int *list::get(int slice_number) { + list_node *temp = head; + + for (int i = 0; i < slice_number; ++i) { + temp = temp->next; + } + + return temp->slice; +} diff --git a/lib/list.h b/lib/list.h new file mode 100644 index 0000000..e1a39bc --- /dev/null +++ b/lib/list.h @@ -0,0 +1,18 @@ +#ifndef list_h +#define list_h + +struct list_node; + +class list { +private: + list_node *head, *tail; +public: + list(); + int length; + void push(int *); + void pop(); + void clear(); + int *get(int); +}; + +#endif From 94296f3420abbfe177c1bd814ce638eed6061920 Mon Sep 17 00:00:00 2001 From: mille-nium Date: Mon, 16 Apr 2018 12:05:29 +0300 Subject: [PATCH 2/5] Implement `some` Implemented #9 --- example/array.usage.cpp | 5 ++++- lib/array.cpp | 17 +++++++++++++++++ lib/array.h | 1 + 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/example/array.usage.cpp b/example/array.usage.cpp index 8c93dc7..7c31091 100644 --- a/example/array.usage.cpp +++ b/example/array.usage.cpp @@ -6,7 +6,10 @@ using namespace std; int main() { Array a (10); - for (int i = 0; i < 1000; ++i) a[i] = i; + for (int i = 0; i < 100; ++i) a[i] = i; cout << a << endl; + cout << a.some([](int item) { return item == 100; }) << endl; + cout << a.filter([](int item) { return item % 10 == 0; }) << endl; + cout << a.map([](int item) { return item % 2; }) << endl; } diff --git a/lib/array.cpp b/lib/array.cpp index 2ad00de..6e54015 100644 --- a/lib/array.cpp +++ b/lib/array.cpp @@ -111,6 +111,23 @@ Array Array::filter(bool (*fn)(int)) { return filtered; } + +// Check, whether array contains +// appropriate element +// fn - function, which returns +// true or false +// +bool Array::some(bool (*fn)(int)) { + for (int i = 0; i < this->length; ++i) { + int item = this->get(i); + int some = fn(item); + if (some) return 1; + } + + return 0; +} + + // Create string from elements of array // separated by specified separator // diff --git a/lib/array.h b/lib/array.h index 522b67a..4713b8c 100644 --- a/lib/array.h +++ b/lib/array.h @@ -20,6 +20,7 @@ class Array { int pop(); Array map(int (*)(int)); Array filter(bool (*)(int)); + bool some(bool (*)(int)); std::string join(const char*) const; std::string to_string() const; From ddd34428b74671c03d4588afdb5bc79b5f28431e Mon Sep 17 00:00:00 2001 From: mille-nium Date: Mon, 16 Apr 2018 21:21:04 +0300 Subject: [PATCH 3/5] Implement `every` Implemented #10 --- example/array.usage.cpp | 1 + lib/array.cpp | 17 ++++++++++++++++- lib/array.h | 1 + 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/example/array.usage.cpp b/example/array.usage.cpp index 7c31091..ad65d19 100644 --- a/example/array.usage.cpp +++ b/example/array.usage.cpp @@ -9,6 +9,7 @@ int main() { for (int i = 0; i < 100; ++i) a[i] = i; cout << a << endl; + cout << a.every([](int item) { return item < 100; }) << endl; cout << a.some([](int item) { return item == 100; }) << endl; cout << a.filter([](int item) { return item % 10 == 0; }) << endl; cout << a.map([](int item) { return item % 2; }) << endl; diff --git a/lib/array.cpp b/lib/array.cpp index 6e54015..2aef5cd 100644 --- a/lib/array.cpp +++ b/lib/array.cpp @@ -112,11 +112,26 @@ Array Array::filter(bool (*fn)(int)) { } +// Check, whether all of elements +// in array satisfy requirement +// fn - function, which returns +// true or false +// +bool Array::every(bool (*fn)(int)) { + for (int i = 0; i < this->length; ++i) { + int item = this->get(i); + int satisfies = fn(item); + if (!satisfies) return 0; + } + + return 1; +} + // Check, whether array contains // appropriate element // fn - function, which returns // true or false -// +// bool Array::some(bool (*fn)(int)) { for (int i = 0; i < this->length; ++i) { int item = this->get(i); diff --git a/lib/array.h b/lib/array.h index 4713b8c..a577af5 100644 --- a/lib/array.h +++ b/lib/array.h @@ -20,6 +20,7 @@ class Array { int pop(); Array map(int (*)(int)); Array filter(bool (*)(int)); + bool every(bool (*)(int)); bool some(bool (*)(int)); std::string join(const char*) const; std::string to_string() const; From 9763181055cbcdbeccf4631b16f065dccd49defa Mon Sep 17 00:00:00 2001 From: mille-nium Date: Mon, 16 Apr 2018 22:50:43 +0300 Subject: [PATCH 4/5] Implement `forEach` Implemented #12 --- example/array.usage.cpp | 1 + lib/array.cpp | 13 +++++++++++++ lib/array.h | 1 + 3 files changed, 15 insertions(+) diff --git a/example/array.usage.cpp b/example/array.usage.cpp index ad65d19..b95db2a 100644 --- a/example/array.usage.cpp +++ b/example/array.usage.cpp @@ -9,6 +9,7 @@ int main() { for (int i = 0; i < 100; ++i) a[i] = i; cout << a << endl; + a.forEach([](int item) { cout << item << endl; }); cout << a.every([](int item) { return item < 100; }) << endl; cout << a.some([](int item) { return item == 100; }) << endl; cout << a.filter([](int item) { return item % 10 == 0; }) << endl; diff --git a/lib/array.cpp b/lib/array.cpp index 2aef5cd..6597d7c 100644 --- a/lib/array.cpp +++ b/lib/array.cpp @@ -112,6 +112,19 @@ Array Array::filter(bool (*fn)(int)) { } +// Execute a provided function for +// each element in array +// fn - function to be applied +// to each element +// +void Array::forEach(void (*fn)(int)) { + for (int i = 0; i < this->length; ++i) { + int item = this->get(i); + fn(item); + } +} + + // Check, whether all of elements // in array satisfy requirement // fn - function, which returns diff --git a/lib/array.h b/lib/array.h index a577af5..091c738 100644 --- a/lib/array.h +++ b/lib/array.h @@ -20,6 +20,7 @@ class Array { int pop(); Array map(int (*)(int)); Array filter(bool (*)(int)); + void forEach(void (*)(int)); bool every(bool (*)(int)); bool some(bool (*)(int)); std::string join(const char*) const; From 827dfdaf0975b148789b436a8e72a090ccd9d775 Mon Sep 17 00:00:00 2001 From: mille-nium Date: Tue, 17 Apr 2018 00:47:58 +0300 Subject: [PATCH 5/5] Implement `forEach` Implemented #12 --- lib/array.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/array.h b/lib/array.h index 1929e7e..8e12939 100644 --- a/lib/array.h +++ b/lib/array.h @@ -136,6 +136,19 @@ class Array { }; + // Execute a provided function for + // each element in array + // fn - function to be applied + // to each element + // + void forEach(void (*fn)(T)) { + for (int i = 0; i < this->length; ++i) { + T item = this->get(i); + fn(item); + } + }; + + // Create string from elements of array // separated by specified separator //