Permalink
Browse files

Rename refactoring

  • Loading branch information...
metopa committed May 5, 2018
1 parent 34160aa commit 05f345dc668e596faa0916cd67a0cc1d541a40aa
@@ -3,30 +3,24 @@
#include "test_framework/generic_test.h"
#include "test_framework/serialization_traits.h"
using std::vector;
namespace drawing_skyline {
struct Rectangle {
struct Rect {
int left, right, height;
};
typedef vector<Rectangle> Skyline;
typedef vector<Rect> Skyline;
Skyline ComputeSkyline(const vector<Rectangle>& buildings) {
Skyline ComputeSkyline(const vector<Rect>& buildings) {
// TODO - you fill in here.
return {};
}
bool operator==(const Rectangle& a, const Rectangle& b) {
bool operator==(const Rect& a, const Rect& b) {
return a.left == b.left && a.right == b.right && a.height == b.height;
}
} // namespace drawing_skyline
using drawing_skyline::ComputeSkyline;
template <>
struct SerializationTraits<drawing_skyline::Rectangle>
: UserSerTraits<drawing_skyline::Rectangle, int, int, int> {};
struct SerializationTraits<Rect> : UserSerTraits<Rect, int, int, int> {};
std::ostream& operator<<(std::ostream& out,
const drawing_skyline::Rectangle& r) {
std::ostream& operator<<(std::ostream& out, const Rect& r) {
return PrintTo(out, std::make_tuple(r.left, r.right, r.height));
}
@@ -2,41 +2,33 @@
#include "test_framework/fmt_print.h"
#include "test_framework/generic_test.h"
#include "test_framework/serialization_traits.h"
namespace rectangle_intersection {
struct Rectangle {
struct Rect {
int x, y, width, height;
};
Rectangle IntersectRectangle(const Rectangle& R1, const Rectangle& R2) {
Rect IntersectRectangle(const Rect& R1, const Rect& R2) {
// TODO - you fill in here.
return {0, 0, 0, 0};
}
bool operator==(const Rectangle& r1, const Rectangle& r2) {
bool operator==(const Rect& r1, const Rect& r2) {
return std::tie(r1.x, r1.y, r1.width, r1.height) ==
std::tie(r2.x, r2.y, r2.width, r2.height);
}
} // namespace rectangle_intersection
using rectangle_intersection::IntersectRectangle;
template <>
struct SerializationTraits<rectangle_intersection::Rectangle>
: UserSerTraits<rectangle_intersection::Rectangle, int, int, int, int> {
struct SerializationTraits<Rect> : UserSerTraits<Rect, int, int, int, int> {
static std::vector<std::string> GetMetricNames(const std::string& arg_name) {
return {FmtStr("area({})", arg_name), FmtStr("perimeter({})", arg_name),
FmtStr("max(w, h)({})", arg_name)};
}
static std::vector<int> GetMetrics(
const rectangle_intersection::Rectangle& x) {
static std::vector<int> GetMetrics(const Rect& x) {
return {x.height * x.width, 2 * (x.height + x.width),
std::max(x.height, x.width)};
}
};
std::ostream& operator<<(std::ostream& out,
const rectangle_intersection::Rectangle& r) {
std::ostream& operator<<(std::ostream& out, const Rect& r) {
return PrintTo(out, std::make_tuple(r.x, r.y, r.width, r.height));
}
@@ -10,25 +10,24 @@ using std::min;
using std::numeric_limits;
using std::vector;
namespace drawing_skyline {
struct Rectangle;
typedef vector<Rectangle> Skyline;
struct Rect;
typedef vector<Rect> Skyline;
struct Rectangle {
struct Rect {
int left, right, height;
};
typedef vector<Rectangle> Skyline;
typedef vector<Rect> Skyline;
Skyline ComputeSkyline(const vector<Rectangle>& buildings) {
Skyline ComputeSkyline(const vector<Rect>& buildings) {
int min_left = numeric_limits<int>::max(),
max_right = numeric_limits<int>::min();
for (const Rectangle& building : buildings) {
for (const Rect& building : buildings) {
min_left = min(min_left, building.left);
max_right = max(max_right, building.right);
}
vector<int> heights(max_right - min_left + 1, 0);
for (const Rectangle& building : buildings) {
for (const Rect& building : buildings) {
for (int i = building.left; i <= building.right; ++i) {
heights[i - min_left] = max(heights[i - min_left], building.height);
}
@@ -39,27 +38,22 @@ Skyline ComputeSkyline(const vector<Rectangle>& buildings) {
for (int i = 1; i < size(heights); ++i) {
if (heights[i] != heights[i - 1]) {
result.emplace_back(
Rectangle{left + min_left, i - 1 + min_left, heights[i - 1]});
Rect{left + min_left, i - 1 + min_left, heights[i - 1]});
left = i;
}
}
result.emplace_back(Rectangle{left + min_left, max_right, heights.back()});
result.emplace_back(Rect{left + min_left, max_right, heights.back()});
return result;
}
bool operator==(const Rectangle& a, const Rectangle& b) {
bool operator==(const Rect& a, const Rect& b) {
return a.left == b.left && a.right == b.right && a.height == b.height;
}
} // namespace drawing_skyline
using drawing_skyline::ComputeSkyline;
template <>
struct SerializationTraits<drawing_skyline::Rectangle>
: UserSerTraits<drawing_skyline::Rectangle, int, int, int> {};
struct SerializationTraits<Rect> : UserSerTraits<Rect, int, int, int> {};
std::ostream& operator<<(std::ostream& out,
const drawing_skyline::Rectangle& r) {
std::ostream& operator<<(std::ostream& out, const Rect& r) {
return PrintTo(out, std::make_tuple(r.left, r.right, r.height));
}
@@ -8,16 +8,14 @@
using std::max;
using std::min;
namespace rectangle_intersection {
struct Rectangle;
struct Rect;
bool IsIntersect(const Rect&, const Rect&);
bool IsIntersect(const Rectangle&, const Rectangle&);
struct Rectangle {
struct Rect {
int x, y, width, height;
};
Rectangle IntersectRectangle(const Rectangle& R1, const Rectangle& R2) {
Rect IntersectRectangle(const Rect& R1, const Rect& R2) {
if (!IsIntersect(R1, R2)) {
return {0, 0, -1, -1}; // No intersection.
}
@@ -26,37 +24,30 @@ Rectangle IntersectRectangle(const Rectangle& R1, const Rectangle& R2) {
min(R1.y + R1.height, R2.y + R2.height) - max(R1.y, R2.y)};
}
bool IsIntersect(const Rectangle& R1, const Rectangle& R2) {
bool IsIntersect(const Rect& R1, const Rect& R2) {
return R1.x <= R2.x + R2.width && R1.x + R1.width >= R2.x &&
R1.y <= R2.y + R2.height && R1.y + R1.height >= R2.y;
}
bool operator==(const Rectangle& r1, const Rectangle& r2) {
bool operator==(const Rect& r1, const Rect& r2) {
return std::tie(r1.x, r1.y, r1.width, r1.height) ==
std::tie(r2.x, r2.y, r2.width, r2.height);
}
} // namespace rectangle_intersection
using rectangle_intersection::IntersectRectangle;
template <>
struct SerializationTraits<rectangle_intersection::Rectangle>
: UserSerTraits<rectangle_intersection::Rectangle, int, int, int, int> {
struct SerializationTraits<Rect> : UserSerTraits<Rect, int, int, int, int> {
static std::vector<std::string> GetMetricNames(const std::string& arg_name) {
return {FmtStr("area({})", arg_name), FmtStr("perimeter({})", arg_name),
FmtStr("max(w, h)({})", arg_name)};
}
static std::vector<int> GetMetrics(
const rectangle_intersection::Rectangle& x) {
static std::vector<int> GetMetrics(const Rect& x) {
return {x.height * x.width, 2 * (x.height + x.width),
std::max(x.height, x.width)};
}
};
std::ostream& operator<<(std::ostream& out,
const rectangle_intersection::Rectangle& r) {
std::ostream& operator<<(std::ostream& out, const Rect& r) {
return PrintTo(out, std::make_tuple(r.x, r.y, r.width, r.height));
}

0 comments on commit 05f345d

Please sign in to comment.