[pacman-dev,1/5] graph.h: replace hardcoded values with an enum

Message ID 20170415215707.22767-1-andrew.gregory.8@gmail.com
State Accepted, archived
Headers show
Series [pacman-dev,1/5] graph.h: replace hardcoded values with an enum | expand

Commit Message

Andrew Gregory April 15, 2017, 9:57 p.m. UTC
Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com>
---

This series is an attempt to clean up sortbydeps primarily by giving variables
better names and factoring out the dependency cycle warning.  There shouldn't
be any change in behavior.

 lib/libalpm/delta.c |  4 ++--
 lib/libalpm/deps.c  | 10 +++++-----
 lib/libalpm/graph.h |  8 +++++++-
 3 files changed, 14 insertions(+), 8 deletions(-)

Comments

Allan McRae April 16, 2017, 11:25 a.m. UTC | #1
On 16/04/17 07:57, Andrew Gregory wrote:
> Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com>
> ---

<snip>

> +enum __alpm_graph_vertex_state {
> +	ALPM_GRAPH_STATE_UNPROCESSED = 0,
> +	ALPM_GRAPH_STATE_PROCESSING = -1,
> +	ALPM_GRAPH_STATE_PROCESSED = 1
> +};
> +

Why keep the -1, 0, 1 state when switching to an enum?  I can't see a
point when it matters?

>  typedef struct __alpm_graph_t {
>  	void *data;
>  	struct __alpm_graph_t *parent; /* where did we come from? */
>  	alpm_list_t *children;
>  	alpm_list_t *childptr; /* points to a child in children list */
>  	off_t weight; /* weight of the node */
> -	signed char state; /* 0: untouched, -1: entered, other: leaving time */
> +	signed char state;

struct __alpm_graph_vertex_state state
?

>  } alpm_graph_t;
>  
>  alpm_graph_t *_alpm_graph_new(void);
>

Patch

diff --git a/lib/libalpm/delta.c b/lib/libalpm/delta.c
index c5571b29..58b1b41e 100644
--- a/lib/libalpm/delta.c
+++ b/lib/libalpm/delta.c
@@ -130,7 +130,7 @@  static void dijkstra(alpm_list_t *vertices)
 		for(i = vertices; i; i = i->next) {
 			alpm_graph_t *v_i = i->data;
 
-			if(v_i->state == -1) {
+			if(v_i->state == ALPM_GRAPH_STATE_PROCESSING) {
 				continue;
 			}
 
@@ -142,7 +142,7 @@  static void dijkstra(alpm_list_t *vertices)
 			break;
 		}
 
-		v->state = -1;
+		v->state = ALPM_GRAPH_STATE_PROCESSING;
 
 		v->childptr = v->children;
 		while(v->childptr) {
diff --git a/lib/libalpm/deps.c b/lib/libalpm/deps.c
index 40b2be6d..e5527d6c 100644
--- a/lib/libalpm/deps.c
+++ b/lib/libalpm/deps.c
@@ -194,16 +194,16 @@  alpm_list_t *_alpm_sortbydeps(alpm_handle_t *handle,
 	vertex = vertices->data;
 	while(vptr) {
 		/* mark that we touched the vertex */
-		vertex->state = -1;
+		vertex->state = ALPM_GRAPH_STATE_PROCESSING;
 		int found = 0;
 		while(vertex->childptr && !found) {
 			alpm_graph_t *nextchild = vertex->childptr->data;
 			vertex->childptr = vertex->childptr->next;
-			if(nextchild->state == 0) {
+			if(nextchild->state == ALPM_GRAPH_STATE_UNPROCESSED) {
 				found = 1;
 				nextchild->parent = vertex;
 				vertex = nextchild;
-			} else if(nextchild->state == -1) {
+			} else if(nextchild->state == ALPM_GRAPH_STATE_PROCESSING) {
 				/* child is an ancestor of vertex */
 				alpm_graph_t *transvertex = vertex;
 
@@ -244,13 +244,13 @@  alpm_list_t *_alpm_sortbydeps(alpm_handle_t *handle,
 				newtargs = alpm_list_add(newtargs, vertex->data);
 			}
 			/* mark that we've left this vertex */
-			vertex->state = 1;
+			vertex->state = ALPM_GRAPH_STATE_PROCESSED;
 			vertex = vertex->parent;
 			if(!vertex) {
 				/* top level vertex reached, move to the next unprocessed vertex */
 				for( vptr = vptr->next; vptr; vptr = vptr->next) {
 					vertex = vptr->data;
-					if(vertex->state == 0) {
+					if(vertex->state == ALPM_GRAPH_STATE_UNPROCESSED) {
 						break;
 					}
 				}
diff --git a/lib/libalpm/graph.h b/lib/libalpm/graph.h
index 2e9eac10..f1da837c 100644
--- a/lib/libalpm/graph.h
+++ b/lib/libalpm/graph.h
@@ -23,13 +23,19 @@ 
 
 #include "alpm_list.h"
 
+enum __alpm_graph_vertex_state {
+	ALPM_GRAPH_STATE_UNPROCESSED = 0,
+	ALPM_GRAPH_STATE_PROCESSING = -1,
+	ALPM_GRAPH_STATE_PROCESSED = 1
+};
+
 typedef struct __alpm_graph_t {
 	void *data;
 	struct __alpm_graph_t *parent; /* where did we come from? */
 	alpm_list_t *children;
 	alpm_list_t *childptr; /* points to a child in children list */
 	off_t weight; /* weight of the node */
-	signed char state; /* 0: untouched, -1: entered, other: leaving time */
+	signed char state;
 } alpm_graph_t;
 
 alpm_graph_t *_alpm_graph_new(void);