From e64de09c8423d81db47f869f060d0dbda79cc7fb Mon Sep 17 00:00:00 2001 From: Brando Date: Mon, 21 Apr 2025 23:20:02 -0700 Subject: [PATCH 01/27] saving work --- bflibcpp/src/array.hpp | 228 ++++++++++++++++++++++++++++++++++++++-- bflibcpp/src/list.hpp | 85 +++++++++++++++ bflibcpp/src/string.cpp | 4 - bflibcpp/src/string.hpp | 13 ++- bflibcpp/src/vector.hpp | 20 +--- bflibcpp/todo.md | 2 + 6 files changed, 318 insertions(+), 34 deletions(-) diff --git a/bflibcpp/src/array.hpp b/bflibcpp/src/array.hpp index df20bf19..d5a7e6ac 100644 --- a/bflibcpp/src/array.hpp +++ b/bflibcpp/src/array.hpp @@ -382,12 +382,6 @@ class Array : public Vector { public: - /* - T operator[](S index) const { - return this->objectAtIndex(index); - } - */ - void operator=(const std::initializer_list & list) { this->saveArray(list); } @@ -400,6 +394,14 @@ class Array : public Vector { return *this; } + virtual T operator[](S index) const { + return this->objectAtIndex(index); + } + + virtual T & operator[](S index) { + return this->refObjectAtIndex(index); + } + // Comparators public: /** @@ -414,6 +416,220 @@ class Array : public Vector { return 0; } } + +public: + + /** + * sorts the vector in ascending order + * + * default algorithm is merge sort + */ + virtual int sort(VectorSort type = kVectorSortMerge) { + switch (type) { + case kVectorSortBubble: + return Array::sortBubble(*this); + case kVectorSortInsertion: + return Array::sortInsertion(*this); + case kVectorSortSelection: + return Array::sortSelection(*this); + case kVectorSortQuick: + return Array::sortQuick(*this); + case kVectorSortMerge: + default: + return Array::sortMerge(*this); + } + } + +private: + + /** BUBBLE SORT - START **/ + + static int sortBubble(Array & c) { + int n = c.size(); + bool swapped; + + for (int i = 0; i < n - 1; i++) { + swapped = false; + for (int j = 0; j < n - i - 1; j++) { + if (c[j] > c[j + 1]) { + BFSwap(c[j], c[j + 1]); + swapped = true; + } + } + + // If no two elements were swapped, then break + if (!swapped) + break; + } + return 0; + } + + /** BUBBLE SORT - END **/ + /** INSERTION SORT - START **/ + + static int sortInsertion(Array & c) { + int i, j; + T key; + int n = c.size(); + for (i = 1; i < n; i++) { + key = c[i]; + j = i - 1; + + // Move elements of arr[0..i-1], + // that are greater than key, to one + // position ahead of their + // current position + while (j >= 0 && c[j] > key) { + c[j + 1] = c[j]; + j = j - 1; + } + c[j + 1] = key; + } + return 0; + } + + /** INSERTION SORT - END **/ + /** SELECTION SORT - START **/ + + static int sortSelection(Array & c) { + int i, j, min_idx; + int n = c.size(); + + // One by one move boundary of + // unsorted subarray + for (i = 0; i < n-1; i++) { + // Find the minimum element in + // unsorted array + min_idx = i; + for (j = i+1; j < n; j++) + if (c[j] < c[min_idx]) + min_idx = j; + + // Swap the found minimum element + // with the first element + if (c[min_idx] != c[i]) { + BFSwap(c[min_idx], c[i]); + } + } + return 0; + } + + /** SELECTION SORT - END **/ + /** QUICK SORT - START **/ + + static int sortQuick(Array & c) { + return sortQuick(c, 0, c.size() - 1); + } + + static int sortQuick(Array & c, int low, int high) { + if (low < high) { + // pi is the partition return index of pivot + int pi = sortQuickPartition(c, low, high); + + // Recursion calls for smaller elements + // and greater or equals elements + sortQuick(c, low, pi - 1); + sortQuick(c, pi + 1, high); + } + + return 0; + } + + static int sortQuickPartition(Array & c, int low, int high) { + // Choose the pivot + T pivot = c[high]; + + // Index of smaller element and indicates + // the right position of pivot found so far + int i = low - 1; + + // Traverse arr[;ow..high] and move all smaller + // elements on left side. Elements from low to + // i are smaller after every iteration + for (int j = low; j <= high - 1; j++) { + if (c[j] < pivot) { + i++; + if (c[i] != c[j]) { + BFSwap(c[i], c[j]); + } + } + } + + // Move pivot after smaller elements and + // return its position + if (c[i + 1] != c[high]) { + BFSwap(c[i + 1], c[high]); + } + return i + 1; + } + + /** QUICK SORT - END **/ + /** MERGE SORT - START **/ + + static int sortMerge(Array & c) { + return sortMerge(c, 0, c.size() - 1); + } + + static int sortMerge(Array & c, S left, S right) { + if (left >= right) + return 0; + + S mid = left + (right - left) / 2; + sortMerge(c, left, mid); + sortMerge(c, mid + 1, right); + sortMerge(c, left, mid, right); + return 0; + } + + static int sortMerge(Array & c, S left, S mid, S right) { + int n1 = mid - left + 1; + int n2 = right - mid; + + // Create temp vectors + int L[n1], R[n2]; + + // Copy data to temp vectors L[] and R[] + for (int i = 0; i < n1; i++) + L[i] = c[left + i]; + for (int j = 0; j < n2; j++) + R[j] = c[mid + 1 + j]; + + int i = 0, j = 0; + int k = left; + + // Merge the temp vectors back + // into arr[left..right] + while (i < n1 && j < n2) { + if (L[i] <= R[j]) { + c[k] = L[i]; + i++; + } + else { + c[k] = R[j]; + j++; + } + k++; + } + + // Copy the remaining elements of L[], + // if there are any + while (i < n1) { + c[k] = L[i]; + i++; + k++; + } + + // Copy the remaining elements of R[], + // if there are any + while (j < n2) { + c[k] = R[j]; + j++; + k++; + } + return 0; + } + + /** MERGE SORT - END **/ }; } // namespace BF diff --git a/bflibcpp/src/list.hpp b/bflibcpp/src/list.hpp index 86eefe81..483f5355 100644 --- a/bflibcpp/src/list.hpp +++ b/bflibcpp/src/list.hpp @@ -484,6 +484,14 @@ class List : public Vector { this->set(list); } + virtual L operator[](S index) const { + return this->objectAtIndex(index); + } + + virtual L & operator[](S index) { + return this->refObjectAtIndex(index); + } + public: /** @@ -520,6 +528,83 @@ class List : public Vector { /// required interfaces: begin() & end() Iterator begin() const { return this->first(); } Iterator end() const { return NULL; } + +public: + + /** + * sorts the vector in ascending order + * + * default algorithm is merge sort + */ + virtual int sort(VectorSort type = kVectorSortMerge) { + switch (type) { + case kVectorSortBubble: + return List::sortBubble(*this); + case kVectorSortInsertion: + return List::sortInsertion(*this); + case kVectorSortSelection: + return List::sortSelection(*this); + case kVectorSortQuick: + return List::sortQuick(*this); + case kVectorSortMerge: + default: + return List::sortMerge(*this); + } + } + +private: + + /** BUBBLE SORT - START **/ + + static int sortBubble(List & c) { + return 0; + } + + /** BUBBLE SORT - END **/ + /** INSERTION SORT - START **/ + + static int sortInsertion(List & c) { + return 0; + } + + /** INSERTION SORT - END **/ + /** SELECTION SORT - START **/ + + static int sortSelection(List & c) { + return 0; + } + + /** SELECTION SORT - END **/ + /** QUICK SORT - START **/ + + static int sortQuick(List & c) { + return sortQuick(c, 0, c.size() - 1); + } + + static int sortQuick(List & c, int low, int high) { + return 0; + } + + static int sortQuickPartition(List & c, int low, int high) { + return 0; + } + + /** QUICK SORT - END **/ + /** MERGE SORT - START **/ + + static int sortMerge(List & c) { + return sortMerge(c, 0, c.size() - 1); + } + + static int sortMerge(List & c, S left, S right) { + return 0; + } + + static int sortMerge(List & c, S left, S mid, S right) { + return 0; + } + + /** MERGE SORT - END **/ }; } // namespace BF diff --git a/bflibcpp/src/string.cpp b/bflibcpp/src/string.cpp index 886ae2d4..6056b840 100644 --- a/bflibcpp/src/string.cpp +++ b/bflibcpp/src/string.cpp @@ -212,10 +212,6 @@ String & String::operator=(const String & str) { return *this; } -const char String::operator[](size_t index) const { - return this->objectAtIndex(index); -} - int String::toi(const String & s) { return atoi(s.cString()); } diff --git a/bflibcpp/src/string.hpp b/bflibcpp/src/string.hpp index 8ac7d39b..34a9b478 100644 --- a/bflibcpp/src/string.hpp +++ b/bflibcpp/src/string.hpp @@ -132,14 +132,13 @@ class String : public Array { return out << s.cString(); } - bool operator==(const String & s) const; - bool operator==(const char * s) const; - bool operator<(const String & s) const; - bool operator>(const String & s) const; - bool operator!=(const String & s) const; - bool operator!=(const char * s) const; + virtual bool operator==(const String & s) const; + virtual bool operator==(const char * s) const; + virtual bool operator<(const String & s) const; + virtual bool operator>(const String & s) const; + virtual bool operator!=(const String & s) const; + virtual bool operator!=(const char * s) const; virtual String & operator=(const String & str); - const char operator[](size_t index) const; // Conversions public: diff --git a/bflibcpp/src/vector.hpp b/bflibcpp/src/vector.hpp index 90fad20d..87ab5f61 100644 --- a/bflibcpp/src/vector.hpp +++ b/bflibcpp/src/vector.hpp @@ -37,11 +37,11 @@ class Vector : public Collection { virtual T & refObjectAtIndex(S index) = 0; virtual T max() const = 0; - T operator[](S index) const { + virtual T operator[](S index) const { return this->objectAtIndex(index); } - T & operator[](S index) { + virtual T & operator[](S index) { return this->refObjectAtIndex(index); } @@ -50,21 +50,7 @@ class Vector : public Collection { * * default algorithm is merge sort */ - int sort(VectorSort type = kVectorSortMerge) { - switch (type) { - case kVectorSortBubble: - return Vector::sortBubble(*this); - case kVectorSortInsertion: - return Vector::sortInsertion(*this); - case kVectorSortSelection: - return Vector::sortSelection(*this); - case kVectorSortQuick: - return Vector::sortQuick(*this); - case kVectorSortMerge: - default: - return Vector::sortMerge(*this); - } - } + virtual int sort(VectorSort type = kVectorSortMerge) = 0; protected: Vector() : Collection() { } diff --git a/bflibcpp/todo.md b/bflibcpp/todo.md index ec8578cf..0e7c9a1c 100644 --- a/bflibcpp/todo.md +++ b/bflibcpp/todo.md @@ -1,6 +1,8 @@ - [ ] BF::Array to use new/delete for memory allocation - [ ] improve List sorting + - [ ] move sort logic to array and list + - [ ] reevaluate logic location - [ ] Vectors to sort strings - [x] String append - [x] fix leaks in (RBTree specifically) - replaced with map From dceff3fb696c4a1688c92f0fb832df7be9b0f5e5 Mon Sep 17 00:00:00 2001 From: Brando Date: Tue, 22 Apr 2025 22:09:33 -0700 Subject: [PATCH 02/27] Swap --- bflibcpp/src/array.hpp | 23 ++++++++++++----------- bflibcpp/src/string.cpp | 10 +++++++++- bflibcpp/src/string.hpp | 2 ++ bflibcpp/src/swap.hpp | 21 +++++++++++++++++++++ 4 files changed, 44 insertions(+), 12 deletions(-) create mode 100644 bflibcpp/src/swap.hpp diff --git a/bflibcpp/src/array.hpp b/bflibcpp/src/array.hpp index d5a7e6ac..921f096c 100644 --- a/bflibcpp/src/array.hpp +++ b/bflibcpp/src/array.hpp @@ -14,6 +14,7 @@ #include "vector.hpp" #include #include "exception.hpp" +#include "swap.hpp" namespace BF { @@ -452,7 +453,7 @@ class Array : public Vector { swapped = false; for (int j = 0; j < n - i - 1; j++) { if (c[j] > c[j + 1]) { - BFSwap(c[j], c[j + 1]); + swap(c[j], c[j + 1]); swapped = true; } } @@ -508,7 +509,7 @@ class Array : public Vector { // Swap the found minimum element // with the first element if (c[min_idx] != c[i]) { - BFSwap(c[min_idx], c[i]); + swap(c[min_idx], c[i]); } } return 0; @@ -550,7 +551,7 @@ class Array : public Vector { if (c[j] < pivot) { i++; if (c[i] != c[j]) { - BFSwap(c[i], c[j]); + swap(c[i], c[j]); } } } @@ -558,7 +559,7 @@ class Array : public Vector { // Move pivot after smaller elements and // return its position if (c[i + 1] != c[high]) { - BFSwap(c[i + 1], c[high]); + swap(c[i + 1], c[high]); } return i + 1; } @@ -582,20 +583,20 @@ class Array : public Vector { } static int sortMerge(Array & c, S left, S mid, S right) { - int n1 = mid - left + 1; - int n2 = right - mid; + S n1 = mid - left + 1; + S n2 = right - mid; // Create temp vectors - int L[n1], R[n2]; + T L[n1], R[n2]; // Copy data to temp vectors L[] and R[] - for (int i = 0; i < n1; i++) + for (S i = 0; i < n1; i++) L[i] = c[left + i]; - for (int j = 0; j < n2; j++) + for (S j = 0; j < n2; j++) R[j] = c[mid + 1 + j]; - int i = 0, j = 0; - int k = left; + S i = 0, j = 0; + S k = left; // Merge the temp vectors back // into arr[left..right] diff --git a/bflibcpp/src/string.cpp b/bflibcpp/src/string.cpp index 6056b840..45c58b0a 100644 --- a/bflibcpp/src/string.cpp +++ b/bflibcpp/src/string.cpp @@ -182,8 +182,16 @@ bool String::operator<(const String & s) const { return this->compareString(s) < 0; } +bool String::operator<=(const String & s) const { + return this->compareString(s) <= 0; +} + bool String::operator>(const String & s) const { - return this->compareString(s) >0; + return this->compareString(s) > 0; +} + +bool String::operator>=(const String & s) const { + return this->compareString(s) >= 0; } int String::compareString(const String & s) const { diff --git a/bflibcpp/src/string.hpp b/bflibcpp/src/string.hpp index 34a9b478..3f5689d5 100644 --- a/bflibcpp/src/string.hpp +++ b/bflibcpp/src/string.hpp @@ -135,7 +135,9 @@ class String : public Array { virtual bool operator==(const String & s) const; virtual bool operator==(const char * s) const; virtual bool operator<(const String & s) const; + virtual bool operator<=(const String & s) const; virtual bool operator>(const String & s) const; + virtual bool operator>=(const String & s) const; virtual bool operator!=(const String & s) const; virtual bool operator!=(const char * s) const; virtual String & operator=(const String & str); diff --git a/bflibcpp/src/swap.hpp b/bflibcpp/src/swap.hpp new file mode 100644 index 00000000..a06c76df --- /dev/null +++ b/bflibcpp/src/swap.hpp @@ -0,0 +1,21 @@ +/** + * author: brando + * date: 4/22/25 + */ + +#ifndef SWAP_HPP +#define SWAP_HPP + +namespace BF { + +template +void swap(T & a, T & b) { + T tmp = a; + a = b; + b = tmp; +} + +} + +#endif // SWAP_HPP + From 8085fdffaf730a6d5bea0dc25c327ed14d998bea Mon Sep 17 00:00:00 2001 From: Brando Date: Tue, 22 Apr 2025 22:24:36 -0700 Subject: [PATCH 03/27] bubble sort --- bflibcpp/src/list.hpp | 40 ++++++++++++++++++++++++++++++++++++ bflibcpp/testbench/tests.cpp | 6 ++++++ 2 files changed, 46 insertions(+) diff --git a/bflibcpp/src/list.hpp b/bflibcpp/src/list.hpp index 483f5355..7984f3cc 100644 --- a/bflibcpp/src/list.hpp +++ b/bflibcpp/src/list.hpp @@ -557,6 +557,46 @@ class List : public Vector { /** BUBBLE SORT - START **/ static int sortBubble(List & c) { + Node * head = c.first(); + S len = c.size(); + int itr = 0; + bool swapped = false; + + while (itr < len) { + Node * trav = head; + Node * prev = head; + swapped = false; + + while (trav->next()) { + Node * ptr = trav->next(); + if (trav->object() > ptr->object()) { + swapped = true; + if (trav == head) { + trav->right = ptr->right; + ptr->right = trav; + prev = ptr; + head = prev; + } else { + trav->right = ptr->right; + ptr->right = trav; + prev->right = ptr; + prev = ptr; + } + + continue; + } + prev = trav; + trav = trav->next(); + } + + if (!swapped) { + break; + } + ++itr; + } + + c._head = head; + return 0; } diff --git a/bflibcpp/testbench/tests.cpp b/bflibcpp/testbench/tests.cpp index 77ef3eac..cdd8c36f 100644 --- a/bflibcpp/testbench/tests.cpp +++ b/bflibcpp/testbench/tests.cpp @@ -22,6 +22,7 @@ #include "defer_tests.hpp" BFTEST_SUITE_FUNC({ + /* BFTEST_SUITE_LAUNCH(atomic_tests); BFTEST_SUITE_LAUNCH(string_tests); BFTEST_SUITE_LAUNCH(stack_tests); @@ -29,14 +30,19 @@ BFTEST_SUITE_FUNC({ BFTEST_SUITE_LAUNCH(time_tests); BFTEST_SUITE_LAUNCH(data_tests); BFTEST_SUITE_LAUNCH(array_tests); + */ BFTEST_SUITE_LAUNCH(vector_tests); + /* BFTEST_SUITE_LAUNCH(map_tests); BFTEST_SUITE_LAUNCH(object_tests); + */ BFTEST_SUITE_LAUNCH(list_tests); + /* BFTEST_SUITE_LAUNCH(hashmap_tests); BFTEST_SUITE_LAUNCH(exception_tests); BFTEST_SUITE_LAUNCH(tree_tests); BFTEST_SUITE_LAUNCH(url_tests); BFTEST_SUITE_LAUNCH(defer_tests); + */ }) From b1c71c5fe26afa5b7820d16b80b60a20985f3e7d Mon Sep 17 00:00:00 2001 From: Brando Date: Wed, 23 Apr 2025 07:14:57 -0700 Subject: [PATCH 04/27] insertion sort --- bflibcpp/src/list.hpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/bflibcpp/src/list.hpp b/bflibcpp/src/list.hpp index 7984f3cc..4823306d 100644 --- a/bflibcpp/src/list.hpp +++ b/bflibcpp/src/list.hpp @@ -603,7 +603,36 @@ class List : public Vector { /** BUBBLE SORT - END **/ /** INSERTION SORT - START **/ + static Node * sortInsertion(Node * newNode, Node * sorted) { + if (!sorted || sorted->object() >= newNode->object()) { + newNode->right = sorted; + sorted = newNode; + } else { + Node * curr = sorted; + while (curr->right && curr->right->object() < newNode->object()) { + curr = curr->next(); + } + + newNode->right = curr->right; + curr->right = newNode; + } + + return sorted; + } + static int sortInsertion(List & c) { + Node * head = c.first(); + Node * sorted = NULL; + Node * curr = head; + + while (curr) { + Node * next = curr->next(); + sorted = sortInsertion(curr, sorted); + curr = next; + } + + c._head = sorted; + return 0; } From de003dc3f1168819e97901684ef693b0882bf588 Mon Sep 17 00:00:00 2001 From: Brando Date: Wed, 23 Apr 2025 07:19:38 -0700 Subject: [PATCH 05/27] selection sort --- bflibcpp/src/list.hpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/bflibcpp/src/list.hpp b/bflibcpp/src/list.hpp index 4823306d..c3c931ed 100644 --- a/bflibcpp/src/list.hpp +++ b/bflibcpp/src/list.hpp @@ -640,6 +640,23 @@ class List : public Vector { /** SELECTION SORT - START **/ static int sortSelection(List & c) { + Node * head = c.first(); + for (Node * start = head; start; start = start->next()) { + Node * min = start; + for (Node * curr = start->next(); curr; curr = curr->next()) { + if (curr->object() < min->object()) { + min = curr; + } + } + + if (min != start) { + L val = start->object(); + start->obj = min->object(); + min->obj = val; + } + } + + c._head = head; return 0; } From 1da111269906c706af45e6679ec73da4be97a9cf Mon Sep 17 00:00:00 2001 From: Brando Date: Wed, 23 Apr 2025 09:44:19 -0700 Subject: [PATCH 06/27] quick sort --- bflibcpp/src/list.hpp | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/bflibcpp/src/list.hpp b/bflibcpp/src/list.hpp index c3c931ed..6a2a24b7 100644 --- a/bflibcpp/src/list.hpp +++ b/bflibcpp/src/list.hpp @@ -9,6 +9,7 @@ #include "access.hpp" #include "vector.hpp" #include "exception.hpp" +#include "swap.hpp" #include #include @@ -664,15 +665,44 @@ class List : public Vector { /** QUICK SORT - START **/ static int sortQuick(List & c) { - return sortQuick(c, 0, c.size() - 1); - } + Node * head = c.first(); + Node * tail = c.last(); + + sortQuick(head, tail); - static int sortQuick(List & c, int low, int high) { + c._head = head; return 0; } + + static void sortQuick(Node * head, Node * tail) { + if (!head || head == tail) { + return; + } - static int sortQuickPartition(List & c, int low, int high) { - return 0; + Node * pivot = sortQuickGetPivot(head, tail); + + sortQuick(head, pivot); + sortQuick(pivot->next(), tail); + } + + static Node * sortQuickGetPivot(Node * head, Node * tail) { + Node * pivot = head; + + Node * pre = head; + Node * curr = head; + + while (curr != tail->next()) { + if (curr->object() < pivot->object()) { + BF::swap(curr->obj, pre->right->obj); + pre = pre->next(); + } + + curr = curr->next(); + } + + BF::swap(pivot->obj, pre->obj); + + return pre; } /** QUICK SORT - END **/ From 3d7cc765f5adf4f06a8fa1cff18728e2d32d8f52 Mon Sep 17 00:00:00 2001 From: Brando Date: Wed, 23 Apr 2025 10:01:46 -0700 Subject: [PATCH 07/27] good sorting algorithms --- bflibcpp/src/list.hpp | 44 +++++++++++++++++++++++++---- bflibcpp/testbench/vector_tests.hpp | 14 ++++----- 2 files changed, 46 insertions(+), 12 deletions(-) diff --git a/bflibcpp/src/list.hpp b/bflibcpp/src/list.hpp index 6a2a24b7..cdd2a9a7 100644 --- a/bflibcpp/src/list.hpp +++ b/bflibcpp/src/list.hpp @@ -709,15 +709,49 @@ class List : public Vector { /** MERGE SORT - START **/ static int sortMerge(List & c) { - return sortMerge(c, 0, c.size() - 1); + c._head = sortMerge(c._head); + return 0; } - static int sortMerge(List & c, S left, S right) { - return 0; + static Node * sortMerge(Node * head) { + if (!head || !head->next()) { + return head; + } + + Node * second = sortMergeSplit(head); + head = sortMerge(head); + second = sortMerge(second); + + return sortMerge(head, second); } + + static Node * sortMerge(Node * first, Node * second) { + if (!first) return second; + if (!second) return first; - static int sortMerge(List & c, S left, S mid, S right) { - return 0; + if (first->object() < second->object()) { + first->right = sortMerge(first->next(), second); + return first; + } else { + second->right = sortMerge(first, second->next()); + return second; + } + } + + static Node * sortMergeSplit(Node * head) { + Node * fast = head; + Node * slow = head; + + while (fast && fast->next()) { + fast = fast->right->right; + if (fast) { + slow = slow->right; + } + } + + Node * tmp = slow->right; + slow->right = NULL; + return tmp; } /** MERGE SORT - END **/ diff --git a/bflibcpp/testbench/vector_tests.hpp b/bflibcpp/testbench/vector_tests.hpp index 5985dadd..05abdd60 100644 --- a/bflibcpp/testbench/vector_tests.hpp +++ b/bflibcpp/testbench/vector_tests.hpp @@ -67,7 +67,7 @@ BFTEST_UNIT_FUNC(test_vectorListSortBubble, 1, { }) BFTEST_UNIT_FUNC(test_vectorListSortInsertion, 1, { - int err = run_vectorSort>(kVectorSortInsertion, 2 << 10, 1); + int err = run_vectorSort>(kVectorSortInsertion, 2 << 9, 1); BF_ASSERT(err == 0); }) @@ -82,32 +82,32 @@ BFTEST_UNIT_FUNC(test_vectorListSortQuick, 1, { }) BFTEST_UNIT_FUNC(test_vectorListSortMerge, 1, { - int err = run_vectorSort>(kVectorSortMerge, 2 << 12, 1); + int err = run_vectorSort>(kVectorSortMerge, 2 << 9, 1); BF_ASSERT(err == 0); }) BFTEST_UNIT_FUNC(test_vectorArraySortBubble, 1, { - int err = run_vectorSort>(kVectorSortBubble, 2 << 12, 4); + int err = run_vectorSort>(kVectorSortBubble, 2 << 9, 4); BF_ASSERT(err == 0); }) BFTEST_UNIT_FUNC(test_vectorArraySortInsertion, 1, { - int err = run_vectorSort>(kVectorSortInsertion, 2 << 13, 4); + int err = run_vectorSort>(kVectorSortInsertion, 2 << 9, 4); BF_ASSERT(err == 0); }) BFTEST_UNIT_FUNC(test_vectorArraySortSelection, 1, { - int err = run_vectorSort>(kVectorSortSelection, 2 << 13, 4); + int err = run_vectorSort>(kVectorSortSelection, 2 << 9, 4); BF_ASSERT(err == 0); }) BFTEST_UNIT_FUNC(test_vectorArraySortQuick, 1, { - int err = run_vectorSort>(kVectorSortQuick, 2 << 13, 4); + int err = run_vectorSort>(kVectorSortQuick, 2 << 9, 4); BF_ASSERT(err == 0); }) BFTEST_UNIT_FUNC(test_vectorArraySortMerge, 1, { - int err = run_vectorSort>(kVectorSortMerge, 2 << 12, 4); + int err = run_vectorSort>(kVectorSortMerge, 2 << 9, 4); BF_ASSERT(err == 0); }) From 88007aa3998a06e8807c8bdc844bbefc8d85dccf Mon Sep 17 00:00:00 2001 From: Brando Date: Wed, 23 Apr 2025 13:35:56 -0700 Subject: [PATCH 08/27] cleaning up vec --- bflibcpp/src/list.hpp | 17 +-- bflibcpp/src/sort.hpp | 18 +++ bflibcpp/src/vector.hpp | 190 ------------------------------ bflibcpp/testbench/list_tests.hpp | 63 +--------- bflibcpp/testbench/tests.cpp | 2 +- 5 files changed, 21 insertions(+), 269 deletions(-) create mode 100644 bflibcpp/src/sort.hpp diff --git a/bflibcpp/src/list.hpp b/bflibcpp/src/list.hpp index cdd2a9a7..91bd2331 100644 --- a/bflibcpp/src/list.hpp +++ b/bflibcpp/src/list.hpp @@ -278,8 +278,7 @@ class List : public Vector { // Get random node int r = rand() % (i + 1); Node * tmp = this->nodeAtIndex(r, this->_head, 0); - int err = this->swap(n, tmp); // swap nodes - if (err) return err; // leave if there was an error in swapping + BF::swap(n->obj, tmp->obj); // swap nodes i--; } @@ -317,20 +316,6 @@ class List : public Vector { private: - /** - * Swaps a and b objects - */ - static int swap(Node * a, Node * b) { - // Don't continue with this if a or b are null - if (!a || !b) return -1; - else { - L obj = a->obj; - a->obj = b->obj; - b->obj = obj; - return 0; - } - } - /** * Allows us to set list with {...} notation * diff --git a/bflibcpp/src/sort.hpp b/bflibcpp/src/sort.hpp new file mode 100644 index 00000000..20a8c7f6 --- /dev/null +++ b/bflibcpp/src/sort.hpp @@ -0,0 +1,18 @@ +/** + * author: brando + * date: 4/23/25 + */ + +#ifndef SORT_HPP +#define SORT_HPP + +#include "list.hpp" +#include "list.hpp" + +namespace BF { + +template struct Sort; +} + +#endif // SORT_HPP + diff --git a/bflibcpp/src/vector.hpp b/bflibcpp/src/vector.hpp index 87ab5f61..13d1ada6 100644 --- a/bflibcpp/src/vector.hpp +++ b/bflibcpp/src/vector.hpp @@ -54,196 +54,6 @@ class Vector : public Collection { protected: Vector() : Collection() { } - -private: - - /** BUBBLE SORT - START **/ - - static int sortBubble(Vector & c) { - int n = c.size(); - bool swapped; - - for (int i = 0; i < n - 1; i++) { - swapped = false; - for (int j = 0; j < n - i - 1; j++) { - if (c[j] > c[j + 1]) { - BFSwap(c[j], c[j + 1]); - swapped = true; - } - } - - // If no two elements were swapped, then break - if (!swapped) - break; - } - return 0; - } - - /** BUBBLE SORT - END **/ - /** INSERTION SORT - START **/ - - static int sortInsertion(Vector & c) { - int i, key, j; - int n = c.size(); - for (i = 1; i < n; i++) { - key = c[i]; - j = i - 1; - - // Move elements of arr[0..i-1], - // that are greater than key, to one - // position ahead of their - // current position - while (j >= 0 && c[j] > key) { - c[j + 1] = c[j]; - j = j - 1; - } - c[j + 1] = key; - } - return 0; - } - - /** INSERTION SORT - END **/ - /** SELECTION SORT - START **/ - - static int sortSelection(Vector & c) { - int i, j, min_idx; - int n = c.size(); - - // One by one move boundary of - // unsorted subarray - for (i = 0; i < n-1; i++) { - // Find the minimum element in - // unsorted array - min_idx = i; - for (j = i+1; j < n; j++) - if (c[j] < c[min_idx]) - min_idx = j; - - // Swap the found minimum element - // with the first element - if (c[min_idx] != c[i]) { - BFSwap(c[min_idx], c[i]); - } - } - return 0; - } - - /** SELECTION SORT - END **/ - /** QUICK SORT - START **/ - - static int sortQuick(Vector & c) { - return sortQuick(c, 0, c.size() - 1); - } - - static int sortQuick(Vector & c, int low, int high) { - if (low < high) { - // pi is the partition return index of pivot - int pi = sortQuickPartition(c, low, high); - - // Recursion calls for smaller elements - // and greater or equals elements - sortQuick(c, low, pi - 1); - sortQuick(c, pi + 1, high); - } - - return 0; - } - - static int sortQuickPartition(Vector & c, int low, int high) { - // Choose the pivot - int pivot = c[high]; - - // Index of smaller element and indicates - // the right position of pivot found so far - int i = low - 1; - - // Traverse arr[;ow..high] and move all smaller - // elements on left side. Elements from low to - // i are smaller after every iteration - for (int j = low; j <= high - 1; j++) { - if (c[j] < pivot) { - i++; - if (c[i] != c[j]) { - BFSwap(c[i], c[j]); - } - } - } - - // Move pivot after smaller elements and - // return its position - if (c[i + 1] != c[high]) { - BFSwap(c[i + 1], c[high]); - } - return i + 1; - } - - /** QUICK SORT - END **/ - /** MERGE SORT - START **/ - - static int sortMerge(Vector & c) { - return sortMerge(c, 0, c.size() - 1); - } - - static int sortMerge(Vector & c, S left, S right) { - if (left >= right) - return 0; - - S mid = left + (right - left) / 2; - sortMerge(c, left, mid); - sortMerge(c, mid + 1, right); - sortMerge(c, left, mid, right); - return 0; - } - - static int sortMerge(Vector & c, S left, S mid, S right) { - int n1 = mid - left + 1; - int n2 = right - mid; - - // Create temp vectors - int L[n1], R[n2]; - - // Copy data to temp vectors L[] and R[] - for (int i = 0; i < n1; i++) - L[i] = c[left + i]; - for (int j = 0; j < n2; j++) - R[j] = c[mid + 1 + j]; - - int i = 0, j = 0; - int k = left; - - // Merge the temp vectors back - // into arr[left..right] - while (i < n1 && j < n2) { - if (L[i] <= R[j]) { - c[k] = L[i]; - i++; - } - else { - c[k] = R[j]; - j++; - } - k++; - } - - // Copy the remaining elements of L[], - // if there are any - while (i < n1) { - c[k] = L[i]; - i++; - k++; - } - - // Copy the remaining elements of R[], - // if there are any - while (j < n2) { - c[k] = R[j]; - j++; - k++; - } - return 0; - } - - /** MERGE SORT - END **/ }; } diff --git a/bflibcpp/testbench/list_tests.hpp b/bflibcpp/testbench/list_tests.hpp index f2d84783..15f5fcfb 100644 --- a/bflibcpp/testbench/list_tests.hpp +++ b/bflibcpp/testbench/list_tests.hpp @@ -8,7 +8,7 @@ #define ASSERT_PUBLIC_MEMBER_ACCESS -#include +#include "list.hpp" #include "release.hpp" extern "C" { @@ -191,65 +191,6 @@ BFTEST_UNIT_FUNC(test_InitializingFromRawArray, 2<<10, { } }) -BFTEST_UNIT_FUNC(test_ListNullSwap, 2<<10, { - List::Node a, b; - a.obj = 0; - b.obj = 1; - BF_ASSERT(List::swap(&a, 0) != 0); - BF_ASSERT(List::swap(0, &b) != 0); - BF_ASSERT(List::swap(0, 0) != 0); -}) - -BFTEST_UNIT_FUNC(test_ListSwap, 2<<10, { - List::Node * a = new List::Node; - a->obj = 1; - List::Node * al = new List::Node; - List::Node * ar = new List::Node; - a->left = al; - a->right = ar; - al->right = a; - ar->left = a; - - List::Node * b = new List::Node; - b->obj = 2; - List::Node * bl = new List::Node; - List::Node * br = new List::Node; - b->left = bl; - b->right = br; - bl->right = b; - br->left = b; - - BF_ASSERT(a->obj == 1); - BF_ASSERT(a->prev() == al); - BF_ASSERT(a->next() == ar); - BF_ASSERT(al->next() == a); - BF_ASSERT(ar->prev() == a); - BF_ASSERT(b->obj == 2); - BF_ASSERT(b->prev() == bl); - BF_ASSERT(b->next() == br); - BF_ASSERT(bl->next() == b); - BF_ASSERT(br->prev() == b); - - BF_ASSERT(!List::swap(a, b)); - BF_ASSERT(a->obj == 2); - BF_ASSERT(a->prev() == al); - BF_ASSERT(a->next() == ar); - BF_ASSERT(al->next() == a); - BF_ASSERT(ar->prev() == a); - BF_ASSERT(b->obj == 1); - BF_ASSERT(b->prev() == bl); - BF_ASSERT(b->next() == br); - BF_ASSERT(bl->next() == b); - BF_ASSERT(br->prev() == b); - - BFRelease(a); - BFRelease(al); - BFRelease(ar); - BFRelease(b); - BFRelease(bl); - BFRelease(br); -}) - BFTEST_UNIT_FUNC(test_shuffle, 1, { const int size = 2 << 14; int array[size]; @@ -414,9 +355,7 @@ BFTEST_COVERAGE_FUNC(list_tests, { BFTEST_LAUNCH(test_ListContains); BFTEST_LAUNCH(test_InitializingWithInitList); BFTEST_LAUNCH(test_InitializingFromRawArray); - BFTEST_LAUNCH(test_ListSwap); BFTEST_LAUNCH(test_shuffle); - BFTEST_LAUNCH(test_ListNullSwap); BFTEST_LAUNCH(test_ShuffleLargeDataSet); BFTEST_LAUNCH(test_pluckingObject); BFTEST_LAUNCH(test_rangeBasedLooping); diff --git a/bflibcpp/testbench/tests.cpp b/bflibcpp/testbench/tests.cpp index cdd8c36f..83e595f5 100644 --- a/bflibcpp/testbench/tests.cpp +++ b/bflibcpp/testbench/tests.cpp @@ -29,8 +29,8 @@ BFTEST_SUITE_FUNC({ BFTEST_SUITE_LAUNCH(queue_tests); BFTEST_SUITE_LAUNCH(time_tests); BFTEST_SUITE_LAUNCH(data_tests); - BFTEST_SUITE_LAUNCH(array_tests); */ + BFTEST_SUITE_LAUNCH(array_tests); BFTEST_SUITE_LAUNCH(vector_tests); /* BFTEST_SUITE_LAUNCH(map_tests); From 2cb99f59951f571d601edda1641643420d73a914 Mon Sep 17 00:00:00 2001 From: Brando Date: Wed, 23 Apr 2025 13:52:45 -0700 Subject: [PATCH 09/27] can compile --- bflibcpp/src/list.hpp | 8 ++++++++ bflibcpp/src/sort.hpp | 12 ++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/bflibcpp/src/list.hpp b/bflibcpp/src/list.hpp index 91bd2331..6ff26d1d 100644 --- a/bflibcpp/src/list.hpp +++ b/bflibcpp/src/list.hpp @@ -10,6 +10,7 @@ #include "vector.hpp" #include "exception.hpp" #include "swap.hpp" +#include "sort.hpp" #include #include @@ -742,6 +743,13 @@ class List : public Vector { /** MERGE SORT - END **/ }; +template <> struct Sort { + template + int operator()(List & list) { + return 0; + } +}; + } // namespace BF #endif // LIST_HPP diff --git a/bflibcpp/src/sort.hpp b/bflibcpp/src/sort.hpp index 20a8c7f6..04206ce8 100644 --- a/bflibcpp/src/sort.hpp +++ b/bflibcpp/src/sort.hpp @@ -6,12 +6,20 @@ #ifndef SORT_HPP #define SORT_HPP -#include "list.hpp" #include "list.hpp" namespace BF { -template struct Sort; +typedef enum { + kSortStrategyBubble = 1, + kSortStrategyInsertion = 2, + kSortStrategySelection = 3, + kSortStrategyMerge = 4, + kSortStrategyQuick = 5, +} SortStrategy; + +template