-
Notifications
You must be signed in to change notification settings - Fork 0
/
barry.hpp
11670 lines (8562 loc) · 314 KB
/
barry.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <cstdarg>
#include <vector>
#include <unordered_map>
#include <functional>
#include <stdexcept>
#include <cmath>
#include <map>
#include <algorithm>
#include <utility>
#include <random>
#include <climits>
#include <cfloat>
#include <string>
#include <cstdint>
#include <memory>
#include <regex>
#include <iterator>
#if defined(__OPENMP) || defined(_OPENMP)
#include <omp.h>
// Set the number of threads to match the number of cores
// in the machine
#endif
#ifndef BARRY_HPP
#define BARRY_HPP
#define BARRY_VERSION_MAYOR 0
#define BARRY_VERSION_MINOR 1
#define BARRY_VERSION BARRY_VERSION_MAYOR ## . ## BARRY_VERSION_MINOR
/**
* @brief barry: Your go-to motif accountant
*/
namespace barry {
//! Tree class and TreeIterator class
/*//////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
Start of -include/barry/typedefs.hpp-
////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////*/
#ifndef BARRY_TYPEDEFS_HPP
#define BARRY_TYPEDEFS_HPP 1
// Configuration ---------------------------------------------------------------
/*//////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
Start of -include/barry//barry-configuration.hpp-
////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////*/
#ifndef BARRY_CONFIGURATION_HPP
#define BARRY_CONFIGURATION_HPP
/**
* @name Configuration MACROS
* @details These are mostly related to performance. The definitions follow:
*
* - `BARRY_USE_UNORDERED_MAP` If specified, then barry is compiled using
* `std::unordered_map`. Otherwise it will use `std::map` for the arrays.
*
* - `BARRY_USE_SAFE_EXP` When specified, it will multiply all likelihoods
* in `Model` by (1/-100)/(1/-100) so that numerical overflows are avoided.
*
* - `BARRY_USE_ISFINITE` When specified, it will introduce a macro that
* checks whether the likelihood is finite or not.
*
* - `printf_barry` If not specified, will be defined as `printf`.
*
* - `BARRY_DEBUG_LEVEL`, when defined, will make things verbose.
*/
///@{
#ifdef BARRY_USE_UNORDERED_MAP
template<typename Ta,typename Tb>
using Map = std::unordered_map<Ta,Tb>;
#else
template<typename Ta,typename Tb>
using Map = std::map<Ta,Tb>;
#endif
#ifdef BARRY_USE_SAFE_EXP
#define BARRY_SAFE_EXP
#else
#define BARRY_SAFE_EXP -100.0
#endif
#ifdef BARRY_USE_ISFINITE
#define BARRY_ISFINITE(a) if (!std::isfinite( (a) )) \
throw std::overflow_error("The likelihood function has overflowed.");
#else
#define BARRY_ISFINITE(a)
#endif
#ifdef BARRAY_USE_CHECK_SUPPORT
#define BARRY_CHECK_SUPPORT(x, maxs) if ((x).size() > (maxs)) \
throw std::length_error("The support has exceeded its maximum size.");
#else
#define BARRY_CHECK_SUPPORT(x, maxs)
#endif
#ifndef printf_barry
#define printf_barry printf
#endif
#ifndef BARRY_MAX_NUM_ELEMENTS
#define BARRY_MAX_NUM_ELEMENTS static_cast< size_t >(std::numeric_limits< size_t >::max() /2u)
#endif
#if defined(__OPENMP) || defined(_OPENMP)
#define BARRY_WITH_OMP
#include <omp.h>
#endif
#ifdef BARRY_USE_LATEX
#define BARRY_WITH_LATEX
#else
#undef BARRY_WITH_LATEX
#endif
// BARRY_DEBUG_LEVEL: See barry-debug.hpp
// BARRY_PROGRESS_BAR_WIDTH: See progress.hpp
///@}
#endif
/*//////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
End of -include/barry//barry-configuration.hpp-
////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////*/
// Debug
/*//////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
Start of -include/barry//barry-debug.hpp-
////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////*/
#ifndef BARRY_DEBUG_HPP
#define BARRY_DEBUG_HPP
#ifndef BARRY_DEBUG_LEVEL
#define BARRY_DEBUG_LEVEL 0
#else
// The start of the line in every debug print
#define BARRY_DEBUG_HEADER "[barry]"
#define BARRY_DEBUG_MSG(a) \
printf_barry("%s %s\n", BARRY_DEBUG_HEADER, (a));
// Generic printer (default)
template <typename T>
void BARRY_DEBUG_VEC_PRINT(const std::vector<T> & a) {
printf_barry("%s [", BARRY_DEBUG_HEADER);
for(const auto & iter : (a))
printf_barry("%.4f ", static_cast<double>(iter));
printf_barry("]\n");
return;
}
// Specialization for the printer
template<>
inline void BARRY_DEBUG_VEC_PRINT(const std::vector< int > & a) {
printf_barry("%s [", BARRY_DEBUG_HEADER);
for(const auto & iter : (a))
printf_barry("%i ", iter);
printf_barry("]\n");
return;
}
template<>
inline void BARRY_DEBUG_VEC_PRINT(const std::vector< std::string > & a) {
printf_barry("%s \n", BARRY_DEBUG_HEADER);
for(const auto & iter : (a))
printf_barry("%s %s\n", BARRY_DEBUG_HEADER, iter.c_str());
printf_barry("%s \n", BARRY_DEBUG_HEADER);
return;
}
#endif
#endif
/*//////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
End of -include/barry//barry-debug.hpp-
////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////*/
// Progress bar
/*//////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
Start of -include/barry//progress.hpp-
////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////*/
#ifndef BARRY_PROGRESS_HPP
#define BARRY_PROGRESS_HPP
#ifndef BARRY_PROGRESS_BAR_WIDTH
#define BARRY_PROGRESS_BAR_WIDTH 80
#endif
/**
* @brief A simple progress bar
*/
class Progress {
private:
int width; ///< Total width size (number of bars)
int n; ///< Total number of iterations
double step_size; ///< Size of the step
int last_loc; ///< Last location of the bar
int cur_loc; ///< Last location of the bar
int i; ///< Current iteration step
public:
Progress(int n_, int width_);
~Progress() {};
void next();
void end();
};
inline Progress::Progress(int n_, int width_) {
width = std::max(7, width_ - 7);
n = n_;
step_size = static_cast<double>(width)/static_cast<double>(n);
last_loc = 0;
i = 0;
}
inline void Progress::next() {
cur_loc = std::floor((++i) * step_size);
for (int j = 0; j < (cur_loc - last_loc); ++j)
printf_barry("|");
last_loc = cur_loc;
}
inline void Progress::end() {
printf_barry(" done.\n");
}
#endif
/*//////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
End of -include/barry//progress.hpp-
////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////*/
// -----------------------------------------------------------------------------
// Basic types
// See this thread
// https://stackoverflow.com/questions/35055042/difference-between-size_t8-t-size_t-fast8-t-and-size_t-least8-t
// Mostly relevant for the BArray definition -----------------------------------
// Constants
/**
* @brief Integer constants used to specify which cell
* should be check.
*/
namespace CHECK {
const int BOTH = -1;
const int NONE = 0;
const int ONE = 1;
const int TWO = 2;
}
/**
* @brief Integer constants used to specify which cell
* should be check to exist or not.
*/
namespace EXISTS {
const int BOTH = -1;
const int NONE = 0;
const int ONE = 1;
const int TWO = 1;
const int UKNOWN = -1;
const int AS_ZERO = 0;
const int AS_ONE = 1;
}
/***
* A single count
*/
typedef std::vector< std::pair< std::vector<double>, size_t > > Counts_type;
// class Counts_type
// {
// private:
// std::vector< std::size_t_fast32_t > stats_counts;
// std::vector< double > stats_values;
// size_t n_stats;
// size_t n_obs;
// public:
// std::vector< double > operator()
// }
template <class Type_A > class Cell;
template<typename Cell_Type>
using Row_type = Map< size_t, Cell<Cell_Type> >;
template<typename Cell_Type>
using Col_type = Map< size_t, Cell<Cell_Type>* >;
/**
* @brief A wrapper class to store `source`, `target`, `val` from a `BArray` object.
*
* @tparam Cell_Type Any type
*/
template<typename Cell_Type>
class Entries {
public:
std::vector< size_t > source;
std::vector< size_t > target;
std::vector< Cell_Type > val;
Entries() : source(0u), target(0u), val(0u) {};
Entries(size_t n) {
source.reserve(n);
target.reserve(n);
val.reserve(n);
return;
};
~Entries() {};
void resize(size_t n) {
source.resize(n);
target.resize(n);
val.resize(n);
return;
}
};
// Relevant for anything using vecHasher function ------------------------------
template <typename T>
struct vecHasher
{
std::size_t operator()(std::vector< T > const& dat) const noexcept
{
std::hash< T > hasher;
std::size_t hash = hasher(dat[0u]);
// ^ makes bitwise XOR
// 0x9e3779b9 is a 32 bit constant (comes from the golden ratio)
// << is a shift operator, something like lhs * 2^(rhs)
if (dat.size() > 1u)
for (size_t i = 1u; i < dat.size(); ++i)
hash ^= hasher(dat[i]) + 0x9e3779b9 + (hash<<6) + (hash>>2);
return hash;
}
};
template<typename Ta = double, typename Tb = size_t>
using MapVec_type = std::unordered_map< std::vector< Ta >, Tb, vecHasher<Ta>>;
/**
* @brief Ascending sorting an array
*
* It will sort an array solving ties using the next column. Data is
* stored column-wise.
*
* @tparam T
* @param v
* @param nrows
* @return std::vector<size_t> The sorting index.
*/
inline std::vector< size_t > sort_array(
const double * v,
size_t start,
size_t ncols,
size_t nrows
) {
// initialize original index locations
std::vector<size_t> idx(nrows);
std::iota(idx.begin(), idx.end(), 0);
std::sort(idx.begin(), idx.end(),
[&v,nrows,ncols,start](size_t i1, size_t i2) {
for (size_t j = 0u; j < ncols; ++j)
{
if (*(v + (nrows * j + i1+start)) == *(v + (nrows * j + i2 + start)))
continue;
else
return *(v + (nrows * j + i1+start)) < *(v + (nrows * j + i2 + start));
}
return false;
});
return idx;
}
// Mostly relevant in the case of the stats count functions -------------------
template <typename Cell_Type, typename Data_Type> class BArray;
template <typename Array_Type, typename Counter_Type> class Counter;
template <typename Cell_Type, typename Data_Type> class BArrayDense;
/**
* @brief Counter and rule functions
* @param Array_Type a BArray
* @param unit, size_t Focal cell
* @param Data_Type Data associated with the function, for example, id of the attribute
* in the Array.
* @return `Counter_fun_type` a double (the change statistic)
* @return `Rule_fun_type` a bool. True if the cell is blocked.
*/
///@{
template <typename Array_Type, typename Data_Type>
using Counter_fun_type = std::function<double(const Array_Type &, size_t, size_t, Data_Type &)>;
template <typename Array_Type, typename Data_Type>
using Rule_fun_type = std::function<bool(const Array_Type &, size_t, size_t, Data_Type &)>;
///@}
/**
* @brief Hasher function used by the counter
* @details Used to characterize the support of the array.
*
* @tparam Array_Type
*/
template <typename Array_Type, typename Data_Type>
using Hasher_fun_type = std::function<std::vector<double>(const Array_Type &, Data_Type *)>;
// Misc ------------------------------------------------------------------------
/**
* @brief Compares if -a- and -b- are equal
* @param a,b Two vectors of the same length
* @return `true` if all elements are equal.
*/
///@{
template <typename T>
inline bool vec_equal(
const std::vector< T > & a,
const std::vector< T > & b
) {
if (a.size() != b.size())
{
std::string err = "-a- and -b- should have the same length. length(a) = " +
std::to_string(a.size()) + " and length(b) = " + std::to_string(b.size()) +
std::string(".");
throw std::length_error(err);
}
size_t i = 0;
while (a[i] == b[i]) {
if (++i == a.size())
return true;
}
return false;
}
template <typename T>
inline bool vec_equal_approx(
const std::vector< T > & a,
const std::vector< T > & b,
double eps = 1e-100
) {
if (a.size() != b.size())
{
std::string err = "-a- and -b- should have the same length. length(a) = " +
std::to_string(a.size()) + " and length(b) = " + std::to_string(b.size()) +
std::string(".");
throw std::length_error(err);
}
size_t i = 0;
while (static_cast<double>(std::fabs(a[i] - b[i])) < eps) {
if (++i == a.size())
return true;
}
return false;
}
///@}
#if defined(__OPENMP) || defined(_OPENMP)
#pragma omp declare simd
#endif
template <typename T>
inline T vec_inner_prod(
const T * a,
const T * b,
size_t n
) {
double res = 0.0;
#if defined(__OPENMP) || defined(_OPENMP)
#pragma omp simd reduction(+:res)
#elif defined(__GNUC__) && !defined(__clang__)
#pragma GCC ivdep
#endif
for (size_t i = 0u; i < n; ++i)
res += (*(a + i) * *(b + i));
return res;
}
#if defined(__OPENMP) || defined(_OPENMP)
#pragma omp declare simd
#endif
template <>
inline double vec_inner_prod(
const double * a,
const double * b,
size_t n
) {
double res = 0.0;
#if defined(__OPENMP) || defined(_OPENMP)
#pragma omp simd reduction(+:res)
#elif defined(__GNUC__) && !defined(__clang__)
#pragma GCC ivdep
#endif
for (size_t i = 0u; i < n; ++i)
res += (*(a + i) * *(b + i));
return res;
}
#endif
/*//////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
End of -include/barry/typedefs.hpp-
////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////*/
/*//////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
Start of -include/barry/barry-macros.hpp-
////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////*/
#ifndef BARRY_BARRY_MACROS_HPP
#define BARRY_BARRY_MACROS_HPP
#define BARRY_ZERO Cell<Cell_Type>(0.0)
#define BARRY_ZERO_DENSE static_cast<Cell_Type>(0.0)
#define BARRY_ONE Cell<Cell_Type>(1.0)
#define BARRY_ONE_DENSE static_cast<Cell_Type>(1.0)
#define BARRY_UNUSED(expr) do { (void)(expr); } while (0);
#if defined(_OPENMP) || defined(__OPENMP)
#define BARRY_NCORES_ARG(default) size_t ncores default
#else
#define BARRY_NCORES_ARG(default) size_t ncores default
#endif
#endif
/*//////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
End of -include/barry/barry-macros.hpp-
////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////*/
/*//////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
Start of -include/barry/freqtable.hpp-
////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////*/
#ifndef BARRY_STATSDB_HPP
#define BARRY_STATSDB_HPP 1
/**
* @brief Frequency table of vectors
*
* This is mostly used in `Support`. The main data is contained in the
* `data` double vector. The matrix is stored in a row-wise fashion, where
* the first element is the frequency with which the vector is observed.
*
* For example, in a model with `k` terms the first k + 1 elements of
* `data` would be:
*
* - weights
* - term 1
* - term 2
* - ...
* - term k
*
*/
template<typename T = double>
class FreqTable {
private:
std::unordered_map<size_t, size_t> index;
std::vector< double > data;
size_t k = 0u;
size_t n = 0u;
typename std::unordered_map<size_t, size_t>::iterator iter;
public:
// size_t ncols;
FreqTable() {};
~FreqTable() {};
size_t add(const std::vector< T > & x, size_t * h_precomp);
Counts_type as_vector() const;
const std::vector< double > & get_data() const {return data;};
const std::unordered_map<size_t,size_t> & get_index() const {return index;};
void clear();
void reserve(size_t n, size_t k);
void print() const;
/**
* @brief Number of unique elements in the table.
* (
* @return size_t
*/
size_t size() const noexcept;
size_t make_hash(const std::vector< T > & x) const;
};
template<typename T>
inline size_t FreqTable<T>::add(
const std::vector< T > & x,
size_t * h_precomp
) {
// The term exists, then we add it to the list and we initialize it
// with a single count
size_t h;
if (h_precomp == nullptr)
h = make_hash(x);
else
h = *h_precomp;
if (k == 0u)
{
index.insert({h, 0u});
data.push_back(1.0);
data.insert(data.end(), x.begin(), x.end());
k = x.size();
n++;
return h;
}
else
{
if (x.size() != k)
throw std::length_error(
"The value you are trying to add doesn't have the same lenght used in the database."
);
#if __cplusplus > 201700L
auto iter2 = index.try_emplace(h, data.size());
if (!iter2.second)
{
data[(iter2.first)->second] += 1.0;
}
else
{
data.push_back(1.0);
data.insert(data.end(), x.begin(), x.end());
n++;
}
#else
iter = index.find(h);
if (iter == index.end())
{
index.insert({h, data.size()});
data.push_back(1.0);
data.insert(data.end(), x.begin(), x.end());
n++;
return h;
}
data[(*iter).second] += 1.0;
#endif
}
return h;
}
template<typename T>
inline Counts_type FreqTable<T>::as_vector() const
{
Counts_type ans;
ans.reserve(index.size());
for (size_t i = 0u; i < n; ++i)
{
std::vector< double > tmp(k, 0.0);
for (size_t j = 1u; j < (k + 1u); ++j)
tmp[j - 1u] = data[i * (k + 1) + j];
ans.push_back(
std::make_pair<std::vector<double>,size_t>(
std::move(tmp),
static_cast<size_t>(data[i * (k + 1u)])
)
);
}
return ans;
}
template<typename T>
inline void FreqTable<T>::clear()
{
index.clear();
data.clear();
n = 0u;
k = 0u;
return;
}
template<typename T>
inline void FreqTable<T>::reserve(
size_t n,
size_t k
)
{
// Figuring out the max size
auto nk = std::min(BARRY_MAX_NUM_ELEMENTS, n * k);
n = nk / k;
data.reserve(nk);
index.reserve(n);
return;
}
// inline void StatsDB::rehash() {
// stats.rehash();
// return;
// }
template<typename T>
inline void FreqTable<T>::print() const
{
size_t grand_total = 0u;
printf_barry("%7s | %s\n", "Counts", "Stats");
for (size_t i = 0u; i < n; ++i)
{
printf_barry("%7i | ", static_cast<int>(data[i * (k + 1u)]));
for (size_t j = 1u; j < (k + 1u); ++j)
printf_barry(" %.2f", data[i * (k + 1) + j]);
printf_barry("\n");
grand_total += static_cast<size_t>(data[i * (k + 1u)]);
}
printf_barry("Grand total: %li\n", grand_total);
return;
}
template<typename T>
inline size_t FreqTable<T>::size() const noexcept
{
return index.size();
}
template<typename T>
inline size_t FreqTable<T>::make_hash(const std::vector< T > & x) const
{
std::hash< T > hasher;
std::size_t hash = hasher(x[0u]);
// ^ makes bitwise XOR
// 0x9e3779b9 is a 32 bit constant (comes from the golden ratio)
// << is a shift operator, something like lhs * 2^(rhs)
if (x.size() > 1u)
for (size_t i = 1u; i < x.size(); ++i)
hash ^= hasher(x[i]) + 0x9e3779b9 + (hash<<6) + (hash>>2);
return hash;
}
#endif
/*//////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
End of -include/barry/freqtable.hpp-
////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////*/
/*//////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
Start of -include/barry/cell-bones.hpp-
////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////*/
#ifndef BARRY_CELL_BONES_HPP
#define BARRY_CELL_BONES_HPP 1
/**
* @brief Entries in BArray.
* For now, it only has two members:
* - value: the content
* - visited: boolean (just a convenient)
*/
template <class Cell_Type > class Cell {
public:
Cell_Type value;
bool visited;
bool active;
Cell();
Cell(Cell_Type value_, bool visited_ = false, bool active_ = true) :
value(value_), visited(visited_), active(active_) {};
~Cell() {};
// This is an explicit declaration since in other cases it seems
// to try to use the move operator, which I do not intent to use.
Cell(const Cell<Cell_Type>& arg) :
value(arg.value), visited(arg.visited), active(arg.active) {};
// Copy by assignment
Cell<Cell_Type>& operator=(const Cell<Cell_Type>& other);
// Move constructor
Cell(Cell<Cell_Type>&& arg) noexcept:
value(std::move(arg.value)),
visited(std::move(arg.visited)),
active(std::move(arg.active)) {} ;
// Move assign operator
Cell<Cell_Type>& operator=(Cell<Cell_Type>&& other) noexcept;
void add(Cell_Type x);
// Casting operator (implicit and explicit)
// int x = Cell<int>(1); // returns 1
operator Cell_Type() const {return this->value;};
bool operator==(const Cell<Cell_Type>& rhs ) const;
bool operator!=(const Cell<Cell_Type>& rhs ) const;
};
#endif
/*//////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
End of -include/barry/cell-bones.hpp-
////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////*/
/*//////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
Start of -include/barry/cell-meat.hpp-
////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////*/
#ifndef BARRY_CELL_MEAT_HPP
#define BARRY_CELL_MEAT_HPP 1
template <typename Cell_Type>
Cell<Cell_Type>& Cell<Cell_Type>::operator=(const Cell<Cell_Type>& other) {
this->value = other.value;
this->visited = other.visited;
this->active = other.active;
return *this;
}
template <typename Cell_Type>
Cell<Cell_Type>& Cell<Cell_Type>::operator=(Cell<Cell_Type>&& other) noexcept {
this->value = std::move(other.value);
this->visited = std::move(other.visited);
this->active = std::move(other.active);
return *this;
}
template<typename Cell_Type>
bool Cell<Cell_Type>::operator==(const Cell<Cell_Type>& rhs ) const {