| 86 | |
| 87 | |
| 88 | LIST * order( FRAME * frame, int flags ) |
| 89 | { |
| 90 | LIST * arg = lol_get( frame->args, 0 ); |
| 91 | LIST * result = L0; |
| 92 | int src; |
| 93 | LISTITER iter = list_begin( arg ); |
| 94 | LISTITER const end = list_end( arg ); |
| 95 | |
| 96 | /* We need to create a graph of order dependencies between the passed |
| 97 | * objects. We assume there are no duplicates passed to 'add_pair'. |
| 98 | */ |
| 99 | int length = list_length( arg ); |
| 100 | int * * graph = ( int * * )BJAM_CALLOC( length, sizeof( int * ) ); |
| 101 | int * order = ( int * )BJAM_MALLOC( ( length + 1 ) * sizeof( int ) ); |
| 102 | |
| 103 | for ( src = 0; iter != end; iter = list_next( iter ), ++src ) |
| 104 | { |
| 105 | /* For all objects this one depends upon, add elements to 'graph'. */ |
| 106 | LIST * dependencies = var_get( frame->module, list_item( iter ) ); |
| 107 | int index = 0; |
| 108 | LISTITER dep_iter = list_begin( dependencies ); |
| 109 | LISTITER const dep_end = list_end( dependencies ); |
| 110 | |
| 111 | graph[ src ] = ( int * )BJAM_CALLOC( list_length( dependencies ) + 1, |
| 112 | sizeof( int ) ); |
| 113 | for ( ; dep_iter != dep_end; dep_iter = list_next( dep_iter ) ) |
| 114 | { |
| 115 | int const dst = list_index( arg, list_item( dep_iter ) ); |
| 116 | if ( dst != -1 ) |
| 117 | graph[ src ][ index++ ] = dst; |
| 118 | } |
| 119 | graph[ src ][ index ] = -1; |
| 120 | } |
| 121 | |
| 122 | topological_sort( graph, length, order ); |
| 123 | |
| 124 | { |
| 125 | int index = length - 1; |
| 126 | for ( ; index >= 0; --index ) |
| 127 | { |
| 128 | int i; |
| 129 | LISTITER iter = list_begin( arg ); |
| 130 | LISTITER const end = list_end( arg ); |
| 131 | for ( i = 0; i < order[ index ]; ++i, iter = list_next( iter ) ); |
| 132 | result = list_push_back( result, object_copy( list_item( iter ) ) ); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /* Clean up */ |
| 137 | { |
| 138 | int i; |
| 139 | for ( i = 0; i < length; ++i ) |
| 140 | BJAM_FREE( graph[ i ] ); |
| 141 | BJAM_FREE( graph ); |
| 142 | BJAM_FREE( order ); |
| 143 | } |
| 144 | |
| 145 | return result; |
nothing calls this directly
no test coverage detected