Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

Narwhalsss360
Copy link

@Narwhalsss360 Narwhalsss360 commented Jun 30, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants