7 if (!isValidCell(start) || !isValidCell(end)) {
8 throw std::domain_error(
"Невалидные H3 индексы");
12 throw std::runtime_error(
"Стартовая и конечная точка равны");
15 const int startRes = getResolution(start);
16 const int endRes = getResolution(end);
18 if (startRes != endRes) {
19 throw std::domain_error(
"Для Dijkstra H3 индексы только одинакового разрешения");
24 std::priority_queue<Node, std::vector<Node>, std::greater<>> pq;
27 std::unordered_map<H3Index, double, H3IndexHash> distances;
30 std::unordered_map<H3Index, H3Index, H3IndexHash> previous;
33 std::unordered_set<H3Index, H3IndexHash> visited;
36 distances[start] = 0.0;
37 pq.push({start, 0.0});
40 auto [cell, distance] = pq.top();
49 if (visited.contains(cell)) {
56 for (std::vector<H3Index> neighbors =
getNeighbors(cell);
const H3Index &neighbor : neighbors) {
57 if (visited.contains(neighbor)) {
65 if (
double newDistance = distances[cell] + edgeDistance;
66 !distances.contains(neighbor) || newDistance < distances[neighbor]) {
67 distances[neighbor] = newDistance;
68 previous[neighbor] = cell;
69 pq.push({neighbor, newDistance});
80 std::vector<H3Index> neighbors;
84 H3Error err = maxGridDiskSize(1, &maxSize);
85 if (err != E_SUCCESS) {
89 std::vector<H3Index> ring(maxSize);
90 err = gridDisk(cell, 1, ring.data());
91 if (err != E_SUCCESS) {
96 for (
const H3Index &idx : ring) {
97 if (idx != 0 && idx != cell) {
98 neighbors.emplace_back(idx);
105 LatLng coord1, coord2;
108 const H3Error err1 = cellToLatLng(cell1, &coord1);
109 const H3Error err2 = cellToLatLng(cell2, &coord2);
111 if (err1 != E_SUCCESS || err2 != E_SUCCESS) {
116 return greatCircleDistanceM(&coord1, &coord2);
119 H3Index start,
const H3Index end) {
120 std::vector<H3Index> path;
121 H3Index current = end;
123 while (current != start) {
124 path.emplace_back(current);
125 auto it = previous.find(current);
126 if (it == previous.end()) {
129 current = it->second;
132 path.emplace_back(start);
133 std::ranges::reverse(path);