Skip to content

Instantly share code, notes, and snippets.

@danielgospodinow
Last active September 22, 2019 19:50
Show Gist options
  • Save danielgospodinow/4d741f22921e5337c48eca78020730c0 to your computer and use it in GitHub Desktop.
Save danielgospodinow/4d741f22921e5337c48eca78020730c0 to your computer and use it in GitHub Desktop.
Array List resize operation
/**
* 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