Last active
June 6, 2016 21:09
-
-
Save acgetchell/ae7dce8f33df626c8d39532d4e31844f to your computer and use it in GitHub Desktop.
A templatized vector with memory allocator
This file contains hidden or 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
| #include <iostream> | |
| //#include <stdexcept> | |
| using namespace std; | |
| template<class T, class A = allocator<T>> | |
| class vector { | |
| int sz; | |
| T* elem; | |
| int space; | |
| A a; | |
| public: | |
| vector() : sz{0}, elem{0}, space{0} {} | |
| vector(int s) : sz{s}, elem{new T[s]}, space{s} { | |
| for (int i = 0; i < sz; ++i) { | |
| elem[i] = 0; | |
| } | |
| } | |
| vector(const vector&); | |
| vector& operator=(const vector& v); | |
| ~vector() {delete[] elem;} | |
| T& at(int n); | |
| const T& at(int n) const; | |
| T& operator[](int n); | |
| const T& operator[](int n) const; | |
| int size() const { return sz;} | |
| int capacity() const { return space;} | |
| void reserve(int alloc_size); | |
| void resize(int resize_size, T val = T()); | |
| void push_back(const T& d); | |
| }; | |
| template<class T, class A> | |
| void vector<T, A>::reserve(int alloc_size) { | |
| if (alloc_size <= space) return; | |
| T* p = a.allocate(alloc_size); | |
| for (int i = 0; i < sz; ++i) { | |
| a.construct(&p[i], elem[i]); | |
| } | |
| for (int j = 0; j < sz; ++j) { | |
| a.destroy(&elem[j]); | |
| } | |
| a.deallocate(elem, space); | |
| elem = p; | |
| space = alloc_size; | |
| } | |
| template<class T, class A> | |
| T& vector<T, A>::at(int n) { | |
| if (n < 0 || sz <= n) throw out_of_range("at too small"); | |
| return elem[n]; | |
| } | |
| template<class T, class A> | |
| const T& vector<T, A>::at(int n) const { | |
| if (n < 0 || sz <= n) throw out_of_range("at too small"); | |
| return elem[n]; | |
| } | |
| template <class T, class A> | |
| T& vector<T, A>::operator[](int n) { | |
| return elem[n]; | |
| } | |
| template <class T, class A> | |
| const T& vector<T, A>::operator[](int n) const { | |
| return elem[n]; | |
| } | |
| template <class T, class A> | |
| vector<T, A>& vector<T, A>::operator=(const vector<T, A>& v) { | |
| if(this == &v) return *this; | |
| if (v.sz <= space) { | |
| for (int i = 0; i < v.sz; ++i) { | |
| elem[i] = v.elem[i]; | |
| } | |
| sz = v.sz; | |
| return *this; | |
| } | |
| T* p = a.allocate(space); | |
| for (int j = 0; j < sz; ++j) { | |
| a.construct(&p[j], v.elem[j]); | |
| } | |
| for (int k = 0; k < sz; ++k) { | |
| a.destroy(&v.elem[k]); | |
| } | |
| a.deallocate(elem, space); | |
| space = sz = v.sz; | |
| elem = p; | |
| return *this; | |
| } | |
| template<class T, class A> | |
| void vector<T, A>::resize(int resize_size, T val) { | |
| reserve(resize_size); | |
| for (int i = 0; i < resize_size; ++i) { | |
| a.construct(&elem[i]); | |
| } | |
| for (int j = 0; j < resize_size; ++j) { | |
| a.destroy(&elem[j]); | |
| } | |
| sz = resize_size; | |
| } | |
| template<class T, class A> | |
| void vector<T, A>::push_back(const T &d) { | |
| if (space == 0) { | |
| reserve(10); | |
| } else if (sz == space) { | |
| reserve(2*space); | |
| } | |
| elem[sz] = d; | |
| ++sz; | |
| } | |
| int main() { | |
| vector<double> dv(3); | |
| dv.resize(5); | |
| for (int i = 0; i < dv.size(); ++i) { | |
| cout << dv[i] << " "; | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment