diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9a81de6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +Unittest.h diff --git a/AFArray.h b/AFArray.h index ab543c0..3370004 100644 --- a/AFArray.h +++ b/AFArray.h @@ -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 class AFArray : public GenericIterator{ + private: + + static unsigned int MAX_LENGTH_ARRAY; + + static unsigned int get_free_ram(); + protected: T* arr; @@ -26,23 +33,23 @@ template class AFArray : public GenericIterator{ AFArray(); - AFArray(AFArray*); + AFArray(const AFArray*); - AFArray(T*, const unsigned int); + AFArray(const T*, const unsigned int); ~AFArray(); //Some useful functions - AFArray& find(T); + AFArray& find(const T); AFArray& 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& get_from_indexes(AFArray&); @@ -52,7 +59,7 @@ template class AFArray : public GenericIterator{ unsigned int size(); - unsigned int n_occurrences (T); + unsigned int n_occurrences (const T); bool is_full(); @@ -74,22 +81,26 @@ template class AFArray : public GenericIterator{ AFArray& operator=(AFArray&); - AFArray& operator+(T); + AFArray& operator+(const T); AFArray& operator+(AFArray&); - AFArray& operator+=(T); + AFArray& operator+=(const T); AFArray& operator+=(AFArray&); }; +template +unsigned int AFArray::MAX_LENGTH_ARRAY; + template void AFArray::init(){ arr = new T[real_len = INIT_DIMENSION]; n = 0; index = 0; GenericIterator::index_iterator = 0; + MAX_LENGTH_ARRAY = ArduinoBoardManager::SRAM_SIZE / 2; } template @@ -101,7 +112,7 @@ void inline AFArray::incr(){ template void AFArray::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::AFArray(){ } template -AFArray::AFArray(AFArray* acopy){ +AFArray::AFArray(const AFArray* acopy){ (*this) = (*acopy); } template -AFArray::AFArray(T* acopy, const unsigned int len){ +AFArray::AFArray(const T* acopy, const unsigned int len){ init(); for (unsigned int i=0; i::add(T el){ } template -bool AFArray::set(const unsigned int index, T el){ +bool AFArray::set(const unsigned int index, const T el){ if (!is_valid_index(index)) return false; arr[index] = el; @@ -192,7 +203,7 @@ unsigned int AFArray::size(){ } template -unsigned int AFArray::n_occurrences (T el){ +unsigned int AFArray::n_occurrences (const T el){ return find(el).size(); } @@ -202,7 +213,7 @@ bool AFArray::is_full(){ } template -AFArray& AFArray::find(T el){ +AFArray& AFArray::find(const T el){ AFArray index_list; for (unsigned int i=0; i& AFArray::operator=(AFArray& el){ } template -AFArray& AFArray::operator+(T el){ +AFArray& AFArray::operator+(const T el){ add(el); return *this; } @@ -262,7 +273,7 @@ AFArray& AFArray::operator+=(AFArray& el){ } template -AFArray& AFArray::operator+=(T el){ +AFArray& AFArray::operator+=(const T el){ (*this) = (*this) + el; return *this; } @@ -316,4 +327,11 @@ T* AFArray::to_array(int *len){ return buffer; } +template +unsigned int AFArray::get_free_ram(){ + extern int __heap_start, *__brkval; + int v; + return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); +} + #endif diff --git a/AFArrayType.h b/AFArrayType.h new file mode 100644 index 0000000..50ae237 --- /dev/null +++ b/AFArrayType.h @@ -0,0 +1,218 @@ +#ifndef AFARRAYTYPE_H +#define AFARRAYTYPE_H + +#include +#include "AFArray.h" + +class AFALong : public AFArray{ + + public: + + AFALong& operator=(AFArray&); + +}; + +AFALong& AFALong::operator=(AFArray& el){ + AFArray::operator=(el); + return *this; +} + +class AFAULong : public AFArray{ + + public: + + AFAULong& operator=(AFArray&); + +}; + +AFAULong& AFAULong::operator=(AFArray& el){ + AFArray::operator=(el); + return *this; +} + +class AFAInt : public AFArray{ + + public: + + AFAInt& operator=(AFArray&); + +}; + +AFAInt& AFAInt::operator=(AFArray& el){ + AFArray::operator=(el); + return *this; +} + +class AFAUInt : public AFArray{ + + public: + + AFAUInt& operator=(AFArray&); + +}; + +AFAUInt& AFAUInt::operator=(AFArray& el){ + AFArray::operator=(el); + return *this; +} + +class AFAFloat : public AFArray{ + + public: + + AFAFloat& operator=(AFArray&); + +}; + +AFAFloat& AFAFloat::operator=(AFArray& el){ + AFArray::operator=(el); + return *this; +} + +class AFADouble : public AFArray{ + + public: + + AFADouble& operator=(AFArray&); + +}; + +AFADouble& AFADouble::operator=(AFArray& el){ + AFArray::operator=(el); + return *this; +} + +class AFAChar : public AFArray{ + + public: + + AFAChar& operator=(AFArray&); + +}; + +AFAChar& AFAChar::operator=(AFArray& el){ + AFArray::operator=(el); + return *this; +} + + +class AFAString : public AFArray{ + + 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&); + + //TODO: + + AFAString& operator=(AFArray&); + + AFAString& operator=(AFArray&); + + AFAString& operator=(AFArray&); + + AFAString& operator=(AFArray&); + + AFAString& operator=(AFArray&); + + AFAString& operator=(AFArray&); + + AFAString& operator=(AFArray&); + +}; + +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& el){ + AFArray::operator=(el); + return *this; +} + +#endif diff --git a/ArduinoBoardManager b/ArduinoBoardManager new file mode 160000 index 0000000..2b7be8a --- /dev/null +++ b/ArduinoBoardManager @@ -0,0 +1 @@ +Subproject commit 2b7be8a3c9a8b0ed30a3ce7cd093c98d77912804