4 #ifndef BARRY_BARRAYDENSE_MEAT_HPP
5 #define BARRY_BARRAYDENSE_MEAT_HPP
7 template<
typename Cell_Type,
typename Data_Type>
10 template<
typename Cell_Type,
typename Data_Type>
13 template<
typename Cell_Type,
typename Data_Type>
16 template<
typename Cell_Type,
typename Data_Type>
19 template<
typename Cell_Type,
typename Data_Type>
23 #define ROW(a) this->el_ij[a]
24 #define COL(a) this->el_ji[a]
25 #define POS(a,b) (b)*N + (a)
26 #define POS_N(a,b,c) (b)*(c) + (a)
28 template<
typename Cell_Type,
typename Data_Type>
31 #define ZERO_CELL static_cast<Cell_Type>(0.0)
34 template<
typename Cell_Type,
typename Data_Type>
38 const std::vector< size_t > & source,
39 const std::vector< size_t > & target,
40 const std::vector< Cell_Type > & value,
44 if (source.size() != target.size())
45 throw std::length_error(
"-source- and -target- don't match on length.");
46 if (source.size() != value.size())
47 throw std::length_error(
"-sorce- and -value- don't match on length.");
50 for (
size_t i = 0u;
i < source.size(); ++
i)
54 bool empty =
is_empty(source[
i], target[
i],
true);
58 Cell_Type tmp = el[
POS(source[
i], target[
i])];
60 el_rowsums[source[
i]] += (value[
i] - tmp);
61 el_colsums[target[
i]] += (value[
i] - tmp);
63 el[
POS(source[
i], target[
i])] += value[
i];
70 throw std::logic_error(
"The value already exists. Use 'add = true'.");
72 el[
POS(source[
i], target[
i])] = value[
i];
74 el_rowsums[source[
i]] += value[
i];
75 el_colsums[target[
i]] += value[
i];
85 template<
typename Cell_Type,
typename Data_Type>
88 const std::vector< size_t > & source,
89 const std::vector< size_t > & target,
93 std::vector< Cell_Type > value(source.size(),
static_cast<Cell_Type
>(1.0));
95 if (source.size() != target.size())
96 throw std::length_error(
"-source- and -target- don't match on length.");
97 if (source.size() != value.size())
98 throw std::length_error(
"-sorce- and -value- don't match on length.");
102 for (
size_t i = 0u;
i < source.size(); ++
i)
106 bool empty =
is_empty(source[
i], target[
i],
true);
110 Cell_Type tmp = el[
POS(source[
i], target[
i])];
112 el_rowsums[source[
i]] += (value[
i] - tmp);
113 el_colsums[target[
i]] += (value[
i] - tmp);
115 el[
POS(source[
i], target[
i])] += value[
i];
122 throw std::logic_error(
"The value already exists. Use 'add = true'.");
124 el[
POS(source[
i], target[
i])] = value[
i];
126 el_rowsums[source[
i]] += value[
i];
127 el_colsums[target[
i]] += value[
i];
134 template<
typename Cell_Type,
typename Data_Type>
138 ) : N(Array_.N), M(Array_.M){
142 el_rowsums = Array_.el_rowsums;
143 el_colsums = Array_.el_colsums;
156 if (Array_.data !=
nullptr)
162 data =
new Data_Type(*Array_.data);
178 template<
typename Cell_Type,
typename Data_Type>
188 el_rowsums = Array_.el_rowsums;
189 el_colsums = Array_.el_colsums;
214 if (Array_.data !=
nullptr)
217 data =
new Data_Type(*Array_.data);
228 template<
typename Cell_Type,
typename Data_Type>
232 N(std::move(x.N)), M(std::move(x.M)),
235 el_rowsums(std::move(x.el_rowsums)),
236 el_colsums(std::move(x.el_colsums)),
237 data(std::move(x.data)),
238 delete_data(std::move(x.delete_data))
242 x.delete_data =
false;
246 template<
typename Cell_Type,
typename Data_Type>
260 std::swap(el_rowsums, x.el_rowsums);
261 std::swap(el_colsums, x.el_colsums);
273 if (x.data !=
nullptr)
276 data = std::move(x.data);
277 delete_data = x.delete_data;
279 x.delete_data =
false;
290 template<
typename Cell_Type,
typename Data_Type>
296 if ( (N != Array_.
nrow()) | (M != Array_.
ncol()) )
300 if ((!
data & Array_.data) | (
data & !Array_.data))
303 if (this->el != Array_.el)
309 template<
typename Cell_Type,
typename Data_Type>
312 if (delete_data && (
data !=
nullptr))
318 template<
typename Cell_Type,
typename Data_Type>
324 if ((
data !=
nullptr) && delete_data)
328 delete_data = delete_data_;
334 template<
typename Cell_Type,
typename Data_Type>
339 template<
typename Cell_Type,
typename Data_Type>
344 template<
typename Cell_Type,
typename Data_Type>
349 template<
typename Cell_Type,
typename Data_Type>
354 template<
typename Cell_Type,
typename Data_Type>
362 std::string err_msg =
"The row is out of range: " + std::to_string(
i) +
" >= " + std::to_string(N);
363 throw std::range_error(err_msg);
367 std::string err_msg =
"The column is out of range: " + std::to_string(
j) +
" >= " + std::to_string(M);
368 throw std::range_error(err_msg);
375 template<
typename Cell_Type,
typename Data_Type>
386 return el[
POS(
i,
j)];
390 template<
typename Cell_Type,
typename Data_Type>
400 std::vector< Cell_Type > ans;
402 for (
size_t j = 0u;
j < M; ++
j)
403 ans.push_back(el[
POS(
i,
j)]);
410 std::vector<Cell_Type> * x,
419 for (
size_t j = 0u;
j < M; ++
j)
420 x->operator[](
j) = el[
POS(
i,
j)];
433 std::vector< Cell_Type > ans;
435 for (
size_t j = 0u;
j < N; ++
j)
436 ans.push_back(el[
POS(
j,
i)]);
443 std::vector<Cell_Type> * x,
452 #ifdef __INTEL_LLVM_COMPILER
453 #pragma code_align 32
455 #if defined(__OPENMP) || defined(_OPENMP)
458 for (
size_t j = 0u;
j < N; ++
j)
459 x->operator[](
j) = el[
POS(
j,
i)];
462 template<
typename Cell_Type,
typename Data_Type>
475 template<
typename Cell_Type,
typename Data_Type>
488 template<
typename Cell_Type,
typename Data_Type>
502 template<
typename Cell_Type,
typename Data_Type>
518 size_t nzero = this->nnozero();
522 for (
size_t i = 0u;
i < N; ++
i)
524 for (
size_t j = 0u;
j < M; ++
j)
530 res.source.push_back(
i),
531 res.target.push_back(
j),
576 template<
typename Cell_Type,
typename Data_Type>
578 return this->Cell_default;
581 template<
typename Cell_Type,
typename Data_Type>
583 const std::pair<size_t,size_t> & coords
587 size_t i = coords.first;
588 size_t j = coords.second;
600 template<
typename Cell_Type,
typename Data_Type>
602 const std::pair<size_t,size_t> & coords
605 size_t i = coords.first;
606 size_t j = coords.second;
610 Cell_Type old = el[
POS(
i,
j)];
613 el_rowsums[
i] -= old;
614 el_colsums[
j] -= old;
620 template<
typename Cell_Type,
typename Data_Type>
631 template<
typename Cell_Type,
typename Data_Type>
645 template<
typename Cell_Type,
typename Data_Type>
660 el_rowsums[
i] -= el[
POS(
i,
j)];
661 el_colsums[
j] -= el[
POS(
i,
j)];
668 template<
typename Cell_Type,
typename Data_Type>
690 Cell_Type old = el[
POS(
i,
j)];
691 el_rowsums[
i] += (v.
value - old);
692 el_colsums[
j] += (v.
value - old);
724 Cell_Type old = el[
POS(
i,
j)];
725 el_rowsums[
i] += (v - old);
726 el_colsums[
j] += (v - old);
735 size_t i0,
size_t j0,
736 size_t i1,
size_t j1,
749 if (report !=
nullptr)
753 if ((i0 == i1) && (j0 == j1))
757 Cell_Type val0 = el[
POS(i0,j0)];
758 Cell_Type val1 = el[
POS(i1,j1)];
760 rm_cell(i0, j0,
false,
false);
761 rm_cell(i1, j1,
false,
false);
764 insert_cell(i0, j0, val1,
false,
false);
765 insert_cell(i1, j1, val0,
false,
false);
782 insert_cell(
i,
j,1,
false,
false);
784 rm_cell(
i,
j,
false,
false);
809 for (
size_t j = 0u;
j < M; ++
j)
810 std::swap(el[
POS(i0,
j)], el[
POS(i1,
j)]);
812 std::swap(el_rowsums[i0], el_rowsums[i1]);
827 out_of_range(0u, j0);
828 out_of_range(0u, j1);
837 for (
size_t i = 0u;
i < N; ++
i)
838 std::swap(el[
POS(
i, j0)], el[
POS(
i, j1)]);
840 std::swap(el_colsums[j0], el_colsums[j1]);
857 for (
size_t col = 0u; col < M; col++)
858 rm_cell(
i, col,
false,
false);
876 for (
size_t row = 0u; row < N; row++)
877 rm_cell(row,
j,
false,
false);
897 std::vector< Cell< Cell_Type > > tmp_el(std::move(el));
899 for (
size_t i = 0u;
i < N; ++
i)
900 for (
size_t j = 0u;
j < M; ++
j)
905 std::swap(el_rowsums, el_colsums);
917 std::fill(el.begin(), el.end(),
ZERO_CELL);
918 std::fill(el_rowsums.begin(), el_rowsums.end(),
ZERO_CELL);
919 std::fill(el_colsums.begin(), el_colsums.end(),
ZERO_CELL);
931 std::vector< Cell_Type > el_tmp(el);
936 for (
size_t i = 0u;
i < N; ++
i)
942 for (
size_t j = 0u;
j < M; ++
j)
948 insert_cell(
i,
j, el_tmp[
POS_N(
i,
j, N_)],
false,
false);
964 el_rowsums.reserve(N);
965 el_colsums.reserve(M);
981 for (
size_t i = 0u;
i < N; ++
i)
986 for (
size_t j = 0u;
j < M; ++
j)
989 if (this->is_empty(
i,
j,
false))
992 printf_barry(
" %.2f ",
static_cast<double>(this->get_cell(
i,
j,
false)));
1011 return el_rowsums[
i];
1016 return el_colsums[
j];
#define BARRY_UNUSED(expr)
friend class BArrayDenseCol_const< Cell_Type, Data_Type >
friend class BArrayDenseCol< Cell_Type, Data_Type >
Baseline class for binary arrays.
Cell< Cell_Type > default_val() const
std::vector< Cell_Type > get_col_vec(size_t i, bool check_bounds=true) const
void zero_row(size_t i, bool check_bounds=true)
bool is_empty(size_t i, size_t j, bool check_bounds=true) const
BArrayDense< Cell_Type, Data_Type > & operator=(const BArrayDense< Cell_Type, Data_Type > &Array_)
Assignment constructor.
bool operator==(const BArrayDense< Cell_Type, Data_Type > &Array_)
void swap_cols(size_t j0, size_t j1, bool check_bounds=true)
const Cell_Type rowsum(size_t i) const
size_t ncol() const noexcept
const std::vector< Cell_Type > & get_data() const
void print(const char *fmt=nullptr,...) const
size_t nrow() const noexcept
size_t nnozero() const noexcept
void swap_cells(size_t i0, size_t j0, size_t i1, size_t j1, bool check_bounds=true, int check_exists=CHECK::BOTH, int *report=nullptr)
void insert_cell(size_t i, size_t j, const Cell< Cell_Type > &v, bool check_bounds, bool)
BArrayDenseCol< Cell_Type, Data_Type > & col(size_t j, bool check_bounds=true)
void rm_cell(size_t i, size_t j, bool check_bounds=true, bool check_exists=true)
void clear(bool hard=true)
void set_data(Data_Type *data_, bool delete_data_=false)
Set the data object.
std::vector< Cell_Type > get_row_vec(size_t i, bool check_bounds=true) const
Entries< Cell_Type > get_entries() const
Get the edgelist.
void out_of_range(size_t i, size_t j) const
void toggle_cell(size_t i, size_t j, bool check_bounds=true, int check_exists=EXISTS::UKNOWN)
void zero_col(size_t j, bool check_bounds=true)
void swap_rows(size_t i0, size_t i1, bool check_bounds=true)
BArrayDense< Cell_Type, Data_Type > & operator+=(const std::pair< size_t, size_t > &coords)
BArrayDenseCell< Cell_Type, Data_Type > operator()(size_t i, size_t j, bool check_bounds=true)
BArrayDenseRow< Cell_Type, Data_Type > & row(size_t i, bool check_bounds=true)
const Cell_Type colsum(size_t i) const
BArrayDense()
Zero-size array.
void resize(size_t N_, size_t M_)
Cell_Type get_cell(size_t i, size_t j, bool check_bounds=true) const
BArrayDense< Cell_Type, Data_Type > & operator-=(const std::pair< size_t, size_t > &coords)
Entries in BArray. For now, it only has two members:
A wrapper class to store source, target, val from a BArray object.
Data_Type &&counter_ data(std::move(counter_.data))
Data_Type &&counter_ noexcept
Data_Type Counter_fun_type< Array_Type, Data_Type > Hasher_fun_type< Array_Type, Data_Type > Data_Type data_