TD5 Questions 1 et 2.
This commit is contained in:
parent
6c85293853
commit
0b57df1fe5
4 changed files with 363 additions and 0 deletions
104
td/td5/include/deque.h
Normal file
104
td/td5/include/deque.h
Normal file
|
@ -0,0 +1,104 @@
|
|||
#ifndef HEADER_DEQUE
|
||||
#define HEADER_DEQUE
|
||||
|
||||
#include "vector.h"
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
|
||||
template <class T>
|
||||
class Deque : public Vector<T> {
|
||||
public:
|
||||
Deque() : Vector<T>(), _frontIndex(-1) {};
|
||||
Deque(size_t capacity): Vector<T>(capacity), _frontIndex(-1) {};
|
||||
Deque(size_t capacity, T value): Vector<T>(capacity,value), _frontIndex(-1) {};
|
||||
Deque(const Vector<T>& other) : Vector<T>(other), _frontIndex(-1) {};
|
||||
void PushFront(T value);
|
||||
Deque<T> GetFront(void);
|
||||
Deque<T> GetBack(void);
|
||||
void RemoveFront(void);
|
||||
void RemoveBack(void);
|
||||
void ShrinkToFit(void);
|
||||
private:
|
||||
int64_t _frontIndex;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
void Deque<T>::PushFront(T value)
|
||||
{
|
||||
// if the next size will overload the heap
|
||||
// We create a new allocation, and shift each data
|
||||
if (this->_capacity < this->_size + 1) {
|
||||
auto oldPtr = this->_values;
|
||||
this->_values = new T[this->_size * 2];
|
||||
this->_capacity *= 2;
|
||||
for (size_t i = 0; i < this->_size; i++)
|
||||
this->_values[i+1] = oldPtr[i];
|
||||
delete[] oldPtr;
|
||||
}
|
||||
// Else we only shift the data
|
||||
else{
|
||||
// _values[0] will be overwritten, so it serves as a temporary value
|
||||
// To store the overwritten index
|
||||
auto tempActual(this->_values[0]);
|
||||
for(size_t i=0; i< this->_size ; i++)
|
||||
{
|
||||
this->_values[0] = this->_values[i+1];
|
||||
this->_values[i+1] = tempActual;
|
||||
tempActual = this->_values[0];
|
||||
}
|
||||
}
|
||||
this->_values[0] = value;
|
||||
this->_size++;
|
||||
_frontIndex++;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
Deque<T> Deque<T>::GetFront(void)
|
||||
{
|
||||
if(this->_frontIndex == -1)
|
||||
throw "This Deque doesn't have front elements";
|
||||
Deque<T> returnDeque(this->_frontIndex+1);
|
||||
returnDeque._size = this->_frontIndex+1;
|
||||
for(int64_t i=0; i< this->_frontIndex+1 ; i++)
|
||||
returnDeque._values[i] = this->_values[i];
|
||||
return returnDeque;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
Deque<T> Deque<T>::GetBack(void)
|
||||
{
|
||||
Deque<T> returnDeque(this->_size-(this->_frontIndex+1));
|
||||
returnDeque._size = this->_size-(this->_frontIndex+1);
|
||||
for(size_t i = this->_frontIndex+1, j=0 ; i<this->_size ; i++, j++)
|
||||
returnDeque._values[j] = this->_values[i];
|
||||
return returnDeque;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void Deque<T>::RemoveFront(void)
|
||||
{
|
||||
for(size_t i = this->_frontIndex+1, j=0 ; i<this->_size ; i++, j++)
|
||||
this->_values[j] = this->_values[i];
|
||||
this->_size -= (this->_frontIndex+1);
|
||||
this->_frontIndex = -1;
|
||||
}
|
||||
|
||||
// The Capacity is not shrunk during this process.
|
||||
template <class T>
|
||||
void Deque<T>::RemoveBack(void)
|
||||
{
|
||||
this->_size = this->_frontIndex+1;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void Deque<T>::ShrinkToFit(void)
|
||||
{
|
||||
auto oldPtr = this->_values;
|
||||
this->_values = new T[this->_size];
|
||||
for(size_t i=0; i<this->_size; i++)
|
||||
this->_values[i] = oldPtr[i];
|
||||
delete[] oldPtr;
|
||||
this->_capacity = this->_size;
|
||||
}
|
||||
|
||||
#endif //HEADER_DEQUE
|
47
td/td5/include/smart_vector.h
Normal file
47
td/td5/include/smart_vector.h
Normal file
|
@ -0,0 +1,47 @@
|
|||
#ifndef HEADER_SMART_VECTOR
|
||||
#define HEADER_SMART_VECTOR
|
||||
|
||||
#include "vector.h"
|
||||
#include <algorithm>
|
||||
|
||||
template <class T>
|
||||
class Smart_Vector : public Vector<T> {
|
||||
public:
|
||||
Smart_Vector() : Vector<T>() {};
|
||||
Smart_Vector(size_t capacity): Vector<T>(capacity) {};
|
||||
Smart_Vector(size_t capacity, T value): Vector<T>(capacity,value) {};
|
||||
Smart_Vector(const Vector<T>& other) : Vector<T>(other) {};
|
||||
Smart_Vector<T> operator+(const Smart_Vector<T>& other);
|
||||
Smart_Vector<T>& operator+=(const Smart_Vector<T>& other);
|
||||
};
|
||||
|
||||
template <class T>
|
||||
Smart_Vector<T> Smart_Vector<T>::operator+(const Smart_Vector<T>& other)
|
||||
{
|
||||
// Gathering the vector with the greater size.
|
||||
auto& maxVector = (this->_size > other._size ? *this : other);
|
||||
Smart_Vector<T> returnVector(maxVector._size);
|
||||
returnVector._size = returnVector._capacity;
|
||||
size_t i(0);
|
||||
|
||||
// Adding both vectors from 0 to the minimum size
|
||||
for(i=0; i<std::min(this->_size,other._size); i++)
|
||||
returnVector._values[i] = this->_values[i] + other._values[i];
|
||||
// Then simply copying the max vector values onto the return vector.
|
||||
for(size_t j=i; j<returnVector._size; j++)
|
||||
returnVector._values[j] = maxVector._values[j];
|
||||
return returnVector;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
Smart_Vector<T>& Smart_Vector<T>::operator+=(const Smart_Vector<T>& other)
|
||||
{
|
||||
size_t i(0), minimumSize(std::min(this->_size,other._size));
|
||||
for(i = 0; i<minimumSize ; i++)
|
||||
this->_values[i] += other._values[i];
|
||||
for(size_t j=i; j<other._size ; j++)
|
||||
this->PushBack(other._values[j]);
|
||||
return *this;
|
||||
}
|
||||
|
||||
#endif // HEADER_SMART_VECTOR
|
198
td/td5/include/vector.h
Normal file
198
td/td5/include/vector.h
Normal file
|
@ -0,0 +1,198 @@
|
|||
#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);
|
||||
template <class Tfriend>
|
||||
friend Vector<Tfriend> operator*(const Vector<Tfriend>& vect, Tfriend value);
|
||||
template <class Tfriend>
|
||||
friend Vector<Tfriend> operator*(const Vector<Tfriend>& vect, Tfriend value);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
Vector<T> operator*(const Vector<T>& vect, T value)
|
||||
{
|
||||
auto returnVector = vect;
|
||||
for(size_t i=0; i<vect._size; i++)
|
||||
returnVector._values[i] *= value;
|
||||
return returnVector;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
Vector<T> operator*(T value, const Vector<T>& vect)
|
||||
{
|
||||
return (vect * value);
|
||||
}
|
||||
|
||||
#endif // HEADER_VECTOR
|
14
td/td5/src/main.cpp
Normal file
14
td/td5/src/main.cpp
Normal file
|
@ -0,0 +1,14 @@
|
|||
#include "../include/vector.h"
|
||||
#include "../include/smart_vector.h"
|
||||
#include "../include/deque.h"
|
||||
// Joker : slice.h
|
||||
// Joker : vector_iteraor.h
|
||||
#include <iostream>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
Vector<float> floatVect(4,3.5f);
|
||||
std::cout << floatVect * 9.0f << std::endl;
|
||||
std::cout << 9.0f * floatVect << std::endl;
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Reference in a new issue