33
44
55#include " Arduino.h"
6+ #include " Memory.h"
67
78template <typename E>
89class ArrayList {
@@ -14,8 +15,7 @@ class ArrayList {
1415 if (newCapacity < minCapacity)
1516 newCapacity = minCapacity;
1617 E *newArray = new E[newCapacity];
17- memmove (newArray, _array, _size * sizeof (E));
18- delete[] _array;
18+ arraycopy (_array, newArray, _size);
1919 _array = newArray;
2020 _capacity = newCapacity;
2121 }
@@ -24,16 +24,12 @@ class ArrayList {
2424 grow (minCapacity);
2525 }
2626 void ensureCapacityInternal (size_t minCapacity) { ensureExplicitCapacity (max (10 , minCapacity)); }
27- void add (E *elements, size_t size) {
28- ensureCapacityInternal (_size + size);
29- memcpy (_array + _size, elements, size * sizeof (E));
30- }
3127
3228public:
3329 explicit ArrayList (size_t initialCapacity = 10 ) :
3430 _size(0 ), _capacity(initialCapacity), _array(new E[initialCapacity]) {}
35- ArrayList (E const *const initialArray, size_t size) : _size(0 ), _capacity(size), _array(new E[size]) {
36- memcpy (_array, initialArray , size * sizeof (E) );
31+ ArrayList (E const *const initialArray, size_t size) : _size(size ), _capacity(size), _array(new E[size]) {
32+ arraycopy (initialArray, _array , size);
3733 }
3834 ArrayList (const ArrayList<E> &other) : ArrayList(other._array, other._size) {}
3935 ~ArrayList () { delete[] _array; }
@@ -42,19 +38,18 @@ class ArrayList {
4238 void set (int index, const E &element) { _array[index] = element; }
4339 E get (int index) { return _array[index]; }
4440 void clear () {
45- delete[] _array;
4641 _array = new E[0 ];
4742 _size = _capacity = 0 ;
4843 }
44+ void add (E const *const elements, size_t size) {
45+ ensureCapacityInternal (_size + size);
46+ arraycopy (elements, _array, size, 0 , _size);
47+ _size += size;
48+ }
4949 void add (const E &element) {
5050 ensureCapacityInternal (_size + 1 );
5151 _array[_size++] = element;
5252 }
53- ArrayList<E> add (const ArrayList<E> &other) {
54- ArrayList<E> result = copy ();
55- result += other;
56- return result;
57- }
5853 bool contains (const E &element) { return indexOf (element) >= 0 ; }
5954 int indexOfInRange (const E &element, int start, int stop) {
6055 for (int i = start; i < stop; i++)
@@ -70,13 +65,18 @@ class ArrayList {
7065 return -1 ;
7166 }
7267 int lastIndexOf (const E &element) { return lastIndexOfInRange (element, 0 , _size); }
73- ArrayList<E> copy () { return ArrayList<E>(reinterpret_cast <size_t >(this )); }
68+ ArrayList<E> copy () { return ArrayList<E>(*this ); }
69+ E operator [](int index) { return get (index); }
7470 ArrayList<E> operator +(const E &element) {
7571 ArrayList<E> result = copy ();
7672 result += element;
7773 return result;
7874 }
79- ArrayList<E> operator +(const ArrayList<E> &other) { return add (other); }
75+ ArrayList<E> operator +(const ArrayList<E> &other) {
76+ ArrayList<E> result = copy ();
77+ result += other;
78+ return result;
79+ }
8080 ArrayList<E> &operator +=(const E &element) {
8181 add (element);
8282 return *this ;
@@ -88,8 +88,12 @@ class ArrayList {
8888 ArrayList<E> &operator =(const ArrayList<E> &other) {
8989 if (this != &other) {
9090 ensureCapacityInternal (other._capacity );
91- if (other._size > 0 )
92- memcpy (_array, other._array , _size * sizeof (E));
91+ if (other._size > 0 ) {
92+ _array = new E[other._capacity ];
93+ _capacity = other._capacity ;
94+ arraycopy (other._array , _array, other._size );
95+ _size = other._size ;
96+ }
9397 }
9498 return *this ;
9599 }
0 commit comments