-
Notifications
You must be signed in to change notification settings - Fork 30
/
plan_transfers.hpp
73 lines (65 loc) · 2 KB
/
plan_transfers.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
#pragma once
#include <limits>
#include <vector>
#include <string>
#include <cassert>
#include <cstdint>
struct BedNeedle {
enum Bed : char {
Front = 'f',
FrontSliders = 'F',
BackSliders = 'B',
Back = 'b'
} bed;
int32_t needle;
bool operator==(BedNeedle const &o) const {
return bed == o.bed && needle == o.needle;
}
bool operator!=(BedNeedle const &o) const {
return !(*this == o);
}
BedNeedle(Bed bed_ = Front, int32_t needle_ = 0) : bed(bed_), needle(needle_) { }
std::string to_string() const {
if (bed == Front) return "f" + std::to_string(needle);
else if (bed == FrontSliders) return "fs" + std::to_string(needle);
else if (bed == BackSliders) return "bs" + std::to_string(needle);
else if (bed == Back) return "b" + std::to_string(needle);
else {
assert(0 && "Should have handled all cases");
return "?" + std::to_string(needle); //never executed
}
}
};
template< >
struct std::hash< BedNeedle > {
size_t operator()(BedNeedle const &bn) const {
return (size_t(bn.bed) << (8*(sizeof(size_t)-sizeof(bn.bed)))) ^ size_t(bn.needle);
};
};
struct Constraints {
int32_t min_free = std::numeric_limits< int32_t >::min();
int32_t max_free = std::numeric_limits< int32_t >::max();
uint32_t max_racking = 4;
};
struct Transfer {
Transfer() = default;
Transfer(BedNeedle const &from_, BedNeedle const &to_, std::string const &why_ = "") : from(from_), to(to_), why(why_) { }
BedNeedle from;
BedNeedle to;
std::string why;
std::string to_string() const {
std::string ret = from.to_string() + " -> " + to.to_string();
if (why != "") ret += " (" + why + ")";
return ret;
}
};
typedef int32_t Slack;
constexpr const Slack SlackForNoYarn = std::numeric_limits< Slack >::max();
bool plan_transfers(
Constraints const &constraints,
std::vector< BedNeedle > const &from_ccw,
std::vector< BedNeedle > const &to_ccw,
std::vector< Slack > const &slack, //slack between stitch and its ccw-ward neighbor
std::vector< Transfer> *transfers,
std::string *error = nullptr
);