Skip to content
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Unittest.h
56 changes: 37 additions & 19 deletions AFArray.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
#ifndef AFArray_H
#define AFArray_H
#ifndef AFARRAY_H
#define AFARRAY_H

#include "ArduinoBoardManager/ArduinoBoardManager.h"
#include "GenericIterator.h"

#define INIT_DIMENSION 20
#define MAX_LENGTH_ARRAY 160
#define OFFSET_SRAM 100

template <typename T> class AFArray : public GenericIterator<T>{

private:

static unsigned int MAX_LENGTH_ARRAY;

static unsigned int get_free_ram();

protected:

T* arr;
Expand All @@ -26,23 +33,23 @@ template <typename T> class AFArray : public GenericIterator<T>{

AFArray();

AFArray(AFArray<T>*);
AFArray(const AFArray<T>*);

AFArray(T*, const unsigned int);
AFArray(const T*, const unsigned int);

~AFArray();

//Some useful functions

AFArray<unsigned int>& find(T);
AFArray<unsigned int>& find(const T);

AFArray<T>& slice(const unsigned int, const unsigned int, const unsigned int = 1);

T* to_array(int * = -1);

bool add(T);
bool add(const T);

bool set(const unsigned int, T);
bool set(const unsigned int, const T);

AFArray<T>& get_from_indexes(AFArray<unsigned int>&);

Expand All @@ -52,7 +59,7 @@ template <typename T> class AFArray : public GenericIterator<T>{

unsigned int size();

unsigned int n_occurrences (T);
unsigned int n_occurrences (const T);

bool is_full();

Expand All @@ -74,22 +81,26 @@ template <typename T> class AFArray : public GenericIterator<T>{

AFArray<T>& operator=(AFArray<T>&);

AFArray<T>& operator+(T);
AFArray<T>& operator+(const T);

AFArray<T>& operator+(AFArray<T>&);

AFArray<T>& operator+=(T);
AFArray<T>& operator+=(const T);

AFArray<T>& operator+=(AFArray<T>&);

};

template <class T>
unsigned int AFArray<T>::MAX_LENGTH_ARRAY;

template <class T>
void AFArray<T>::init(){
arr = new T[real_len = INIT_DIMENSION];
n = 0;
index = 0;
GenericIterator<T>::index_iterator = 0;
MAX_LENGTH_ARRAY = ArduinoBoardManager::SRAM_SIZE / 2;
}

template <class T>
Expand All @@ -101,7 +112,7 @@ void inline AFArray<T>::incr(){

template <class T>
void AFArray<T>::amortize(){
if (real_len == MAX_LENGTH_ARRAY)
if (real_len == MAX_LENGTH_ARRAY || get_free_ram() < OFFSET_SRAM)
return;
T copy_arr[real_len];
for (unsigned int i=0; i<n; i++){
Expand Down Expand Up @@ -143,12 +154,12 @@ AFArray<T>::AFArray(){
}

template <class T>
AFArray<T>::AFArray(AFArray<T>* acopy){
AFArray<T>::AFArray(const AFArray<T>* acopy){
(*this) = (*acopy);
}

template <class T>
AFArray<T>::AFArray(T* acopy, const unsigned int len){
AFArray<T>::AFArray(const T* acopy, const unsigned int len){
init();
for (unsigned int i=0; i<len; i++){
add(acopy[i]);
Expand All @@ -173,7 +184,7 @@ bool AFArray<T>::add(T el){
}

template <class T>
bool AFArray<T>::set(const unsigned int index, T el){
bool AFArray<T>::set(const unsigned int index, const T el){
if (!is_valid_index(index))
return false;
arr[index] = el;
Expand All @@ -192,7 +203,7 @@ unsigned int AFArray<T>::size(){
}

template <class T>
unsigned int AFArray<T>::n_occurrences (T el){
unsigned int AFArray<T>::n_occurrences (const T el){
return find(el).size();
}

Expand All @@ -202,7 +213,7 @@ bool AFArray<T>::is_full(){
}

template <class T>
AFArray<unsigned int>& AFArray<T>::find(T el){
AFArray<unsigned int>& AFArray<T>::find(const T el){
AFArray<unsigned int> index_list;
for (unsigned int i=0; i<size(); i++){
if (el == arr[i])
Expand Down Expand Up @@ -242,7 +253,7 @@ AFArray<T>& AFArray<T>::operator=(AFArray<T>& el){
}

template <class T>
AFArray<T>& AFArray<T>::operator+(T el){
AFArray<T>& AFArray<T>::operator+(const T el){
add(el);
return *this;
}
Expand All @@ -262,7 +273,7 @@ AFArray<T>& AFArray<T>::operator+=(AFArray<T>& el){
}

template <class T>
AFArray<T>& AFArray<T>::operator+=(T el){
AFArray<T>& AFArray<T>::operator+=(const T el){
(*this) = (*this) + el;
return *this;
}
Expand Down Expand Up @@ -316,4 +327,11 @@ T* AFArray<T>::to_array(int *len){
return buffer;
}

template <class T>
unsigned int AFArray<T>::get_free_ram(){
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}

#endif
218 changes: 218 additions & 0 deletions AFArrayType.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
#ifndef AFARRAYTYPE_H
#define AFARRAYTYPE_H

#include <Arduino.h>
#include "AFArray.h"

class AFALong : public AFArray<long>{

public:

AFALong& operator=(AFArray<long>&);

};

AFALong& AFALong::operator=(AFArray<long>& el){
AFArray<long>::operator=(el);
return *this;
}

class AFAULong : public AFArray<unsigned long>{

public:

AFAULong& operator=(AFArray<unsigned long>&);

};

AFAULong& AFAULong::operator=(AFArray<unsigned long>& el){
AFArray<unsigned long>::operator=(el);
return *this;
}

class AFAInt : public AFArray<int>{

public:

AFAInt& operator=(AFArray<int>&);

};

AFAInt& AFAInt::operator=(AFArray<int>& el){
AFArray<int>::operator=(el);
return *this;
}

class AFAUInt : public AFArray<unsigned int>{

public:

AFAUInt& operator=(AFArray<unsigned int>&);

};

AFAUInt& AFAUInt::operator=(AFArray<unsigned int>& el){
AFArray<unsigned int>::operator=(el);
return *this;
}

class AFAFloat : public AFArray<float>{

public:

AFAFloat& operator=(AFArray<float>&);

};

AFAFloat& AFAFloat::operator=(AFArray<float>& el){
AFArray<float>::operator=(el);
return *this;
}

class AFADouble : public AFArray<double>{

public:

AFADouble& operator=(AFArray<double>&);

};

AFADouble& AFADouble::operator=(AFArray<double>& el){
AFArray<double>::operator=(el);
return *this;
}

class AFAChar : public AFArray<char>{

public:

AFAChar& operator=(AFArray<char>&);

};

AFAChar& AFAChar::operator=(AFArray<char>& el){
AFArray<char>::operator=(el);
return *this;
}


class AFAString : public AFArray<String>{

public:

static AFAString& explode(const String&, const String&);

static AFAString& explode(const char, const String&);

static AFAString& explode(const char*, const String&);

static String implode(const String&, AFAString&);

static String implode(const char, AFAString&);

static String implode(const char*, AFAString&);

String implode(const String&);

String implode(const char);

String implode(const char*);

AFAString& operator=(AFArray<String>&);

//TODO:

AFAString& operator=(AFArray<int>&);

AFAString& operator=(AFArray<unsigned int>&);

AFAString& operator=(AFArray<long>&);

AFAString& operator=(AFArray<unsigned long>&);

AFAString& operator=(AFArray<float>&);

AFAString& operator=(AFArray<double>&);

AFAString& operator=(AFArray<char>&);

};

AFAString& AFAString::explode(const String& delimiter, const String& s){
AFAString* explosion = new AFAString;
String group_set = "";
unsigned int length = s.length(), length_del = delimiter.length(), i = 0;
bool is_del = false;
while(i < s.length()){
if (i+length_del-1 < length){ // delimiter?
unsigned int j = i, k = 0;
is_del = true;
while (k < length_del){
if (delimiter[k] != s[j]){
is_del = false;
break;
}
j++;
k++;
}//while
if (is_del){
explosion->add(group_set);
group_set = "";
i = i+length_del;
if (i >= length)
break;
is_del = false;
}
}//if
group_set += s[i];
i++;
}//while
if (group_set.length() > 0 || is_del)
explosion->add(group_set);
return *explosion;
}

AFAString& AFAString::explode(const char delimiter, const String& s){
return AFAString::explode(String(delimiter), s);
}

AFAString& AFAString::explode(const char* delimiter, const String& s){
return AFAString::explode(String(delimiter), s);
}

String AFAString::implode(const String& glue, AFAString& list){
if (list.size() == 0)
return "";
String glued = list[1];
for (unsigned int i=1; i<list.size(); i++){
glued += (glue + list[i]);
}
return glued;
}

String AFAString::implode(const char delimiter, AFAString& s){
return AFAString::implode(String(delimiter), s);
}

String AFAString::implode(const char* delimiter, AFAString& s){
return AFAString::implode(String(delimiter), s);
}

String AFAString::implode(const String& delimiter){
return AFAString::implode(delimiter, *this);
}

String AFAString::implode(const char delimiter){
return AFAString::implode(String(delimiter));
}

String AFAString::implode(const char* delimiter){
return AFAString::implode(String(delimiter));
}

AFAString& AFAString::operator=(AFArray<String>& el){
AFArray<String>::operator=(el);
return *this;
}

#endif
1 change: 1 addition & 0 deletions ArduinoBoardManager
Submodule ArduinoBoardManager added at 2b7be8