#include <stdint.h>
(uintptr_t)
error: use of undeclared identifier 'uintptr_t'
int edges_start[3000];
int edges_end[3000];
float edges_len[3000];
int g_links_id[1000][100];
float g_links_weight[1000][100];
int cache_id[1000];
float cache_distance[1000];
int cache_prev[1000];
int cache_links_id[1000][100];
float cache_links_weight[1000][100];
int queue_id[1000];
float queue_distance[1000];
int queue_prev[1000];
int queue_links_id[1000][100];
float queue_links_weight[1000][100];
void* memcpy(char* dst, char* src, int count) {
while(count--) *dst++ = *src++;
}
void init_cpp() {
for (int i=0; i < 1000; i++) {
cache_id[i] = -2;
queue_id[i] = -2;
cache_distance[i] = 100000;
queue_distance[i] = 100000;
cache_prev[i] = -2;
queue_prev[i] = -2;
for (int j=0; j < 100; j++) {
queue_links_id[i][j] = -2;
queue_links_weight[i][j] = 100000;
cache_links_id[i][j] = -2;
cache_links_weight[i][j] = 100000;
}
}
}
void init_edges_cpp() {
for (int i=0; i < 3000; i++) {
edges_start[i] = -2;
edges_end[i] = -2;
edges_len[i] = -2.0;
}
for (int i=0; i < 1000; i++) {
for (int j=0; j < 100; j++) {
g_links_id[i][j] = -2;
g_links_weight[i][j] = -2.0;
}
}
}
void add_edge_cpp(int index, int start, int end, float len) {
edges_start[index] = start;
edges_end[index] = end;
edges_len[index] = len;
}
void fill_graph_cpp() {
for (int i=0; i < 3000; i++) {
int s = edges_start[i];
int e = edges_end[i];
float len = edges_len[i];
if (s == -2) {
break;
}
//console.log(s, e, len)
//var links_len = get_len(g_links_id[s])
int links_len = 0;
for (int j=0; j < 100; j++) {
if (g_links_id[s][j] == -2) {
links_len = j;
break;
}
}
//console.log(g_links_id)
g_links_id[s][links_len] = e;
g_links_weight[s][links_len] = len;
//console.log(get_len(g_links_id[s]))
//var links_len = get_len(g_links_id[e])
for (int j=0; j < 100; j++) {
if (g_links_id[e][j] == -2) {
links_len = j;
break;
}
}
g_links_id[e][links_len] = s;
g_links_weight[e][links_len] = len;
}
}
void get_dists_cpp(int a, int L) {
// initialization
//int L=1000
/*
console.log('new cache: ')
console.log(cache_links_id)
console.log(cache_links_weight)
*/
int i = L;
while (--i >= 0) {
//v_links_id = g_links_id[i];
//v_links_weight = g_links_weight[i];
//cache_distance[i] = 100000;
//cache_prev[i] = -2;
for (int j = 0; j < 100; j++) {
//console.log()
if (g_links_id[i][j] == -2) {
break;
}
cache_links_id[i][j] = g_links_id[i][j];
cache_links_weight[i][j] = g_links_weight[i][j];
}
//console.log(v.links)
//console.log(cache_links_id[i])
//console.log(cache_links_weight[i])
cache_id[i] = i;
}
queue_id[0] = cache_id[a];
cache_distance[a] = 0;
queue_distance[0] = cache_distance[a];
queue_prev[0] = queue_prev[a];
for (int j=0; j < 100; j++) {
queue_links_id[0][j] = cache_links_id[a][j];
queue_links_weight[0][j] = cache_links_weight[a][j];
}
i=0;
int queue_len = 1;
while (i < queue_len) {
int node_id = queue_id[i];
float node_distance = queue_distance[i];
int node_prev = queue_prev[i];
//links_id = queue_links_id[i]
//links_weight = queue_links_weight[i]
//j = get_len(links_id)
int j=0;
for (int k=0; k < 100; k++) {
if (queue_links_id[i][k] == -2) {
j=k;
break;
}
}
while (--j >= 0) {
int link_id = queue_links_id[i][j];
float link_weight = queue_links_weight[i][j];
int c_id = cache_id[link_id];
float c_distance = cache_distance[link_id];
int c_prev = cache_prev[link_id];
float d = node_distance + link_weight;
if (d < c_distance) {
cache_prev[link_id] = node_id;
cache_distance[link_id] = d;
//last_ind = get_len(queue_id)
/*
for (var k=0; k < 1000; k++) {
if (queue_id[k] == undefined) {
last_ind = k;
break;
}
}*/
int last_ind = queue_len;
queue_id[last_ind] = cache_id[link_id];
queue_distance[last_ind] = cache_distance[link_id];
//console.log(last_ind, queue_links_id[last_ind])
for (int k=0; k < 100; k++) {
if (cache_links_id[link_id][k] == -2) {
break;
}
queue_links_id[last_ind][k] = cache_links_id[link_id][k];
queue_links_weight[last_ind][k] = cache_links_weight[link_id][k];
}
queue_prev[last_ind] = cache_prev[link_id];
queue_len++;
}
}
i++;
}
}
float get_cache_distance(int index) {
return cache_distance[index];
}
int get_cache_prev(int index) {
return cache_prev[index];
}
int main() {
init_edges_cpp();
init_cpp();
add_edge_cpp(0, 0, 2, 1);
add_edge_cpp(1, 0, 1, 1);
add_edge_cpp(2, 1, 2, 1);
add_edge_cpp(3, 2, 3, 1);
add_edge_cpp(4, 2, 4, 1);
add_edge_cpp(5, 3, 4, 1);
fill_graph_cpp();
get_dists_cpp(0, 10);
/*
for (int i=0; i < 10; i++) {
cout << i << " " << get_cache_distance(i) << " " << get_cache_prev(i) << endl;
}
*/
return 0;
}
var wasmModule = new WebAssembly.Module(wasmCode);
var wasmInstance = new WebAssembly.Instance(wasmModule, wasmImports);
var wasm_module = wasmInstance.exports;
console.log(wasm_module)
wasm_module.main();
for (var i=0; i < 5; i++) {
var c_dist = wasm_module.get_cache_distance(i)
console.log(c_dist)
}
0
100000
100000
100000
100000
Uncaught DOMException: Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.
#include <iostream>
using namespace std;
int v[1000];
float cache_1[1000];
int cache_2[1000];
int s[5000];
int e[5000];
float l[5000];
int graph_1[1000][20];
float graph_2[1000][20];
float queue_1[1000];
int queue_2[1000];
int queue_ids[1000];
int links_1[1000][20];
float links_2[1000][20];
void* memcpy(char* dst, char* src, int count) {
while(count--) *dst++ = *src++;
}
void init_arr() {
for (int i=0; i < 1000; ++i) {
v[i] = -2;
cache_1[i] = -2.0;
cache_2[i] = -2;
}
for (int i=0; i < 5000; i++) {
s[i] = -2;
e[i] = -2;
l[i] = -2.0;
}
for (int i=0; i < 1000; i++) {
for (int j=0; j < 20; j++) {
graph_1[i][j] = -2;
graph_2[i][j] = -2.0;
}
}
}
void add_point(int i, int point) {
v[i] = point;
}
void add_edge(int i, int s_p, int e_p, float edge_len) {
s[i] = s_p;
e[i] = e_p;
l[i] = edge_len;
}
void fill_graph() {
for (int i=0; i < 5000; i++) {
int start = s[i];
int end = e[i];
float dist = l[i];
if (start == -2) {
break;
}
// найдём место, куда вставить
int j = 0;
while (graph_1[start][j] > -2) {
j++;
}
graph_1[start][j] = end;
graph_2[start][j] = dist;
j=0;
while (graph_1[end][j] > -2) {
j++;
}
graph_1[end][j] = start;
graph_2[end][j] = dist;
}
}
void get_dists(int a) {
int L=0;
for (int i=0; i<1000; i++) {
if (graph_1[i][0] > -2) {
L++;
} else {
break;
}
}
for (int i=0; i < 1000; i++) {
for (int j=0; j < 20; j++) {
links_1[i][j] = -2;
links_2[i][j] = -2.0;
}
}
int i = L;
while (--i >= 0) {
cache_1[i] = 100000.0;
cache_2[i] = i;
for (int j=0; j < 20; j++) {
links_1[i][j] = graph_1[i][j];
links_2[i][j] = graph_2[i][j];
}
}
float root_1 = cache_1[a];
int root_2 = cache_2[a];
cache_1[a] = 0;
cache_2[a] = -1;
root_1 = 0.0;
root_2 = -1;
for (int j=0; j < 1000; j++) {
queue_1[j] = -2.0;
queue_ids[j] = -2;
}
queue_1[0] = root_1;
queue_ids[0] = 0;
i=0;
int len = 1;
while (i < len) {
float node_1 = queue_1[i];
int node_id = queue_ids[i];
int node_verts_len = 0;
for (int j=0; j < 20; j++) {
if (links_1[node_id][j] > -2) {
node_verts_len++;
}
}
int j = node_verts_len-1;
while (j >= 0) {
int link_vert = links_1[node_id][j];
float link_dist = links_2[node_id][j];
float c1 = cache_1[link_vert];
int c2 = cache_2[link_vert];
float d = node_1 + link_dist;
if (d < c1) {
cache_1[link_vert] = d;
cache_2[link_vert] = node_id;
queue_1[len] = d;
queue_ids[len] = link_vert;
len+=1;
}
j--;
}
i++;
}
}
float get_cache_1(int i) {
return cache_1[i];
}
int get_cache_2(int i) {
return cache_2[i];
}
int main() {
init_arr();
add_point(0, 0);
add_point(1, 1);
add_point(2, 2);
add_point(3, 3);
add_point(4, 4);
add_edge(0, 0, 2, 1);
add_edge(1, 0, 1, 1);
add_edge(2, 1, 2, 1);
add_edge(3, 2, 3, 1);
add_edge(4, 2, 4, 1);
add_edge(5, 3, 4, 1);
fill_graph();
get_dists(0);
for (int i=0; i < 5; i++) {
//cout << i << " " << get_cache_1(i) << " " << get_cache_2(i) << endl;
}
return 0;
}