From aa407d0fabcc05f0fb84ac0873bdfa628548f040 Mon Sep 17 00:00:00 2001 From: marco <marco@5a81b35b-ba03-0410-adc8-b2c5c5119f08> Date: Tue, 27 Apr 2010 12:32:29 +0000 Subject: [PATCH] triangular matrix template git-svn-id: https://dng.biozentrum.unibas.ch/svn/openstructure/trunk@2120 5a81b35b-ba03-0410-adc8-b2c5c5119f08 --- modules/base/src/CMakeLists.txt | 1 + modules/base/src/tri_matrix.hh | 61 +++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 modules/base/src/tri_matrix.hh diff --git a/modules/base/src/CMakeLists.txt b/modules/base/src/CMakeLists.txt index b30a8312b..ecdc389db 100644 --- a/modules/base/src/CMakeLists.txt +++ b/modules/base/src/CMakeLists.txt @@ -28,6 +28,7 @@ platform.hh string_ref.hh pod_vector.hh fixed_string.hh +tri_matrix.hh ) module(NAME base SOURCES ${OST_BASE_SOURCES} diff --git a/modules/base/src/tri_matrix.hh b/modules/base/src/tri_matrix.hh new file mode 100644 index 000000000..21545ffda --- /dev/null +++ b/modules/base/src/tri_matrix.hh @@ -0,0 +1,61 @@ +#ifndef OST_TRI_MATRIX_HH +#define OST_TRI_MATRIX_HH + +#include <vector> +#include <cassert> +#include <ost/module_config.hh> + + +namespace ost { + +/// \brief triangular matrix template +template <typename T> +class TEMPLATE_EXPORT TriMatrix { +public: + TriMatrix(int n, const T& def_val=T()): + data_((n*(n+1))/2, def_val), n_(n) + { } + + void Set(int i, int j, const T& sim) + { + data_[this->GetIndex(i, j)]=sim; + } + + const T& Get(int i, int j) const + { + return data_[this->GetIndex(i, j)]; + } + + T& operator()(int i, int j) + { + return data_[this->GetIndex(i, j)]; + } + + const T& operator()(int i, int j) const + { + return data_[this->GetIndex(i, j)]; + } + + int GetSize() const + { + return n_; + } + std::vector<T>& Data() + { + return data_; + } +private: + int GetIndex(int i, int j) const { + assert(i<n_); + assert(j<n_); + if (j>i) + std::swap(j, i); + return ((2*n_-j+1)*j)/2+i-j; + } + std::vector<T> data_; + int n_; +}; + +} + +#endif -- GitLab