66
77template <typename E>
88class ArrayList {
9- private :
10- int _size;
9+ protected :
10+ size_t _size, _capacity ;
1111 E *_array;
1212 void grow (size_t minCapacity) {
13- int oldCapacity = _array.length ;
14- int newCapacity = oldCapacity + (oldCapacity >> 1 );
13+ size_t newCapacity = _capacity + (_capacity >> 1 );
1514 if (newCapacity < minCapacity)
1615 newCapacity = minCapacity;
17- E newArray = new E[newCapacity];
18- copy (begin (_array), end (_array), begin (newArray));
16+ E *newArray = new E[newCapacity];
17+ memmove (newArray, _array, _size * sizeof (E));
18+ delete[] _array;
1919 _array = newArray;
20+ _capacity = newCapacity;
2021 }
2122 void ensureExplicitCapacity (size_t minCapacity) {
22- if (minCapacity > _array. length )
23+ if (minCapacity > _capacity )
2324 grow (minCapacity);
2425 }
25- void ensureCapacityInternal (size_t minCapacity) {
26- if (_array. length == 0 )
27- minCapacity = max ( 10 , minCapacity );
28- ensureExplicitCapacity (minCapacity );
26+ 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) );
2930 }
3031
3132public:
32- ArrayList (size_t initialCapacity = 10 ) : _size(0 ), _array(new E[initialCapacity]) {}
33- ArrayList (E *const initialArray) : _size(0 ), _array(initialArray) {}
33+ explicit ArrayList (size_t initialCapacity = 10 ) :
34+ _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));
37+ }
38+ ArrayList (const ArrayList<E> &other) : ArrayList(other._array, other._size) {}
3439 ~ArrayList () { delete[] _array; }
3540 int size () { return _size; }
36- E *const &toArray () { return _array; }
41+ E const *toArray () { return _array; }
42+ void set (int index, E element) { _array[index] = element; }
3743 E get (int index) { return _array[index]; }
44+ void clear () {
45+ delete[] _array;
46+ _array = new E[0 ];
47+ _size = _capacity = 0 ;
48+ }
3849 void add (E element) {
3950 ensureCapacityInternal (_size + 1 );
4051 _array[_size++] = element;
4152 }
42- bool insert (int index, E element);
53+ ArrayList<E> add (const ArrayList<E> &other) {
54+ ArrayList<E> result = copy ();
55+ result.add (other._array , other._size );
56+ return result;
57+ }
4358 bool contains (E element) { return indexOf (element) >= 0 ; }
4459 int indexOfInRange (E element, int start, int stop) {
4560 for (int i = start; i < stop; i++)
@@ -55,6 +70,15 @@ class ArrayList {
5570 return -1 ;
5671 }
5772 int lastIndexOf (E element) { return lastIndexOfInRange (element, 0 , _size); }
73+ ArrayList<E> copy () { return ArrayList<E>(this ); }
74+ ArrayList<E> operator +(const ArrayList<E> &other) { return add (other); }
75+ ArrayList<E> &operator =(const ArrayList<E> &other) {
76+ if (this != &other) {
77+ ensureCapacityInternal (other._capacity );
78+ memcpy (_array, other._array , _size * sizeof (E));
79+ }
80+ return *this ;
81+ }
5882};
5983
6084
0 commit comments