]> wirehaze git hosting - graph-theory.git/blobdiff - graph/adj.c

wirehaze git hosting

dfs
[graph-theory.git] / graph / adj.c
index ec9cd0c8690d0d8acf381d05409a0f515160350c..e44f58344175baab1ddc32e21e19466aadde1283 100644 (file)
@@ -1,14 +1,17 @@
-#include "adj.h"
-#include <stdlib.h>
+#include "graph/adj.h"
 #include <errno.h>
+#include <stdlib.h>
 
 struct graph *
 new_graph (unsigned int nv)
 {
-  struct graph *g;  
+  struct graph *g;
 
   if (!nv)
-    return NULL;
+    {
+      errno = EINVAL;
+      return NULL;
+    }
 
   if (!(g = malloc (sizeof *g)))
     return NULL;
@@ -30,10 +33,10 @@ new_arc (struct graph *g, unsigned int vi, unsigned int vj)
   struct edge *e;
 
   if (vi >= g->nv || vj >= g->nv)
-    return -EINVAL;
+    return -(errno = EINVAL);
 
   if (!(e = malloc (sizeof *e)))
-    return -ENOMEM;
+    return -errno;
 
   e->to = &g->v[vj];
   e->next = g->v[vi].adj;
@@ -47,11 +50,14 @@ new_edge (struct graph *g, unsigned int vi, unsigned int vj)
 {
   int err;
 
+  if (vi == vj)
+    return -(errno = EINVAL);
+
   if ((err = new_arc (g, vi, vj)))
     return err;
 
-  if (vi != vj && (err = new_arc (g, vj, vi)))
-    return err; /* should delete the other arc but fuck it we ball */
+  if ((err = new_arc (g, vj, vi)))
+    return err;
 
   return 0;
 }