tahoma2d/toonz/sources/include/tlin/tlin_sparsemat.h

103 lines
2.8 KiB
C
Raw Normal View History

2016-05-17 03:04:11 +12:00
#pragma once
2016-03-19 06:57:51 +13:00
#ifndef TLIN_SPARSEMAT_H
#define TLIN_SPARSEMAT_H
#include "tlin_basicops.h"
#include "tcg/tcg_hash.h"
2016-06-15 18:43:10 +12:00
namespace tlin {
2016-03-19 06:57:51 +13:00
//-------------------------------------------------------------------------------
/*!
2016-06-15 18:43:10 +12:00
The tlin::sparse_matrix class implements a hash-based format for storing
sparse
2016-03-19 06:57:51 +13:00
matrices.
2016-06-15 18:43:10 +12:00
A tlin::sparse_matrix is a tlin::matrix-compliant class that allows the user
to
access the matrix in a purely random fashion, without sacrificing storage
space or
efficiency. This is achieved using a single, fast, dynamic hash container that
maps
2016-03-19 06:57:51 +13:00
(row, col) entries into their values, the hash function being like:
(row, col) -> row * nCols + col; (taken modulo nBuckets)
2016-06-15 18:43:10 +12:00
2016-03-19 06:57:51 +13:00
Observe that if the number of buckets of the hash map is nCols, the resulting
representation is similar to a CCS (Harwell-Boeing), with the difference that
buckets are not sorted by row.
*/
template <typename T>
2016-06-15 18:43:10 +12:00
class sparse_matrix {
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
struct IdxFunctor {
int m_cols;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
IdxFunctor(int cols) : m_cols(cols) {}
int operator()(const std::pair<int, int> &key) {
return key.first * m_cols + key.second;
}
};
2016-03-19 06:57:51 +13:00
private:
2016-06-15 18:43:10 +12:00
int m_rows;
int m_cols;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
tcg::hash<std::pair<int, int>, T, IdxFunctor> m_hash;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
sparse_matrix() : m_rows(0), m_cols(0), m_hash(IdxFunctor(0)) {}
sparse_matrix(int rows, int cols)
: m_rows(rows), m_cols(cols), m_hash(cols) {}
~sparse_matrix() {}
sparse_matrix(const sparse_matrix &mat)
: m_rows(mat.m_rows), m_cols(mat.m_cols), m_hash(mat.m_hash) {}
sparse_matrix &operator=(const sparse_matrix &mat) {
m_rows = mat.m_rows, m_cols = mat.m_cols, m_hash = mat.m_hash;
return *this;
}
int rows() const { return m_rows; }
int cols() const { return m_cols; }
typedef typename tcg::hash<std::pair<int, int>, T, IdxFunctor> HashMap;
tcg::hash<std::pair<int, int>, T, IdxFunctor> &entries() { return m_hash; }
const tcg::hash<std::pair<int, int>, T, IdxFunctor> &entries() const {
return m_hash;
}
//!\warning Assignments of kind: A.at(1,1) = A.at(0,0) are potentially
//!harmful.
//! Use A.get(x,y) on the right side instead.
T &at(int row, int col) { return m_hash.touch(std::make_pair(row, col), 0); }
const T get(int row, int col) const {
typename HashMap::const_iterator it = m_hash.find(std::make_pair(row, col));
return (it == m_hash.end()) ? 0 : it->m_val;
}
const T operator()(int row, int col) const { return get(row, col); }
void clear() { m_hash.clear(); }
friend void swap(sparse_matrix &a, sparse_matrix &b) {
using std::swap;
swap(a.m_rows, b.m_rows), swap(a.m_cols, b.m_cols);
swap(a.m_hash, b.m_hash);
}
2016-03-19 06:57:51 +13:00
};
//-------------------------------------------------------------------
// The Standard matrix data type in tlin is double
typedef tlin::sparse_matrix<double> spmat;
}
2016-06-15 18:43:10 +12:00
#endif // TLIN_SPARSEMAT_H