lasd/librerie/exercise2/list/list.cpp

266 lines
5.8 KiB
C++
Raw Permalink Normal View History

2021-04-10 13:34:50 +02:00
namespace lasd {
/* ************************************************************************** */
// Specific constructors
template <typename Data>
List<Data>::Node::Node(const Data& newValue){
value = newValue;
next = nullptr;
}
2021-04-10 13:34:50 +02:00
// Copy constructor
template <typename Data>
List<Data>::Node::Node(const Node& copyFrom){
value = copyFrom.value;
next = nullptr;
}
// Move constructor
template <typename Data>
List<Data>::Node::Node(Node&& moveFrom){
std::swap(value, moveFrom.value);
std::swap(next, moveFrom.next);
}
template <typename Data>
List<Data>::Node::Node(Data&& moveFrom){
std::swap(value, moveFrom);
}
template <typename Data>
bool List<Data>::Node::operator==(const Node& node)const noexcept{
return (node.value == value);
}
template <typename Data>
bool List<Data>::Node::operator!=(const Node& node)const noexcept{
return !(*this == node);
}
template <typename Data>
List<Data>::List(const LinearContainer<Data>& con){
for(ulong i = 0; i<con.Size(); ++i){
InsertAtBack(con[i]);
}
}
template <typename Data>
List<Data>::List(const List<Data>& copyFrom){
for(ulong i = 0; i<copyFrom.Size(); ++i){
InsertAtBack(copyFrom[i]);
2021-04-23 21:56:01 +02:00
}
}
template <typename Data>
List<Data>::List(List<Data>&& moveFrom){
std::swap(size, moveFrom.size);
std::swap(head, moveFrom.head);
std::swap(tail, moveFrom.tail);
}
template <typename Data>
List<Data>::~List(){
Clear();
}
template <typename Data>
List<Data>& List<Data>::operator=(const List<Data>& copyFrom){
if(*this != copyFrom){
Clear();
2021-04-23 21:56:01 +02:00
for(ulong i = 0 ; i<copyFrom.Size() ; ++i){
InsertAtBack(copyFrom[i]);
}
}
return *this;
}
template <typename Data>
List<Data>& List<Data>::operator=(List<Data>&& moveFrom)noexcept{
if(*this != moveFrom){
std::swap(size, moveFrom.size);
std::swap(head, moveFrom.head);
std::swap(tail, moveFrom.tail);
}
return *this;
}
template <typename Data>
bool List<Data>::operator==(const List<Data>& list) const noexcept{
2021-04-23 21:56:01 +02:00
if(size != list.Size()) return false;
for(ulong i = 0 ; i < size ; ++i){
if((*this)[i] != list[i]) return false;
}
return true;
}
template <typename Data>
bool List<Data>::operator!=(const List<Data>& list) const noexcept{
return !(*this==list);
}
2021-04-23 21:56:01 +02:00
template <typename Data>
void List<Data>::InsertAtFront(const Data& data){
struct Node* tmp = new Node(data);
tmp->next = head;
head = tmp;
size++;
if(size == 1){
tail = head;
}
}
2021-04-23 21:56:01 +02:00
template <typename Data>
void List<Data>::InsertAtFront(Data&& data){
struct Node* tmp = new Node(std::move(data));
tmp->next = head;
head = tmp;
size++;
if(size == 1){
tail = head;
}
2021-04-23 21:56:01 +02:00
}
2021-04-23 21:56:01 +02:00
template <typename Data>
void List<Data>::RemoveFromFront(){
if(head == nullptr){
throw std::length_error("List is empty!");
}
else{
struct Node* tmp = head;
head = head->next;
tmp->next = nullptr;
delete tmp;
size--;
if(head==nullptr){
tail=nullptr;
}
}
}
2021-04-23 21:56:01 +02:00
template <typename Data>
Data List<Data>::FrontNRemove(){
if(head == nullptr){
throw std::length_error("List is empty!");
}
else{
Data value = head->value;
RemoveFromFront();
return value;
}
2021-04-23 21:56:01 +02:00
}
2021-04-23 21:56:01 +02:00
template <typename Data>
void List<Data>::InsertAtBack(const Data& data){
if(size == 0){
InsertAtFront(data);
}
2021-04-23 21:56:01 +02:00
else{
struct Node* last = new Node(data);
tail->next = last;
tail = last;
size++;
}
}
2021-04-23 21:56:01 +02:00
template <typename Data>
void List<Data>::InsertAtBack(Data&& data){
if(size == 0){
2021-06-09 09:31:27 +02:00
InsertAtFront(std::move(data));
2021-04-23 21:56:01 +02:00
}
else{
struct Node* last = new Node(std::move(data));
tail->next = last;
tail = last;
size++;
}
2021-04-23 21:56:01 +02:00
}
2021-04-23 21:56:01 +02:00
template <typename Data>
void List<Data>::Clear(){
while(head != nullptr){
RemoveFromFront();
}
2021-04-23 21:56:01 +02:00
}
2021-04-23 21:56:01 +02:00
template <typename Data>
Data& List<Data>::Front() const{
if(size == 0){
throw std::length_error("List is empty!");
}else{
return head->value;
}
2021-04-23 21:56:01 +02:00
}
2021-04-23 21:56:01 +02:00
template <typename Data>
Data& List<Data>::Back() const{
if(size == 0){
throw std::length_error("List is empty!");
}else{
return tail->value;
}
2021-04-23 21:56:01 +02:00
}
2021-04-23 21:56:01 +02:00
template <typename Data>
Data& List<Data>::operator[](const ulong index) const{
if(index >= size || index < 0){
throw std::out_of_range("Out of Range!");
}else{
struct Node* tmp = head;
for(ulong i=0; i<index; ++i){
tmp = tmp->next;
}
2021-04-23 21:56:01 +02:00
return tmp->value;
}
2021-04-23 21:56:01 +02:00
}
2021-04-23 21:56:01 +02:00
template <typename Data>
void List<Data>::MapPreOrder(MapFunctor function, void* par){
MapPreOrder(function, par, head);
}
2021-04-23 21:56:01 +02:00
template <typename Data>
void List<Data>::MapPostOrder(MapFunctor function, void* par){
MapPostOrder(function, par, head);
}
2021-04-23 21:56:01 +02:00
template <typename Data>
void List<Data>::FoldPreOrder(FoldFunctor function, const void* constPar, void* par) const{
FoldPreOrder(function, constPar, par, head);
}
2021-04-23 21:56:01 +02:00
template <typename Data>
void List<Data>::FoldPostOrder(FoldFunctor function, const void* constPar, void* par) const{
FoldPostOrder(function, constPar, par, head);
}
2021-04-23 21:56:01 +02:00
template <typename Data>
void List<Data>::MapPreOrder(MapFunctor function, void* par, struct Node* node){
if(node == nullptr) return;
function(node->value, par);
MapPreOrder(function, par, node->next);
}
2021-04-23 21:56:01 +02:00
template <typename Data>
void List<Data>::MapPostOrder(MapFunctor function, void* par, struct Node* node){
if(node == nullptr) return;
MapPostOrder(function, par, node->next);
function(node->value, par);
}
2021-04-23 21:56:01 +02:00
template <typename Data>
void List<Data>::FoldPreOrder(FoldFunctor function, const void* constPar, void* par, struct Node* node) const{
if(node == nullptr) return;
function(node->value, constPar, par);
FoldPreOrder(function, constPar, par, node->next);
}
2021-04-23 21:56:01 +02:00
template <typename Data>
void List<Data>::FoldPostOrder(FoldFunctor function, const void* constPar, void* par, struct Node* node) const{
if(node == nullptr) return;
FoldPostOrder(function, constPar, par, node->next);
function(node->value, constPar, par);
}
2021-04-10 13:34:50 +02:00
}