From 4a7c9ece47f98354a326088e47a0342da15375a5 Mon Sep 17 00:00:00 2001 From: Franklyn Vasquez Date: Thu, 30 Jun 2022 02:19:02 -0400 Subject: [PATCH 1/5] [CLEAN] Tried to clean code formatting and make more readable. --- LinkedList.h | 76 +++++++++++++++++++++++++++------------------------- 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/LinkedList.h b/LinkedList.h index 742933a..0557fbc 100644 --- a/LinkedList.h +++ b/LinkedList.h @@ -16,8 +16,7 @@ #include template -struct ListNode -{ +struct ListNode { T data; ListNode *next; }; @@ -33,6 +32,7 @@ class LinkedList{ // Helps "get" method, by saving last position ListNode *lastNodeGot; int lastIndexGot; + // isCached should be set to FALSE // everytime the list suffer changes bool isCached; @@ -50,40 +50,48 @@ class LinkedList{ Returns current size of LinkedList */ virtual int size(); + /* Adds a T object in the specified index; Unlink and link the LinkedList correcly; Increment _size */ virtual bool add(int index, T); + /* Adds a T object in the end of the LinkedList; Increment _size; */ virtual bool add(T); + /* Adds a T object in the start of the LinkedList; Increment _size; */ virtual bool unshift(T); + /* Set the object at index, with T; */ virtual bool set(int index, T); + /* Remove object at index; If index is not reachable, returns false; else, decrement _size */ virtual T remove(int index); + /* Remove last object; */ virtual T pop(); + /* Remove first object; */ virtual T shift(); + /* Get the index'th element on the list; Return Element if accessible, @@ -101,7 +109,9 @@ class LinkedList{ */ virtual void sort(int (*cmp)(T &, T &)); - // add support to array brakets [] operator + /* + add support to array brakets [] operator + */ inline T& operator[](int index); inline T& operator[](size_t& i) { return this->get(i); } inline const T& operator[](const size_t& i) const { return this->get(i); } @@ -123,8 +133,7 @@ LinkedList::LinkedList() // Clear Nodes and free Memory template -LinkedList::~LinkedList() -{ +LinkedList::~LinkedList() { ListNode* tmp; while(root!=NULL) { @@ -142,26 +151,26 @@ LinkedList::~LinkedList() */ template -ListNode* LinkedList::getNode(int index){ +ListNode* LinkedList::getNode(int index) { int _pos = 0; ListNode* current = root; // Check if the node trying to get is // immediatly AFTER the previous got one - if(isCached && lastIndexGot <= index){ + if(isCached && lastIndexGot <= index) { _pos = lastIndexGot; current = lastNodeGot; } - while(_pos < index && current){ + while(_pos < index && current) { current = current->next; _pos++; } // Check if the object index got is the same as the required - if(_pos == index){ + if(_pos == index) { isCached = true; lastIndexGot = index; lastNodeGot = current; @@ -173,19 +182,19 @@ ListNode* LinkedList::getNode(int index){ } template -int LinkedList::size(){ +int LinkedList::size() { return _size; } template -LinkedList::LinkedList(int sizeIndex, T _t){ +LinkedList::LinkedList(int sizeIndex, T _t) { for (int i = 0; i < sizeIndex; i++){ add(_t); } } template -bool LinkedList::add(int index, T _t){ +bool LinkedList::add(int index, T _t) { if(index >= _size) return add(_t); @@ -206,7 +215,7 @@ bool LinkedList::add(int index, T _t){ } template -bool LinkedList::add(T _t){ +bool LinkedList::add(T _t) { ListNode *tmp = new ListNode(); tmp->data = _t; @@ -229,7 +238,7 @@ bool LinkedList::add(T _t){ } template -bool LinkedList::unshift(T _t){ +bool LinkedList::unshift(T _t) { if(_size == 0) return add(_t); @@ -245,14 +254,13 @@ bool LinkedList::unshift(T _t){ return true; } - template T& LinkedList::operator[](int index) { return getNode(index)->data; } template -bool LinkedList::set(int index, T _t){ +bool LinkedList::set(int index, T _t) { // Check if index position is in bounds if(index < 0 || index >= _size) return false; @@ -262,13 +270,13 @@ bool LinkedList::set(int index, T _t){ } template -T LinkedList::pop(){ +T LinkedList::pop() { if(_size <= 0) return T(); isCached = false; - if(_size >= 2){ + if(_size >= 2) { ListNode *tmp = getNode(_size - 2); T ret = tmp->next->data; delete(tmp->next); @@ -276,7 +284,7 @@ T LinkedList::pop(){ last = tmp; _size--; return ret; - }else{ + } else { // Only one element left on the list T ret = root->data; delete(root); @@ -288,11 +296,11 @@ T LinkedList::pop(){ } template -T LinkedList::shift(){ +T LinkedList::shift() { if(_size <= 0) return T(); - if(_size > 1){ + if(_size > 1) { ListNode *_next = root->next; T ret = root->data; delete(root); @@ -301,7 +309,7 @@ T LinkedList::shift(){ isCached = false; return ret; - }else{ + } else { // Only one left, then pop() return pop(); } @@ -309,17 +317,15 @@ T LinkedList::shift(){ } template -T LinkedList::remove(int index){ - if (index < 0 || index >= _size) - { +T LinkedList::remove(int index) { + if (index < 0 || index >= _size) { return T(); } if(index == 0) return shift(); - if (index == _size-1) - { + if (index == _size-1) { return pop(); } @@ -333,9 +339,8 @@ T LinkedList::remove(int index){ return ret; } - template -T LinkedList::get(int index){ +T LinkedList::get(int index) { ListNode *tmp = getNode(index); return (tmp ? tmp->data : T()); @@ -348,7 +353,7 @@ void LinkedList::clear(){ } template -void LinkedList::sort(int (*cmp)(T &, T &)){ +void LinkedList::sort(int (*cmp)(T &, T &)) { if(_size < 2) return; // trivial case; for(;;) { @@ -364,8 +369,7 @@ void LinkedList::sort(int (*cmp)(T &, T &)){ last = a_end; isCached = false; return; - } - else { + } else { break; } } @@ -383,8 +387,7 @@ void LinkedList::sort(int (*cmp)(T &, T &)){ *joinPoint = a; joinPoint = &a->next; a = a->next; - } - else { + } else { *joinPoint = b; joinPoint = &b->next; b = b->next; @@ -396,8 +399,7 @@ void LinkedList::sort(int (*cmp)(T &, T &)){ while(a->next) a = a->next; a->next = tail; joinPoint = &a->next; - } - else { + } else { *joinPoint = b; while(b->next) b = b->next; b->next = tail; @@ -416,4 +418,4 @@ ListNode* LinkedList::findEndOfSortedString(ListNode *p, int (*cmp)(T & return p; } -#endif +#endif \ No newline at end of file From 4861993b15491c6b0b6375bed42122e873d02894 Mon Sep 17 00:00:00 2001 From: Franklyn Vasquez Date: Thu, 30 Jun 2022 02:19:32 -0400 Subject: [PATCH 2/5] [FIX] Using Initilizer list --- LinkedList.h | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/LinkedList.h b/LinkedList.h index 0557fbc..59d2315 100644 --- a/LinkedList.h +++ b/LinkedList.h @@ -121,14 +121,8 @@ class LinkedList{ // Initialize LinkedList with false values template LinkedList::LinkedList() + : root(NULL), last(NULL), _size(0), lastNodeGot(root), lastIndexGot(0), isCached(false) { - root=NULL; - last=NULL; - _size=0; - - lastNodeGot = root; - lastIndexGot = 0; - isCached = false; } // Clear Nodes and free Memory From dc6e6ed27fb4d381fddb5f0f4602461866706c3e Mon Sep 17 00:00:00 2001 From: Franklyn Vasquez Date: Thu, 30 Jun 2022 02:19:47 -0400 Subject: [PATCH 3/5] [ADD] Support for range-based for loops --- LinkedList.h | 67 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 64 insertions(+), 3 deletions(-) diff --git a/LinkedList.h b/LinkedList.h index 59d2315..8d4f6bc 100644 --- a/LinkedList.h +++ b/LinkedList.h @@ -21,9 +21,49 @@ struct ListNode { ListNode *next; }; -template -class LinkedList{ +//Ranged-based for loop support. +template +class INode +{ +private: + ListNode* current; +public: + INode(ListNode*); + bool operator!=(INode); + T& operator*(); + void operator++(); + ~INode(); +}; + +template +INode::INode(ListNode* start) + : current(start) +{ +} + +template +bool INode::operator!=(INode) { + return (current != nullptr); +} + +template +T& INode::operator*() { + return current->data; +} + +template +void INode::operator++() { + current = current->next; +} +template +INode::~INode() { + +} + +template +class LinkedList +{ protected: int _size; ListNode *root; @@ -99,6 +139,16 @@ class LinkedList{ */ virtual T get(int index); + /* + Ranged-based for loop support. + */ + INode begin(); + + /* + Ranged-based for loop support. + */ + INode end(); + /* Clear the entire array */ @@ -341,7 +391,18 @@ T LinkedList::get(int index) { } template -void LinkedList::clear(){ +INode LinkedList::begin() { + if (!size()) return nullptr; + return INode(root); +} + +template +INode LinkedList::end() { + return INode(last); +} + +template +void LinkedList::clear() { while(size() > 0) shift(); } From ec38850f0e34b0bf5a8af9e4e8de1c1dba7aaebd Mon Sep 17 00:00:00 2001 From: Franklyn Vasquez Date: Thu, 30 Jun 2022 02:45:03 -0400 Subject: [PATCH 4/5] [ADD] rfor example --- examples/range_for_loop/range_for_loop.ino | 68 ++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 examples/range_for_loop/range_for_loop.ino diff --git a/examples/range_for_loop/range_for_loop.ino b/examples/range_for_loop/range_for_loop.ino new file mode 100644 index 0000000..d6b3ff9 --- /dev/null +++ b/examples/range_for_loop/range_for_loop.ino @@ -0,0 +1,68 @@ +#include + +/* + How does range-based for loops work? It's similar to a macro. + + ``` + for (int number : numbers) + { + Serial.println(number); + } + ``` + + get replaced with: + + ``` + for (INode __begin = numbers.begin(), __end = numbers.end(); ++ __begin) + { + int number = *__begin; + { + Serial.println(number); + } + } + ``` +*/ + +#define foreach_node(t_ident, list, body) + +LinkedList numbers = LinkedList(); //Create list. + +int toAdda = 10; +int toAddb = 20; +int toAddc = 30; + +void setup() { + //ADding a bunch'o numbers. + numbers.add(toAdda); + numbers.add(toAddb); + numbers.add(toAddc); + numbers.add(40); + numbers.add(50); + + Serial.begin(9600); + while (!Serial); //Wait for serial. + + +// for (int number : numbers) //Range-based for loop. Does not copy, changing number changes value in list. + for (int number : numbers) //<- Copies value in list, does not change value in list. + { + Serial.println(number); + } + + //If you do want to change values in list (like add 10 to each), use int& (reference). + + for (int& number : numbers) + { + number += 10; + } + + //Re-print values. + for (int number : numbers) + { + Serial.println(number); + } +} + +void loop() { + +} \ No newline at end of file From 9a8a4e64ec648a2dc61c6622073f0050a954b083 Mon Sep 17 00:00:00 2001 From: Franklyn Vasquez Date: Thu, 30 Jun 2022 02:57:39 -0400 Subject: [PATCH 5/5] [ADD] Docs --- README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/README.md b/README.md index 902380b..b349674 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,16 @@ myList.clear(); myList.sort(myComparator); ``` +#### Iterating through elements +```c++ + +// Iterate through list using rfor. INode iterable. +for (int& myObject : myList) +{ + myObject += 1; +} +``` + ------------------------ ## Library Reference @@ -137,6 +147,18 @@ myList.sort(myComparator); - `ListNode` `*next` - Pointer to the next Node +### `INode` class + +- `INode(ListNode* start)` - Constructor. + +- `bool` `INode::operator!=(INode)` - Returns false when the current element being held is `NULL`. + +- `T&` `INode::operator*()` - Returns the element being held by reference. + +- `void` `INode::operator++()` - Sets the current element as the next element. + +- `~INode()` - Destructor. + ### `LinkedList` class **`boolean` methods returns if succeeded** @@ -163,6 +185,10 @@ myList.sort(myComparator); - `T` `LinkedList::get(int index)` - Return the element at `index`. +- `INode` `LinkedList::begin()` - Returns iterable that holds first element in list. + +- `INode` `LinkedList::end()` - Returns iterable that hold last element in list. + - `void` `LinkedList::clear()` - Removes all elements. Does not free pointer memory. - `void` `LinkedList::sort(int (*cmp)(T &, T &))` - Sorts the linked list according to a comparator funcrion. The comparator should return < 0 if the first argument should be sorted before the second, and > 0 if the first argument should be sorted after the first element. (Same as how `strcmp()` works.)