11
11
* Copyright (c) 2004-2005 The Regents of the University of California.
12
12
* All rights reserved.
13
13
* 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
15
15
* reserved.
16
16
* Copyright (c) 2016 Cisco Systems, Inc. All rights reserved.
17
17
* $COPYRIGHT$
@@ -186,25 +186,16 @@ static void opal_adjacency_list_destruct(opal_adjacency_list_t *aj_list)
186
186
static void delete_all_edges_conceded_to_vertex (opal_graph_t * graph , opal_graph_vertex_t * vertex )
187
187
{
188
188
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 ;
192
190
193
191
/**
194
192
* for all the adjacency list in the graph
195
193
*/
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 ) {
200
195
/**
201
196
* for all the edges in the adjacency list
202
197
*/
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 ) {
208
199
/**
209
200
* if the edge is ended in the vertex
210
201
*/
@@ -228,15 +219,11 @@ static void delete_all_edges_conceded_to_vertex(opal_graph_t *graph, opal_graph_
228
219
void opal_graph_add_vertex (opal_graph_t * graph , opal_graph_vertex_t * vertex )
229
220
{
230
221
opal_adjacency_list_t * aj_list ;
231
- opal_list_item_t * item ;
232
222
233
223
/**
234
224
* Find if this vertex already exists in the graph.
235
225
*/
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 ) {
240
227
if (aj_list -> vertex == vertex ) {
241
228
/* If this vertex exists, dont do anything. */
242
229
return ;
@@ -270,17 +257,13 @@ void opal_graph_add_vertex(opal_graph_t *graph, opal_graph_vertex_t *vertex)
270
257
int opal_graph_add_edge (opal_graph_t * graph , opal_graph_edge_t * edge )
271
258
{
272
259
opal_adjacency_list_t * aj_list , * start_aj_list = NULL ;
273
- opal_list_item_t * item ;
274
260
bool start_found = false, end_found = false;
275
261
276
262
277
263
/**
278
264
* find the vertices that this edge should connect.
279
265
*/
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 ) {
284
267
if (aj_list -> vertex == edge -> start ) {
285
268
start_found = true;
286
269
start_aj_list = aj_list ;
@@ -372,7 +355,6 @@ void opal_graph_remove_vertex(opal_graph_t *graph, opal_graph_vertex_t *vertex)
372
355
uint32_t opal_graph_adjacent (opal_graph_t * graph , opal_graph_vertex_t * vertex1 , opal_graph_vertex_t * vertex2 )
373
356
{
374
357
opal_adjacency_list_t * adj_list ;
375
- opal_list_item_t * item ;
376
358
opal_graph_edge_t * edge ;
377
359
378
360
/**
@@ -401,10 +383,7 @@ uint32_t opal_graph_adjacent(opal_graph_t *graph, opal_graph_vertex_t *vertex1,
401
383
* vertex.
402
384
*/
403
385
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 ) {
408
387
if (edge -> end == vertex2 ) {
409
388
/* if the second vertex was found in the adjacency list of the first one, return the weight */
410
389
return edge -> weight ;
@@ -452,15 +431,11 @@ int opal_graph_get_size(opal_graph_t *graph)
452
431
opal_graph_vertex_t * opal_graph_find_vertex (opal_graph_t * graph , void * vertex_data )
453
432
{
454
433
opal_adjacency_list_t * aj_list ;
455
- opal_list_item_t * item ;
456
434
457
435
/**
458
436
* Run on all the vertices of the graph
459
437
*/
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 ) {
464
439
if (NULL != aj_list -> vertex -> compare_vertex ) {
465
440
/* if the vertex data of a vertex is equal to the vertex data */
466
441
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
489
464
int opal_graph_get_graph_vertices (opal_graph_t * graph , opal_pointer_array_t * vertices_list )
490
465
{
491
466
opal_adjacency_list_t * aj_list ;
492
- opal_list_item_t * item ;
493
- int i ;
494
467
495
468
/**
496
469
* 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
499
472
return 0 ;
500
473
}
501
474
/* 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 ) {
506
476
/* Add the vertex to the vertices array */
507
477
opal_pointer_array_add (vertices_list ,(void * )aj_list -> vertex );
508
478
}
@@ -528,9 +498,7 @@ int opal_graph_get_adjacent_vertices(opal_graph_t *graph, opal_graph_vertex_t *v
528
498
opal_adjacency_list_t * adj_list ;
529
499
opal_graph_edge_t * edge ;
530
500
int adjacents_number ;
531
- opal_list_item_t * item ;
532
501
vertex_distance_from_t distance_from ;
533
- int i ;
534
502
535
503
/**
536
504
* 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
546
514
/* find the number of adjcents of this vertex */
547
515
adjacents_number = opal_list_get_size (adj_list -> edges );
548
516
/* 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 ) {
553
518
/* assign vertices and their weight in the adjcents list */
554
519
distance_from .vertex = edge -> end ;
555
520
distance_from .weight = edge -> weight ;
@@ -663,7 +628,6 @@ uint32_t opal_graph_dijkstra(opal_graph_t *graph, opal_graph_vertex_t *vertex, o
663
628
{
664
629
int graph_order ;
665
630
vertex_distance_from_t * Q , * q_start , * current_vertex ;
666
- opal_list_item_t * adj_list_item ;
667
631
opal_adjacency_list_t * adj_list ;
668
632
int number_of_items_in_q ;
669
633
int i ;
@@ -683,22 +647,15 @@ uint32_t opal_graph_dijkstra(opal_graph_t *graph, opal_graph_vertex_t *vertex, o
683
647
/* assign a pointer to the start of the queue */
684
648
q_start = Q ;
685
649
/* 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 ) {
690
652
/* insert the vertices pointes to the working queue */
691
653
Q [i ].vertex = adj_list -> vertex ;
692
654
/**
693
655
* assign an infinity distance to all the vertices in the queue
694
656
* except the reference vertex which its distance should be 0.
695
657
*/
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 ;
702
659
}
703
660
number_of_items_in_q = i ;
704
661
/* 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
750
707
void opal_graph_duplicate (opal_graph_t * * dest , opal_graph_t * src )
751
708
{
752
709
opal_adjacency_list_t * aj_list ;
753
- opal_list_item_t * aj_list_item , * edg_item ;
754
710
opal_graph_vertex_t * vertex ;
755
711
opal_graph_edge_t * edge , * new_edge ;
756
712
757
713
/* construct a new graph */
758
714
* dest = OBJ_NEW (opal_graph_t );
759
715
/* 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 ) {
764
717
/* for each vertex in the src graph, construct a new vertex */
765
718
vertex = OBJ_NEW (opal_graph_vertex_t );
766
719
/* 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)
789
742
* Now, copy all the edges from the source graph
790
743
*/
791
744
/* 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 ) {
796
746
/* 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 ) {
801
748
/* construct new edge for the new graph */
802
749
new_edge = OBJ_NEW (opal_graph_edge_t );
803
750
/* copy the edge weight from the original edge */
@@ -818,20 +765,15 @@ void opal_graph_duplicate(opal_graph_t **dest, opal_graph_t *src)
818
765
void opal_graph_print (opal_graph_t * graph )
819
766
{
820
767
opal_adjacency_list_t * aj_list ;
821
- opal_list_item_t * aj_list_item ;
822
768
opal_graph_edge_t * edge ;
823
- opal_list_item_t * edge_item ;
824
769
char * tmp_str1 , * tmp_str2 ;
825
770
bool need_free1 , need_free2 ;
826
771
827
772
/* print header */
828
773
opal_output (0 , " Graph " );
829
774
opal_output (0 , "====================" );
830
775
/* 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 ) {
835
777
/* print vertex data to temporary string*/
836
778
if (NULL != aj_list -> vertex -> print_vertex ) {
837
779
need_free1 = true;
@@ -844,10 +786,7 @@ void opal_graph_print(opal_graph_t *graph)
844
786
/* print vertex */
845
787
opal_output (0 , "V(%s) Connections:" ,tmp_str1 );
846
788
/* 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 ) {
851
790
/* print the vertex data of the vertex in the end of the edge to a temporary string */
852
791
if (NULL != edge -> end -> print_vertex ) {
853
792
need_free2 = true;
0 commit comments