1b8e80941Smrg/* 2b8e80941Smrg * Copyright © 2019 Broadcom 3b8e80941Smrg * 4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a 5b8e80941Smrg * copy of this software and associated documentation files (the "Software"), 6b8e80941Smrg * to deal in the Software without restriction, including without limitation 7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the 9b8e80941Smrg * Software is furnished to do so, subject to the following conditions: 10b8e80941Smrg * 11b8e80941Smrg * The above copyright notice and this permission notice (including the next 12b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the 13b8e80941Smrg * Software. 14b8e80941Smrg * 15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20b8e80941Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21b8e80941Smrg * IN THE SOFTWARE. 22b8e80941Smrg */ 23b8e80941Smrg 24b8e80941Smrg#include "util/set.h" 25b8e80941Smrg#include "util/dag.h" 26b8e80941Smrg 27b8e80941Smrg/** 28b8e80941Smrg * Adds a directed edge from the parent node to the child. 29b8e80941Smrg * 30b8e80941Smrg * Both nodes should have been initialized with dag_init_node(). The edge 31b8e80941Smrg * list may contain multiple edges to the same child with different data. 32b8e80941Smrg */ 33b8e80941Smrgvoid 34b8e80941Smrgdag_add_edge(struct dag_node *parent, struct dag_node *child, void *data) 35b8e80941Smrg{ 36b8e80941Smrg util_dynarray_foreach(&parent->edges, struct dag_edge, edge) { 37b8e80941Smrg if (edge->child == child && edge->data == data) 38b8e80941Smrg return; 39b8e80941Smrg } 40b8e80941Smrg /* Remove the child as a DAG head. */ 41b8e80941Smrg list_delinit(&child->link); 42b8e80941Smrg 43b8e80941Smrg struct dag_edge edge = { 44b8e80941Smrg .child = child, 45b8e80941Smrg .data = data, 46b8e80941Smrg }; 47b8e80941Smrg 48b8e80941Smrg util_dynarray_append(&parent->edges, struct dag_edge, edge); 49b8e80941Smrg child->parent_count++; 50b8e80941Smrg} 51b8e80941Smrg 52b8e80941Smrg/* Removes a single edge from the graph, promoting the child to a DAG head. 53b8e80941Smrg * 54b8e80941Smrg * Note that calling this other than through dag_prune_head() means that you 55b8e80941Smrg * need to be careful when iterating the edges of remaining nodes for NULL 56b8e80941Smrg * children. 57b8e80941Smrg */ 58b8e80941Smrgvoid 59b8e80941Smrgdag_remove_edge(struct dag *dag, struct dag_edge *edge) 60b8e80941Smrg{ 61b8e80941Smrg if (!edge->child) 62b8e80941Smrg return; 63b8e80941Smrg 64b8e80941Smrg struct dag_node *child = edge->child; 65b8e80941Smrg child->parent_count--; 66b8e80941Smrg if (child->parent_count == 0) 67b8e80941Smrg list_addtail(&child->link, &dag->heads); 68b8e80941Smrg 69b8e80941Smrg edge->child = NULL; 70b8e80941Smrg edge->data = NULL; 71b8e80941Smrg} 72b8e80941Smrg 73b8e80941Smrg/** 74b8e80941Smrg * Removes a DAG head from the graph, and moves any new dag heads into the 75b8e80941Smrg * heads list. 76b8e80941Smrg */ 77b8e80941Smrgvoid 78b8e80941Smrgdag_prune_head(struct dag *dag, struct dag_node *node) 79b8e80941Smrg{ 80b8e80941Smrg assert(!node->parent_count); 81b8e80941Smrg 82b8e80941Smrg list_delinit(&node->link); 83b8e80941Smrg 84b8e80941Smrg util_dynarray_foreach(&node->edges, struct dag_edge, edge) { 85b8e80941Smrg dag_remove_edge(dag, edge); 86b8e80941Smrg } 87b8e80941Smrg} 88b8e80941Smrg 89b8e80941Smrg/** 90b8e80941Smrg * Initializes DAG node (probably embedded in some other datastructure in the 91b8e80941Smrg * user). 92b8e80941Smrg */ 93b8e80941Smrgvoid 94b8e80941Smrgdag_init_node(struct dag *dag, struct dag_node *node) 95b8e80941Smrg{ 96b8e80941Smrg util_dynarray_init(&node->edges, dag); 97b8e80941Smrg list_addtail(&node->link, &dag->heads); 98b8e80941Smrg} 99b8e80941Smrg 100b8e80941Smrgstruct dag_traverse_bottom_up_state { 101b8e80941Smrg struct set *seen; 102b8e80941Smrg void *data; 103b8e80941Smrg}; 104b8e80941Smrg 105b8e80941Smrgstatic void 106b8e80941Smrgdag_traverse_bottom_up_node(struct dag_node *node, 107b8e80941Smrg void (*cb)(struct dag_node *node, 108b8e80941Smrg void *data), 109b8e80941Smrg struct dag_traverse_bottom_up_state *state) 110b8e80941Smrg{ 111b8e80941Smrg if (_mesa_set_search(state->seen, node)) 112b8e80941Smrg return; 113b8e80941Smrg 114b8e80941Smrg util_dynarray_foreach(&node->edges, struct dag_edge, edge) { 115b8e80941Smrg dag_traverse_bottom_up_node(edge->child, cb, state); 116b8e80941Smrg } 117b8e80941Smrg 118b8e80941Smrg cb(node, state->data); 119b8e80941Smrg _mesa_set_add(state->seen, node); 120b8e80941Smrg} 121b8e80941Smrg 122b8e80941Smrg/** 123b8e80941Smrg * Walks the DAG from leaves to the root, ensuring that each node is only seen 124b8e80941Smrg * once its children have been, and each node is only traversed once. 125b8e80941Smrg */ 126b8e80941Smrgvoid 127b8e80941Smrgdag_traverse_bottom_up(struct dag *dag, void (*cb)(struct dag_node *node, 128b8e80941Smrg void *data), void *data) 129b8e80941Smrg{ 130b8e80941Smrg struct dag_traverse_bottom_up_state state = { 131b8e80941Smrg .seen = _mesa_pointer_set_create(NULL), 132b8e80941Smrg .data = data, 133b8e80941Smrg }; 134b8e80941Smrg 135b8e80941Smrg list_for_each_entry(struct dag_node, node, &dag->heads, link) { 136b8e80941Smrg dag_traverse_bottom_up_node(node, cb, &state); 137b8e80941Smrg } 138b8e80941Smrg 139b8e80941Smrg ralloc_free(state.seen); 140b8e80941Smrg} 141b8e80941Smrg 142b8e80941Smrg/** 143b8e80941Smrg * Creates an empty DAG datastructure. 144b8e80941Smrg */ 145b8e80941Smrgstruct dag * 146b8e80941Smrgdag_create(void *mem_ctx) 147b8e80941Smrg{ 148b8e80941Smrg struct dag *dag = rzalloc(mem_ctx, struct dag); 149b8e80941Smrg 150b8e80941Smrg list_inithead(&dag->heads); 151b8e80941Smrg 152b8e80941Smrg return dag; 153b8e80941Smrg} 154b8e80941Smrg 155