1#ifndef TATAMI_COMPRESS_SPARSE_TRIPLETS_H
2#define TATAMI_COMPRESS_SPARSE_TRIPLETS_H
16namespace compress_triplets {
21template<
class Primary,
class Secondary>
23 if (!std::is_sorted(primary.begin(), primary.end())) {
28 while (
start < primary.size()) {
29 size_t end =
start + 1;
30 while (end < primary.size() && primary[end] == primary[
start]) {
31 if (secondary[end] < secondary[end - 1]) {
43template<
class Primary,
class Secondary>
47 while (
start < primary.size()) {
48 size_t end =
start + 1;
49 while (end < primary.size() && primary[end] == primary[
start]) {
54 if (!std::is_sorted(secondary.begin() +
start, secondary.begin() + end)) {
56 return secondary[left] < secondary[right];
64 if (primary[left] == primary[right]) {
65 return (secondary[left] < secondary[right]);
97template<
class Values_,
class RowIndices_,
class ColumnIndices_>
98std::vector<size_t>
compress_sparse_triplets(
size_t nrow,
size_t ncol, Values_& values, RowIndices_& row_indices, ColumnIndices_& column_indices,
bool csr) {
99 const size_t N = row_indices.size();
100 if (N != column_indices.size() || values.size() != N) {
101 throw std::runtime_error(
"'row_indices', 'column_indices' and 'values' should have the same length");
104 int order_status = 0;
106 order_status = compress_triplets::is_ordered(row_indices, column_indices);
108 order_status = compress_triplets::is_ordered(column_indices, row_indices);
111 if (order_status != 0) {
112 std::vector<size_t> indices(N);
113 for (
size_t i = 0; i < N; ++i) {
119 compress_triplets::order(order_status, indices, row_indices, column_indices);
121 compress_triplets::order(order_status, indices, column_indices, row_indices);
127 for (
size_t i = 0; i < indices.size(); ++i) {
128 if (indices[i] ==
static_cast<size_t>(-1)) {
132 size_t current = i, replacement = indices[i];
135 while (replacement != i) {
136 std::swap(row_indices[current], row_indices[replacement]);
137 std::swap(column_indices[current], column_indices[replacement]);
138 std::swap(values[current], values[replacement]);
140 current = replacement;
141 auto next_replacement = indices[replacement];
142 indices[replacement] = -1;
143 replacement = next_replacement;
149 std::vector<size_t> output(csr ? nrow + 1 : ncol + 1);
151 for (
auto t : row_indices) {
155 for (
auto t : column_indices) {
159 std::partial_sum(output.begin(), output.end(), output.begin());
168template<
bool row_,
class Values_,
class RowIndices_,
class ColumnIndices_>
169std::vector<size_t> compress_sparse_triplets(
size_t nrow,
size_t ncol, Values_& values, RowIndices_& row_indices, ColumnIndices_& column_indices) {
170 return compress_sparse_triplets(nrow, ncol, values, row_indices, column_indices, row_);
Flexible representations for matrix data.
Definition Extractor.hpp:15
std::vector< size_t > compress_sparse_triplets(size_t nrow, size_t ncol, Values_ &values, RowIndices_ &row_indices, ColumnIndices_ &column_indices, bool csr)
Definition compress_sparse_triplets.hpp:98
auto consecutive_extractor(const Matrix< Value_, Index_ > *mat, bool row, Index_ iter_start, Index_ iter_length, Args_ &&... args)
Definition consecutive_extractor.hpp:35