Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++実装のリファクタリング/高速化(again) #9

Merged
merged 6 commits into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "cpp/external_projects/robin-hood-hashing"]
path = cpp/external_projects/robin-hood-hashing
url = https://github.com/martinus/robin-hood-hashing
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ Simple benchmarks of Dijkstra algorithm among C++, Go, Julia, Python(+Cython), J

# Requirement

Submodules are contained.
You need to `git submodule update --init --recursive` at first.

This benchmark uses [hyperfine](https://github.com/sharkdp/hyperfine).

And runs on languages below:
Expand Down
2 changes: 1 addition & 1 deletion cpp/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ clean:
rm main

main: src/main.cpp
clang++ -std=c++17 -O3 -Wall -Wextra -pedantic-errors -march=native -mtune=native -o main src/main.cpp
clang++ -std=c++17 -O3 -Wall -Wextra -pedantic-errors -march=native -mtune=native -I external_projects/robin-hood-hashing/src/include -o main src/main.cpp
1 change: 1 addition & 0 deletions cpp/external_projects/robin-hood-hashing
Submodule robin-hood-hashing added at 900e80
18 changes: 10 additions & 8 deletions cpp/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@
#include<string_view>
#include<iostream>
#include<queue>
#include<cstdint>
#include<limits>
#include"robin_hood.h"

using NodeId = int;
using NodeIndex = int;
using Distance = int;
using Distance = std::int32_t;
using Edge = std::pair<NodeIndex, Distance>;

constexpr int DISTANCE_MULTIPLE = 100;

bool is_debug = false;

struct G {
std::unordered_map<NodeId,NodeIndex> id2idx;
robin_hood::unordered_map<NodeId,NodeIndex> id2idx;
std::vector<NodeId> idx2id = {0};
NodeIndex idx = 1;
std::vector<std::vector<Edge>> edge = {std::vector<Edge>()};
Expand Down Expand Up @@ -81,10 +84,10 @@ void load() {
break;
}
while (!isgraph(line.back())) line.pop_back(); // strip
NodeId s = 0, e = 0;
Distance d = 0;
NodeId s, e;
Distance d;
for (std::string::size_type idx=0, pos=0, prev_pos=0; pos <= line.length(); pos++) {
if (line[pos] == ',' || line[pos] == '\r' || pos == line.length()) {
if (line[pos] == ',' || pos == line.length()) {
const auto field = std::string_view{line}.substr(prev_pos, pos-prev_pos);
switch (idx) {
case 2: s = stoi_unchecked(field); break;
Expand All @@ -109,8 +112,7 @@ inline std::pair<Distance, std::vector<NodeId>> dijkstra(NodeId start, NodeId en
const NodeIndex e = get_idx(end);

const int size = g.idx;
std::vector<Distance> d(size);
std::fill(d.begin(),d.end(),INT32_MAX);
std::vector<Distance> d(size, std::numeric_limits<Distance>::max());
std::vector<NodeIndex> prev(size);

std::priority_queue<Visit, std::vector<Visit>, std::greater<Visit>> queue;
Expand Down Expand Up @@ -143,7 +145,7 @@ inline std::pair<Distance, std::vector<NodeId>> dijkstra(NodeId start, NodeId en
NodeIndex n = e;
result.push_back(g.idx2id[n]);

while (d[n] != INT32_MAX && n != s && n != 0) {
while (d[n] != std::numeric_limits<Distance>::max() && n != s && n != 0) {
n = prev[n];
result.push_back(g.idx2id[n]);
}
Expand Down