Skip to content

Commit e88ebd1

Browse files
committed
Don't use std::tie because some might not have it available.
1 parent 38ee0bf commit e88ebd1

File tree

3 files changed

+73
-13
lines changed

3 files changed

+73
-13
lines changed

bench/run.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <random>
55
#include <string>
66
#include <vector>
7+
#include <iostream>
78

89
std::vector<double> generate_uniform(std::size_t n) {
910
std::vector<double> coords;
@@ -35,6 +36,48 @@ void BM_uniform(benchmark::State& state) {
3536
state.SetComplexityN(state.range(0));
3637
}
3738

39+
class MyFixture : public ::benchmark::Fixture
40+
{
41+
public:
42+
MyFixture() : vals(generate_uniform(10000000))
43+
{
44+
}
45+
46+
virtual void SetUp(::benchmark::State&)
47+
{}
48+
49+
virtual void TearDown(::benchmark::State&)
50+
{}
51+
52+
std::vector<double> vals;
53+
};
54+
55+
/**
56+
BENCHMARK_DEFINE_F(MyFixture, minmax)(::benchmark::State&state)
57+
{
58+
std::cerr << "vals size = " << vals.size() << "!\n";
59+
double mn = std::numeric_limits<double>::max();
60+
double mx = std::numeric_limits<double>::lowest();
61+
while (state.KeepRunning())
62+
{
63+
for (int i = 0; i < 100; ++i)
64+
{
65+
for (double& v : vals)
66+
{
67+
mn = std::min(mn, v);
68+
mx = std::max(mx, v);
69+
if (v < mn)
70+
mn = v;
71+
if (v > mx)
72+
mx = v;
73+
}
74+
}
75+
}
76+
std::cerr << "MIN/MAX = " << mn << "/" << mx << "!\n";
77+
}
78+
BENCHMARK_REGISTER_F(MyFixture, minmax)->Unit(benchmark::kMillisecond);
79+
**/
80+
3881
BENCHMARK(BM_45K_geojson_nodes)->Unit(benchmark::kMillisecond);
3982
BENCHMARK(BM_uniform)->Arg(2000)->Arg(100000)->Arg(200000)->Arg(500000)->Arg(1000000)->Unit(benchmark::kMillisecond);
4083

include/delaunator.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ inline bool orient(
7474
return (qy - py) * (rx - qx) - (qx - px) * (ry - qy) < 0.0;
7575
}
7676

77-
inline std::pair<double, double> circumcenter(
77+
inline Point circumcenter(
7878
const double ax,
7979
const double ay,
8080
const double bx,
@@ -93,7 +93,7 @@ inline std::pair<double, double> circumcenter(
9393
const double x = ax + (ey * bl - dy * cl) * 0.5 / d;
9494
const double y = ay + (dx * cl - ex * bl) * 0.5 / d;
9595

96-
return std::make_pair(x, y);
96+
return Point(x, y);
9797
}
9898

9999

@@ -102,16 +102,16 @@ struct compare {
102102
std::vector<double> const& coords;
103103
std::vector<double> dists;
104104

105-
compare(std::vector<double> const& coords,
106-
double center_x, double center_y) : coords(coords)
105+
compare(std::vector<double> const& coords, const Point& center) :
106+
coords(coords)
107107
{
108108
size_t n = coords.size() / 2;
109109
dists.reserve(n);
110110
double const *xcoord = coords.data();
111111
double const *ycoord = coords.data() + 1;
112112
while (n--)
113113
{
114-
dists.push_back(dist(*xcoord, *ycoord, center_x, center_y));
114+
dists.push_back(dist(*xcoord, *ycoord, center.x(), center.y()));
115115
xcoord += 2;
116116
ycoord += 2;
117117
}
@@ -192,8 +192,6 @@ Delaunator::Delaunator(std::vector<double> const& in_coords)
192192
hull_tri(),
193193
hull_start(),
194194
m_hash(),
195-
m_center_x(),
196-
m_center_y(),
197195
m_hash_size(),
198196
m_edge_stack() {
199197
std::size_t n = coords.size() >> 1;
@@ -279,10 +277,10 @@ Delaunator::Delaunator(std::vector<double> const& in_coords)
279277
std::swap(i1y, i2y);
280278
}
281279

282-
std::tie(m_center_x, m_center_y) = circumcenter(i0x, i0y, i1x, i1y, i2x, i2y);
280+
m_center = circumcenter(i0x, i0y, i1x, i1y, i2x, i2y);
283281

284282
// sort the points by distance from the seed triangle circumcenter
285-
std::sort(ids.begin(), ids.end(), compare{ coords, m_center_x, m_center_y });
283+
std::sort(ids.begin(), ids.end(), compare{ coords, m_center });
286284

287285
// initialize a hash table for storing edges of the advancing convex hull
288286
m_hash_size = static_cast<std::size_t>(std::llround(std::ceil(std::sqrt(n))));
@@ -516,8 +514,8 @@ std::size_t Delaunator::legalize(std::size_t a) {
516514
}
517515

518516
std::size_t Delaunator::hash_key(const double x, const double y) const {
519-
const double dx = x - m_center_x;
520-
const double dy = y - m_center_y;
517+
const double dx = x - m_center.x();
518+
const double dy = y - m_center.y();
521519
return fast_mod(
522520
static_cast<std::size_t>(std::llround(std::floor(pseudo_angle(dx, dy) * static_cast<double>(m_hash_size)))),
523521
m_hash_size);

include/delaunator.hpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,26 @@ namespace delaunator {
1414
constexpr std::size_t INVALID_INDEX =
1515
std::numeric_limits<std::size_t>::max();
1616

17+
class Point
18+
{
19+
public:
20+
Point(double x, double y) : m_x(x), m_y(y)
21+
{}
22+
Point() : m_x(0), m_y(0)
23+
{}
24+
25+
26+
double x() const
27+
{ return m_x; }
28+
29+
double y() const
30+
{ return m_y; }
31+
32+
private:
33+
double m_x;
34+
double m_y;
35+
};
36+
1737
class Delaunator {
1838

1939
public:
@@ -30,8 +50,7 @@ class Delaunator {
3050

3151
private:
3252
std::vector<std::size_t> m_hash;
33-
double m_center_x;
34-
double m_center_y;
53+
Point m_center;
3554
std::size_t m_hash_size;
3655
std::vector<std::size_t> m_edge_stack;
3756

0 commit comments

Comments
 (0)