Simple Array Structure for Arduino Framework with template.
Version 0.4.0
Tested with ATmega328p, ESP32, ESP8266.
With AFArray is very simple to initialize:
AFArray<int> v1;
As you can see from Header, there are some copy contructors too. Comparing operator == and != are implemented.
if (v1 == v2)
Serial.println("This two AFArray are equals!");
AFArray has only one method to insert an element in tail.
v1.add(3);
v1.add(6);
v1.add(9);
Serial.println("Size : "+v1.size()); // Size : 3
Note: if element is inserted, add method will return true, otherwise false. This could happen when AFArray reaches maximum size (MAX_LENGTH_ARRAY). To check it, use is_full method.
AFArray has many overloaded operators, add method could be replaced by:
v1 += 9;
v1 = v1 + 100;
operators are overloaded for AFArray too.
AFArray<int> v2;
AFArray<int> v3;
//...Add elements in v2 and v3
AFArray<int> v4 = v3 + v2;
AFarray overloads [] operator. So
int a = v1[10];
is correct. But, if you aren't sure if an index exists, use is_valid_index method before:
int a;
if (v1.is_valid_index(10))
a = v1[10];
In the same way, you can set values:
if (v1.is_valid_index(10))
v1[10] = -4;
If you don't want to waste your time, set method fit the bill 👌 :
if (v1.set(10, -4))
Serial.println("-4 has inserted.");
If your type has overrided compare operator ==, find(x) returns an AFArray<unsgined int> that contains all indexes where x is in. n_occurrences returns only number of occurrences.
v1.add(3);
v1.add(-3);
v1.add(0);
v1.add(3);
v1.add(10);
AFArray<unsigned int> list_indexes = v1.find(3); //list_indexes has [0, 3] elements.
Serial.println(v1.n_occurrences(3)); //Prints "2".
If you want to get some element from a list of indexes, get_from_indexes accepts an array or AFArray int and returns an AFArray with those elements.
unsigned int indexes[4] = {2, 4, 8, 9};
AFArray<int> some_elements = v1.get_from_indexes(indexes, 4); //some_elements contains only elements in positions 2,4,8,9.
You can combine this method with find:
AFArray<int> found = v1.find(5);
AFArray<int> some_elements = v1.get_from_indexes(found); //some_elements contains only elements in positions where 5 was found.
Sometimes, you need a part of a list with a specific step. slice method returns an AFArray<T> with all elements from start index to end one with a step (default = 1).
v1.add(2);
v1.add(1);
v1.add(10);
v1.add(-4);
v1.add(12);
v1.add(6);
AFArray<int> part = v1.slice(1, 5, 2); //1, -4, 6.
with slice method, you can remove an element from AFArray:
//v1 is initialized and add elements
AFArray<int> removed = v1.slice(0, 3) + v1.slice(5, 10) ; //removes 5th element.
reset() method destroy object and recreate it.
AFArray could be converted into array with template type.
v1.add(2);
v1.add(1);
v1.add(10);
int n;
int *arr = v1.to_array(&n);
AFArray could remove an element from index.
v1.add(56);
v1.add(45);
v1.add(13);
v1.remove_from_index(1); //Remove 56. true if operation succeeds.
AFArray inherits GenericIterator, a very stupid iterator.
while (v1.has_next())
Serial.println(v1.next());
When while cycle exits, you'll could iterate it again.
Version 0.2 introduces specific AFArray for each fundamental type in Arduino like AFAInt, AFAUInt, AFAString etc. In particular, AFAString has two new methods: explode and implode.
Explode method converts a string with a separator into AFArray<String>.
String s = "hi;this;is;a;string;with;semicolo;separator";
AFAString exploded = AFAString::explode(';', s); //[hi, this, is, a ...]
Implode method is the opposite one. It converts a AFArray<String> into a string with a separator.
AFAString collection;
collection.add("Now");
collection.add("I");
collection.add("want");
collection.add("to");
collection.add("join");
String s = collection.implode(' ', collection);//"Now I want to join"
Note: AFAString corrisponding to AFArray<String>, but it has explode and implode method. Actually, AFAInt is equivalent to AFArray<int>, in the same way AFALong is like AFArray<long> and so on.