barry: Your go-to motif accountant  0.0-1
Full enumeration of sample space and fast count of sufficient statistics for binary arrays
powerset-meat.hpp
Go to the documentation of this file.
1 #ifndef BARRY_POWERSET_MEAT_HPP
2 #define BARRY_POWERSET_MEAT_HPP 1
3 
4 template <typename Array_Type, typename Data_Rule_Type>
6  const Array_Type & array
7 ) : EmptyArray(array), data(0u),
8  rules(new Rules<Array_Type,Data_Rule_Type>()), N(array.nrow()), M(array.ncol()) {
9 
10 }
11 
12 template <typename Array_Type, typename Data_Rule_Type>
14  if (!this->rules_deleted)
15  delete rules;
16 }
17 
18 template <typename Array_Type, typename Data_Rule_Type>
20 {
21 
22  // Computing the locations
23  coordinates_free.clear();
24  coordinates_locked.clear();
25  rules->get_seq(EmptyArray, &coordinates_free, &coordinates_locked);
26 
27  n_free = coordinates_free.size() / 2u;
28  n_locked = coordinates_locked.size() / 2u;
29 
30  // Computing initial statistics
31  if (EmptyArray.nnozero() > 0u)
32  {
33 
34  if (EmptyArray.is_dense())
35  {
36 
37  for (size_t i = 0u; i < n_free; ++i)
38  EmptyArray(
39  coordinates_free[i * 2u],
40  coordinates_free[i * 2u + 1u]
41  ) = 0;
42 
43  }
44  else
45  {
46 
47  for (size_t i = 0u; i < n_free; ++i)
48  EmptyArray.rm_cell(
49  coordinates_free[i * 2u],
50  coordinates_free[i * 2u + 1u],
51  false,
52  true
53  );
54 
55 
56  }
57 
58  }
59 
60  // EmptyArray.clear(true);
61  // EmptyArray.reserve();
62 
63  // Resizing support
64  data.reserve(pow(2.0, n_free));
65 
66  // Adding the empty array to the set
67  data.push_back(EmptyArray);
68 
69  return;
70 }
71 
72 template <typename Array_Type, typename Data_Rule_Type>
74  size_t pos
75 )
76 {
77 
78  // Did we reached the end??
79  if (pos >= n_free)
80  return;
81 
82  // We will pass it to the next step, if the iteration makes sense.
83  calc_backend_sparse(pos + 1u);
84 
85  // Toggle the cell (we will toggle it back after calling the counter)
86  EmptyArray.insert_cell(
87  coordinates_free[pos * 2u],
88  coordinates_free[pos * 2u + 1u],
89  EmptyArray.default_val().value,
90  false, false
91  );
92 
93  data.push_back(EmptyArray);
94 
95  #ifdef BARRY_USER_INTERRUPT
96  if (data.size() % 1000u == 0u)
97  {
98  BARRY_USER_INTERRUPT
99  }
100  #endif
101 
102  // Again, we only pass it to the next level iff the next level is not
103  // passed the last step.
104  calc_backend_sparse(pos + 1u);
105 
106  // We need to restore the state of the cell
107  EmptyArray.rm_cell(
108  coordinates_free[pos * 2u],
109  coordinates_free[pos * 2u + 1u],
110  false, false
111  );
112 
113  return;
114 
115 }
116 
117 template <typename Array_Type, typename Data_Rule_Type>
119  size_t pos
120 )
121 {
122 
123  // Did we reached the end??
124  if (pos >= n_free)
125  return;
126 
127  // We will pass it to the next step, if the iteration makes sense.
128  calc_backend_dense(pos + 1u);
129 
130  // Toggle the cell (we will toggle it back after calling the counter)
131  EmptyArray(coordinates_free[pos * 2u], coordinates_free[pos * 2u + 1u]) = 1;
132 
133  data.push_back(EmptyArray);
134 
135  // Again, we only pass it to the next level iff the next level is not
136  // passed the last step.
137  calc_backend_dense(pos + 1u);
138 
139  // We need to restore the state of the cell
140  EmptyArray(coordinates_free[pos * 2u], coordinates_free[pos * 2u + 1u]) = 0;
141 
142  return;
143 
144 }
145 
146 
147 /***
148  * Function to generate the powerset of the
149  */
150 template <typename Array_Type, typename Data_Rule_Type>
152 
153  // Generating sequence
154  this->init_support();
155 
156  // Recursive function to count
157  if (EmptyArray.is_dense())
158  calc_backend_dense(0u);
159  else
160  calc_backend_sparse(0u);
161 
162  return;
163 
164 }
165 
166 template <typename Array_Type, typename Data_Rule_Type>
168  size_t N_,
169  size_t M_
170 ) {
171 
172  data.empty();
173  N = N_, M = M_;
174 
175  return;
176 
177 }
178 
179 template <typename Array_Type, typename Data_Rule_Type>
182 ) {
183 
184  rules->add_rule(rule);
185  return;
186 }
187 
188 template <typename Array_Type, typename Data_Rule_Type>
191  Data_Rule_Type data_
192 ) {
193 
194  rules->add_rule(
195  rule_fun_,
196  data_
197  );
198 
199  return;
200 
201 }
202 
203 #endif
Powerset of a binary array.
void add_rule(Rule< Array_Type, Data_Rule_Type > rule)
void init_support()
void reset(size_t N_, size_t M_)
Rule for determining if a cell should be included in a sequence.
Definition: rules-bones.hpp:20
Vector of objects of class Rule.
Definition: rules-bones.hpp:71
Data_Type &&counter_ data(std::move(counter_.data))
size_t i
Data_Type Counter_fun_type< Array_Type, Data_Type > Hasher_fun_type< Array_Type, Data_Type > Data_Type data_
std::function< bool(const Array_Type &, size_t, size_t, Data_Type &)> Rule_fun_type
Definition: typedefs.hpp:190