/*
 *  dmatrix.h
 *  (C) Porkolab Zoltan, ELTE, Budapest, Hungary
 *  (C) 2003
 */

#ifndef DMATRIX_H
#define DMATRIX_H

#include <algorithm>
#include <vector>


template<typename T>
class dmatrix
{
public:
    dmatrix( int n, int k) : n_(n), k_(k), v_(n*(2*k-1)) { }
    T get( int i, int j) const { return indiag(i,j) ? v_[atindex(i,j)] : T(); }
    void set(int i, int j, const T& t) { v_[atindex(i,j)] = t; }  

    typedef typename std::vector<T>::iterator iterator;
    typedef typename std::vector<T>::const_iterator const_iterator;

    iterator begin() { return v_.begin(); }
    iterator end()   { return v_.end();   }
    const_iterator begin() const { return v_.begin(); }
    const_iterator end() const   { return v_.end();   }

    T& operator()(int i, int j)       { return v_[atindex(i,j)]; }
    T  operator()(int i, int j) const { return v_[atindex(i,j)]; }

private:
    int n_;
    int k_;
    std::vector<T> v_;

    bool indiag(int i, int j) const { return i-j>0 ? i-j<k_ : j-i<k_; } 
    int atindex(int i, int j) const { return i*(2*k_-1) + (i-j+k_-1); }
};

#endif