2021-04-10 13:34:50 +02:00
|
|
|
|
|
|
|
namespace lasd {
|
|
|
|
|
2021-04-14 08:42:18 +02:00
|
|
|
template <typename Data>
|
|
|
|
QueueVec<Data>::QueueVec(){
|
|
|
|
size = 4;
|
2021-04-15 12:34:16 +02:00
|
|
|
rear = 0;
|
|
|
|
front = 0;
|
2021-04-14 08:42:18 +02:00
|
|
|
Elements = new Data[size];
|
|
|
|
}
|
2021-04-10 13:34:50 +02:00
|
|
|
|
2021-04-14 08:42:18 +02:00
|
|
|
template <typename Data>
|
2021-04-19 12:32:33 +02:00
|
|
|
QueueVec<Data>::QueueVec(const LinearContainer<Data>& linear){
|
2021-04-20 18:16:51 +02:00
|
|
|
size = linear.Size()+1; // 1 free cell
|
2021-04-19 15:44:27 +02:00
|
|
|
Elements = new Data[size]; //forse da espandere
|
2021-04-14 08:42:18 +02:00
|
|
|
for(ulong i=0 ; i<linear.Size() ; ++i){
|
|
|
|
Elements[i] = linear[i];
|
|
|
|
}
|
2021-04-15 12:34:16 +02:00
|
|
|
front = 0;
|
2021-04-19 15:44:27 +02:00
|
|
|
rear = size-1; // the vector will be full
|
2021-04-15 12:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Data>
|
|
|
|
QueueVec<Data>::QueueVec(const QueueVec& toCopy){
|
2021-04-19 12:32:33 +02:00
|
|
|
size = toCopy.size;
|
2021-04-15 12:34:16 +02:00
|
|
|
ulong index_of_the_element = toCopy.front , i=0;
|
|
|
|
Elements = new Data[size];
|
|
|
|
|
|
|
|
while(index_of_the_element != toCopy.rear){
|
|
|
|
Elements[i] = toCopy[index_of_the_element];
|
|
|
|
++i;
|
|
|
|
index_of_the_element = (index_of_the_element+1)%size;
|
|
|
|
}
|
|
|
|
front = 0;
|
2021-04-19 12:32:33 +02:00
|
|
|
rear = i;
|
2021-04-15 12:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Data>
|
|
|
|
QueueVec<Data>::QueueVec(QueueVec&& toMove) noexcept{
|
2021-04-23 20:16:33 +02:00
|
|
|
Clear();
|
2021-04-15 12:34:16 +02:00
|
|
|
std::swap(Elements, toMove.Elements);
|
|
|
|
std::swap(rear, toMove.rear);
|
|
|
|
std::swap(front, toMove.front);
|
|
|
|
std::swap(size, toMove.size);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Data>
|
2021-04-19 12:32:33 +02:00
|
|
|
QueueVec<Data>::~QueueVec(){
|
2021-04-20 18:16:51 +02:00
|
|
|
//vector destructor will be automatically called I hope
|
2021-04-15 12:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Data>
|
|
|
|
QueueVec<Data>& QueueVec<Data>::operator=(const QueueVec& toCopy){
|
|
|
|
QueueVec<Data>* tmpQueue = new QueueVec<Data>(toCopy);
|
|
|
|
std::swap(*tmpQueue, *this);
|
|
|
|
delete tmpQueue;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Data>
|
|
|
|
QueueVec<Data>& QueueVec<Data>::operator=(QueueVec&& toMove) noexcept{
|
|
|
|
std::swap(Elements, toMove.Elements);
|
|
|
|
std::swap(size, toMove.size);
|
|
|
|
std::swap(rear, toMove.rear);
|
|
|
|
std::swap(front, toMove.front);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Data>
|
|
|
|
bool QueueVec<Data>::operator==(const QueueVec& toCompare) const noexcept{
|
|
|
|
if(Size() == toCompare.Size()){
|
|
|
|
ulong indexToCompare = toCompare.front;
|
|
|
|
ulong index = front;
|
|
|
|
while(indexToCompare != toCompare.rear){
|
|
|
|
if(Elements[index]!=toCompare[indexToCompare]){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
index = (index+1)%size;
|
|
|
|
indexToCompare = (indexToCompare+1)%size;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}else{
|
|
|
|
return false;
|
|
|
|
}
|
2021-04-14 08:42:18 +02:00
|
|
|
}
|
2021-04-10 13:34:50 +02:00
|
|
|
|
2021-04-15 12:34:16 +02:00
|
|
|
template <typename Data>
|
|
|
|
bool QueueVec<Data>::operator!=(const QueueVec& toCompare) const noexcept{
|
|
|
|
return !(*this == toCompare);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Data>
|
|
|
|
void QueueVec<Data>::Enqueue(const Data& data){
|
|
|
|
if((rear+1)%size == front){
|
|
|
|
Expand();
|
|
|
|
}
|
|
|
|
Elements[rear] = data;
|
|
|
|
rear = (rear + 1) % size;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Data>
|
|
|
|
void QueueVec<Data>::Enqueue(Data&& data){
|
|
|
|
if((rear+1)%size == front){
|
|
|
|
Expand();
|
|
|
|
}
|
|
|
|
std::swap(Elements[rear],data);
|
|
|
|
rear = (rear + 1) % size;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Data>
|
|
|
|
Data& QueueVec<Data>::Head() const{
|
|
|
|
if(Size()<=0){
|
2021-04-20 18:16:51 +02:00
|
|
|
throw std::length_error("Queue is empty!");
|
2021-04-15 12:34:16 +02:00
|
|
|
}
|
|
|
|
return Elements[front];
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Data>
|
|
|
|
void QueueVec<Data>::Dequeue(){
|
2021-04-19 12:32:33 +02:00
|
|
|
if(Size() <= 0){
|
2021-04-20 18:16:51 +02:00
|
|
|
throw std::length_error("Queue is empty!");
|
2021-04-15 12:34:16 +02:00
|
|
|
}
|
|
|
|
front = (front + 1) % size;
|
|
|
|
if(Size() < size/4){
|
|
|
|
Reduce();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Data>
|
|
|
|
Data QueueVec<Data>::HeadNDequeue(){
|
|
|
|
Data tmp = Head();
|
|
|
|
Dequeue();
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Data>
|
|
|
|
bool QueueVec<Data>::Empty() const noexcept{
|
|
|
|
return (front == rear);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Data>
|
2021-04-19 12:32:33 +02:00
|
|
|
ulong QueueVec<Data>::Size() const noexcept{
|
2021-04-23 20:50:23 +02:00
|
|
|
//if(size == 0) return 0; // this won't ever get executed, it's here just in case
|
2021-04-15 12:34:16 +02:00
|
|
|
return ((rear + size) - front) % size;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Data>
|
2021-04-19 12:32:33 +02:00
|
|
|
void QueueVec<Data>::Clear(){
|
|
|
|
if(size!=4){
|
|
|
|
delete[] Elements;
|
|
|
|
Elements = new Data[4];
|
|
|
|
size = 4;
|
|
|
|
}
|
|
|
|
front = 0;
|
|
|
|
rear = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Data>
|
|
|
|
void QueueVec<Data>::Expand(){
|
|
|
|
Data* tmp = new Data[size * 2];
|
|
|
|
ulong current_index = front , i=0;
|
|
|
|
while(current_index != rear){
|
|
|
|
tmp[i] = Elements[current_index];
|
|
|
|
current_index = (current_index+1)%size;
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
delete[] Elements;
|
|
|
|
Elements = tmp;
|
|
|
|
front = 0;
|
|
|
|
rear = i;
|
|
|
|
size *= 2;
|
|
|
|
}
|
2021-04-15 12:34:16 +02:00
|
|
|
|
2021-04-19 12:32:33 +02:00
|
|
|
template <typename Data>
|
|
|
|
void QueueVec<Data>::Reduce(){
|
|
|
|
if(size<=4) return; // we are not going to have vectors with less than 4 Elements
|
|
|
|
ulong newsize = (ulong)size/2;
|
|
|
|
Data* tmp = new Data[newsize];
|
|
|
|
ulong current_index = front , i=0;
|
2021-04-22 18:30:26 +02:00
|
|
|
while(current_index != rear){
|
2021-04-19 12:32:33 +02:00
|
|
|
tmp[i] = Elements[current_index];
|
|
|
|
current_index = (current_index+1)%size;
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
delete[] Elements;
|
|
|
|
Elements = tmp;
|
|
|
|
front = 0;
|
|
|
|
rear = i;
|
2021-04-22 18:30:26 +02:00
|
|
|
size = newsize;
|
2021-04-15 12:34:16 +02:00
|
|
|
}
|
2021-04-10 13:34:50 +02:00
|
|
|
|
|
|
|
}
|