aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-03-17 17:50:38 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-03-17 17:50:38 +0800
commit1914eb19f3ce24e59332df7950a83f2d00bf2531 (patch)
tree5d40aa5c8dee9caee4e264b78b5b622400d95adc
parent6d0df82c81a01cacc3551ab525468da3b875ce41 (diff)
downloadadvent-of-code-1914eb19f3ce24e59332df7950a83f2d00bf2531.tar.gz
advent-of-code-1914eb19f3ce24e59332df7950a83f2d00bf2531.zip
const noexcept
-rw-r--r--src/2015/day7/aoc.h18
-rw-r--r--src/common.h13
-rw-r--r--test/test_2015.cpp4
3 files changed, 23 insertions, 12 deletions
diff --git a/src/2015/day7/aoc.h b/src/2015/day7/aoc.h
index 7d76546..b03c3a8 100644
--- a/src/2015/day7/aoc.h
+++ b/src/2015/day7/aoc.h
@@ -75,33 +75,33 @@ struct cals {
}
}
- std::function<result()> op_and(line_view x, line_view y) {
+ std::function<result()> op_and(line_view x, line_view y) const noexcept {
return [&x, &y, this]() -> result { return compute(x).op_and(compute(y)); };
}
- std::function<result()> op_or(line_view x, line_view y) {
+ std::function<result()> op_or(line_view x, line_view y) const noexcept {
return [&x, &y, this]() -> result { return compute(x).op_or(compute(y)); };
}
- std::function<result()> op_leftshift(line_view x, line_view y) {
+ std::function<result()> op_leftshift(line_view x, line_view y) const noexcept {
return [&x, &y, this]() -> result { return compute(x).op_leftshift(compute(y)); };
}
- std::function<result()> op_rightshift(line_view x, line_view y) {
+ std::function<result()> op_rightshift(line_view x, line_view y) const noexcept {
return [&x, &y, this]() -> result { return compute(x).op_rightshift(compute(y)); };
}
- std::function<result()> op_not(line_view x, line_view y) {
+ std::function<result()> op_not(line_view x, line_view y) const noexcept {
return [&x, this]() -> result { return compute(x).op_not(); };
}
- std::function<result()> op_value(line_view x, line_view y) {
+ std::function<result()> op_value(line_view x, line_view y) const noexcept {
return [&x, this]() -> result { return compute(x); };
}
- std::function<result()> parse(line_view line, line_view* x, line_view* y, line_view* w) {
- struct _ {
- std::function<result()> (cals::*op)(line_view, line_view);
+ std::function<result()> parse(line_view line, line_view* x, line_view* y, line_view* w) const noexcept {
+ static struct _ {
+ std::function<result()> (cals::*op)(line_view, line_view) const noexcept;
const char* label;
} ops[] = {
{&cals::op_and, "AND"},
diff --git a/src/common.h b/src/common.h
index a88e16e..e9dd01c 100644
--- a/src/common.h
+++ b/src/common.h
@@ -13,16 +13,25 @@ struct line_view {
line_view(const char* s = nullptr) : line(s) { length = s == nullptr ? 0 : strlen(s); }
line_view(const char* s, size_t l) : line(s), length(l) {}
- line_view(const char* p1, const char* p2) : line(p1), length(p2 - p1 + 1) {}
+ line_view(const char* p1, const char* p2) : line(p1), length(p2 - p1) {}
friend std::ostream& operator<<(std::ostream& o, const line_view& lv) {
+ o << "[" << lv.length << ":";
for (size_t i = 0; i < lv.length; i++) {
o << lv.line[i];
}
+ o << "]";
return o;
}
bool operator<(const line_view& lv) const noexcept {
- return std::string(line, length) < std::string(lv.line, lv.length);
+ size_t i = 0;
+ size_t j = 0;
+ while (i < length && j < lv.length) {
+ if (line[i++] < line[j++]) {
+ return true;
+ }
+ }
+ return j < lv.length;
}
bool operator==(const line_view& lv) const noexcept {
diff --git a/test/test_2015.cpp b/test/test_2015.cpp
index 30e8d3b..c3b60b9 100644
--- a/test/test_2015.cpp
+++ b/test/test_2015.cpp
@@ -90,5 +90,7 @@ TEST_CASE("Probably a Fire Hazard", "[day6]") {
}
TEST_CASE("Some Assembly Required", "[day 7]") {
-
+ aoc2015::cals cals;
+ cals.parse("1 OR 2 -> ab");
+ printf("%d\n", cals.compute("ab").value);
}