#ifndef SPARSE_HPP
#define SPARSE_HPP

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>&) {}
};

#endif
