Library 2

Cleaner code
This commit is contained in:
Alessandro Ferro
2021-04-20 18:16:51 +02:00
parent 3fdd2e86cb
commit aa9dcc16cf
8 changed files with 158 additions and 113 deletions

View File

@@ -1,27 +1,17 @@
namespace lasd {
/* ************************************************************************** */
template <typename Data>
QueueLst<Data>::QueueLst(const LinearContainer<Data>& linear){
for(ulong i=0; i<linear.Size() ; ++i){
Enqueue(linear[i]);
}
}
QueueLst<Data>::QueueLst(const LinearContainer<Data>& linear)
: List<Data>(linear) {}
template <typename Data>
QueueLst<Data>::QueueLst(const QueueLst& copyFrom){
for(ulong i=0; i<copyFrom.Size() ; ++i){
Enqueue(copyFrom[i]);
}
}
QueueLst<Data>::QueueLst(const QueueLst& copyFrom)
: List<Data>(copyFrom) {}
template <typename Data>
QueueLst<Data>::QueueLst(QueueLst&& moveFrom) noexcept{
std::swap(head,moveFrom.head);
std::swap(tail,moveFrom.tail);
std::swap(size,moveFrom.size);
}
QueueLst<Data>::QueueLst(QueueLst&& moveFrom) noexcept
: List<Data>(std::move(moveFrom)) {}
template <typename Data>
QueueLst<Data>::~QueueLst(){
@@ -30,11 +20,6 @@ QueueLst<Data>::~QueueLst(){
template <typename Data>
QueueLst<Data>& QueueLst<Data>::operator=(const QueueLst& toCopy){
// QueueLst<Data>* tmp = new QueueLst<Data>(toCopy);
// std::swap(*this, *tmp);
// delete tmp;
// return *this;
List<Data>::operator=(toCopy);
return *this;
}
@@ -62,7 +47,7 @@ template <typename Data>
template <typename Data>
void QueueLst<Data>::Enqueue(Data&& data){
List<Data>::InsertAtBack(data);
List<Data>::InsertAtBack(std::move(data));
}
template <typename Data>
@@ -84,6 +69,5 @@ template <typename Data>
void QueueLst<Data>::Clear(){
List<Data>::Clear();
}
/* ************************************************************************** */
}

View File

@@ -1,7 +1,6 @@
namespace lasd {
/* ************************************************************************** */
template <typename Data>
QueueVec<Data>::QueueVec(){
size = 4;
@@ -12,7 +11,7 @@ QueueVec<Data>::QueueVec(){
template <typename Data>
QueueVec<Data>::QueueVec(const LinearContainer<Data>& linear){
size = linear.Size()+1;
size = linear.Size()+1; // 1 free cell
Elements = new Data[size]; //forse da espandere
for(ulong i=0 ; i<linear.Size() ; ++i){
Elements[i] = linear[i];
@@ -38,10 +37,8 @@ QueueVec<Data>::QueueVec(const QueueVec& toCopy){
template <typename Data>
QueueVec<Data>::QueueVec(QueueVec&& toMove) noexcept{
/*
we initialize size=4 so the swapped vector won't be in an
inconsistent state (size=0 can never be accettable)
*/
/* we initialize size=4 so the swapped vector won't be in an
inconsistent state (size=0 can never be acceptable) */
size = 4;
std::swap(Elements, toMove.Elements);
std::swap(rear, toMove.rear);
@@ -51,7 +48,7 @@ QueueVec<Data>::QueueVec(QueueVec&& toMove) noexcept{
template <typename Data>
QueueVec<Data>::~QueueVec(){
//vector destructor will be called automatically i hope
//vector destructor will be automatically called I hope
}
template <typename Data>
@@ -64,6 +61,8 @@ QueueVec<Data>& QueueVec<Data>::operator=(const QueueVec& toCopy){
template <typename Data>
QueueVec<Data>& QueueVec<Data>::operator=(QueueVec&& toMove) noexcept{
/* here we do not need size=4 since the QueueVec with at least size=4
exists already */
std::swap(Elements, toMove.Elements);
std::swap(size, toMove.size);
std::swap(rear, toMove.rear);
@@ -115,7 +114,7 @@ void QueueVec<Data>::Enqueue(Data&& data){
template <typename Data>
Data& QueueVec<Data>::Head() const{
if(Size()<=0){
throw std::length_error("Queuevec is empty!");
throw std::length_error("Queue is empty!");
}
return Elements[front];
}
@@ -123,7 +122,7 @@ Data& QueueVec<Data>::Head() const{
template <typename Data>
void QueueVec<Data>::Dequeue(){
if(Size() <= 0){
throw std::length_error("Queuevec is empty!");
throw std::length_error("Queue is empty!");
}
front = (front + 1) % size;
if(Size() < size/4){
@@ -192,6 +191,5 @@ void QueueVec<Data>::Reduce(){
rear = i;
size *= 2;
}
/* ************************************************************************** */
}