00001 #ifndef OST_TRI_MATRIX_HH
00002 #define OST_TRI_MATRIX_HH
00003
00004 #include <vector>
00005 #include <cassert>
00006 #include <ost/module_config.hh>
00007
00008
00009 namespace ost {
00010
00012 template <typename T>
00013 class TEMPLATE_EXPORT TriMatrix {
00014 public:
00015 TriMatrix(int n, const T& def_val=T()):
00016 data_((n*(n+1))/2, def_val), n_(n)
00017 { }
00018
00019 void Set(int i, int j, const T& sim)
00020 {
00021 data_[this->GetIndex(i, j)]=sim;
00022 }
00023
00024 const T& Get(int i, int j) const
00025 {
00026 return data_[this->GetIndex(i, j)];
00027 }
00028
00029 T& operator()(int i, int j)
00030 {
00031 return data_[this->GetIndex(i, j)];
00032 }
00033
00034 const T& operator()(int i, int j) const
00035 {
00036 return data_[this->GetIndex(i, j)];
00037 }
00038
00039 int GetSize() const
00040 {
00041 return n_;
00042 }
00043 std::vector<T>& Data()
00044 {
00045 return data_;
00046 }
00047 private:
00048 int GetIndex(int i, int j) const {
00049 assert(i<n_);
00050 assert(j<n_);
00051 if (j>i)
00052 std::swap(j, i);
00053 return ((2*n_-j+1)*j)/2+i-j;
00054 }
00055 std::vector<T> data_;
00056 int n_;
00057 };
00058
00059 }
00060
00061 #endif