+void bfs_edge(struct graph_edge *e, struct graph_node *node, struct graph_node *anode, int dir)
+{
+ ASSERT(dir < 3);
+ struct graph_edge *candidate = NULL;
+
+ if ((e->num > 31) && (e->num < 48)) printf("Searching for neighbours of %d, in longline %u, BFS dir is %d, edge's dir is %d\n", e->num, e->longline, dir, e->dir);
+
+
+ for (uns i=0; i<GARY_SIZE(node->edges); i++)
+ {
+ struct graph_edge *other = node->edges[i];
+printf("Touching %d\n", other->num);
+if (other->num == 44) printf("Longline of 44 is %u\n", other->longline);
+ if ((other->longline != (uns) -1) && (other->longline != e->longline)) continue;
+
+ if (! other->visited) {
+ struct graph_edge **e_ptr = GARY_PUSH(bfs_queue);
+ *e_ptr = other;
+ other->visited = 1;
+ }
+
+ if (((other->n1->id == node->id) && (other->n2->id == anode->id)) ||
+ ((other->n2->id == node->id) && (other->n1->id == anode->id)))
+ continue;
+
+ if (((other->n1->id == node->id) || (other->n2->id == node->id)) &&
+ (e->text) && (other->text) && (e->text->text == other->text->text))
+ {
+ if (! candidate || (other->length > candidate->length))
+ candidate = other;
+ }
+ }
+
+ if (candidate)
+ {
+ struct graph_edge *other = candidate;
+dbg_num_hits++;
+ other->longline = e->longline;
+ other->dir = dir;
+ other->anode = (other->n1->id == node->id ? other->n2 : other->n1);
+ other->bnode = (other->n1->id == node->id ? other->n1 : other->n2);
+ switch (dir)
+ {
+ case 1:
+ e->prev = other;
+ other->next = e;
+ longlines[other->longline].first = other;
+ break;
+ case 2:
+ e->next = other;
+ other->prev = e;
+ break;
+ default:
+ printf("Oops\n");
+ ASSERT(0);
+ }
+ }
+}
+
+void bfs(void)
+{
+ for (uns i=0; i<GARY_SIZE(bfs_queue); i++)
+ {
+ struct graph_edge *cur = bfs_queue[i];
+ printf("Exploring new edge, %d\n", cur->num);
+ //ASSERT(! cur->visited);
+
+ cur->visited = 1;
+ if (cur->longline == (uns) -1)
+ {
+ GARY_PUSH(longlines);
+ cur->longline = num_longlines++;
+ longlines[cur->longline].first = cur;
+ }
+
+ if (!cur->anode)
+ {
+ bfs_edge(cur, cur->n1, cur->n2, 1);
+ bfs_edge(cur, cur->n2, cur->n1, 2);
+ }
+ else
+ {
+ bfs_edge(cur, cur->anode, cur->bnode, cur->dir);
+ }
+ }
+}
+
+void bfs_wrapper(void)
+{
+ GARY_INIT(bfs_queue, 0);
+ GARY_INIT(longlines, 0);
+
+ HASH_FOR_ALL(hash, node)
+ {
+ for (uns i=0; i<GARY_SIZE(node->edges); i++)
+ {
+ if (! node->edges[i]->visited)
+ {
+ printf("Running new BFS\n");
+ GARY_RESIZE(bfs_queue, 0);
+ struct graph_edge **e = GARY_PUSH(bfs_queue);
+ *e = node->edges[i];
+ bfs();
+ //dump_longlines();
+ printf("Joined %d edges\n", dbg_num_hits); dbg_num_hits = 0;
+ printf("Planned %u edges\n", GARY_SIZE(bfs_queue));
+ }
+ }
+ }
+ HASH_END_FOR;
+
+ GARY_FREE(bfs_queue);
+}
+
+void oldbfs(void)
+{
+ printf("Starting outer BFS\n");
+ printf("There are %u buffered lines and %d eges\n", GARY_SIZE(buffer_line), num_edges_dbg);
+
+ GARY_INIT(bfs_queue, 0);
+ GARY_INIT(longlines, 0);
+
+ int dbg_bfs_continues = 0;
+
+ HASH_FOR_ALL(hash, node)
+ {
+ // FIXME: Skip if visited node
+
+ for (uns i=0; i<GARY_SIZE(node->edges); i++)
+ {
+ struct graph_edge *e = node->edges[i];
+
+ if (e->visited) continue;
+
+ // BFS itself
+ for (uns i1=0; i1<GARY_SIZE(e->n1->edges); i1++)
+ {
+ struct graph_edge *other = e->n1->edges[i1];
+ if (other->visited) { dbg_bfs_continues++; continue; }
+
+ if (((other->n1->id == e->n1->id) || (other->n2->id == e->n1->id)) &&
+ (e->text) && (other->text) && (e->text->text == other->text->text))
+ {
+// printf("Hit\n");
+ other->visited = 1;
+ }
+ }
+ for (uns i2=0; i2<GARY_SIZE(e->n2->edges); i2++)
+ {
+ struct graph_edge *other = e->n2->edges[i2];
+ if (other->visited) {dbg_bfs_continues++; continue; }
+
+ if (((other->n1->id == e->n2->id) || (other->n2->id == e->n2->id)) &&
+ (e->text) && (other->text) && (e->text->text == other->text->text))
+ {
+// printf("Hit\n");
+ other->visited = 1;
+ }
+ }
+ }
+ }
+
+ HASH_END_FOR;
+ printf("Total: %d hits, %d visited edges skipped\n", dbg_num_hits, dbg_bfs_continues);
+
+ GARY_FREE(bfs_queue);
+}
+
+void dump_longlines(void)
+{
+ for (uns i=0; i<GARY_SIZE(longlines); i++)
+ {
+ struct graph_edge *e = longlines[i].first;
+
+ printf("> Longline %u;", i);
+ if (longlines[i].first->text) printf(" labelled %s", osm_val_decode(longlines[i].first->text->text));
+ printf("\n");
+ while (e)
+ {
+ printf("\t#%ju (%d)", e->id, e->num);
+ switch (e->dir)
+ {
+ case 1:
+ printf("[%.2f; %.2f] -- #%u [%.2f; %.2f] (dir %d)\n", e->anode->o->x, e->anode->o->y, e->bnode->o->o.id, e->bnode->o->x, e->bnode->o->y, e->dir);
+ break;
+ case 2:
+ printf("[%.2f; %.2f] -- #%u [%.2f; %.2f] (dir %d)\n", e->bnode->o->x, e->bnode->o->y, e->anode->o->o.id, e->anode->o->x, e->anode->o->y, e->dir);
+ break;
+ case 0:
+ printf("[%.2f; %.2f] -- #%u [%.2f; %.2f] (dir %d)\n", e->n1->o->x, e->n1->o->y, e->n2->o->o.id, e->n2->o->x, e->n2->o->y, e->dir);
+ break;
+ default:
+ printf("%d\n", e->dir);
+ ASSERT(0);
+ }
+ e = e->next;
+ }
+ }
+}
+