4 template<
typename Cell_Type>
7 template<
typename Cell_Type>
10 #ifndef BARRY_BARRAY_MEAT_HPP
11 #define BARRY_BARRAY_MEAT_HPP
13 #define ROW(a) this->el_ij[a]
14 #define COL(a) this->el_ji[a]
17 template<
typename Cell_Type,
typename Data_Type>
24 const std::vector< size_t > & source,
25 const std::vector< size_t > & target,
26 const std::vector< Cell_Type > & value,
30 if (source.size() != target.size())
31 throw std::length_error(
"-source- and -target- don't match on length.");
32 if (source.size() != value.size())
33 throw std::length_error(
"-sorce- and -value- don't match on length.");
44 for (
size_t i = 0u;
i < source.size(); ++
i) {
47 bool empty = this->
is_empty(source[
i], target[
i],
true);
49 ROW(source[
i])[target[
i]].add(value[
i]);
54 throw std::logic_error(
"The value already exists. Use 'add = true'.");
64 template<
typename Cell_Type,
typename Data_Type>
67 const std::vector< size_t > & source,
68 const std::vector< size_t > & target,
72 std::vector< Cell_Type > value(source.size(), (Cell_Type) 1.0);
74 if (source.size() != target.size())
75 throw std::length_error(
"-source- and -target- don't match on length.");
76 if (source.size() != value.size())
77 throw std::length_error(
"-sorce- and -value- don't match on length.");
88 for (
size_t i = 0u;
i < source.size(); ++
i) {
91 if ((source[
i] >= N_) || (target[
i] >= M_))
92 throw std::range_error(
"Either source or target point to an element outside of the range by (N,M).");
95 auto search =
ROW(source[
i]).find(target[
i]);
96 if (search !=
ROW(source[
i]).end()) {
98 throw std::logic_error(
"The value already exists. Use 'add = true'.");
102 ROW(source[
i])[target[
i]].add(value[
i]);
107 ROW(source[
i]).emplace(
114 COL(target[
i]).emplace(
116 &
ROW(source[
i])[target[
i]]
127 template<
typename Cell_Type,
typename Data_Type>
131 ) : N(Array_.N), M(Array_.M)
138 std::copy(Array_.el_ij.begin(), Array_.el_ij.end(), std::back_inserter(el_ij));
139 std::copy(Array_.el_ji.begin(), Array_.el_ji.end(), std::back_inserter(el_ji));
142 for (
size_t i = 0u;
i < N; ++
i)
145 for (
auto& r: row(
i,
false))
150 this->NCells = Array_.NCells;
151 this->visited = Array_.
visited;
154 if (Array_.data !=
nullptr)
160 data =
new Data_Type(* Array_.data );
176 template<
typename Cell_Type,
typename Data_Type>
186 this->
resize(Array_.N, Array_.M);
189 for (
size_t i = 0u;
i < N; ++
i)
195 for (
auto& r : Array_.
row(
i,
false))
196 this->
insert_cell(i, r.first, r.second.value,
false,
false);
212 if (Array_.data !=
nullptr)
215 data =
new Data_Type(*Array_.data);
229 N(0u), M(0u), NCells(0u),
231 delete_data(x.delete_data)
238 for (
size_t i = 0u;
i < N; ++
i) {
240 if (x.nnozero() == nnozero())
243 for (
auto& r : x.row(
i,
false))
244 this->insert_cell(
i, r.first, r.second.value,
false,
false);
249 if (x.data !=
nullptr)
255 data =
new Data_Type(*x.data);
279 for (
size_t i = 0u;
i < N; ++
i) {
281 if (x.nnozero() == nnozero())
284 for (
auto& r : x.row(
i,
false))
285 this->insert_cell(
i, r.first, r.second.value,
false,
false);
300 if (x.data !=
nullptr)
303 data =
new Data_Type( *x.data );
322 if ((N != Array_.
nrow()) | (M != Array_.
ncol()) | (NCells != Array_.
nnozero()))
326 if ((!
data & Array_.data) | (
data & !Array_.data))
329 if (this->el_ij != Array_.el_ij)
337 if (delete_data && (
data !=
nullptr))
344 Data_Type *
data_,
bool delete_data_
347 if ((
data !=
nullptr) && delete_data)
351 delete_data = delete_data_;
362 template<
typename Cell_Type,
typename Data_Type>
373 template<
typename Cell_Type,
typename Data_Type>
379 template<
typename Cell_Type,
typename Data_Type>
401 throw std::range_error(
"The row is out of range.");
403 throw std::range_error(
"The column is out of range.");
418 if (
ROW(
i).size() == 0u)
419 return (Cell_Type) 0.0;
422 auto search =
ROW(
i).find(
j);
423 if (search !=
ROW(
i).end())
424 return search->second.value;
427 return (Cell_Type) 0.0;
440 std::vector< Cell_Type > ans(
ncol(), (Cell_Type)
false);
441 for (
const auto & iter :
row(
i,
false))
442 ans[iter.first] = iter.second.value;
449 std::vector< Cell_Type > * x,
458 for (
const auto & iter :
row(
i,
false))
459 x->at(iter.first) = iter.second.value;
472 std::vector< Cell_Type > ans(
nrow(), (Cell_Type)
false);
473 for (
const auto iter :
col(
i,
false))
474 ans[iter.first] = iter.second->value;
481 std::vector<Cell_Type> * x,
490 for (
const auto & iter :
col(
i,
false))
491 x->at(iter.first) = iter.second->value;
503 return this->el_ij[
i];
515 return this->el_ji[
i];
523 for (
size_t i = 0u;
i < N; ++
i) {
525 if (
ROW(
i).size() == 0u)
529 res.source.push_back(
i),
530 res.target.push_back(
col->first),
531 res.val.push_back(
col->second.value);
547 if (
ROW(
i).size() == 0u)
549 else if (
COL(
j).size() == 0u)
575 return this->Cell_default;
579 const std::pair<size_t,size_t> & coords
594 const std::pair<size_t,size_t> & coords
607 template<
typename Cell_Type,
typename Data_Type>
618 template<
typename Cell_Type,
typename Data_Type>
642 if (
ROW(
i).size() == 0u)
646 if (
COL(
j).size() == 0u)
678 if (
ROW(
i).size() == 0u) {
695 throw std::logic_error(
"The cell already exists.");
725 if (
ROW(
i).size() == 0u) {
742 throw std::logic_error(
"The cell already exists.");
771 size_t i0,
size_t j0,
772 size_t i1,
size_t j1,
788 if (report !=
nullptr)
792 if ((i0 == i1) && (j0 == j1))
830 if (report !=
nullptr)
837 if (report !=
nullptr)
841 if ((i0 == i1) && (j0 == j1))
852 }
else if (!check0 & check1) {
854 if (report !=
nullptr)
860 }
else if (check0 & !check1) {
862 if (report !=
nullptr)
917 bool move0=
true, move1=
true;
918 if (
ROW(i0).size() == 0u) move0 =
false;
919 if (
ROW(i1).size() == 0u) move1 =
false;
921 if (!move0 && !move1)
930 for (
auto&
i:
row(i1,
false))
931 COL(
i.first).erase(i0);
934 for (
auto&
i:
row(i0,
false))
935 COL(
i.first).erase(i1);
940 for (
auto&
i:
row(i0,
false))
941 COL(
i.first)[i0] = &
ROW(i0)[
i.first];
944 for (
auto&
i:
row(i1,
false))
945 COL(
i.first)[i1] = &
ROW(i1)[
i.first];
964 bool check0 =
true, check1 =
true;
965 if (
COL(j0).size() == 0u) check0 =
false;
966 if (
COL(j1).size() == 0u) check1 =
false;
968 if (check0 && check1) {
974 for (
auto iter = col1.begin(); iter != col1.end(); ++iter) {
981 col_tmp.erase(iter->first);
986 if (col_tmp.size() != 0u) {
988 for (
auto iter = col_tmp.begin(); iter != col_tmp.end(); ++iter) {
989 insert_cell(iter->first, j0, *iter->second,
false,
false);
995 }
else if (check0 && !check1) {
998 for (
auto iter =
COL(j0).begin(); iter !=
COL(j0).begin(); ++iter)
999 insert_cell(iter->first, j1, *iter->second,
false,
false);
1004 }
else if (!check0 && check1) {
1007 for (
auto iter =
COL(j1).begin(); iter !=
COL(j1).begin(); ++iter) {
1010 insert_cell(iter->first, j0, *iter->second,
false,
false);
1032 if (
ROW(
i).size() == 0u)
1037 for (
auto row = row0.begin();
row != row0.end(); ++
row)
1053 if (
COL(
j).size() == 0u)
1058 for (
auto col = col0.begin();
col != col0.end(); ++
col)
1071 if (N > M) el_ji.resize(N);
1072 else if (N < M) el_ij.resize(M);
1076 for (
size_t i = 0u;
i < N; ++
i)
1080 if (
ROW(
i).size() == 0u)
1089 if (
i ==
col->first)
1116 if (N > M) el_ij.resize(M);
1117 else if (N < M) el_ji.resize(N);
1142 for (
size_t i = 0u;
i < N; ++
i)
1158 for (
size_t i = N_;
i < N; ++
i)
1163 for (
size_t j = M_;
j < M; ++
j)
1182 template<
typename Cell_Type,
typename Data_Type>
1184 #ifdef BARRAY_USE_UNORDERED_MAP
1185 for (
size_t i = 0u;
i < N;
i++)
1188 for (
size_t i = 0u;
i < M;
i++)
1195 template<
typename Cell_Type,
typename Data_Type>
1203 va_start(args, fmt);
1211 template<
typename Cell_Type,
typename Data_Type>
1226 va_start(args, fmt);
1230 for (
size_t i = 0u;
i <
nrow; ++
i)
1233 #ifdef BARRY_DEBUG_LEVEL
1234 #if BARRY_DEBUG_LEVEL > 1
1240 for (
size_t j = 0u;
j <
ncol; ++
j) {
Baseline class for binary arrays.
size_t nnozero() const noexcept
void print(const char *fmt=nullptr,...) const
BArray< Cell_Type, Data_Type > & operator-=(const std::pair< size_t, size_t > &coords)
void swap_rows(size_t i0, size_t i1, bool check_bounds=true)
void out_of_range(size_t i, size_t j) const
Cell< Cell_Type > default_val() const
bool operator==(const BArray< Cell_Type, Data_Type > &Array_)
void swap_cols(size_t j0, size_t j1, bool check_bounds=true)
void zero_col(size_t j, bool check_bounds=true)
friend class BArrayCell< Cell_Type, Data_Type >
void zero_row(size_t i, bool check_bounds=true)
void print_n(size_t nrow, size_t ncol, const char *fmt=nullptr,...) const
void resize(size_t N_, size_t M_)
void clear(bool hard=true)
const Col_type< Cell_Type > & col(size_t i, bool check_bounds=true) const
BArrayCell< Cell_Type, Data_Type > operator()(size_t i, 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 toggle_cell(size_t i, size_t j, bool check_bounds=true, int check_exists=EXISTS::UKNOWN)
void insert_cell(size_t i, size_t j, const Cell< Cell_Type > &v, bool check_bounds, bool check_exists)
size_t ncol() const noexcept
BArray< Cell_Type, Data_Type > & operator=(const BArray< Cell_Type, Data_Type > &Array_)
Assignment constructor.
BArray< Cell_Type, Data_Type > & operator+=(const std::pair< size_t, size_t > &coords)
Cell_Type get_cell(size_t i, size_t j, bool check_bounds=true) const
bool is_empty(size_t i, size_t j, bool check_bounds=true) const
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)
Entries< Cell_Type > get_entries() const
Get the edgelist.
void set_data(Data_Type *data_, bool delete_data_=false)
Set the data object.
const Row_type< Cell_Type > & row(size_t i, bool check_bounds=true) const
std::vector< Cell_Type > get_col_vec(size_t i, bool check_bounds=true) const
size_t nrow() const noexcept
std::vector< Cell_Type > get_row_vec(size_t i, bool check_bounds=true) const
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_
current_stats resize(counters->size(), 0.0)
Map< size_t, Cell< Cell_Type > > Row_type
Map< size_t, Cell< Cell_Type > * > Col_type