118 lines
2.2 KiB
C++
118 lines
2.2 KiB
C++
#include <algorithm>
|
|
#include <iostream>
|
|
|
|
#include "list.h"
|
|
|
|
// TDDE18 - Lab 6 - Templates
|
|
|
|
|
|
template <typename T>
|
|
void List<T>::insert(T const& d)
|
|
{
|
|
first = new Link(d, first);
|
|
}
|
|
|
|
//-----------------------------------------------------//
|
|
// Important copy and assignment stuff
|
|
template <typename T>
|
|
typename List<T>::Link*
|
|
List<T>::Link::clone(Link const* dolly)
|
|
{
|
|
if ( dolly != nullptr )
|
|
return new Link(dolly->data, clone(dolly->next));
|
|
else
|
|
return nullptr;
|
|
}
|
|
|
|
template <typename T>
|
|
List<T>::List() : first(nullptr)
|
|
{
|
|
std::clog << "***Default construction" << std::endl;
|
|
}
|
|
|
|
template <typename T>
|
|
List<T>::List(List const& l)
|
|
{
|
|
std::clog << "***Copy construction" << std::endl;
|
|
first = Link::clone(l.first);
|
|
}
|
|
|
|
template <typename T>
|
|
List<T>::List(List&& l)
|
|
{
|
|
std::clog << "***Move construction" << std::endl;
|
|
first = l.first;
|
|
l.first = nullptr;
|
|
}
|
|
|
|
template <typename T>
|
|
List<T>& List<T>::operator=(List const& rhs)
|
|
{
|
|
std::clog << "***Copy assignment" << std::endl;
|
|
if (&rhs != this)
|
|
{
|
|
List copy(rhs);
|
|
std::swap(first, copy.first);
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
template <typename T>
|
|
List<T>& List<T>::operator=(List&& rhs)
|
|
{
|
|
std::clog << "***Move assignment" << std::endl;
|
|
if (&rhs != this)
|
|
{
|
|
std::swap(first, rhs.first);
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
|
|
/////////////////////
|
|
/// List Iterator ///
|
|
/////////////////////
|
|
|
|
template <typename T>
|
|
List<T>::Iterator::Iterator(Link* ln): l{ln}
|
|
{}
|
|
|
|
template <typename T>
|
|
typename List<T>::Iterator& List<T>::Iterator::operator++() {
|
|
l = l->next;
|
|
return *this;
|
|
}
|
|
|
|
template <typename T>
|
|
bool List<T>::Iterator::operator!=(Iterator const& other) const {
|
|
return l != other.l;
|
|
}
|
|
|
|
template <typename T>
|
|
bool List<T>::Iterator::operator==(Iterator const& other) const {
|
|
return l == other.l;
|
|
}
|
|
|
|
template <typename T>
|
|
T& List<T>::Iterator::operator*() const {
|
|
return l->data;
|
|
}
|
|
|
|
/// Begin and End functions
|
|
template <typename T>
|
|
typename List<T>::Iterator List<T>::begin() const {
|
|
return Iterator(first);
|
|
}
|
|
|
|
template <typename T>
|
|
typename List<T>::Iterator List<T>::end() const {
|
|
return Iterator(nullptr);
|
|
}
|
|
|
|
// OutStream operator
|
|
template <typename T>
|
|
std::ostream& operator<<(std::ostream& os, const List<T>& lst){
|
|
for (auto i : lst)
|
|
os << i << " ";
|
|
return os;
|
|
}
|