Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions examples/at/at.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @file at.ino
* @author Alexander Tonn (https://github.com/AlexanderTonn)
* @brief This example shows the usage of ::at Operator
* ! If the inserted index is out of range, the program will return the value of the last index
* ! Code tested with Arduino MEGA2560
* @date 2024-01-05
*/

#include "Array.h"

Array<uint32_t,12> test1 = {{34,32,66,34,35,23,44,78,56,240,2323,234}};
const Array<uint32_t,10> test2 = {{1,7,3,7,35,23,44,78,56,240}};
Array<float,5> test3 = {{1.45,425.0,234.9,234.4,567.4}};
Array<float,8> test4 = {{false,false,true,true,false,true,true,true}};
void setup()
{
Serial.begin(9600);

// Try to Access an out of range index
Serial.println(test1.at(16));
Serial.println(test2.at(14));
Serial.println(test3.at(10));
Serial.println(test4.at(20));

Serial.println("########");

//Try to Access an in range index
Serial.println(test1.at(5));
Serial.println(test2.at(3));
Serial.println(test3.at(2));
Serial.println(test4.at(6));

}

void loop()
{

}
22 changes: 20 additions & 2 deletions src/Array/ArrayDefinitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,32 @@ template <typename T,
size_t MAX_SIZE>
const T & Array<T,MAX_SIZE>::at(size_t index) const
{
return values_[index];
auto arraySize = sizeof(values_)/sizeof(values_[0]);

if(index < 0 || index >= arraySize)
{
Serial.println(" Array Index out of Range! Last valid Index will be returned.");

auto outOfRangeIndex = arraySize-1;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove the local (not good named) variable and inserting the subtraction directly in the bracket operator

- auto outOfRangeIndex = arraySize-1;
- return values_[outOfRangeIndex]; 
+ return values_[arraySize-1]; 

return values_[outOfRangeIndex];
}else
return values_[index];

}

template <typename T,
size_t MAX_SIZE>
T & Array<T,MAX_SIZE>::at(size_t index)
{
return values_[index];
auto arraySize = sizeof(values_)/sizeof(values_[0]);
if(index < 0 || index >= arraySize)
{
Serial.println(" Array Index out of Range! Last valid Index will be returned.");

auto outOfRangeIndex = arraySize-1;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove the local (not good named) variable and inserting the subtraction directly in the bracket operator

- auto outOfRangeIndex = arraySize-1;
- return values_[outOfRangeIndex]; 
+ return values_[arraySize-1]; 

return values_[outOfRangeIndex];
}else
return values_[index];
}

template <typename T,
Expand Down