forked from mirastenger/receipt_calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomb.h
74 lines (67 loc) · 2.05 KB
/
comb.h
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
#include <iostream>
#include <vector>
#ifndef COMB_H
#define COMB_H
class comb {
public:
comb (std::vector<double> reciepts, double half) : inputs(reciepts), target(half){
populate_v();
}
std::vector<double> inputs;
double target;
std::vector<int> indexes;
std::vector<std::vector<int>> index_v;
std::vector<std::pair<double, std::vector<double>>> v;
void populate_index_v();
void populate_v();
};
void comb::populate_index_v() {
for (size_t i = 0; i < inputs.size(); i++) {
indexes.push_back(i); //Populate indexes, attempt to avoid confusion by duplicated numbers
}
for (size_t length = 1; length <= indexes.size(); length++) {
for (size_t i = 0; i < indexes.size() - (length - 1); i++) { // length - 1 is to reduce unnecessary loops
if (length == 1) {
std::vector<int> buff{indexes[i]};
index_v.push_back(buff);
} else {
size_t size = index_v.size();
for (size_t vpos = 0; vpos < size; vpos++) {
if ((index_v[vpos]).size() == (length - 1) && index_v[vpos][0] > indexes[i]) {
std::vector<int> buffer = index_v[vpos];
buffer.insert(buffer.begin(), indexes[i]);
index_v.push_back(buffer);
}
}
}
}
}
}
void comb::populate_v() {
populate_index_v();
for (size_t i = 0; i < index_v.size(); i++) {
std::vector<double> buffer;
double sub_sum = 0;
for (size_t j = 0; j < index_v[i].size(); j++) {
double value = inputs[index_v[i][j]];
buffer.push_back(value);
sub_sum = sub_sum + value;
}
double dist = target - sub_sum;
if (dist < 0) {
dist = dist*(-1);
}
v.push_back(std::make_pair(dist, buffer));
std::cout << "\r[";
int progress = (i*30 / index_v.size());
for (size_t j = 0; j < progress; j++) {
std::cout << "I";
}
for (size_t j = 0; j < 30 - progress; j++) {
std::cout << " ";
}
std::cout << "] ";
std::cout << i << "/" << index_v.size();
}
}
#endif