barry: Your go-to motif accountant  0.0-1
Full enumeration of sample space and fast count of sufficient statistics for binary arrays
barraydense-bones.hpp
Go to the documentation of this file.
1 #ifndef BARRY_BARRAYDENSE_BONES_HPP
2 #define BARRY_BARRAYDENSE_BONES_HPP 1
3 
4 template<typename Cell_Type, typename Data_Type>
5 class BArrayDenseRow;
6 
7 template<typename Cell_Type, typename Data_Type>
9 
10 template<typename Cell_Type, typename Data_Type>
11 class BArrayDenseCol;
12 
13 template<typename Cell_Type, typename Data_Type>
15 
16 template<typename Cell_Type, typename Data_Type>
17 class BArrayDenseCell;
18 
19 template<typename Cell_Type, typename Data_Type>
21 
32 template <typename Cell_Type = bool, typename Data_Type = bool>
33 class BArrayDense {
34  friend class BArrayDenseCell<Cell_Type,Data_Type>;
35  friend class BArrayDenseCol<Cell_Type,Data_Type>;
36  friend class BArrayDenseCol_const<Cell_Type,Data_Type>;
37  friend class BArrayDenseRow<Cell_Type,Data_Type>;
38  friend class BArrayDenseRow_const<Cell_Type,Data_Type>;
39  // friend class Support<Cell_Type,Data_Type>;
40  // friend class StatsCounter<Cell_Type,Data_Type>;
41 private:
42  size_t N;
43  size_t M;
44  // size_t NCells = 0u;
45  std::vector< Cell_Type > el;
46  std::vector< Cell_Type > el_rowsums;
47  std::vector< Cell_Type > el_colsums;
48  Data_Type * data = nullptr;
49  bool delete_data = false;
50 
51  static Cell_Type Cell_default;
52  static const bool dense = true;
53 
54 public:
55 
63  bool visited = false;
64 
65 
77 
79  BArrayDense() : N(0u), M(0u), el(0u), el_rowsums(0u), el_colsums(0u) {};
80 
82  BArrayDense (size_t N_, size_t M_, Cell_Type value = static_cast<Cell_Type>(0)) :
83  N(N_), M(M_), el(N_ * M_, value),
84  el_rowsums(N_, static_cast<Cell_Type>(value * M_)), el_colsums(M_, static_cast<Cell_Type>(value * N_)) {};
85 
87  BArrayDense (
88  size_t N_,
89  size_t M_,
90  const std::vector< size_t > & source,
91  const std::vector< size_t > & target,
92  const std::vector< Cell_Type > & value,
93  bool add = true
94  );
95 
97  BArrayDense (
98  size_t N_, size_t M_,
99  const std::vector< size_t > & source,
100  const std::vector< size_t > & target,
101  bool add = true
102  );
103 
105  BArrayDense(const BArrayDense<Cell_Type,Data_Type> & Array_, bool copy_data = false);
106 
109 
112 
116 
117  bool operator==(const BArrayDense<Cell_Type,Data_Type> & Array_);
118 
119  ~BArrayDense();
120 
121  // In principle, copy can be faster by using openmp on the rows
122  // since those are independent.
123  // BArrayDense(BArrayDense & A);
124 
132  void set_data(Data_Type * data_, bool delete_data_ = false);
133  Data_Type * D_ptr();
134  const Data_Type * D_ptr() const;
135  Data_Type & D();
136  const Data_Type & D() const;
138 
139  // Function to access the elements
140  // bool check_cell
141  void out_of_range(size_t i, size_t j) const;
142  Cell_Type get_cell(size_t i, size_t j, bool check_bounds = true) const;
143  std::vector< Cell_Type > get_col_vec(size_t i, bool check_bounds = true) const;
144  std::vector< Cell_Type > get_row_vec(size_t i, bool check_bounds = true) const;
145  void get_col_vec(std::vector< Cell_Type > * x, size_t i, bool check_bounds = true) const;
146  void get_row_vec(std::vector< Cell_Type > * x, size_t i, bool check_bounds = true) const;
147 
148  BArrayDenseRow<Cell_Type,Data_Type> & row(size_t i, bool check_bounds = true);
149  const BArrayDenseRow_const<Cell_Type,Data_Type> row(size_t i, bool check_bounds = true) const;
150 
151  BArrayDenseCol<Cell_Type,Data_Type> & col(size_t j, bool check_bounds = true);
152  const BArrayDenseCol_const<Cell_Type,Data_Type> col(size_t j, bool check_bounds = true) const;
153 
164 
173  bool is_empty(size_t i, size_t j, bool check_bounds = true) const;
174  size_t nrow() const noexcept;
175  size_t ncol() const noexcept;
176  size_t nnozero() const noexcept;
177  Cell<Cell_Type> default_val() const;
179 
190  BArrayDense<Cell_Type,Data_Type> & operator+=(const std::pair<size_t, size_t> & coords);
191  BArrayDense<Cell_Type,Data_Type> & operator-=(const std::pair<size_t, size_t> & coords);
192  BArrayDenseCell<Cell_Type,Data_Type> operator()(size_t i, size_t j, bool check_bounds = true);
193  const Cell_Type operator()(size_t i, size_t j, bool check_bounds = true) const;
194 
195  void rm_cell(size_t i, size_t j, bool check_bounds = true, bool check_exists = true);
196 
197  void insert_cell(size_t i, size_t j, const Cell< Cell_Type > & v, bool check_bounds, bool);
198  // void insert_cell(size_t i, size_t j, Cell< Cell_Type > && v, bool check_bounds, bool check_exists);
199  void insert_cell(size_t i, size_t j, Cell_Type v, bool check_bounds, bool);
200 
201  void swap_cells(
202  size_t i0, size_t j0, size_t i1, size_t j1, bool check_bounds = true,
203  int check_exists = CHECK::BOTH,
204  int * report = nullptr
205  );
206 
207  void toggle_cell(size_t i, size_t j, bool check_bounds = true, int check_exists = EXISTS::UKNOWN);
208  void toggle_lock(size_t i, size_t j, bool check_bounds = true);
210 
213  void swap_rows(size_t i0, size_t i1, bool check_bounds = true);
214  void swap_cols(size_t j0, size_t j1, bool check_bounds = true);
215 
216  void zero_row(size_t i, bool check_bounds = true);
217  void zero_col(size_t j, bool check_bounds = true);
219 
220  void transpose();
221  void clear(bool hard = true);
222  void resize(size_t N_, size_t M_);
223  void reserve();
224 
225  // Advances operators
226  // void toggle_iterator
227 
228  // Misc
229  void print(const char * fmt = nullptr, ...) const;
230 
236  BArrayDense<Cell_Type,Data_Type>& operator+=(const BArrayDense<Cell_Type,Data_Type>& rhs);
237  BArrayDense<Cell_Type,Data_Type>& operator+=(const Cell_Type & rhs);
238 
239  BArrayDense<Cell_Type,Data_Type>& operator-=(const BArrayDense<Cell_Type,Data_Type>& rhs);
240  BArrayDense<Cell_Type,Data_Type>& operator-=(const Cell_Type & rhs);
241 
242  BArrayDense<Cell_Type,Data_Type>& operator/=(const Cell_Type & rhs);
243  BArrayDense<Cell_Type,Data_Type>& operator*=(const Cell_Type & rhs);
245 
246  // /**
247  // * @name Casting between types
248  // */
249  // ///@{
250  // operator BArrayDense<double,bool>() const;
251  // operator BArrayDense<int,bool>() const;
252  // operator BArrayDense<size_t,bool>() const;
253  // operator BArrayDense<bool,bool>() const;
254  // ///@}
255 
256  bool is_dense() const noexcept {return dense;};
257 
258  const std::vector< Cell_Type > & get_data() const;
259  const Cell_Type rowsum(size_t i) const;
260  const Cell_Type colsum(size_t i) const;
261 };
262 
263 #endif
Data_Type & rhs
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.
Data_Type & D()
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)
Data_Type * D_ptr()
void swap_rows(size_t i0, size_t i1, bool check_bounds=true)
BArrayDense(size_t N_, size_t M_, Cell_Type value=static_cast< Cell_Type >(0))
Empty array.
bool is_dense() const noexcept
BArrayDenseRow< Cell_Type, Data_Type > & row(size_t i, bool check_bounds=true)
void toggle_lock(size_t i, size_t j, 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
Entries in BArray. For now, it only has two members:
Definition: cell-bones.hpp:10
A wrapper class to store source, target, val from a BArray object.
Definition: typedefs.hpp:78
Data_Type &&counter_ data(std::move(counter_.data))
size_t size_t j
size_t i
Data_Type &&counter_ noexcept
Data_Type Counter_fun_type< Array_Type, Data_Type > Hasher_fun_type< Array_Type, Data_Type > Data_Type data_
Integer constants used to specify which cell should be check.
Definition: typedefs.hpp:26
const int BOTH
Definition: typedefs.hpp:27
Integer constants used to specify which cell should be check to exist or not.
Definition: typedefs.hpp:37
const int UKNOWN
Definition: typedefs.hpp:43