#include "sparse.hpp"

template <class T>
SparseArray<T>::~SparseArray()
{
    while (first) {
        Item *tmp = first;
        first = first->next;
        delete tmp;
    }
}

template <class T>
const T& SparseArray<T>::operator[](int idx) const
{
    Item *tmp;
    for (tmp = first; tmp; tmp = tmp->next)
        if (tmp->index == idx)
            return tmp->value;
    return defval;
}

template <class T>
int SparseArray<T>::NonzeroCount() const
{
    int sum = 0;
    for (Item *tmp = first; tmp; tmp = tmp->next)
        sum++;
    return sum;
}

template <class T>
T& SparseArray<T>::Interm::Provide()
{
    Item *tmp;
    for (tmp = master->first; tmp; tmp = tmp->next)
        if (tmp->index == index)
            return tmp->value;
    tmp = new Item;
    tmp->index = index;
    tmp->value = master->defval;
    tmp->next = master->first;
    master->first = tmp;
    return tmp->value;
}

template<class T>
void SparseArray<T>::Interm::Remove()
{
    Item **tmp;
    for (tmp = &(master->first); *tmp; tmp = &(*tmp)->next)
        if ((*tmp)->index == index) {
            Item *to_delete = *tmp;
            *tmp = (*tmp)->next;
            delete to_delete;
            return;
        }
}

template <class T>
T SparseArray<T>::Interm::operator=(const T &x)
{
    if (x == master->defval)
        Remove();
    else
        Provide() = x;
    return x;
}

template <class T>
T SparseArray<T>::Interm::operator=(const Interm& x)
{
    T value = x;
    return *this = value;
}

template <class T>
T SparseArray<T>::Interm::operator+=(const T& x)
{
    T& location = Provide();
    location += x;
    T res = location;
    if (res == master->defval)
        Remove();
    return res;
}

template <class T>
T SparseArray<T>::Interm::operator-=(const T& x)
{
    T& location = Provide();
    location -= x;
    T res = location;
    if (res == master->defval)
        Remove();
    return res;
}

template <class T>
T SparseArray<T>::Interm::operator*=(const T& x)
{
    T& location = Provide();
    location *= x;
    T res = location;
    if (res == master->defval)
        Remove();
    return res;
}

template <class T>
T SparseArray<T>::Interm::operator/=(const T& x)
{
    T& location = Provide();
    location /= x;
    T res = location;
    if (res == master->defval)
        Remove();
    return res;
}

template <class T>
T SparseArray<T>::Interm::operator%=(const T& x)
{
    T& location = Provide();
    location %= x;
    T res = location;
    if (res == master->defval)
        Remove();
    return res;
}

template <class T>
T SparseArray<T>::Interm::operator<<=(int n)
{
    T& location = Provide();
    location <<= n;
    T res = location;
    if (res == master->defval)
        Remove();
    return res;
}

template <class T>
T SparseArray<T>::Interm::operator>>=(int n)
{
    T& location = Provide();
    location >>= n;
    T res = location;
    if (res == master->defval)
        Remove();
    return res;
}

template <class T>
T SparseArray<T>::Interm::operator|=(const T& x)
{
    T& location = Provide();
    location |= x;
    T res = location;
    if (res == master->defval)
        Remove();
    return res;
}

template <class T>
T SparseArray<T>::Interm::operator&=(const T& x)
{
    T& location = Provide();
    location &= x;
    T res = location;
    if (res == master->defval)
        Remove();
    return res;
}

template <class T>
T SparseArray<T>::Interm::operator^=(const T& x)
{
    T& location = Provide();
    location ^= x;
    T res = location;
    if (res == master->defval)
        Remove();
    return res;
}

template <class T>
T SparseArray<T>::Interm::operator++()
{
    T& location = Provide();
    ++location;
    T res = location;
    if (location == master->defval)
        Remove();
    return res;
}

template <class T>
T SparseArray<T>::Interm::operator++(int)
{
    T& location = Provide();
    T res = location;
    location++;
    if (location == master->defval)
        Remove();
    return res;
}

template <class T>
T SparseArray<T>::Interm::operator--()
{
    T& location = Provide();
    --location;
    T res = location;
    if (location == master->defval)
        Remove();
    return res;
}

template <class T>
T SparseArray<T>::Interm::operator--(int)
{
    T& location = Provide();
    T res = location;
    location--;
    if (location == master->defval)
        Remove();
    return res;
}

template <class T>
SparseArray<T>::Interm::operator T() const
{
    Item *tmp;
    for (tmp = master->first; tmp; tmp = tmp->next)
        if (tmp->index == index)
            return tmp->value;
    return master->defval;
}
