.. _program_listing_file_src_sparsebase_format_coo.cc: Program Listing for File coo.cc =============================== |exhale_lsh| :ref:`Return to documentation for file ` (``src/sparsebase/format/coo.cc``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp #include "sparsebase/format/coo.h" #include "sparsebase/utils/logger.h" namespace sparsebase::format { template COO::COO(COO &&rhs) : col_(std::move(rhs.col_)), row_(std::move(rhs.row_)), vals_(std::move(rhs.vals_)) { this->nnz_ = rhs.get_num_nnz(); this->order_ = 2; this->dimension_ = rhs.dimension_; rhs.col_ = std::unique_ptr>( nullptr, BlankDeleter()); rhs.row_ = std::unique_ptr>( nullptr, BlankDeleter()); rhs.vals_ = std::unique_ptr>( nullptr, BlankDeleter()); this->context_ = std::unique_ptr( new sparsebase::context::CPUContext); } template COO &COO::operator=( const COO &rhs) { this->nnz_ = rhs.nnz_; this->order_ = 2; this->dimension_ = rhs.dimension_; auto col = new IDType[rhs.get_num_nnz()]; std::copy(rhs.get_col(), rhs.get_col() + rhs.get_num_nnz(), col); auto row = new IDType[rhs.get_num_nnz()]; std::copy(rhs.get_row(), rhs.get_row() + rhs.get_num_nnz(), row); ValueType *vals = nullptr; if constexpr (!std::is_same_v) { if (rhs.get_vals() != nullptr) { vals = new ValueType[rhs.get_num_nnz()]; std::copy(rhs.get_vals(), rhs.get_vals() + rhs.get_num_nnz(), vals); } } this->col_ = std::unique_ptr>( col, Deleter()); this->row_ = std::unique_ptr>( row, Deleter()); this->vals_ = std::unique_ptr>( vals, Deleter()); return *this; } template COO::COO(const COO &rhs) : col_(nullptr, BlankDeleter()), row_(nullptr, BlankDeleter()), vals_(nullptr, BlankDeleter()) { this->nnz_ = rhs.nnz_; this->order_ = 2; this->dimension_ = rhs.dimension_; auto col = new IDType[rhs.get_num_nnz()]; std::copy(rhs.get_col(), rhs.get_col() + rhs.get_num_nnz(), col); auto row = new IDType[rhs.get_num_nnz()]; std::copy(rhs.get_row(), rhs.get_row() + rhs.get_num_nnz(), row); ValueType *vals = nullptr; if constexpr (!std::is_same_v) { if (rhs.get_vals() != nullptr) { vals = new ValueType[rhs.get_num_nnz()]; std::copy(rhs.get_vals(), rhs.get_vals() + rhs.get_num_nnz(), vals); } } this->col_ = std::unique_ptr>( col, Deleter()); this->row_ = std::unique_ptr>( row, Deleter()); this->vals_ = std::unique_ptr>( vals, Deleter()); this->context_ = std::unique_ptr( new sparsebase::context::CPUContext); } template COO::COO(IDType n, IDType m, NNZType nnz, IDType *row, IDType *col, ValueType *vals, Ownership own, bool ignore_sort) : col_(col, BlankDeleter()), row_(row, BlankDeleter()), vals_(vals, BlankDeleter()) { this->nnz_ = nnz; this->order_ = 2; this->dimension_ = {(DimensionType)n, (DimensionType)m}; if (own == kOwned) { this->col_ = std::unique_ptr>( col, Deleter()); this->row_ = std::unique_ptr>( row, Deleter()); this->vals_ = std::unique_ptr>( vals, Deleter()); } this->context_ = std::unique_ptr( new sparsebase::context::CPUContext); bool not_sorted = false; if (!ignore_sort) { IDType prev_row = 0; IDType prev_col = 0; for (DimensionType i = 0; i < nnz; i++) { if (prev_row > row[i] || (prev_row == row[i] && prev_col > col[i])) { not_sorted = true; break; } prev_row = row[i]; prev_col = col[i]; } } if (not_sorted) { utils::Logger logger(typeid(this)); logger.Log("COO arrays must be sorted. Sorting...", utils::LOG_LVL_WARNING); if constexpr (std::is_same_v) { std::vector> sort_vec; for (DimensionType i = 0; i < nnz; i++) { sort_vec.emplace_back(row[i], col[i]); } std::sort(sort_vec.begin(), sort_vec.end(), [](std::pair t1, std::pair t2) { if (t1.first == t2.first) { return t1.second < t2.second; } return t1.first < t2.first; }); for (DimensionType i = 0; i < nnz; i++) { auto &t = sort_vec[i]; row[i] = t.first; col[i] = t.second; } } else { std::vector> sort_vec; for (DimensionType i = 0; i < nnz; i++) { ValueType value = (vals != nullptr) ? vals[i] : 0; sort_vec.emplace_back(row[i], col[i], value); } std::sort(sort_vec.begin(), sort_vec.end(), [](std::tuple t1, std::tuple t2) { if (std::get<0>(t1) == std::get<0>(t2)) { return std::get<1>(t1) < std::get<1>(t2); } return std::get<0>(t1) < std::get<0>(t2); }); for (DimensionType i = 0; i < nnz; i++) { auto &t = sort_vec[i]; row[i] = std::get<0>(t); col[i] = std::get<1>(t); if (vals != nullptr) { vals[i] = std::get<2>(t); } } } } } template Format *COO::Clone() const { return new COO(*this); } template IDType *COO::get_col() const { return col_.get(); } template IDType *COO::get_row() const { return row_.get(); } template ValueType *COO::get_vals() const { return vals_.get(); } template IDType *COO::release_col() { auto col = col_.release(); this->col_ = std::unique_ptr>( col, BlankDeleter()); return col; } template IDType *COO::release_row() { auto row = row_.release(); this->row_ = std::unique_ptr>( row, BlankDeleter()); return row; } template ValueType *COO::release_vals() { auto vals = vals_.release(); this->vals_ = std::unique_ptr>( vals, BlankDeleter()); return vals; } template void COO::set_col(IDType *col, Ownership own) { if (own == kOwned) { this->col_ = std::unique_ptr>( col, Deleter()); } else { this->col_ = std::unique_ptr>( col, BlankDeleter()); } } template void COO::set_row(IDType *row, Ownership own) { if (own == kOwned) { this->row_ = std::unique_ptr>( row, Deleter()); } else { this->row_ = std::unique_ptr>( row, BlankDeleter()); } } template void COO::set_vals(ValueType *vals, Ownership own) { if (own == kOwned) { this->vals_ = std::unique_ptr>( vals, Deleter()); } else { this->vals_ = std::unique_ptr>( vals, BlankDeleter()); } } template bool COO::RowIsOwned() { return (this->row_.get_deleter().target_type() != typeid(BlankDeleter)); } template bool COO::ColIsOwned() { return (this->col_.get_deleter().target_type() != typeid(BlankDeleter)); } template bool COO::ValsIsOwned() { return (this->vals_.get_deleter().target_type() != typeid(BlankDeleter)); } template COO::~COO(){}; #ifndef _HEADER_ONLY #include "init/coo.inc" #endif } // namespace sparsebase::format