OpenStructure
tri_matrix.hh
Go to the documentation of this file.
1 #ifndef OST_TRI_MATRIX_HH
2 #define OST_TRI_MATRIX_HH
3 
4 #include <vector>
5 #include <cassert>
6 #include <ost/module_config.hh>
7 
8 
9 namespace ost {
10 
12 template <typename T>
13 class TEMPLATE_EXPORT TriMatrix {
14 public:
15  TriMatrix(int n, const T& def_val=T()):
16  data_((n*(n+1))/2, def_val), n_(n)
17  { }
18 
19  void Set(int i, int j, const T& sim)
20  {
21  data_[this->GetIndex(i, j)]=sim;
22  }
23 
24  const T& Get(int i, int j) const
25  {
26  return data_[this->GetIndex(i, j)];
27  }
28 
29  T& operator()(int i, int j)
30  {
31  return data_[this->GetIndex(i, j)];
32  }
33 
34  const T& operator()(int i, int j) const
35  {
36  return data_[this->GetIndex(i, j)];
37  }
38 
39  int GetSize() const
40  {
41  return n_;
42  }
43  std::vector<T>& Data()
44  {
45  return data_;
46  }
47 private:
48  int GetIndex(int i, int j) const {
49  assert(i<n_);
50  assert(j<n_);
51  if (j>i)
52  std::swap(j, i);
53  return ((2*n_-j+1)*j)/2+i-j;
54  }
55  std::vector<T> data_;
56  int n_;
57 };
58 
59 }
60 
61 #endif
triangular matrix template
Definition: tri_matrix.hh:13
TriMatrix(int n, const T &def_val=T())
Definition: tri_matrix.hh:15
const T & Get(int i, int j) const
Definition: tri_matrix.hh:24
void Set(int i, int j, const T &sim)
Definition: tri_matrix.hh:19
std::vector< T > & Data()
Definition: tri_matrix.hh:43
T & operator()(int i, int j)
Definition: tri_matrix.hh:29
int GetSize() const
Definition: tri_matrix.hh:39
const T & operator()(int i, int j) const
Definition: tri_matrix.hh:34
Definition: base.dox:1