|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | + |
| 4 | +struct hlist_node; |
| 5 | + |
| 6 | +struct hlist_head { |
| 7 | + struct hlist_node *first; |
| 8 | +}; |
| 9 | + |
| 10 | +struct hlist_node { |
| 11 | + struct hlist_node *next, **pprev; |
| 12 | +}; |
| 13 | + |
| 14 | +static inline void INIT_HLIST_HEAD(struct hlist_head *h) { |
| 15 | + h->first = NULL; |
| 16 | +} |
| 17 | + |
| 18 | +static inline void INIT_HLIST_NODE(struct hlist_node *n) { |
| 19 | + n->next = NULL; |
| 20 | + n->pprev = NULL; |
| 21 | +} |
| 22 | + |
| 23 | +static inline int hlist_empty(struct hlist_head *h) { |
| 24 | + return !h->first; |
| 25 | +} |
| 26 | + |
| 27 | +static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h) |
| 28 | +{ |
| 29 | + if (h->first != NULL) { |
| 30 | + h->first->pprev = &n->next; |
| 31 | + } |
| 32 | + n->next = h->first; |
| 33 | + n->pprev = &h->first; |
| 34 | + h->first = n; |
| 35 | +} |
| 36 | + |
| 37 | +static inline void hlist_del(struct hlist_node *n) |
| 38 | +{ |
| 39 | + struct hlist_node *next = n->next; |
| 40 | + struct hlist_node **pprev = n->pprev; |
| 41 | + *pprev = next; |
| 42 | + if (next != NULL) { |
| 43 | + next->pprev = pprev; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +#define container_of(ptr, type, member) \ |
| 48 | + ((type *)((char *)(ptr) - (size_t)&(((type *)0)->member))) |
| 49 | + |
| 50 | +#define list_entry(ptr, type, member) \ |
| 51 | + container_of(ptr, type, member) |
| 52 | + |
| 53 | +#define hlist_for_each(pos, head) \ |
| 54 | + for (pos = (head)->first; pos; pos = pos->next) |
| 55 | + |
| 56 | +#define hlist_for_each_safe(pos, n, head) \ |
| 57 | + for (pos = (head)->first; pos && ({ n = pos->next; true; }); pos = n) |
| 58 | + |
| 59 | +#define NEIGHBORS_MAX_SIZE 100 |
| 60 | + |
| 61 | +struct UndirectedGraphNode { |
| 62 | + int label; |
| 63 | + struct UndirectedGraphNode *neighbors[NEIGHBORS_MAX_SIZE]; |
| 64 | + int neighborsCount; |
| 65 | +}; |
| 66 | + |
| 67 | +struct label_node { |
| 68 | + struct UndirectedGraphNode *gn; |
| 69 | + struct hlist_node node; |
| 70 | +}; |
| 71 | + |
| 72 | +static struct UndirectedGraphNode *find(int label, int size, struct hlist_head *heads) |
| 73 | +{ |
| 74 | + int hash = (label < 0 ? -label : label) % size; |
| 75 | + struct hlist_node *p; |
| 76 | + hlist_for_each(p, &heads[hash]) { |
| 77 | + struct label_node *ln = list_entry(p, struct label_node, node); |
| 78 | + if (ln->gn->label == label) { |
| 79 | + return ln->gn; |
| 80 | + } |
| 81 | + } |
| 82 | + return NULL; |
| 83 | +} |
| 84 | + |
| 85 | +static struct UndirectedGraphNode *recursive_clone(struct UndirectedGraphNode *graph, struct hlist_head *heads, int size) |
| 86 | +{ |
| 87 | + if (graph == NULL) { |
| 88 | + return NULL; |
| 89 | + } |
| 90 | + |
| 91 | + struct UndirectedGraphNode *node = find(graph->label, size, heads); |
| 92 | + if (node != NULL) { |
| 93 | + return node; |
| 94 | + } |
| 95 | + |
| 96 | + node = malloc(sizeof(*node)); |
| 97 | + node->label = graph->label; |
| 98 | + node->neighborsCount = graph->neighborsCount; |
| 99 | + struct label_node *ln = malloc(sizeof(*ln)); |
| 100 | + ln->gn = node; |
| 101 | + int hash = (node->label < 0 ? -node->label : node->label) % size; |
| 102 | + hlist_add_head(&ln->node, &heads[hash]); |
| 103 | + |
| 104 | + int i; |
| 105 | + for (i = 0; i < node->neighborsCount; i++) { |
| 106 | + node->neighbors[i] = recursive_clone(graph->neighbors[i], heads, size); |
| 107 | + } |
| 108 | + |
| 109 | + return node; |
| 110 | +} |
| 111 | + |
| 112 | +static struct UndirectedGraphNode *cloneGraph(struct UndirectedGraphNode *graph) |
| 113 | +{ |
| 114 | + int i, cap = 1000; |
| 115 | + struct hlist_head *heads = malloc(cap * sizeof(*heads)); |
| 116 | + for (i = 0; i < cap; i++) { |
| 117 | + INIT_HLIST_HEAD(&heads[i]); |
| 118 | + } |
| 119 | + |
| 120 | + return recursive_clone(graph, heads, cap); |
| 121 | +} |
| 122 | + |
| 123 | +int main(void) |
| 124 | +{ |
| 125 | + struct UndirectedGraphNode n0, n1, n2; |
| 126 | + n0.label = 0; |
| 127 | + n1.label = 1; |
| 128 | + n2.label = 2; |
| 129 | + n0.neighborsCount = 2; |
| 130 | + n1.neighborsCount = 1; |
| 131 | + n2.neighborsCount = 1; |
| 132 | + n0.neighbors[0] = &n1; |
| 133 | + n0.neighbors[1] = &n2; |
| 134 | + n1.neighbors[0] = &n2; |
| 135 | + n2.neighbors[0] = &n2; |
| 136 | + |
| 137 | + struct UndirectedGraphNode *res = cloneGraph(&n0); |
| 138 | + return 0; |
| 139 | +} |
0 commit comments