-
Notifications
You must be signed in to change notification settings - Fork 4
/
types.cpp
191 lines (171 loc) · 3.75 KB
/
types.cpp
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
#include "stdafx.h"
#include "types.h"
namespace Denisenko {
namespace Raskroy {
void PartKey::normalize() {
if (can_rotate) {
if (rect.Size[0] < rect.Size[1]) {
std::swap(rect.Size[0], rect.Size[1]);
}
}
}
bool PartKey::operator <(const PartKey & rhs) const {
if (rect.Size[0] < rhs.rect.Size[0])
return true;
else if (rect.Size[0] > rhs.rect.Size[0])
return false;
if (rect.Size[1] < rhs.rect.Size[1])
return true;
else if (rect.Size[1] > rhs.rect.Size[1])
return false;
if (!can_rotate && rhs.can_rotate)
return true;
else
return false;
}
OtherSize::OtherSize(scalar value)
: Value(value)
{
}
OtherSizes::iterator OtherSizes::Find(scalar size)
{
for (auto otherSize = begin(); otherSize != end(); otherSize++)
if (otherSize->Value == size)
return otherSize;
return end();
}
void OtherSizes::SetMin(void)
{
auto min = begin();
for (auto otherSize = begin(); otherSize != end(); otherSize++)
if (otherSize->Value < min->Value)
min = otherSize;
Min = min;
}
Sizes::iterator Sizes::Find(scalar size)
{
for (auto pSize = begin(); pSize != end(); pSize++)
if (pSize->Value == size)
return pSize;
return end();
}
void Sizes::AddSize(scalar size, scalar otherSize, Part * part)
{
auto pSize = Find(size);
if (pSize == end())
{
Size newSize;
newSize.Value = size;
OtherSize other_size(otherSize);
other_size.parts.push_back(part);
newSize.other_sizes.push_back(other_size);
push_back(newSize);
}
else
{
auto pOtherSize = pSize->other_sizes.Find(otherSize);
if (pOtherSize == pSize->other_sizes.end()) {
OtherSize other_size(otherSize);
other_size.parts.push_back(part);
pSize->other_sizes.push_back(other_size);
} else {
pOtherSize->parts.push_back(part);
}
}
}
void Sizes::AddPart(Part &part, unsigned s)
{
if (part.Amount <= 0)
return;
AddSize(part.rect.Size[s], part.rect.Size[!s], &part);
if (part.Rotate && part.rect.Size[s] != part.rect.Size[!s])
AddSize(part.rect.Size[!s], part.rect.Size[s], &part);
}
Amounts& Amounts::operator += (const Amounts &amounts)
{
assert(size() == amounts.size());
auto i1 = begin();
auto i2 = amounts.begin();
for (; i1 != end(); i1++, i2++)
*i1 += *i2;
return *this;
}
Amounts& Amounts::operator -= (const Amounts &amounts)
{
assert(size() == amounts.size());
auto i1 = begin();
auto i2 = amounts.begin();
for (; i1 != end(); i1++, i2++)
*i1 -= *i2;
return *this;
}
Amounts& Amounts::operator *= (unsigned n)
{
if (n == 1)
return *this;
if (n == 0)
{
std::fill(begin(), end(), 0);
return *this;
}
for (auto i = begin(); i != end(); i++)
(*i) *= n;
return *this;
}
Amounts Amounts::operator - (const Amounts &a2) const
{
assert(size() == a2.size());
auto res = Amounts(size());
auto i1 = begin();
auto i2 = a2.begin();
auto ri = res.begin();
for (; i1 != end(); i1++, i2++, ri++)
{
(*ri) = (*i1) - (*i2);
}
return res;
}
Amounts Amounts::operator * (unsigned n) const
{
if (n == 1)
return *this;
auto res = Amounts(size());
if (n == 0)
{
std::fill(res.begin(), res.end(), 0);
return res;
}
auto i = begin();
auto ri = res.begin();
for (; i != end(); i++, ri++)
{
(*ri) = (*i) * n;
}
return res;
}
unsigned Amounts::operator / (const Amounts &a2) const
{
assert(size() == a2.size());
assert(size() != 0);
auto mind = 0u;
auto first = true;
auto i1 = begin();
auto i2 = a2.begin();
for (; i1 != end(); i1++, i2++)
{
if (*i2 == 0)
continue;
auto d = (*i1)/(*i2);
if (d == 0)
return 0;
if (first || d < mind)
{
first = false;
mind = d;
}
}
assert(!first);
return mind;
}
} // Raskroy
} // Denisenko