#include <stdio.h>

template <class T>
class SparseArray {
    struct Item {
        int index;
        T value;
        Item *next;
    };
    Item *first;
    T defval;
public:
    SparseArray(const T &dv) : first(0), defval(dv) {}
    ~SparseArray();
    class Interm {
        friend class SparseArray<T>;
        SparseArray<T> *master;
        int index;
        Interm(SparseArray<T> *a_master, int ind)
            : master(a_master), index(ind) {}
        T& Provide();
        void Remove();
    public:
        operator T() const;
        T operator=(const T& x);
        T operator=(const Interm& x);

        T operator+=(const T& x);
        T operator-=(const T& x);
        T operator*=(const T& x);
        T operator/=(const T& x);
        T operator%=(const T& x);

        T operator<<=(int n);
        T operator>>=(int n);

        T operator|=(const T& x);
        T operator&=(const T& x);
        T operator^=(const T& x);

        T operator++();
        T operator++(int);
        T operator--();
        T operator--(int);

        T operator->() const { return *this; }
    };
    friend class Interm;

    Interm operator[](int idx) { return Interm(this, idx); }
    const T& operator[](int idx) const;

    int NonzeroCount() const;
private:
    SparseArray(const SparseArray<T>&) {}
    void operator=(const SparseArray<T>&) {}
};

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;
}

class Counter {
    int n;
public:
    static int copies;
    Counter(int a = 0) : n(a) {}
    Counter(const Counter& other) : n(other.n) { copies++; }
    void Add(int k) { n += k; }
    int Get() const { return n; }
    bool operator==(const Counter& other) const { return n == other.n; }
};

int Counter::copies = 0;

struct Point {
    int x, y;
};

/* gets the array by const reference, so arr[i] is the const version */
int total(const SparseArray<Counter>& arr, int from, int to)
{
    int sum = 0;
    for (int i = from; i <= to; i++)
        sum += arr[i].Get();
    return sum;
}

/* the const version gives the stored object itself, not a copy */
void test_reference()
{
    SparseArray<Counter> a(Counter(0));
    const SparseArray<Counter>& c = a;
    a[1] = Counter(5);
    a[3] = Counter(7);
    printf("values:               %d %d %d\n",
           c[1].Get(), c[2].Get(), c[3].Get());
    printf("same element twice:   %d\n", &c[1] == &c[1]);
    printf("missing are defval:   %d\n", &c[2] == &c[100]);
    printf("existing is not:      %d\n", &c[1] == &c[2]);

    Counter::copies = 0;
    printf("total:                %d\n", total(a, 0, 10));
    printf("copies by const []:   %d\n", Counter::copies);
    Counter::copies = 0;
    Counter x = a[1];
    printf("copies by Interm:     %d (value %d)\n",
           Counter::copies, x.Get());
}

/* the reference follows the element, and can't be used to change it */
void test_reference_lifetime()
{
    SparseArray<Counter> a(Counter(0));
    const SparseArray<Counter>& c = a;
    a[3] = Counter(7);
    const Counter& r = c[3];
    a[3] = Counter(8);
    printf("reference sees 8:     %d\n", r.Get());
#ifdef ERRORS
    c[3].Add(5);
    c[3] = Counter(1);
#endif
#ifdef DANGLING
    a[3] = Counter(0);          /* removes the element r refers to */
    printf("%d\n", r.Get());    /* reads freed memory */
#endif
}

void test_fixes()
{
    SparseArray<int> a(0);
    SparseArray<Point*> ptrs(0);
    Point p = { 3, 4 };
    a[1] = 10;
    a[2] = 20;
    a[1] = a[2];
    printf("a[1] = a[2]:          %d\n", (int) a[1]);
    ptrs[1] = &p;
    ptrs[1]->x = 30;
    printf("arrow:                %d\n", p.x);
    ptrs[9].operator->();
    printf("count after ->:       %d\n", ptrs.NonzeroCount());
}

int main()
{
    test_reference();
    test_reference_lifetime();
    test_fixes();
    return 0;
}
