LinkedList icon indicating copy to clipboard operation
LinkedList copied to clipboard

[ADD] Iterable in rfor. [CLEAN] code style

Open Narwhalsss360 opened this issue 3 years ago • 0 comments

Fixes

  • Using initializer list in constructor.

  • Tried to cleanup code by making formatting consistent with spacing between comments and code.

    • Function defentions use:
        constructor()
            : init_list()
        {
    
        }
    
        void foo() {
    
        }
    
        void bar() {
    
        }
    

    instead of:

        void foo() {
    
        }
        void bar(){
    
        }
    
        void baz()
        {
    
        }
    

    if, else, for, and while statements use same format. class/struct defenitions also use the same format.

    • Removed white spaces.

New Feature INode<T> Iterable

Motivation

Using the INode<T> iterable to allow the use of range-based for loop for (range-declaration : range-expression) makes code more readable, and easier to use.

Implementation

  • template <class T> INode as fully usable iterable.
  • Iterating to next element is one statement with no looping or conditionals.

Added example: range_for_loop.ino

#include <LinkedList.h>

/*
    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<int> __begin = numbers.begin(), __end = numbers.end(); ++ __begin)
        {
            int number = *__begin;
            {
                Serial.println(number);
            }
        }
    ```
*/

LinkedList<int> numbers = LinkedList<int>(); //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() {

}

Tests

Tested using int primitive with the following:

#include <LinkedList.h>

LinkedList<int> intList = LinkedList<int>();

void setup()
{
    intList.add(4);
    intList.add(75);
    intList.add(541);
    intList.add(53);
    intList.add(634);
    intList.add(53);

    Serial.begin(9600);
    while (!Serial);

    uint8_t i = 0;
    for (int& val : intList)
    {
        Serial.println('[' + String(i) + "]: " + String(val));
        i++;
    }
}

void loop()
{

}

If you see an issue with my test method, please test yourself and comment results.

Narwhalsss360 avatar Jun 30 '22 07:06 Narwhalsss360