barry: Your go-to motif accountant  0.0-1
Full enumeration of sample space and fast count of sufficient statistics for binary arrays
barray-bones.hpp
Go to the documentation of this file.
1 // #include <vector>
2 // #include <unordered_map>
3 // #include "typedefs.hpp"
4 // #include "cell-bones.hpp"
5 // #include "barraycell-bones.hpp"
6 
7 #ifndef BARRAY_BONES_HPP
8 #define BARRAY_BONES_HPP 1
9 
10 template<typename Cell_Type, typename Data_Type>
11 class BArrayCell;
12 
13 template<typename Cell_Type, typename Data_Type>
14 class BArrayCell_const;
15 
27 template <typename Cell_Type = bool, typename Data_Type = bool>
28 class BArray {
29  friend class BArrayCell<Cell_Type,Data_Type>;
30  friend class BArrayCell_const<Cell_Type,Data_Type>;
31  // friend class Support<Cell_Type,Data_Type>;
32  // friend class StatsCounter<Cell_Type,Data_Type>;
33 private:
34  size_t N;
35  size_t M;
36  size_t NCells = 0u;
37  std::vector< Row_type< Cell_Type > > el_ij;
38  std::vector< Col_type< Cell_Type > > el_ji;
39  Data_Type * data = nullptr;
40  bool delete_data = false;
41 
42  static Cell< Cell_Type > Cell_default;
43  static const bool dense = false;
44 
45 public:
46 
54  bool visited = false;
55 
56 
67 
69  BArray() : N(0u), M(0u), NCells(0u), el_ij(0u), el_ji(0u) {};
70 
72  BArray (size_t N_, size_t M_) : N(N_), M(M_), NCells(0u), el_ij(N_), el_ji(M_) {};
73 
76  size_t N_, size_t M_,
77  const std::vector< size_t > & source,
78  const std::vector< size_t > & target,
79  const std::vector< Cell_Type > & value,
80  bool add = true
81  );
82 
85  size_t N_, size_t M_,
86  const std::vector< size_t > & source,
87  const std::vector< size_t > & target,
88  bool add = true
89  );
90 
92  BArray(const BArray<Cell_Type,Data_Type> & Array_, bool copy_data = false);
93 
96 
99 
103 
105 
107 
108  // In principle, copy can be faster by using openmp on the rows
109  // since those are independent.
110  // BArray(BArray & A);
111 
119  void set_data(Data_Type * data_, bool delete_data_ = false);
120  Data_Type * D_ptr();
121  const Data_Type * D_ptr() const;
122  Data_Type & D();
123  const Data_Type & D() const;
124  void flush_data();
126 
127  // Function to access the elements
128  // bool check_cell
129  void out_of_range(size_t i, size_t j) const;
130  Cell_Type get_cell(size_t i, size_t j, bool check_bounds = true) const;
131  std::vector< Cell_Type > get_col_vec(size_t i, bool check_bounds = true) const;
132  std::vector< Cell_Type > get_row_vec(size_t i, bool check_bounds = true) const;
133  void get_col_vec(std::vector< Cell_Type > * x, size_t i, bool check_bounds = true) const;
134  void get_row_vec(std::vector< Cell_Type > * x, size_t i, bool check_bounds = true) const;
135  const Row_type< Cell_Type > & row(size_t i, bool check_bounds = true) const;
136  const Col_type< Cell_Type > & col(size_t i, bool check_bounds = true) const;
137 
148 
157  bool is_empty(size_t i, size_t j, bool check_bounds = true) const;
158  size_t nrow() const noexcept;
159  size_t ncol() const noexcept;
160  size_t nnozero() const noexcept;
161  Cell<Cell_Type> default_val() const;
163 
174  BArray<Cell_Type,Data_Type> & operator+=(const std::pair<size_t, size_t> & coords);
175  BArray<Cell_Type,Data_Type> & operator-=(const std::pair<size_t, size_t> & coords);
176  BArrayCell<Cell_Type,Data_Type> operator()(size_t i, size_t j, bool check_bounds = true);
177  const Cell_Type operator()(size_t i, size_t j, bool check_bounds = true) const;
178 
179  void rm_cell(size_t i, size_t j, bool check_bounds = true, bool check_exists = true);
180 
181  void insert_cell(size_t i, size_t j, const Cell< Cell_Type > & v, bool check_bounds, bool check_exists);
182  void insert_cell(size_t i, size_t j, Cell< Cell_Type > && v, bool check_bounds, bool check_exists);
183  void insert_cell(size_t i, size_t j, Cell_Type v, bool check_bounds, bool check_exists);
184 
186  size_t i0, size_t j0, size_t i1, size_t j1, bool check_bounds = true,
187  int check_exists = CHECK::BOTH,
188  int * report = nullptr
189  );
190 
191  void toggle_cell(size_t i, size_t j, bool check_bounds = true, int check_exists = EXISTS::UKNOWN);
192  void toggle_lock(size_t i, size_t j, bool check_bounds = true);
194 
197  void swap_rows(size_t i0, size_t i1, bool check_bounds = true);
198  void swap_cols(size_t j0, size_t j1, bool check_bounds = true);
199 
200  void zero_row(size_t i, bool check_bounds = true);
201  void zero_col(size_t j, bool check_bounds = true);
203 
204  void transpose();
205  void clear(bool hard = true);
206  void resize(size_t N_, size_t M_);
207  void reserve();
208 
209  // Advances operators
210  // void toggle_iterator
211 
212  // Misc
213  void print(const char * fmt = nullptr, ...) const;
214  void print_n(size_t nrow, size_t ncol, const char * fmt = nullptr, ...) const;
215 
221  BArray<Cell_Type,Data_Type>& operator+=(const BArray<Cell_Type,Data_Type>& rhs);
222  BArray<Cell_Type,Data_Type>& operator+=(const Cell_Type & rhs);
223 
224  BArray<Cell_Type,Data_Type>& operator-=(const BArray<Cell_Type,Data_Type>& rhs);
225  BArray<Cell_Type,Data_Type>& operator-=(const Cell_Type & rhs);
226 
227  BArray<Cell_Type,Data_Type>& operator/=(const Cell_Type & rhs);
228  BArray<Cell_Type,Data_Type>& operator*=(const Cell_Type & rhs);
230 
231  // /**
232  // * @name Casting between types
233  // */
234  // ///@{
235  // operator BArray<double,bool>() const;
236  // operator BArray<int,bool>() const;
237  // operator BArray<size_t,bool>() const;
238  // operator BArray<bool,bool>() const;
239  // ///@}
240 
241  bool is_dense() const noexcept {return dense;};
242 
243 };
244 
245 #endif
Data_Type & rhs
Baseline class for binary arrays.
void flush_data()
void transpose()
size_t nnozero() const noexcept
void print(const char *fmt=nullptr,...) const
void swap_rows(size_t i0, size_t i1, bool check_bounds=true)
bool is_dense() const noexcept
BArray(size_t N_, size_t M_, const std::vector< size_t > &source, const std::vector< size_t > &target, const std::vector< Cell_Type > &value, bool add=true)
Edgelist with data.
BArray(BArray< Cell_Type, Data_Type > &&x) noexcept
Move operator.
void out_of_range(size_t i, size_t j) const
BArray()
Zero-size array.
BArray(const BArray< Cell_Type, Data_Type > &Array_, bool copy_data=false)
Copy constructor.
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)
const Data_Type & D() const
Data_Type * D_ptr()
const Data_Type * D_ptr() const
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 reserve()
void resize(size_t N_, size_t M_)
void clear(bool hard=true)
void toggle_lock(size_t i, size_t j, bool check_bounds=true)
const Col_type< Cell_Type > & col(size_t i, bool check_bounds=true) const
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)
BArray< Cell_Type, Data_Type > & operator=(BArray< Cell_Type, Data_Type > &&x) noexcept
Move assignment.
void insert_cell(size_t i, size_t j, const Cell< Cell_Type > &v, bool check_bounds, bool check_exists)
Data_Type & D()
size_t ncol() const noexcept
BArray< Cell_Type, Data_Type > & operator=(const BArray< Cell_Type, Data_Type > &Array_)
Assignment constructor.
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
void get_col_vec(std::vector< Cell_Type > *x, size_t i, bool check_bounds=true) const
bool visited
std::vector< Cell_Type > get_row_vec(size_t i, bool check_bounds=true) const
BArray(size_t N_, size_t M_, const std::vector< size_t > &source, const std::vector< size_t > &target, bool add=true)
Edgelist with no data (simpler)
BArray(size_t N_, size_t M_)
Empty array.
void get_row_vec(std::vector< Cell_Type > *x, size_t i, 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
Map< size_t, Cell< Cell_Type > > Row_type
Definition: typedefs.hpp:67
Map< size_t, Cell< Cell_Type > * > Col_type
Definition: typedefs.hpp:70