1111 * Copyright (c) 2004-2005 The Regents of the University of California.
1212 * All rights reserved.
1313 * Copyright (c) 2007 Voltaire All rights reserved.
14- * Copyright (c) 2016 Los Alamos National Security, LLC. All rights
14+ * Copyright (c) 2016-2017 Los Alamos National Security, LLC. All rights
1515 * reserved.
1616 * Copyright (c) 2016 Cisco Systems, Inc. All rights reserved.
1717 * $COPYRIGHT$
@@ -186,25 +186,16 @@ static void opal_adjacency_list_destruct(opal_adjacency_list_t *aj_list)
186186static void delete_all_edges_conceded_to_vertex (opal_graph_t * graph , opal_graph_vertex_t * vertex )
187187{
188188 opal_adjacency_list_t * aj_list ;
189- opal_list_item_t * aj_list_item ;
190- opal_graph_edge_t * edge ;
191- opal_list_item_t * edge_item ;
189+ opal_graph_edge_t * edge , * next ;
192190
193191 /**
194192 * for all the adjacency list in the graph
195193 */
196- for (aj_list_item = opal_list_get_first (graph -> adjacency_list );
197- aj_list_item != opal_list_get_end (graph -> adjacency_list );
198- aj_list_item = opal_list_get_next (aj_list_item )) {
199- aj_list = (opal_adjacency_list_t * ) aj_list_item ;
194+ OPAL_LIST_FOREACH (aj_list , graph -> adjacency_list , opal_adjacency_list_t ) {
200195 /**
201196 * for all the edges in the adjacency list
202197 */
203- edge_item = opal_list_get_first (aj_list -> edges );
204- while (edge_item != opal_list_get_end (aj_list -> edges )) {
205- edge = (opal_graph_edge_t * )edge_item ;
206- edge_item = opal_list_get_next (edge_item );
207-
198+ OPAL_LIST_FOREACH_SAFE (edge , next , aj_list -> edges , opal_graph_edge_t ) {
208199 /**
209200 * if the edge is ended in the vertex
210201 */
@@ -228,15 +219,11 @@ static void delete_all_edges_conceded_to_vertex(opal_graph_t *graph, opal_graph_
228219void opal_graph_add_vertex (opal_graph_t * graph , opal_graph_vertex_t * vertex )
229220{
230221 opal_adjacency_list_t * aj_list ;
231- opal_list_item_t * item ;
232222
233223 /**
234224 * Find if this vertex already exists in the graph.
235225 */
236- for (item = opal_list_get_first (graph -> adjacency_list );
237- item != opal_list_get_end (graph -> adjacency_list );
238- item = opal_list_get_next (item )) {
239- aj_list = (opal_adjacency_list_t * ) item ;
226+ OPAL_LIST_FOREACH (aj_list , graph -> adjacency_list , opal_adjacency_list_t ) {
240227 if (aj_list -> vertex == vertex ) {
241228 /* If this vertex exists, dont do anything. */
242229 return ;
@@ -270,17 +257,13 @@ void opal_graph_add_vertex(opal_graph_t *graph, opal_graph_vertex_t *vertex)
270257int opal_graph_add_edge (opal_graph_t * graph , opal_graph_edge_t * edge )
271258{
272259 opal_adjacency_list_t * aj_list , * start_aj_list = NULL ;
273- opal_list_item_t * item ;
274260 bool start_found = false, end_found = false;
275261
276262
277263 /**
278264 * find the vertices that this edge should connect.
279265 */
280- for (item = opal_list_get_first (graph -> adjacency_list );
281- item != opal_list_get_end (graph -> adjacency_list );
282- item = opal_list_get_next (item )) {
283- aj_list = (opal_adjacency_list_t * ) item ;
266+ OPAL_LIST_FOREACH (aj_list , graph -> adjacency_list , opal_adjacency_list_t ) {
284267 if (aj_list -> vertex == edge -> start ) {
285268 start_found = true;
286269 start_aj_list = aj_list ;
@@ -372,7 +355,6 @@ void opal_graph_remove_vertex(opal_graph_t *graph, opal_graph_vertex_t *vertex)
372355uint32_t opal_graph_adjacent (opal_graph_t * graph , opal_graph_vertex_t * vertex1 , opal_graph_vertex_t * vertex2 )
373356{
374357 opal_adjacency_list_t * adj_list ;
375- opal_list_item_t * item ;
376358 opal_graph_edge_t * edge ;
377359
378360 /**
@@ -401,10 +383,7 @@ uint32_t opal_graph_adjacent(opal_graph_t *graph, opal_graph_vertex_t *vertex1,
401383 * vertex.
402384 */
403385 adj_list = (opal_adjacency_list_t * ) vertex1 -> in_adj_list ;
404- for (item = opal_list_get_first (adj_list -> edges );
405- item != opal_list_get_end (adj_list -> edges );
406- item = opal_list_get_next (item )) {
407- edge = (opal_graph_edge_t * )item ;
386+ OPAL_LIST_FOREACH (edge , adj_list -> edges , opal_graph_edge_t ) {
408387 if (edge -> end == vertex2 ) {
409388 /* if the second vertex was found in the adjacency list of the first one, return the weight */
410389 return edge -> weight ;
@@ -452,15 +431,11 @@ int opal_graph_get_size(opal_graph_t *graph)
452431opal_graph_vertex_t * opal_graph_find_vertex (opal_graph_t * graph , void * vertex_data )
453432{
454433 opal_adjacency_list_t * aj_list ;
455- opal_list_item_t * item ;
456434
457435 /**
458436 * Run on all the vertices of the graph
459437 */
460- for (item = opal_list_get_first (graph -> adjacency_list );
461- item != opal_list_get_end (graph -> adjacency_list );
462- item = opal_list_get_next (item )) {
463- aj_list = (opal_adjacency_list_t * ) item ;
438+ OPAL_LIST_FOREACH (aj_list , graph -> adjacency_list , opal_adjacency_list_t ) {
464439 if (NULL != aj_list -> vertex -> compare_vertex ) {
465440 /* if the vertex data of a vertex is equal to the vertex data */
466441 if (0 == aj_list -> vertex -> compare_vertex (aj_list -> vertex -> vertex_data , vertex_data )) {
@@ -489,8 +464,6 @@ opal_graph_vertex_t *opal_graph_find_vertex(opal_graph_t *graph, void *vertex_da
489464int opal_graph_get_graph_vertices (opal_graph_t * graph , opal_pointer_array_t * vertices_list )
490465{
491466 opal_adjacency_list_t * aj_list ;
492- opal_list_item_t * item ;
493- int i ;
494467
495468 /**
496469 * If the graph order is 0, return NULL.
@@ -499,10 +472,7 @@ int opal_graph_get_graph_vertices(opal_graph_t *graph, opal_pointer_array_t *ver
499472 return 0 ;
500473 }
501474 /* Run on all the vertices of the graph */
502- for (item = opal_list_get_first (graph -> adjacency_list ), i = 0 ;
503- item != opal_list_get_end (graph -> adjacency_list );
504- item = opal_list_get_next (item ), i ++ ) {
505- aj_list = (opal_adjacency_list_t * ) item ;
475+ OPAL_LIST_FOREACH (aj_list , graph -> adjacency_list , opal_adjacency_list_t ) {
506476 /* Add the vertex to the vertices array */
507477 opal_pointer_array_add (vertices_list ,(void * )aj_list -> vertex );
508478 }
@@ -528,9 +498,7 @@ int opal_graph_get_adjacent_vertices(opal_graph_t *graph, opal_graph_vertex_t *v
528498 opal_adjacency_list_t * adj_list ;
529499 opal_graph_edge_t * edge ;
530500 int adjacents_number ;
531- opal_list_item_t * item ;
532501 vertex_distance_from_t distance_from ;
533- int i ;
534502
535503 /**
536504 * Verify that the vertex belongs to the graph.
@@ -546,10 +514,7 @@ int opal_graph_get_adjacent_vertices(opal_graph_t *graph, opal_graph_vertex_t *v
546514 /* find the number of adjcents of this vertex */
547515 adjacents_number = opal_list_get_size (adj_list -> edges );
548516 /* Run on all the edges from this vertex */
549- for (item = opal_list_get_first (adj_list -> edges ), i = 0 ;
550- item != opal_list_get_end (adj_list -> edges );
551- item = opal_list_get_next (item ), i ++ ) {
552- edge = (opal_graph_edge_t * )item ;
517+ OPAL_LIST_FOREACH (edge , adj_list -> edges , opal_graph_edge_t ) {
553518 /* assign vertices and their weight in the adjcents list */
554519 distance_from .vertex = edge -> end ;
555520 distance_from .weight = edge -> weight ;
@@ -663,7 +628,6 @@ uint32_t opal_graph_dijkstra(opal_graph_t *graph, opal_graph_vertex_t *vertex, o
663628{
664629 int graph_order ;
665630 vertex_distance_from_t * Q , * q_start , * current_vertex ;
666- opal_list_item_t * adj_list_item ;
667631 opal_adjacency_list_t * adj_list ;
668632 int number_of_items_in_q ;
669633 int i ;
@@ -683,22 +647,15 @@ uint32_t opal_graph_dijkstra(opal_graph_t *graph, opal_graph_vertex_t *vertex, o
683647 /* assign a pointer to the start of the queue */
684648 q_start = Q ;
685649 /* run on all the vertices of the graph */
686- for (adj_list_item = opal_list_get_first (graph -> adjacency_list ), i = 0 ;
687- adj_list_item != opal_list_get_end (graph -> adjacency_list );
688- adj_list_item = opal_list_get_next (adj_list_item ), i ++ ) {
689- adj_list = (opal_adjacency_list_t * )adj_list_item ;
650+ i = 0 ;
651+ OPAL_LIST_FOREACH (adj_list , graph -> adjacency_list , opal_adjacency_list_t ) {
690652 /* insert the vertices pointes to the working queue */
691653 Q [i ].vertex = adj_list -> vertex ;
692654 /**
693655 * assign an infinity distance to all the vertices in the queue
694656 * except the reference vertex which its distance should be 0.
695657 */
696- if (Q [i ].vertex == vertex ) {
697- Q [i ].weight = 0 ;
698- }
699- else {
700- Q [i ].weight = DISTANCE_INFINITY ;
701- }
658+ Q [i ++ ].weight = (adj_list -> vertex == vertex ) ? 0 : DISTANCE_INFINITY ;
702659 }
703660 number_of_items_in_q = i ;
704661 /* sort the working queue according the distance from the reference vertex */
@@ -750,17 +707,13 @@ uint32_t opal_graph_dijkstra(opal_graph_t *graph, opal_graph_vertex_t *vertex, o
750707void opal_graph_duplicate (opal_graph_t * * dest , opal_graph_t * src )
751708{
752709 opal_adjacency_list_t * aj_list ;
753- opal_list_item_t * aj_list_item , * edg_item ;
754710 opal_graph_vertex_t * vertex ;
755711 opal_graph_edge_t * edge , * new_edge ;
756712
757713 /* construct a new graph */
758714 * dest = OBJ_NEW (opal_graph_t );
759715 /* Run on all the vertices of the src graph */
760- for (aj_list_item = opal_list_get_first (src -> adjacency_list );
761- aj_list_item != opal_list_get_end (src -> adjacency_list );
762- aj_list_item = opal_list_get_next (aj_list_item )) {
763- aj_list = (opal_adjacency_list_t * ) aj_list_item ;
716+ OPAL_LIST_FOREACH (aj_list , src -> adjacency_list , opal_adjacency_list_t ) {
764717 /* for each vertex in the src graph, construct a new vertex */
765718 vertex = OBJ_NEW (opal_graph_vertex_t );
766719 /* associate the new vertex to a vertex from the original graph */
@@ -789,15 +742,9 @@ void opal_graph_duplicate(opal_graph_t **dest, opal_graph_t *src)
789742 * Now, copy all the edges from the source graph
790743 */
791744 /* Run on all the adjscency lists in the graph */
792- for (aj_list_item = opal_list_get_first (src -> adjacency_list );
793- aj_list_item != opal_list_get_end (src -> adjacency_list );
794- aj_list_item = opal_list_get_next (aj_list_item )) {
795- aj_list = (opal_adjacency_list_t * ) aj_list_item ;
745+ OPAL_LIST_FOREACH (aj_list , src -> adjacency_list , opal_adjacency_list_t ) {
796746 /* for all the edges in the adjscency list */
797- for (edg_item = opal_list_get_first (aj_list -> edges );
798- edg_item != opal_list_get_end (aj_list -> edges );
799- edg_item = opal_list_get_next (edg_item )) {
800- edge = (opal_graph_edge_t * )edg_item ;
747+ OPAL_LIST_FOREACH (edge , aj_list -> edges , opal_graph_edge_t ) {
801748 /* construct new edge for the new graph */
802749 new_edge = OBJ_NEW (opal_graph_edge_t );
803750 /* copy the edge weight from the original edge */
@@ -818,20 +765,15 @@ void opal_graph_duplicate(opal_graph_t **dest, opal_graph_t *src)
818765void opal_graph_print (opal_graph_t * graph )
819766{
820767 opal_adjacency_list_t * aj_list ;
821- opal_list_item_t * aj_list_item ;
822768 opal_graph_edge_t * edge ;
823- opal_list_item_t * edge_item ;
824769 char * tmp_str1 , * tmp_str2 ;
825770 bool need_free1 , need_free2 ;
826771
827772 /* print header */
828773 opal_output (0 , " Graph " );
829774 opal_output (0 , "====================" );
830775 /* run on all the vertices of the graph */
831- for (aj_list_item = opal_list_get_first (graph -> adjacency_list );
832- aj_list_item != opal_list_get_end (graph -> adjacency_list );
833- aj_list_item = opal_list_get_next (aj_list_item )) {
834- aj_list = (opal_adjacency_list_t * ) aj_list_item ;
776+ OPAL_LIST_FOREACH (aj_list , graph -> adjacency_list , opal_adjacency_list_t ) {
835777 /* print vertex data to temporary string*/
836778 if (NULL != aj_list -> vertex -> print_vertex ) {
837779 need_free1 = true;
@@ -844,10 +786,7 @@ void opal_graph_print(opal_graph_t *graph)
844786 /* print vertex */
845787 opal_output (0 , "V(%s) Connections:" ,tmp_str1 );
846788 /* run on all the edges of the vertex */
847- for (edge_item = opal_list_get_first (aj_list -> edges );
848- edge_item != opal_list_get_end (aj_list -> edges );
849- edge_item = opal_list_get_next (edge_item )) {
850- edge = (opal_graph_edge_t * )edge_item ;
789+ OPAL_LIST_FOREACH (edge , aj_list -> edges , opal_graph_edge_t ) {
851790 /* print the vertex data of the vertex in the end of the edge to a temporary string */
852791 if (NULL != edge -> end -> print_vertex ) {
853792 need_free2 = true;
0 commit comments