Last active
September 22, 2019 19:50
-
-
Save danielgospodinow/4d741f22921e5337c48eca78020730c0 to your computer and use it in GitHub Desktop.
Array List resize operation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Double the size of the data structure. | |
* | |
* @tparam T type | |
*/ | |
template<typename T> | |
void ArrayList<T>::resize() { | |
// Create the new bigger array with double the size of the old one. | |
T *const newStorage = new T[_capacity *= 2]; | |
// Copy all the elements from the old array to the new one. | |
for (int i = 0; i < _size; ++i) { | |
newStorage[i] = _array[i]; | |
} | |
// Make the bigger array to be the current array of | |
// the data structure and clear the old one. | |
T *oldStorage = _array; | |
_array = newStorage; | |
delete[] oldStorage; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment