barry: Your go-to motif accountant  0.0-1
Full enumeration of sample space and fast count of sufficient statistics for binary arrays
counters-meat.hpp
Go to the documentation of this file.
1 #ifndef BARRY_COUNTERS_MEAT_HPP
2 #define BARRY_COUNTERS_MEAT_HPP 1
3 
4 #define COUNTER_TYPE() Counter<Array_Type,Data_Type>
5 
6 #define COUNTER_TEMPLATE_ARGS() <typename Array_Type, typename Data_Type>
7 
8 #define COUNTER_TEMPLATE(a,b) \
9  template COUNTER_TEMPLATE_ARGS() inline a COUNTER_TYPE()::b
10 
13 ) : count_fun(counter_.count_fun), init_fun(counter_.init_fun), hasher_fun(counter_.hasher_fun) {
14 
15  this->data = counter_.data;
16  this->name = counter_.name;
17  this->desc = counter_.desc;
18 
19  return;
20 
21 }
22 
23 
27  count_fun(std::move(counter_.count_fun)),
28  init_fun(std::move(counter_.init_fun)),
29  hasher_fun(std::move(counter_.hasher_fun)),
30  data(std::move(counter_.data)),
31  name(std::move(counter_.name)),
32  desc(std::move(counter_.desc))
33 {
34 
35 }
36 
39 )
40 {
41 
42  if (this != &counter_) {
43 
44  this->count_fun = counter_.count_fun;
45  this->init_fun = counter_.init_fun;
46  this->hasher_fun = counter_.hasher_fun;
47 
48 
49  this->data = counter_.data;
50  this->name = counter_.name;
51  this->desc = counter_.desc;
52 
53  }
54 
55  return *this;
56 
57 }
58 
61 ) noexcept {
62 
63  if (this != &counter_)
64  {
65 
66  this->data = std::move(counter_.data);
67 
68  // Functions
69  this->count_fun = std::move(counter_.count_fun);
70  this->init_fun = std::move(counter_.init_fun);
71  this->hasher_fun = std::move(counter_.hasher_fun);
72 
73  // Descriptions
74  this->name = std::move(counter_.name);
75  this->desc = std::move(counter_.desc);
76 
77  }
78 
79  return *this;
80 
81 }
82 
83 COUNTER_TEMPLATE(double, count)(Array_Type & Array, size_t i, size_t j)
84 {
85 
86  if (count_fun == nullptr)
87  return 0.0;
88 
89  return count_fun(Array, i, j, data);
90 
91 }
92 
93 COUNTER_TEMPLATE(double, init)(Array_Type & Array, size_t i, size_t j)
94 {
95 
96  if (init_fun == nullptr)
97  return 0.0;
98 
99  return init_fun(Array, i, j, data);
100 
101 }
102 
103 COUNTER_TEMPLATE(std::string, get_name)() const {
104  return this->name;
105 }
106 
107 COUNTER_TEMPLATE(std::string, get_description)() const {
108  return this->name;
109 }
110 
112  hasher_fun = fun;
113 }
114 
115 #define TMP_HASHER_CALL Hasher_fun_type<Array_Type,Data_Type>
117  return hasher_fun;
118 }
119 #undef TMP_HASHER_CALL
120 
122 // Counters
124 
125 #define COUNTERS_TYPE() Counters<Array_Type,Data_Type>
126 
127 #define COUNTERS_TEMPLATE_ARGS() <typename Array_Type, typename Data_Type>
128 
129 #define COUNTERS_TEMPLATE(a,b) \
130  template COUNTERS_TEMPLATE_ARGS() inline a COUNTERS_TYPE()::b
131 
132 COUNTERS_TEMPLATE(, Counters)() : data(0u), hasher(nullptr) {}
133 
134 COUNTERS_TEMPLATE(COUNTER_TYPE() &, operator[])(size_t idx) {
135 
136  return data[idx];
137 
138 }
139 
141  data(counter_.data), hasher(counter_.hasher) {}
142 
144  data(std::move(counters_.data)), hasher(std::move(counters_.hasher)) {}
145 
147 
148  if (this != &counter_)
149  {
150  data = counter_.data;
151  hasher = counter_.hasher;
152  }
153 
154  return *this;
155 
156 }
157 
159 {
160 
161  if (this != &counters_) {
162  data = std::move(counters_.data);
163  hasher = std::move(counters_.hasher);
164  }
165 
166  return *this;
167 
168 }
169 
171 {
172 
173  data.push_back(counter);
174 
175  return;
176 }
177 
178 COUNTERS_TEMPLATE(void, add_counter)(
182  Data_Type data_,
183  std::string name_,
184  std::string desc_
185 )
186 {
187 
188  data.emplace_back(Counter<Array_Type,Data_Type>(
189  count_fun_,
190  init_fun_,
191  hasher_fun_,
192  data_,
193  name_,
194  desc_
195  ));
196 
197  return;
198 
199 }
200 
201 COUNTERS_TEMPLATE(std::vector<std::string>, get_names)() const
202 {
203 
204  std::vector< std::string > out;
205  out.reserve(this->size());
206  for (size_t i = 0u; i < this->size(); ++i)
207  out.push_back(this->data.at(i).get_name());
208 
209  return out;
210 
211 }
212 
213 COUNTERS_TEMPLATE(std::vector<std::string>, get_descriptions)() const
214 {
215 
216  std::vector< std::string > out;
217  out.reserve(this->size());
218  for (size_t i = 0u; i < this->size(); ++i)
219  out.push_back(data.at(i).get_description());
220 
221  return out;
222 
223 }
224 
225 COUNTERS_TEMPLATE(std::vector<double>, gen_hash)(
226  const Array_Type & array,
227  bool add_dims
228 )
229 {
230  std::vector<double> res;
231 
232  // Iterating over the counters
233  for (auto & c: data)
234  {
235 
236  // If there's a hasher function, then use it!
237  if (c.get_hasher())
238  {
239 
240  for (auto v: c.get_hasher()(array, &(c.data)))
241  res.push_back(v);
242 
243  }
244 
245  }
246 
247  // Do we need to add the dims?
248  if (add_dims)
249  {
250  res.push_back(array.nrow());
251  res.push_back(array.ncol());
252  }
253 
254  // Ading the global hasher, if one exists
255  if (hasher)
256  {
257  for (auto i: hasher(array, nullptr))
258  res.push_back(i);
259  }
260 
261  // We have to return something...
262  if (res.size() == 0u)
263  res.push_back(0.0);
264 
265  return res;
266 
267 }
268 
269 COUNTERS_TEMPLATE(void, add_hash)(
271 ) {
272 
273  hasher = fun_;
274 
275 }
276 
277 #undef COUNTER_TYPE
278 #undef COUNTER_TEMPLATE_ARGS
279 #undef COUNTER_TEMPLATE
280 #undef COUNTERS_TYPE
281 #undef COUNTERS_TEMPLATE_ARGS
282 #undef COUNTERS_TEMPLATE
283 
284 #endif
return * this
A counter function based on change statistics.
Vector of counters.
#define TMP_HASHER_CALL
Data_Type & counter_
Data_Type &&counter_ desc(std::move(counter_.desc))
Move constructor.
Data_Type &&counter_ name(std::move(counter_.name))
Data_Type fun
Data_Type count_fun_
Data_Type init_fun(counter_.init_fun)
return res
Data_Type &&counter_ data(std::move(counter_.data))
Data_Type hasher_fun(counter_.hasher_fun)
Data_Type Counter_fun_type< Array_Type, Data_Type > init_fun_
Data_Type fun_
Data_Type Counter_fun_type< Array_Type, Data_Type > Hasher_fun_type< Array_Type, Data_Type > hasher_fun_
Data_Type Counter_fun_type< Array_Type, Data_Type > Hasher_fun_type< Array_Type, Data_Type > Data_Type std::string std::string desc_
#define COUNTER_TEMPLATE(a, b)
#define COUNTER_TYPE()
Data_Type hasher(counter_.hasher)
return count_fun(Array, i, j, data)
Data_Type Counter_fun_type< Array_Type, Data_Type > Hasher_fun_type< Array_Type, Data_Type > Data_Type std::string name_
size_t size_t j
bool add_dims
Data_Type counter
#define COUNTERS_TYPE()
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_
#define COUNTERS_TEMPLATE(a, b)
Data_Type * counters_
std::function< std::vector< double >(const Array_Type &, Data_Type *)> Hasher_fun_type
Hasher function used by the counter.
Definition: typedefs.hpp:200
std::function< double(const Array_Type &, size_t, size_t, Data_Type &)> Counter_fun_type
Counter and rule functions.
Definition: typedefs.hpp:187