-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnumberSet.cpp
More file actions
104 lines (89 loc) · 2.66 KB
/
numberSet.cpp
File metadata and controls
104 lines (89 loc) · 2.66 KB
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
#include "numberSet.h"
#include <cmath>
#include <deque>
#include "primeSearchingUtilities.h"
//Most significant figure starts at 0.
int from_digits(std::vector<int> number, int base) {
int out_number = 0;
int coordinate = 1;
for (int i = number.size()-1; i >= 0; --i) {
out_number += number[i]*coordinate;
coordinate *= 10;
}
return out_number;
}
std::vector<int> to_vector(numberSet ns) {
std::vector<int> number_stack{};
return to_vector_impl(ns, number_stack);
}
std::vector<int> to_vector_impl(const numberSet& ns, std::vector<int> current_head) {
std::vector<int> result;
if (ns.inSet) result.push_back(from_digits(current_head, base));
std::vector<std::vector<int>> numbersFromSets{};
for (int i = 0; i != ns.branches.size(); i++) {
if (ns.branches[i] != nullptr) {
numbersFromSets.push_back(
to_vector_impl(*ns.branches[i], concatenate(current_head, std::vector<int>{i}))
);
}
}
return
concatenate(
result,
flatten(numbersFromSets)
);
}
std::deque<int> digit_sequence(int member) {
std::deque<int> digits;
while (member > 0) {
int digit = member % base;
digits.push_front(digit);
member /= 10;
}
return digits;
}
numberSet::numberSet(const numberSet& rhs) :inSet{ rhs.inSet } {
for (int i = 0; i != branches.size(); ++i) {
if (rhs.branches[i] == nullptr) {
branches[i] = nullptr;
}
else {
auto branch = std::unique_ptr<numberSet>{ new numberSet{*rhs.branches[i]} };
branches[i] = std::move(branch);
}
}
}
numberSet& numberSet::operator=(const numberSet& rhs) {
numberSet tmp{ rhs };
std::swap(tmp, *this);
return *this;
}
//add number to set
numberSet with(numberSet set, int member) {
auto digits = digit_sequence(member);
numberSet* setFocus = &set;
for (auto digit : digits) {
if (setFocus->branches[digit] != nullptr) {
setFocus = setFocus->branches[digit].get();
}
else {
setFocus->branches[digit] = std::unique_ptr<numberSet>(new numberSet{});
setFocus = setFocus->branches[digit].get();
}
}
setFocus->inSet = true;
return set;
}
bool has(const numberSet& set, int member) {
auto digits = digit_sequence(member);
const numberSet* setFocus = &set;
for (auto digit : digits) {
if (setFocus->branches[digit] != nullptr) {
setFocus = setFocus->branches[digit].get();
}
else {
return false;
}
}
return setFocus->inSet;
}