TD2 : Fixed some issues when compiling with Warning 4 level. TD3 : Boiler plate for Smart Vector
This commit is contained in:
parent
88c6f71ec1
commit
a5e1c60073
4 changed files with 207 additions and 9 deletions
|
@ -144,7 +144,7 @@ std::istream& operator>>(std::istream& stream, Vector<T>& vec)
|
||||||
template <class T>
|
template <class T>
|
||||||
T& Vector<T>::operator[](const int indice)
|
T& Vector<T>::operator[](const int indice)
|
||||||
{
|
{
|
||||||
if(indice > _size)
|
if (static_cast<size_t>(indice) > _size)
|
||||||
throw "Indice from the vector is not reachable";
|
throw "Indice from the vector is not reachable";
|
||||||
else
|
else
|
||||||
return _values[indice];
|
return _values[indice];
|
||||||
|
@ -155,11 +155,10 @@ Vector<T> Vector<T>::operator+(const Vector<T>& other)
|
||||||
{
|
{
|
||||||
if (_size != other._size)
|
if (_size != other._size)
|
||||||
throw "Cannot add two vectors with different sizes together";
|
throw "Cannot add two vectors with different sizes together";
|
||||||
else
|
else {
|
||||||
{
|
|
||||||
Vector<T> returnVector(_size);
|
Vector<T> returnVector(_size);
|
||||||
returnVector._size = _size;
|
returnVector._size = _size;
|
||||||
for(auto i =0; i < _size; i++)
|
for (size_t i = 0; i < _size; i++)
|
||||||
returnVector._values[i] = _values[i] + other._values[i];
|
returnVector._values[i] = _values[i] + other._values[i];
|
||||||
return returnVector;
|
return returnVector;
|
||||||
}
|
}
|
||||||
|
@ -170,9 +169,8 @@ Vector<T>& Vector<T>::operator+=(const Vector<T>& other)
|
||||||
{
|
{
|
||||||
if (_size != other._size)
|
if (_size != other._size)
|
||||||
throw "Cannot add two vectors with different sizes together";
|
throw "Cannot add two vectors with different sizes together";
|
||||||
else
|
else {
|
||||||
{
|
for (size_t i = 0; i < _size; i++)
|
||||||
for(auto i =0; i < _size; i++)
|
|
||||||
_values[i] += other._values[i];
|
_values[i] += other._values[i];
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
13
td/td3/include/smart_vector.h
Normal file
13
td/td3/include/smart_vector.h
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
#ifndef HEADER_SMART_VECTOR
|
||||||
|
#define HEADER_SMART_VECTOR
|
||||||
|
|
||||||
|
#include "vector.h"
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
class Smart_Vector : public Vector<T> {
|
||||||
|
public:
|
||||||
|
Smart_Vector<T> operator+(const Smart_Vector<T>& other);
|
||||||
|
Smart_Vector<T>& operator+=(const Smart_Vector<T>& other);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // HEADER_SMART_VECTOR
|
179
td/td3/include/vector.h
Normal file
179
td/td3/include/vector.h
Normal file
|
@ -0,0 +1,179 @@
|
||||||
|
#ifndef HEADER_VECTOR
|
||||||
|
#define HEADER_VECTOR
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
#include <ostream>
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
class Vector {
|
||||||
|
public:
|
||||||
|
Vector();
|
||||||
|
Vector(size_t capacity);
|
||||||
|
Vector(size_t capacity, T value);
|
||||||
|
Vector(const Vector<T>& other);
|
||||||
|
~Vector();
|
||||||
|
size_t Size();
|
||||||
|
size_t Capacity();
|
||||||
|
void PushBack(T value);
|
||||||
|
template <class Tfriend>
|
||||||
|
friend std::ostream& operator<<(std::ostream& stream, const Vector<Tfriend>& vec);
|
||||||
|
template <class Tfriend>
|
||||||
|
friend std::istream& operator>>(std::istream& stream, Vector<Tfriend>& vec);
|
||||||
|
Vector<T>& operator=(const Vector<T>& other);
|
||||||
|
Vector<T>& operator()(const Vector<T>& other);
|
||||||
|
T& operator[](const int indice);
|
||||||
|
Vector<T> operator+(const Vector<T>& other);
|
||||||
|
Vector<T>& operator+=(const Vector<T>& other);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
size_t _capacity;
|
||||||
|
size_t _size;
|
||||||
|
T* _values;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
Vector<T>::Vector()
|
||||||
|
: _capacity(1)
|
||||||
|
, _size(0)
|
||||||
|
, _values(new T[1])
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
Vector<T>::Vector(size_t capacity)
|
||||||
|
: _capacity(capacity)
|
||||||
|
, _size(0)
|
||||||
|
{
|
||||||
|
// Allocating the heap with the given capacity
|
||||||
|
_values = new T[capacity];
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
Vector<T>::Vector(size_t capacity, T value)
|
||||||
|
: _capacity(capacity)
|
||||||
|
, _size(capacity)
|
||||||
|
{
|
||||||
|
// Allocating the heap and setting the value to each element.
|
||||||
|
_values = new T[capacity];
|
||||||
|
for (size_t i = 0; i < capacity; i++)
|
||||||
|
_values[i] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
Vector<T>::Vector(const Vector& other)
|
||||||
|
: _capacity(other._capacity)
|
||||||
|
, _size(other._size)
|
||||||
|
, _values(new T[other._capacity])
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < other._size; i++)
|
||||||
|
_values[i] = other._values[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
Vector<T>::~Vector()
|
||||||
|
{
|
||||||
|
delete[] _values;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
Vector<T>& Vector<T>::operator=(const Vector<T>& other)
|
||||||
|
{
|
||||||
|
if (this != &other) {
|
||||||
|
this->_capacity = other._capacity;
|
||||||
|
this->_size = other._size;
|
||||||
|
delete[] this->_values;
|
||||||
|
this->_values = new T[other._capacity];
|
||||||
|
for (size_t i = 0; i < other._size; i++)
|
||||||
|
this->_values[i] = other._values[i];
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
Vector<T>& Vector<T>::operator()(const Vector<T>& other)
|
||||||
|
{
|
||||||
|
*this = other;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
size_t Vector<T>::Capacity() { return _capacity; }
|
||||||
|
template <class T>
|
||||||
|
size_t Vector<T>::Size() { return _size; }
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
void Vector<T>::PushBack(T value)
|
||||||
|
{
|
||||||
|
// if the next size will overload the heap
|
||||||
|
if (_capacity < _size + 1) {
|
||||||
|
// Keep a track of the old pointer
|
||||||
|
T* oldPtr = _values;
|
||||||
|
// Creating a new pointer by resizing its capacity by factor 2.
|
||||||
|
_values = new T[_size * 2];
|
||||||
|
_capacity *= 2;
|
||||||
|
// Copying the old array onto the new
|
||||||
|
for (size_t i = 0; i < _size; i++)
|
||||||
|
_values[i] = oldPtr[i];
|
||||||
|
// Deallocating the old array
|
||||||
|
delete[] oldPtr;
|
||||||
|
}
|
||||||
|
// Setting the given value and incrementing the size
|
||||||
|
_values[_size] = value;
|
||||||
|
_size++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// methode get
|
||||||
|
template <class T>
|
||||||
|
std::ostream& operator<<(std::ostream& stream, const Vector<T>& vec)
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < vec._size; i++)
|
||||||
|
stream << vec._values[i] << ", ";
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
// methode put
|
||||||
|
template <class T>
|
||||||
|
std::istream& operator>>(std::istream& stream, Vector<T>& vec)
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < vec._capacity; i++)
|
||||||
|
stream >> vec._values[i];
|
||||||
|
vec._size = vec._capacity;
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
T& Vector<T>::operator[](const int indice)
|
||||||
|
{
|
||||||
|
if (static_cast<size_t>(indice) > _size)
|
||||||
|
throw "Indice from the vector is not reachable";
|
||||||
|
else
|
||||||
|
return _values[indice];
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
Vector<T> Vector<T>::operator+(const Vector<T>& other)
|
||||||
|
{
|
||||||
|
if (_size != other._size)
|
||||||
|
throw "Cannot add two vectors with different sizes together";
|
||||||
|
else {
|
||||||
|
Vector<T> returnVector(_size);
|
||||||
|
returnVector._size = _size;
|
||||||
|
for (size_t i = 0; i < _size; i++)
|
||||||
|
returnVector._values[i] = _values[i] + other._values[i];
|
||||||
|
return returnVector;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
Vector<T>& Vector<T>::operator+=(const Vector<T>& other)
|
||||||
|
{
|
||||||
|
if (_size != other._size)
|
||||||
|
throw "Cannot add two vectors with different sizes together";
|
||||||
|
else {
|
||||||
|
for (size_t i = 0; i < _size; i++)
|
||||||
|
_values[i] += other._values[i];
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // HEADER_VECTOR
|
8
td/td3/src/main.cpp
Normal file
8
td/td3/src/main.cpp
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
#include "../include/vector.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
std::cout << "Hello TD3 !" << std::endl;
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue