From 235767988d6b3f1de48ca88d76a87a506ad6f5c8 Mon Sep 17 00:00:00 2001 From: Baptiste Wicht Date: Sun, 23 Feb 2014 17:07:53 +0100 Subject: [PATCH] Add copy support --- tstl/include/vector.hpp | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/tstl/include/vector.hpp b/tstl/include/vector.hpp index b1274786..ce15b74d 100644 --- a/tstl/include/vector.hpp +++ b/tstl/include/vector.hpp @@ -34,9 +34,33 @@ public: vector() : data(nullptr), _size(0), _capacity(0) {} explicit vector(uint64_t c) : data(new T[c]), _size(0), _capacity(c) {} - // Disable copy for now - vector(const vector& rhs) = delete; - vector& operator=(const vector& rhs) = delete; + vector(const vector& rhs) : data(nullptr), _size(rhs._size), _capacity(rhs._capacity) { + if(!rhs.empty()){ + data = new T[_capacity]; + + for(size_t i = 0; i < _size; ++i){ + data[i] = rhs.data[i]; + } + } + } + + vector& operator=(const vector& rhs){ + if(data && _capacity < rhs._capacity){ + delete[] data; + data = nullptr; + } + + _size = rhs._size; + _capacity = rhs._capacity; + + if(!rhs.empty()){ + data = new T[_capacity]; + + for(size_t i = 0; i < _size; ++i){ + data[i] = rhs.data[i]; + } + } + } //Move constructors @@ -47,6 +71,10 @@ public: }; vector& operator=(vector&& rhs){ + if(data){ + delete[] data; + } + data = rhs.data; _size = rhs._size; _capacity = rhs._capacity;