@@ -39,46 +39,64 @@ class ArrayList {
3939 ~ArrayList () { delete[] _array; }
4040 int size () { return _size; }
4141 E const *toArray () { return _array; }
42- void set (int index, E element) { _array[index] = element; }
42+ void set (int index, const E & element) { _array[index] = element; }
4343 E get (int index) { return _array[index]; }
4444 void clear () {
4545 delete[] _array;
4646 _array = new E[0 ];
4747 _size = _capacity = 0 ;
4848 }
49- void add (E element) {
49+ void add (const E & element) {
5050 ensureCapacityInternal (_size + 1 );
5151 _array[_size++] = element;
5252 }
5353 ArrayList<E> add (const ArrayList<E> &other) {
5454 ArrayList<E> result = copy ();
55- result. add (other. _array , other. _size ) ;
55+ result += other;
5656 return result;
5757 }
58- bool contains (E element) { return indexOf (element) >= 0 ; }
59- int indexOfInRange (E element, int start, int stop) {
58+ bool contains (const E & element) { return indexOf (element) >= 0 ; }
59+ int indexOfInRange (const E & element, int start, int stop) {
6060 for (int i = start; i < stop; i++)
6161 if (_array[i] == element)
6262 return i;
6363 return -1 ;
6464 }
65- int indexOf (E element) { return indexOfInRange (element, 0 , _size); }
66- int lastIndexOfInRange (E element, int start, int stop) {
65+ int indexOf (const E & element) { return indexOfInRange (element, 0 , _size); }
66+ int lastIndexOfInRange (const E & element, int start, int stop) {
6767 for (int i = stop - 1 ; i >= start; i--)
6868 if (_array[i] == element)
6969 return i;
7070 return -1 ;
7171 }
72- int lastIndexOf (E element) { return lastIndexOfInRange (element, 0 , _size); }
73- ArrayList<E> copy () { return ArrayList<E>(this ); }
72+ int lastIndexOf (const E &element) { return lastIndexOfInRange (element, 0 , _size); }
73+ ArrayList<E> copy () { return ArrayList<E>(reinterpret_cast <size_t >(this )); }
74+ ArrayList<E> operator +(const E &element) {
75+ ArrayList<E> result = copy ();
76+ result += element;
77+ return result;
78+ }
7479 ArrayList<E> operator +(const ArrayList<E> &other) { return add (other); }
80+ ArrayList<E> &operator +=(const E &element) {
81+ add (element);
82+ return *this ;
83+ }
84+ ArrayList<E> &operator +=(const ArrayList<E> &other) {
85+ add (other._array , other._size );
86+ return *this ;
87+ }
7588 ArrayList<E> &operator =(const ArrayList<E> &other) {
7689 if (this != &other) {
7790 ensureCapacityInternal (other._capacity );
78- memcpy (_array, other._array , _size * sizeof (E));
91+ if (other._size > 0 )
92+ memcpy (_array, other._array , _size * sizeof (E));
7993 }
8094 return *this ;
8195 }
96+ E *begin () { return _array; }
97+ E *end () { return _array + _size; }
98+ const E *begin () const { return _array; }
99+ const E *end () const { return _array + _size; }
82100};
83101
84102
0 commit comments