Question: Take this class and retool it to use allocated memory in your dynamic array. My teacher said that you only really need to touch the

Take this class and retool it to use allocated memory in your dynamic array. My teacher said that you only really need to touch the List class to fully retool the program. To show an example of what my teacher wants here is this, and also as you can read from this screenshot. He also wants a static allocator object to be added in the List class.

Take this class and retool it to use allocated memory in your

List class requirements:

The list shall only be expected to work when it holds pointers to whatever data type The list shall hold its items in a dynamic array

* The use of the vector class is not allowed The list shall be sorted

* In the case of Books, they should sort themselves by author, then title 1

* The List only cares about a

The list shall double its capacity when needed The list shall never shrink its capacity Items in the list shall always be contiguous in the dynamic array (no gaps in the array)

Now this is all of the code for the List class that has NOT been retooled yet to use memory allocation.

Code:

List.hpp:

#ifndef ARRAY_LIST_HPP #define ARRAY_LIST_HPP

#include #include

template class List { public: List(); List(T item); List(const List &rhs); List(List &&rhs); ~List(); List& operator=(const List &rhs); List& operator=(List &&rhs); unsigned int size() const noexcept; unsigned int capacity() const noexcept; void insert(T item) noexcept; void erase(T item); // Throws underflow_error if empty, range_error if item doesn't exist const T& at(unsigned int i) const; // Both at() functions can throw T& at(unsigned int i); // out_of_range exception unsigned int search(T item) const; // Throws domain_error if item doesn't exist private: T *_arr; unsigned int _size; unsigned int _capacity;

void grow() noexcept; // EXTRA void displace(unsigned int i); // EXTRA void collapse(unsigned int i) noexcept; // EXTRA };

// LIST CLASS IMPLEMENTATION template List::List() : _arr(new T[0]), _size(0), _capacity(0) { }

template List::List(T item) : _arr(new T[1]), _size(1), _capacity(1) { _arr[0] = item; }

template List::List(const List &rhs) : _arr(new T[rhs._size]), _size(rhs._size), _capacity(rhs._size) { for (unsigned int i = 0; i

template List::List(List &&rhs) : _arr(rhs._arr), _size(rhs._size), _capacity(rhs._capacity) { rhs._arr = nullptr; }

template List::~List() { if (_arr != nullptr) { delete [] _arr; _arr = nullptr; } _size = 0; _capacity = 0; }

template List& List::operator=(const List &rhs) { if (this != &rhs) { delete [] _arr; _arr = new T[rhs._size]; _size = rhs._size; _capacity = rhs._capacity; for (unsigned int i = 0; i

return *this; }

template List& List::operator=(List &&rhs) { if (this != &rhs) { delete [] _arr; _arr = rhs._arr; _size = rhs._size; _capacity = rhs._capacity; rhs._arr = nullptr; }

return *this; }

template unsigned int List::size() const noexcept { return _size; }

template unsigned int List::capacity() const noexcept { return _capacity; }

template void List::insert(T item) noexcept { if (_capacity == 0 || _size == _capacity) { grow(); }

if (_size == 0) { _arr[0] = item; _size++; return; } unsigned int i = 0; while (i

_arr[i] = item; _size++; }

template void List::erase(T item) { if (_size == 0) throw std::underflow_error("erase on empty"); try { int loc = search(item);

collapse(loc); _size--; } catch(std::domain_error &e) { throw std::range_error("erase non-existent item"); } }

template const T& List::at(unsigned int i) const { if (i = _size) throw std::out_of_range("bad index"); return _arr[i]; }

template T& List::at(unsigned int i) { if (i = _size) throw std::out_of_range("bad index"); return _arr[i]; }

template unsigned int List::search(T item) const { for (unsigned int i = 0; i

throw std::domain_error("does not exist"); }

template void List::grow() noexcept { if (_capacity == 0) { delete [] _arr; _arr = new T[1]; _capacity = 1; } else { T *temp = new T[_capacity * 2]; for (unsigned int i = 0; i

template void List::displace(unsigned int i) { if ( !(_size i; counter--) _arr[counter] = _arr[counter - 1]; }

template void List::collapse(unsigned int i) noexcept { for (unsigned int counter = i; counter

#endif

You shall add a static allocator object to the class. . You shall retool the entire class around memory allocation, here's one constructor for free: template List::List(T item) begin(alloc.allocate(1)) end(_begin) -capacityC.begin + 1) alloc.constructcend-+, item)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!